root_vote_observer_unittest.cc 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright 2019 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/execution_context_priority/root_vote_observer.h"
  5. #include "components/performance_manager/graph/frame_node_impl.h"
  6. #include "components/performance_manager/public/execution_context/execution_context.h"
  7. #include "components/performance_manager/test_support/graph_test_harness.h"
  8. #include "components/performance_manager/test_support/mock_graphs.h"
  9. #include "components/performance_manager/test_support/voting.h"
  10. #include "testing/gmock/include/gmock/gmock.h"
  11. #include "testing/gtest/include/gtest/gtest.h"
  12. namespace performance_manager {
  13. namespace execution_context_priority {
  14. namespace {
  15. using testing::_;
  16. static const char kReason[] = "test reason";
  17. class LenientMockFrameNodeObserver : public FrameNode::ObserverDefaultImpl {
  18. public:
  19. LenientMockFrameNodeObserver() = default;
  20. LenientMockFrameNodeObserver(const LenientMockFrameNodeObserver&) = delete;
  21. LenientMockFrameNodeObserver& operator=(const LenientMockFrameNodeObserver&) =
  22. delete;
  23. ~LenientMockFrameNodeObserver() override = default;
  24. MOCK_METHOD2(OnPriorityAndReasonChanged,
  25. void(const FrameNode*, const PriorityAndReason&));
  26. };
  27. using MockFrameNodeObserver =
  28. ::testing::StrictMock<LenientMockFrameNodeObserver>;
  29. class RootVoteObserverTest : public GraphTestHarness {
  30. public:
  31. using Super = GraphTestHarness;
  32. RootVoteObserverTest() = default;
  33. ~RootVoteObserverTest() override = default;
  34. void SetUp() override {
  35. GetGraphFeatures().EnableExecutionContextRegistry();
  36. Super::SetUp();
  37. }
  38. };
  39. } // namespace
  40. TEST_F(RootVoteObserverTest, VotesForwardedToGraph) {
  41. RootVoteObserver root_vote_observer;
  42. MockSinglePageInSingleProcessGraph mock_graph(graph());
  43. auto& frame = mock_graph.frame;
  44. auto* execution_context =
  45. execution_context::ExecutionContext::From(frame.get());
  46. MockFrameNodeObserver obs;
  47. graph()->AddFrameNodeObserver(&obs);
  48. VotingChannel voter = root_vote_observer.GetVotingChannel();
  49. // The priority and reason starts with a default value.
  50. static const PriorityAndReason kDefaultPriorityAndReason(
  51. base::TaskPriority::LOWEST, FrameNodeImpl::kDefaultPriorityReason);
  52. EXPECT_EQ(frame->priority_and_reason(), kDefaultPriorityAndReason);
  53. // Do not expect a notification when an identical vote is submitted.
  54. voter.SubmitVote(execution_context, Vote(kDefaultPriorityAndReason.priority(),
  55. kDefaultPriorityAndReason.reason()));
  56. testing::Mock::VerifyAndClear(&obs);
  57. // Update the vote with a new priority and expect that to propagate.
  58. EXPECT_CALL(obs, OnPriorityAndReasonChanged(frame.get(), _));
  59. voter.ChangeVote(execution_context,
  60. Vote(base::TaskPriority::HIGHEST, kReason));
  61. testing::Mock::VerifyAndClear(&obs);
  62. EXPECT_EQ(frame->priority_and_reason().priority(),
  63. base::TaskPriority::HIGHEST);
  64. EXPECT_EQ(frame->priority_and_reason().reason(), kReason);
  65. // Cancel the existing vote and expect it to go back to the default.
  66. EXPECT_CALL(obs, OnPriorityAndReasonChanged(frame.get(), _));
  67. voter.InvalidateVote(execution_context);
  68. testing::Mock::VerifyAndClear(&obs);
  69. EXPECT_EQ(frame->priority_and_reason(), kDefaultPriorityAndReason);
  70. graph()->RemoveFrameNodeObserver(&obs);
  71. }
  72. } // namespace execution_context_priority
  73. } // namespace performance_manager