voting_unittest.cc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "components/performance_manager/public/voting/voting.h"
  5. #include "base/test/gtest_util.h"
  6. #include "components/performance_manager/test_support/voting.h"
  7. #include "testing/gtest/include/gtest/gtest.h"
  8. namespace performance_manager {
  9. namespace {
  10. using ::testing::AssertionFailure;
  11. using ::testing::AssertionResult;
  12. using ::testing::AssertionSuccess;
  13. using TestVote = voting::Vote<void, int, 0>;
  14. using TestVotingChannel = voting::VotingChannel<TestVote>;
  15. using TestVotingChannelFactory = voting::VotingChannelFactory<TestVote>;
  16. using DummyVoteObserver = voting::test::DummyVoteObserver<TestVote>;
  17. // Some dummy contexts.
  18. const void* kDummyContext1 = reinterpret_cast<const void*>(0xDEADBEEF);
  19. const void* kDummyContext2 = reinterpret_cast<const void*>(0xBAADF00D);
  20. static const char kReason[] = "reason";
  21. } // namespace
  22. TEST(VotingTest, SimpleVoter) {
  23. DummyVoteObserver observer;
  24. TestVotingChannel voting_channel = observer.BuildVotingChannel();
  25. voting::VoterId<TestVote> voter_id = voting_channel.voter_id();
  26. EXPECT_FALSE(observer.HasVote(voter_id, kDummyContext1));
  27. voting_channel.SubmitVote(kDummyContext1, TestVote(5, kReason));
  28. EXPECT_TRUE(observer.HasVote(voter_id, kDummyContext1, 5, kReason));
  29. voting_channel.ChangeVote(kDummyContext1, TestVote(10, kReason));
  30. EXPECT_TRUE(observer.HasVote(voter_id, kDummyContext1, 10, kReason));
  31. voting_channel.InvalidateVote(kDummyContext1);
  32. EXPECT_FALSE(observer.HasVote(voter_id, kDummyContext1));
  33. }
  34. // Tests that an observer can receive votes for different contexts from the same
  35. // voting channel.
  36. TEST(VotingTest, OneVoterMultipleContexts) {
  37. DummyVoteObserver observer;
  38. TestVotingChannel voting_channel = observer.BuildVotingChannel();
  39. voting::VoterId<TestVote> voter_id = voting_channel.voter_id();
  40. EXPECT_FALSE(observer.HasVote(voter_id, kDummyContext1));
  41. voting_channel.SubmitVote(kDummyContext1, TestVote(5, kReason));
  42. EXPECT_TRUE(observer.HasVote(voter_id, kDummyContext1, 5, kReason));
  43. EXPECT_FALSE(observer.HasVote(voter_id, kDummyContext2));
  44. voting_channel.SubmitVote(kDummyContext2, TestVote(100, kReason));
  45. EXPECT_TRUE(observer.HasVote(voter_id, kDummyContext1, 5, kReason));
  46. EXPECT_TRUE(observer.HasVote(voter_id, kDummyContext2, 100, kReason));
  47. voting_channel.ChangeVote(kDummyContext1, TestVote(10, kReason));
  48. EXPECT_TRUE(observer.HasVote(voter_id, kDummyContext1, 10, kReason));
  49. EXPECT_TRUE(observer.HasVote(voter_id, kDummyContext2, 100, kReason));
  50. voting_channel.InvalidateVote(kDummyContext1);
  51. EXPECT_FALSE(observer.HasVote(voter_id, kDummyContext1));
  52. EXPECT_TRUE(observer.HasVote(voter_id, kDummyContext2, 100, kReason));
  53. voting_channel.InvalidateVote(kDummyContext2);
  54. EXPECT_FALSE(observer.HasVote(voter_id, kDummyContext1));
  55. EXPECT_FALSE(observer.HasVote(voter_id, kDummyContext2));
  56. }
  57. // Tests that an observer can receive votes from more than one voting channel.
  58. TEST(VotingTest, TwoVoter) {
  59. DummyVoteObserver observer;
  60. TestVotingChannel voting_channel_1 = observer.BuildVotingChannel();
  61. voting::VoterId<TestVote> voter_id_1 = voting_channel_1.voter_id();
  62. TestVotingChannel voting_channel_2 = observer.BuildVotingChannel();
  63. voting::VoterId<TestVote> voter_id_2 = voting_channel_2.voter_id();
  64. EXPECT_FALSE(observer.HasVote(voter_id_1, kDummyContext1));
  65. EXPECT_FALSE(observer.HasVote(voter_id_2, kDummyContext1));
  66. voting_channel_1.SubmitVote(kDummyContext1, TestVote(5, kReason));
  67. EXPECT_TRUE(observer.HasVote(voter_id_1, kDummyContext1, 5, kReason));
  68. EXPECT_FALSE(observer.HasVote(voter_id_2, kDummyContext1));
  69. voting_channel_2.SubmitVote(kDummyContext1, TestVote(5, kReason));
  70. EXPECT_TRUE(observer.HasVote(voter_id_1, kDummyContext1, 5, kReason));
  71. EXPECT_TRUE(observer.HasVote(voter_id_2, kDummyContext1, 5, kReason));
  72. voting_channel_1.ChangeVote(kDummyContext1, TestVote(10, kReason));
  73. EXPECT_TRUE(observer.HasVote(voter_id_1, kDummyContext1, 10, kReason));
  74. EXPECT_TRUE(observer.HasVote(voter_id_2, kDummyContext1, 5, kReason));
  75. voting_channel_2.ChangeVote(kDummyContext1, TestVote(10, kReason));
  76. EXPECT_TRUE(observer.HasVote(voter_id_1, kDummyContext1, 10, kReason));
  77. EXPECT_TRUE(observer.HasVote(voter_id_2, kDummyContext1, 10, kReason));
  78. voting_channel_1.InvalidateVote(kDummyContext1);
  79. EXPECT_FALSE(observer.HasVote(voter_id_1, kDummyContext1));
  80. EXPECT_TRUE(observer.HasVote(voter_id_2, kDummyContext1, 10, kReason));
  81. voting_channel_2.InvalidateVote(kDummyContext1);
  82. EXPECT_FALSE(observer.HasVote(voter_id_1, kDummyContext1));
  83. EXPECT_FALSE(observer.HasVote(voter_id_2, kDummyContext1));
  84. }
  85. TEST(VotingTest, ResetVotingChannel) {
  86. DummyVoteObserver observer;
  87. TestVotingChannel voting_channel = observer.BuildVotingChannel();
  88. EXPECT_TRUE(voting_channel.IsValid());
  89. voting_channel.Reset();
  90. EXPECT_FALSE(voting_channel.IsValid());
  91. }
  92. // Tests that VotingChannel supports move sementics.
  93. TEST(VotingTest, MoveVotingChannel) {
  94. DummyVoteObserver observer;
  95. // Build the voting channel.
  96. TestVotingChannel voting_channel_1 = observer.BuildVotingChannel();
  97. voting::VoterId<TestVote> voter_id = voting_channel_1.voter_id();
  98. EXPECT_TRUE(voting_channel_1.IsValid());
  99. // Cast a vote with that voting channel.
  100. voting_channel_1.SubmitVote(kDummyContext1, TestVote(5, kReason));
  101. EXPECT_TRUE(observer.HasVote(voter_id, kDummyContext1, 5, kReason));
  102. // Move the voting channel.
  103. TestVotingChannel voting_channel_2 = std::move(voting_channel_1);
  104. EXPECT_TRUE(voting_channel_2.IsValid());
  105. // Use the second variable to change the vote.
  106. voting_channel_2.ChangeVote(kDummyContext1, TestVote(10, kReason));
  107. EXPECT_TRUE(observer.HasVote(voter_id, kDummyContext1, 10, kReason));
  108. // Move the voting channel back using the move assignment operator.
  109. voting_channel_1 = std::move(voting_channel_2);
  110. EXPECT_TRUE(voting_channel_1.IsValid());
  111. // Invalidate the vote.
  112. voting_channel_1.InvalidateVote(kDummyContext1);
  113. EXPECT_FALSE(observer.HasVote(voter_id, kDummyContext1));
  114. }
  115. // Tests that submitting 2 votes for the same context using a VotingChannel
  116. // results in a DCHECK.
  117. TEST(VotingTest, SubmitDuplicateVote) {
  118. DummyVoteObserver observer;
  119. TestVotingChannel voting_channel = observer.BuildVotingChannel();
  120. voting::VoterId<TestVote> voter_id = voting_channel.voter_id();
  121. EXPECT_FALSE(observer.HasVote(voter_id, kDummyContext1));
  122. voting_channel.SubmitVote(kDummyContext1, TestVote(5, kReason));
  123. EXPECT_TRUE(observer.HasVote(voter_id, kDummyContext1, 5, kReason));
  124. EXPECT_DCHECK_DEATH(
  125. voting_channel.SubmitVote(kDummyContext1, TestVote(10, kReason)));
  126. // Clean up.
  127. voting_channel.InvalidateVote(kDummyContext1);
  128. }
  129. // Tests that calling ChangeVote() for a context before a vote was submitted for
  130. // that context results in a DCHECK.
  131. TEST(VotingTest, ChangeNonExisting) {
  132. DummyVoteObserver observer;
  133. TestVotingChannel voting_channel = observer.BuildVotingChannel();
  134. voting::VoterId<TestVote> voter_id = voting_channel.voter_id();
  135. EXPECT_FALSE(observer.HasVote(voter_id, kDummyContext1));
  136. EXPECT_DCHECK_DEATH(
  137. voting_channel.ChangeVote(kDummyContext1, TestVote(5, kReason)));
  138. }
  139. // Tests that calling InvalidateVote() for a context before a vote was submitted
  140. // for that context results in a DCHECK.
  141. TEST(VotingTest, InvalidateNonExisting) {
  142. DummyVoteObserver observer;
  143. TestVotingChannel voting_channel = observer.BuildVotingChannel();
  144. voting::VoterId<TestVote> voter_id = voting_channel.voter_id();
  145. EXPECT_FALSE(observer.HasVote(voter_id, kDummyContext1));
  146. EXPECT_DCHECK_DEATH(voting_channel.InvalidateVote(kDummyContext1));
  147. }
  148. // Tests that destroying a VotingChannelFactory before all of its VotingChannels
  149. // results in a DCHECK.
  150. TEST(VotingTest, DestroyFactoryBeforeChannel) {
  151. TestVotingChannel voting_channel;
  152. auto observer = std::make_unique<DummyVoteObserver>();
  153. voting_channel = observer->BuildVotingChannel();
  154. EXPECT_DCHECK_DEATH(observer.reset());
  155. // Clean up.
  156. voting_channel.Reset();
  157. }
  158. } // namespace performance_manager