frame_audible_voter_unittest.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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_audible_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 FrameAudibleVoter 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_audible_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_audible_voter_);
  35. }
  36. void OnTakenFromGraph(Graph* graph) override {
  37. graph->RemoveFrameNodeObserver(&frame_audible_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. FrameAudibleVoter frame_audible_voter_;
  45. VoterId voter_id_;
  46. };
  47. } // namespace
  48. class FrameAudibleVoterTest : public GraphTestHarness {
  49. public:
  50. using Super = GraphTestHarness;
  51. FrameAudibleVoterTest() = default;
  52. ~FrameAudibleVoterTest() override = default;
  53. FrameAudibleVoterTest(const FrameAudibleVoterTest&) = delete;
  54. FrameAudibleVoterTest& operator=(const FrameAudibleVoterTest&) = delete;
  55. void SetUp() override {
  56. Super::GetGraphFeatures().EnableExecutionContextRegistry();
  57. Super::SetUp();
  58. wrapper_ = graph()->PassToGraph(std::make_unique<GraphOwnedWrapper>());
  59. }
  60. // Exposes the DummyVoteObserver to validate expectations.
  61. const DummyVoteObserver& observer() const { return wrapper_->observer(); }
  62. VoterId voter_id() const { return wrapper_->voter_id(); }
  63. private:
  64. raw_ptr<GraphOwnedWrapper> wrapper_ = nullptr;
  65. };
  66. // Tests that the FrameAudibleVoter correctly casts a vote for a frame
  67. // depending on its audible state.
  68. TEST_F(FrameAudibleVoterTest, AudibleChanged) {
  69. // Create a graph with a single frame page. Its initial audible state should
  70. // be false, resulting in a low priority.
  71. MockSinglePageInSingleProcessGraph mock_graph(graph());
  72. auto& frame_node = mock_graph.frame;
  73. EXPECT_FALSE(frame_node->is_audible());
  74. EXPECT_EQ(observer().GetVoteCount(), 1u);
  75. EXPECT_TRUE(observer().HasVote(
  76. voter_id(), GetExecutionContext(frame_node.get()),
  77. base::TaskPriority::LOWEST, FrameAudibleVoter::kFrameAudibleReason));
  78. // Make the frame visible. This should increase the priority.
  79. mock_graph.frame->SetIsAudible(true);
  80. EXPECT_EQ(observer().GetVoteCount(), 1u);
  81. EXPECT_TRUE(observer().HasVote(voter_id(),
  82. GetExecutionContext(frame_node.get()),
  83. base::TaskPriority::USER_BLOCKING,
  84. FrameAudibleVoter::kFrameAudibleReason));
  85. // Deleting the frame should invalidate the vote.
  86. frame_node.reset();
  87. EXPECT_EQ(observer().GetVoteCount(), 0u);
  88. }
  89. } // namespace execution_context_priority
  90. } // namespace performance_manager