File: frozen_frame_aggregator_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (266 lines) | stat: -rw-r--r-- 8,483 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/performance_manager/freezing/frozen_frame_aggregator.h"

#include <memory>

#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "components/performance_manager/graph/frame_node_impl.h"
#include "components/performance_manager/graph/page_node_impl.h"
#include "components/performance_manager/graph/process_node_impl.h"
#include "components/performance_manager/test_support/graph/mock_process_node_observer.h"
#include "components/performance_manager/test_support/graph_test_harness.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace performance_manager {

namespace {

using LifecycleState = PageNodeImpl::LifecycleState;

}  // namespace

class FrozenFrameAggregatorTest : public GraphTestHarness {
 public:
  FrozenFrameAggregatorTest(const FrozenFrameAggregatorTest&) = delete;
  FrozenFrameAggregatorTest& operator=(const FrozenFrameAggregatorTest&) =
      delete;

 protected:
  using Super = GraphTestHarness;

  FrozenFrameAggregatorTest() = default;
  ~FrozenFrameAggregatorTest() override = default;

  void SetUp() override {
    Super::SetUp();
    ffa_ = new FrozenFrameAggregator();
    graph()->PassToGraph(base::WrapUnique(ffa_.get()));
    process_node_ = CreateNode<ProcessNodeImpl>();
    page_node_ = CreateNode<PageNodeImpl>();
  }

  template <typename NodeType>
  void ExpectData(NodeType* node,
                  uint32_t current_frame_count,
                  uint32_t frozen_frame_count) {
    FrozenData& data = FrozenData::Get(node);
    EXPECT_EQ(current_frame_count, data.current_frame_count());
    EXPECT_EQ(frozen_frame_count, data.frozen_frame_count());
  }

  void ExpectPageData(uint32_t current_frame_count,
                      uint32_t frozen_frame_count) {
    ExpectData(page_node_.get(), current_frame_count, frozen_frame_count);
  }

  void ExpectProcessData(uint32_t current_frame_count,
                         uint32_t frozen_frame_count) {
    ExpectData(process_node_.get(), current_frame_count, frozen_frame_count);
  }

  void ExpectRunning() {
    EXPECT_EQ(LifecycleState::kRunning, page_node_->GetLifecycleState());
  }

  void ExpectFrozen() {
    EXPECT_EQ(LifecycleState::kFrozen, page_node_->GetLifecycleState());
  }

  TestNodeWrapper<FrameNodeImpl> CreateFrame(FrameNodeImpl* parent_frame_node,
                                             bool is_current = true) {
    return TestNodeWrapper<FrameNodeImpl>::Create(
        graph(), process_node_.get(), page_node_.get(), parent_frame_node,
        /*outer_document_for_fenced_frame=*/nullptr, NextTestFrameRoutingId(),
        blink::LocalFrameToken(), content::BrowsingInstanceId(),
        content::SiteInstanceGroupId(), is_current);
  }

  raw_ptr<FrozenFrameAggregator> ffa_;
  TestNodeWrapper<ProcessNodeImpl> process_node_;
  TestNodeWrapper<PageNodeImpl> page_node_;
};

TEST_F(FrozenFrameAggregatorTest, NotCurrent) {
  ExpectProcessData(0, 0);

  // Add a non-current main frame.
  auto f0 = CreateFrame(nullptr, /*is_current=*/false);
  ExpectProcessData(0, 0);

  // Make it current. The frame starts being counted.
  FrameNodeImpl::UpdateCurrentFrame(/*previous_frame_node=*/nullptr,
                                    /*current_frame_node=*/f0.get(), graph());
  ExpectProcessData(1, 0);

  f0->SetLifecycleState(LifecycleState::kFrozen);
  ExpectProcessData(1, 1);

  // Make no longer current. Stops being counted.
  FrameNodeImpl::UpdateCurrentFrame(/*previous_frame_node=*/f0.get(),
                                    /*current_frame_node=*/nullptr, graph());
  ExpectProcessData(0, 0);
}

TEST_F(FrozenFrameAggregatorTest, ProcessAggregation) {
  LenientMockProcessNodeObserver obs;
  graph()->AddProcessNodeObserver(&obs);

  ExpectProcessData(0, 0);

  // Add a main frame.
  auto f0 = CreateFrame(nullptr);
  ExpectProcessData(1, 0);

  // Make the frame frozen and expect a notification.
  EXPECT_CALL(obs, OnAllFramesInProcessFrozen(process_node_.get()));
  f0->SetLifecycleState(LifecycleState::kFrozen);
  testing::Mock::VerifyAndClear(&obs);
  ExpectProcessData(1, 1);

  // Create another process and another page.
  auto proc2 = CreateNode<ProcessNodeImpl>();
  auto page2 = CreateNode<PageNodeImpl>();
  ExpectProcessData(1, 1);

  // Create a child frame for the first page hosted in the second process.
  auto f1 = CreateFrameNodeAutoId(proc2.get(), page_node_.get(), f0.get());
  ExpectProcessData(1, 1);

  // Freeze the child frame and expect |proc2| to receive an event, but not
  // |process_node_|.
  EXPECT_CALL(obs, OnAllFramesInProcessFrozen(proc2.get()));
  f1->SetLifecycleState(LifecycleState::kFrozen);
  ExpectProcessData(1, 1);

  // Unfreeze both frames.
  f0->SetLifecycleState(LifecycleState::kRunning);
  ExpectProcessData(1, 0);
  f1->SetLifecycleState(LifecycleState::kRunning);
  ExpectProcessData(1, 0);

  // Create a main frame in the second page, but that's in the first process.
  auto f2 = CreateFrameNodeAutoId(process_node_.get(), page2.get(), nullptr);
  ExpectProcessData(2, 0);

  // Freeze the main frame in the second page.
  f2->SetLifecycleState(LifecycleState::kFrozen);
  ExpectProcessData(2, 1);

  // Freeze the child frame of the first page, hosted in the other process.
  EXPECT_CALL(obs, OnAllFramesInProcessFrozen(proc2.get()));
  f1->SetLifecycleState(LifecycleState::kFrozen);
  ExpectProcessData(2, 1);

  // Freeze the main frame of the first page.
  EXPECT_CALL(obs, OnAllFramesInProcessFrozen(process_node_.get()));
  f0->SetLifecycleState(LifecycleState::kFrozen);
  testing::Mock::VerifyAndClear(&obs);
  ExpectProcessData(2, 2);

  // Destroy the child frame in the other process, and then kill that process.
  f1.reset();
  ExpectProcessData(2, 2);
  proc2.reset();
  ExpectProcessData(2, 2);

  // Kill the main frame of the second page.
  f2.reset();
  ExpectProcessData(1, 1);

  // Kill the main frame of the first page.
  f0.reset();
  ExpectProcessData(0, 0);

  graph()->RemoveProcessNodeObserver(&obs);
}

TEST_F(FrozenFrameAggregatorTest, PageAggregation) {
  ExpectPageData(0, 0);
  ExpectRunning();

  // Add a current frame.
  auto f0 = CreateFrame(nullptr);
  ExpectPageData(1, 0);
  ExpectRunning();

  // Freeze the frame.
  f0->SetLifecycleState(LifecycleState::kFrozen);
  ExpectPageData(1, 1);
  ExpectFrozen();

  // Unfreeze the frame.
  f0->SetLifecycleState(LifecycleState::kRunning);
  ExpectPageData(1, 0);
  ExpectRunning();

  // Add a child frame.
  auto f1 = CreateFrame(f0.get());
  ExpectPageData(2, 0);
  ExpectRunning();

  // Freeze them both.
  f1->SetLifecycleState(LifecycleState::kFrozen);
  ExpectPageData(2, 1);
  ExpectRunning();
  f0->SetLifecycleState(LifecycleState::kFrozen);
  ExpectPageData(2, 2);
  ExpectFrozen();

  // Unfreeze them both.
  f0->SetLifecycleState(LifecycleState::kRunning);
  ExpectPageData(2, 1);
  ExpectRunning();
  f1->SetLifecycleState(LifecycleState::kRunning);
  ExpectPageData(2, 0);
  ExpectRunning();

  // Create a third frame that is not current.
  auto f1a = CreateFrame(f0.get(), /*is_current=*/false);
  ExpectPageData(2, 0);
  ExpectRunning();

  // Swap the f1 and f1a.
  FrameNodeImpl::UpdateCurrentFrame(/*previous_frame_node=*/f1.get(),
                                    /*current_frame_node=*/f1a.get(), graph());
  ExpectPageData(2, 0);
  ExpectRunning();

  // Freeze the original frame and swap it back.
  f1->SetLifecycleState(LifecycleState::kFrozen);
  FrameNodeImpl::UpdateCurrentFrame(/*previous_frame_node=*/f1a.get(),
                                    /*current_frame_node=*/f1.get(), graph());
  ExpectPageData(2, 1);
  ExpectRunning();

  // Freeze the non-current frame and expect nothing to change.
  f1a->SetLifecycleState(LifecycleState::kFrozen);
  ExpectPageData(2, 1);
  ExpectRunning();

  // Remove the non-current frame and expect nothing to change.
  f1a.reset();
  ExpectPageData(2, 1);
  ExpectRunning();

  // Remove the frozen child frame and expect a change.
  f1.reset();
  ExpectPageData(1, 0);
  ExpectRunning();

  // Freeze the main frame again.
  f0->SetLifecycleState(LifecycleState::kFrozen);
  ExpectPageData(1, 1);
  ExpectFrozen();

  // Remove the main frame. An empty page is always considered as "running".
  f0.reset();
  ExpectPageData(0, 0);
  ExpectRunning();
}

}  // namespace performance_manager