File: session_restore_stats_collector_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (363 lines) | stat: -rw-r--r-- 14,117 bytes parent folder | download | duplicates (6)
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/sessions/session_restore_stats_collector.h"

#include <stddef.h>
#include <memory>
#include <utility>
#include <vector>

#include "base/memory/raw_ptr.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/render_widget_host.h"
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/test_web_contents_factory.h"
#include "content/public/test/web_contents_tester.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

using TabLoaderStats = SessionRestoreStatsCollector::TabLoaderStats;
using StatsReportingDelegate =
    SessionRestoreStatsCollector::StatsReportingDelegate;

// A mock StatsReportingDelegate. This is used by the unittests to validate the
// reporting and lifetime behaviour of the SessionRestoreStatsCollector under
// test.
class MockStatsReportingDelegate : public StatsReportingDelegate {
 public:
  MockStatsReportingDelegate()
      : report_tab_loader_stats_call_count_(0u),
        report_stats_collector_death_call_count_(0u) {}

  MockStatsReportingDelegate(const MockStatsReportingDelegate&) = delete;
  MockStatsReportingDelegate& operator=(const MockStatsReportingDelegate&) =
      delete;

  ~MockStatsReportingDelegate() override = default;

  void ReportTabLoaderStats(const TabLoaderStats& stats) override {
    report_tab_loader_stats_call_count_++;
    tab_loader_stats_ = stats;
  }

  // This is not part of the StatsReportingDelegate, but an added function that
  // is invoked by the PassthroughStatsReportingDelegate when it dies. This
  // allows the tests to be notified the moment the underlying stats collector
  // terminates itself.
  void ReportStatsCollectorDeath() {
    report_stats_collector_death_call_count_++;
  }

  void ExpectReportTabLoaderStatsCalled(
      size_t tab_count,
      int foreground_tab_first_paint_ms,
      SessionRestoreStatsCollector::SessionRestorePaintFinishReasonUma
          finish_reason) {
    EXPECT_EQ(tab_count, tab_loader_stats_.tab_count);
    EXPECT_EQ(base::Milliseconds(foreground_tab_first_paint_ms),
              tab_loader_stats_.foreground_tab_first_paint);
    EXPECT_EQ(tab_loader_stats_.tab_first_paint_reason, finish_reason);
  }

  void EnsureNoUnexpectedCalls() {
    EXPECT_EQ(0u, report_tab_loader_stats_call_count_);
    EXPECT_EQ(0u, report_stats_collector_death_call_count_);

    report_tab_loader_stats_call_count_ = 0u;
    report_stats_collector_death_call_count_ = 0u;
    tab_loader_stats_ = TabLoaderStats();
  }

  void ExpectEnded() {
    EXPECT_EQ(1u, report_tab_loader_stats_call_count_);
    EXPECT_EQ(1u, report_stats_collector_death_call_count_);
  }

 private:
  size_t report_tab_loader_stats_call_count_;
  size_t report_stats_collector_death_call_count_;
  TabLoaderStats tab_loader_stats_;
};

// A pass-through stats reporting delegate. This is used to decouple the
// lifetime of the mock reporting delegate from the SessionRestoreStatsCollector
// under test. The SessionRestoreStatsCollector has ownership of this delegate,
// which will notify the mock delegate upon its death.
class PassthroughStatsReportingDelegate : public StatsReportingDelegate {
 public:
  PassthroughStatsReportingDelegate() : reporting_delegate_(nullptr) {}

  PassthroughStatsReportingDelegate(const PassthroughStatsReportingDelegate&) =
      delete;
  PassthroughStatsReportingDelegate& operator=(
      const PassthroughStatsReportingDelegate&) = delete;

  ~PassthroughStatsReportingDelegate() override {
    reporting_delegate_->ReportStatsCollectorDeath();
  }

  void set_reporting_delegate(MockStatsReportingDelegate* reporting_delegate) {
    reporting_delegate_ = reporting_delegate;
  }

  void ReportTabLoaderStats(const TabLoaderStats& tab_loader_stats) override {
    reporting_delegate_->ReportTabLoaderStats(tab_loader_stats);
  }

 private:
  raw_ptr<MockStatsReportingDelegate> reporting_delegate_;
};

}  // namespace

class SessionRestoreStatsCollectorTest : public testing::Test {
 public:
  using RestoredTab = SessionRestoreDelegate::RestoredTab;

  SessionRestoreStatsCollectorTest()
      : task_environment_{base::test::TaskEnvironment::TimeSource::MOCK_TIME} {}

  SessionRestoreStatsCollectorTest(const SessionRestoreStatsCollectorTest&) =
      delete;
  SessionRestoreStatsCollectorTest& operator=(
      const SessionRestoreStatsCollectorTest&) = delete;

  void SetUp() override {
    test_web_contents_factory_ =
        std::make_unique<content::TestWebContentsFactory>();

    // Ownership of the reporting delegate is passed to the
    // SessionRestoreStatsCollector, but a raw pointer is kept to it so it can
    // be queried by the test.
    passthrough_reporting_delegate_ = new PassthroughStatsReportingDelegate();

    // Force creation of the stats collector.
    stats_collector_ = SessionRestoreStatsCollector::GetOrCreateInstance(
        base::TimeTicks::Now(), std::unique_ptr<StatsReportingDelegate>(
                                    passthrough_reporting_delegate_));
  }

  void TearDown() override {
    passthrough_reporting_delegate_ = nullptr;

    // Clean up any tabs that were generated by the unittest.
    restored_tabs_.clear();
    test_web_contents_factory_.reset();
  }

  // Advances the test clock by 1ms.
  void Tick() { task_environment_.FastForwardBy(base::Milliseconds(1)); }

  void Show(size_t tab_index) {
    restored_tabs_[tab_index].contents()->WasShown();
    GenerateRenderWidgetVisiblityChanged(tab_index, /*visible=*/true);
  }

  void Hide(size_t tab_index) {
    restored_tabs_[tab_index].contents()->WasHidden();
    GenerateRenderWidgetVisiblityChanged(tab_index, /*visible=*/false);
  }

  // Creates a restored tab backed by dummy WebContents/NavigationController/
  // RenderWidgetHost/RenderWidgetHostView.
  void CreateRestoredTab(bool is_active) {
    content::WebContents* contents =
        test_web_contents_factory_->CreateWebContents(&testing_profile_);
    std::vector<std::unique_ptr<content::NavigationEntry>> entries;
    entries.push_back(content::NavigationEntry::Create());
    contents->GetController().Restore(0, content::RestoreType::kRestored,
                                      &entries);
    // Create a last active time in the past.
    content::WebContentsTester::For(contents)->SetLastActiveTime(
        base::Time::Now() - base::Minutes(1));
    restored_tabs_.push_back(
        RestoredTab(contents, is_active, false, false, std::nullopt));
    if (is_active)
      Show(restored_tabs_.size() - 1);
  }

  // Generates a render widget host destroyed notification for the given tab.
  void GenerateRenderWidgetHostDestroyed(size_t tab_index) {
    content::WebContents* contents = restored_tabs_[tab_index].contents();
    stats_collector_->RenderWidgetHostDestroyed(
        contents->GetRenderWidgetHostView()->GetRenderWidgetHost());
  }

  // Generates a paint notification for the given tab.
  void GenerateRenderWidgetHostDidUpdateBackingStore(size_t tab_index) {
    content::WebContents* contents = restored_tabs_[tab_index].contents();
    content::RenderWidgetHost* host =
        contents->GetRenderWidgetHostView()->GetRenderWidgetHost();
    stats_collector_->RenderWidgetHostDidUpdateVisualProperties(host);
  }

  void GenerateRenderWidgetVisiblityChanged(size_t tab_index, bool visible) {
    content::WebContents* contents = restored_tabs_[tab_index].contents();
    content::RenderWidgetHost* host =
        contents->GetRenderWidgetHostView()->GetRenderWidgetHost();
    stats_collector_->RenderWidgetHostVisibilityChanged(host, visible);
  }

  content::BrowserTaskEnvironment task_environment_;
  // |task_environment_| needs to still be alive when
  // |testing_profile_| is destroyed.
  TestingProfile testing_profile_;

  // Inputs to the stats collector. Reset prior to each test.
  std::vector<RestoredTab> restored_tabs_;

  // A new web contents factory is generated per test. This automatically cleans
  // up any tabs created by previous tests.
  std::unique_ptr<content::TestWebContentsFactory> test_web_contents_factory_;

  // These are recreated for each test. The reporting delegate allows the test
  // to observe the behaviour of the SessionRestoreStatsCollector under test.
  raw_ptr<PassthroughStatsReportingDelegate, DanglingUntriaged>
      passthrough_reporting_delegate_;
  raw_ptr<SessionRestoreStatsCollector, DanglingUntriaged> stats_collector_;
};

TEST_F(SessionRestoreStatsCollectorTest, MultipleTabsLoadSerially) {
  MockStatsReportingDelegate mock_reporting_delegate;
  passthrough_reporting_delegate_->set_reporting_delegate(
      &mock_reporting_delegate);

  CreateRestoredTab(true);
  CreateRestoredTab(false);
  CreateRestoredTab(false);
  stats_collector_->TrackTabs(restored_tabs_);
  mock_reporting_delegate.EnsureNoUnexpectedCalls();
  // Foreground tab paints then finishes loading.
  Tick();  // 1ms.
  GenerateRenderWidgetHostDidUpdateBackingStore(0);
  mock_reporting_delegate.ExpectReportTabLoaderStatsCalled(
      3, 1, SessionRestoreStatsCollector::PAINT_FINISHED_UMA_DONE);
  mock_reporting_delegate.ExpectEnded();
}

// Test that if a single restored foreground tab is occluded at any point
// before first paint, the time to first paint is not recorded, and the
// FinishReason is PAINT_FINISHED_UMA_NO_COMPLETELY_VISIBLE_TABS.
TEST_F(SessionRestoreStatsCollectorTest, ForegroundTabOccluded) {
  MockStatsReportingDelegate mock_reporting_delegate;
  passthrough_reporting_delegate_->set_reporting_delegate(
      &mock_reporting_delegate);

  CreateRestoredTab(/*is_active=*/true);
  stats_collector_->TrackTabs(restored_tabs_);
  mock_reporting_delegate.EnsureNoUnexpectedCalls();
  Tick();  // 1ms.

  // Mark the tab as hidden/occluded.
  GenerateRenderWidgetVisiblityChanged(0, /*visible=*/false);
  Tick();  // 2ms.

  // Mark the tab as visible.
  GenerateRenderWidgetVisiblityChanged(0, /*visible=*/true);
  // Mark the tab as having painted. This should cause the
  // stats to be emitted, but with an empty foreground paint value because it
  // was not visible at one point during restore.
  GenerateRenderWidgetHostDidUpdateBackingStore(0);
  // Destroy the tab.
  GenerateRenderWidgetHostDestroyed(0);
  mock_reporting_delegate.ExpectReportTabLoaderStatsCalled(
      1, 0,
      SessionRestoreStatsCollector::
          PAINT_FINISHED_UMA_NO_COMPLETELY_VISIBLE_TABS);
  mock_reporting_delegate.ExpectEnded();
}

// Test that if the first tab painted was hidden/occluded before first paint,
// the first paint time of the second tab to be painted is recorded.
TEST_F(SessionRestoreStatsCollectorTest, FirstOfTwoTabsOccluded) {
  MockStatsReportingDelegate mock_reporting_delegate;
  passthrough_reporting_delegate_->set_reporting_delegate(
      &mock_reporting_delegate);

  CreateRestoredTab(/*is_active=*/true);
  CreateRestoredTab(/*is_active=*/true);
  stats_collector_->TrackTabs(restored_tabs_);
  mock_reporting_delegate.EnsureNoUnexpectedCalls();
  Tick();  // 1ms.

  // Mark the tab as hidden/occluded.
  GenerateRenderWidgetVisiblityChanged(0, /*visible=*/false);
  Tick();  // 2ms.

  // Mark the tab as visible.
  GenerateRenderWidgetVisiblityChanged(0, /*visible=*/true);

  // Mark the tab as having painted. Because it was occluded,
  // no stat should be emitted.
  GenerateRenderWidgetHostDidUpdateBackingStore(0);
  mock_reporting_delegate.EnsureNoUnexpectedCalls();

  Tick();  // 3ms.
  mock_reporting_delegate.EnsureNoUnexpectedCalls();
  Tick();  // 4ms.
  Tick();  // 5ms.

  // Mark the second tab as painted.
  GenerateRenderWidgetHostDidUpdateBackingStore(1);
  mock_reporting_delegate.ExpectReportTabLoaderStatsCalled(
      2, 5, SessionRestoreStatsCollector::PAINT_FINISHED_UMA_DONE);
  mock_reporting_delegate.ExpectEnded();
}

TEST_F(SessionRestoreStatsCollectorTest, LoadingTabDestroyedBeforePaint) {
  MockStatsReportingDelegate mock_reporting_delegate;
  passthrough_reporting_delegate_->set_reporting_delegate(
      &mock_reporting_delegate);

  CreateRestoredTab(true);
  stats_collector_->TrackTabs(restored_tabs_);
  mock_reporting_delegate.EnsureNoUnexpectedCalls();

  // Destroy the tab. Expect all timings to be zero.
  GenerateRenderWidgetHostDestroyed(0);
  mock_reporting_delegate.ExpectReportTabLoaderStatsCalled(
      1, 0, SessionRestoreStatsCollector::PAINT_FINISHED_UMA_NO_PAINT);
  mock_reporting_delegate.ExpectEnded();
}

TEST_F(SessionRestoreStatsCollectorTest, FocusSwitchNoForegroundPaintOrLoad) {
  MockStatsReportingDelegate mock_reporting_delegate;
  passthrough_reporting_delegate_->set_reporting_delegate(
      &mock_reporting_delegate);

  CreateRestoredTab(true);
  stats_collector_->TrackTabs(restored_tabs_);
  mock_reporting_delegate.EnsureNoUnexpectedCalls();

  // Create another tab and make it the foreground tab. This tab is not actually
  // being tracked by the SessionRestoreStatsCollector, but its paint events
  // will be observed.
  CreateRestoredTab(false);
  Hide(0);
  Show(1);

  // Load and paint the restored tab (now the background tab). Don't expect
  // any calls to the mock as a visible tab paint has not yet been observed.
  Tick();  // 1ms.
  GenerateRenderWidgetHostDidUpdateBackingStore(0);
  mock_reporting_delegate.EnsureNoUnexpectedCalls();
  Tick();  // 2ms.
  mock_reporting_delegate.EnsureNoUnexpectedCalls();

  // Mark the new foreground tab as having painted. This should cause the
  // stats to be emitted, but with empty foreground paint and load values.
  Tick();  // 3ms.
  GenerateRenderWidgetHostDidUpdateBackingStore(1);
  mock_reporting_delegate.ExpectReportTabLoaderStatsCalled(
      1, 0,
      SessionRestoreStatsCollector::
          PAINT_FINISHED_NON_RESTORED_TAB_PAINTED_FIRST);
  mock_reporting_delegate.ExpectEnded();
}