frame_visibility_voter_unittest.cc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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/execution_context_priority/frame_visibility_voter.h"
  5. #include "base/memory/raw_ptr.h"
  6. #include "components/performance_manager/public/execution_context/execution_context.h"
  7. #include "components/performance_manager/public/graph/graph.h"
  8. #include "components/performance_manager/test_support/graph_test_harness.h"
  9. #include "components/performance_manager/test_support/mock_graphs.h"
  10. #include "components/performance_manager/test_support/voting.h"
  11. namespace performance_manager {
  12. namespace execution_context_priority {
  13. using DummyVoteObserver = voting::test::DummyVoteObserver<Vote>;
  14. namespace {
  15. const execution_context::ExecutionContext* GetExecutionContext(
  16. const FrameNode* frame_node) {
  17. return execution_context::ExecutionContext::From(frame_node);
  18. }
  19. // Both the voting channel and the FrameVisibilityVoter are expected live on the
  20. // graph, without being actual GraphOwned objects. This class wraps both to
  21. // allow this.
  22. class GraphOwnedWrapper : public GraphOwned {
  23. public:
  24. GraphOwnedWrapper() {
  25. VotingChannel voting_channel = observer_.BuildVotingChannel();
  26. voter_id_ = voting_channel.voter_id();
  27. frame_visibility_voter_.SetVotingChannel(std::move(voting_channel));
  28. }
  29. ~GraphOwnedWrapper() override = default;
  30. GraphOwnedWrapper(const GraphOwnedWrapper&) = delete;
  31. GraphOwnedWrapper& operator=(const GraphOwnedWrapper&) = delete;
  32. // GraphOwned:
  33. void OnPassedToGraph(Graph* graph) override {
  34. graph->AddFrameNodeObserver(&frame_visibility_voter_);
  35. }
  36. void OnTakenFromGraph(Graph* graph) override {
  37. graph->RemoveFrameNodeObserver(&frame_visibility_voter_);
  38. }
  39. // Exposes the DummyVoteObserver to validate expectations.
  40. const DummyVoteObserver& observer() const { return observer_; }
  41. VoterId voter_id() const { return voter_id_; }
  42. private:
  43. DummyVoteObserver observer_;
  44. FrameVisibilityVoter frame_visibility_voter_;
  45. VoterId voter_id_;
  46. };
  47. } // namespace
  48. class FrameVisibilityVoterTest : public GraphTestHarness {
  49. public:
  50. using Super = GraphTestHarness;
  51. FrameVisibilityVoterTest() = default;
  52. ~FrameVisibilityVoterTest() override = default;
  53. FrameVisibilityVoterTest(const FrameVisibilityVoterTest&) = delete;
  54. FrameVisibilityVoterTest& operator=(const FrameVisibilityVoterTest&) = delete;
  55. void SetUp() override {
  56. GetGraphFeatures().EnableExecutionContextRegistry();
  57. Super::SetUp();
  58. auto wrapper = std::make_unique<GraphOwnedWrapper>();
  59. wrapper_ = wrapper.get();
  60. graph()->PassToGraph(std::move(wrapper));
  61. }
  62. // Exposes the DummyVoteObserver to validate expectations.
  63. const DummyVoteObserver& observer() const { return wrapper_->observer(); }
  64. VoterId voter_id() const { return wrapper_->voter_id(); }
  65. private:
  66. raw_ptr<GraphOwnedWrapper> wrapper_ = nullptr;
  67. };
  68. // Tests that the FrameVisibilityVoter correctly casts a vote for a frame
  69. // depending on its visibility.
  70. TEST_F(FrameVisibilityVoterTest, ChangeFrameVisibility) {
  71. // Create a graph with a single frame in a non-visible page. Its initial
  72. // visibility should be kNotVisible, resulting in a low priority.
  73. MockSinglePageInSingleProcessGraph mock_graph(graph());
  74. auto& frame_node = mock_graph.frame;
  75. EXPECT_EQ(frame_node->visibility(), FrameNode::Visibility::kNotVisible);
  76. EXPECT_EQ(observer().GetVoteCount(), 1u);
  77. EXPECT_TRUE(observer().HasVote(voter_id(),
  78. GetExecutionContext(frame_node.get()),
  79. base::TaskPriority::LOWEST,
  80. FrameVisibilityVoter::kFrameVisibilityReason));
  81. // Make the frame visible. This should increase the priority.
  82. mock_graph.frame->SetVisibility(FrameNode::Visibility::kVisible);
  83. EXPECT_EQ(observer().GetVoteCount(), 1u);
  84. EXPECT_TRUE(observer().HasVote(voter_id(),
  85. GetExecutionContext(frame_node.get()),
  86. base::TaskPriority::USER_VISIBLE,
  87. FrameVisibilityVoter::kFrameVisibilityReason));
  88. // Deleting the frame should invalidate the vote.
  89. frame_node.reset();
  90. EXPECT_EQ(observer().GetVoteCount(), 0u);
  91. }
  92. } // namespace execution_context_priority
  93. } // namespace performance_manager