File: waap_ui_metrics_service_unittest.cc

package info (click to toggle)
chromium 143.0.7499.109-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,786,824 kB
  • sloc: cpp: 35,783,839; ansic: 7,477,365; javascript: 3,962,116; python: 1,480,521; xml: 764,832; asm: 710,816; pascal: 188,028; sh: 88,717; perl: 88,692; objc: 79,984; sql: 57,625; cs: 42,265; fortran: 24,101; makefile: 22,509; tcl: 15,277; php: 14,018; yacc: 9,043; ruby: 7,553; awk: 3,720; lisp: 3,233; lex: 1,330; ada: 727; jsp: 228; sed: 36
file content (213 lines) | stat: -rw-r--r-- 8,461 bytes parent folder | download | duplicates (2)
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
// Copyright 2025 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/ui/waap/waap_ui_metrics_service.h"

#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/time/time.h"
#include "chrome/browser/ui/waap/waap_ui_metrics_recorder.h"
#include "chrome/browser/ui/waap/waap_ui_metrics_service_factory.h"
#include "chrome/common/chrome_features.h"
#include "chrome/test/base/testing_profile.h"
#include "components/startup_metric_utils/browser/startup_metric_utils.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"

class WaapUIMetricsServiceTest : public testing::Test {
 public:
  WaapUIMetricsServiceTest() = default;
  ~WaapUIMetricsServiceTest() override = default;

  void SetUp() override {
    // WaapUIMetricsService is only available when the feature is enabled.
    feature_list_.InitAndEnableFeature(features::kInitialWebUIMetrics);
  }

 protected:
  base::HistogramTester* histogram_tester() { return &histogram_tester_; }
  TestingProfile* profile() { return &profile_; }

 private:
  content::BrowserTaskEnvironment task_environment_;
  base::test::ScopedFeatureList feature_list_;
  base::HistogramTester histogram_tester_;
  TestingProfile profile_;
};

// Tests that the WaapUIMetricsService is not created when the kInitialWebUI
// feature is disabled.
TEST(WaapUIMetricsServiceFeatureDisabledTest, ServiceNotCreated) {
  base::test::ScopedFeatureList feature_list;
  feature_list.InitAndDisableFeature(features::kInitialWebUIMetrics);

  content::BrowserTaskEnvironment task_environment;
  TestingProfile profile;

  WaapUIMetricsService* service =
      WaapUIMetricsServiceFactory::GetForProfile(&profile);
  EXPECT_FALSE(service);
}

#if !BUILDFLAG(IS_CHROMEOS)
// Tests that the OnFirstPaint method records a histogram on the first call,
// and does not record it again on subsequent calls.
TEST_F(WaapUIMetricsServiceTest, OnFirstPaint) {
  WaapUIMetricsService* service =
      WaapUIMetricsServiceFactory::GetForProfile(profile());
  ASSERT_TRUE(service);

  const base::TimeTicks paint_time = base::TimeTicks::Now();
  service->OnFirstPaint(paint_time);

  // For tests, startup temperature is undetermined.
  histogram_tester()->ExpectTotalCount(
      "InitialWebUI.Startup.ReloadButton.FirstPaint", 1);

  // Call a second time to ensure it's not recorded again.
  service->OnFirstPaint(paint_time);
  histogram_tester()->ExpectTotalCount(
      "InitialWebUI.Startup.ReloadButton.FirstPaint", 1);
}

// Tests that the OnFirstContentfulPaint method records a histogram on the
// first call, and does not record it again on subsequent calls.
TEST_F(WaapUIMetricsServiceTest, OnFirstContentfulPaint) {
  WaapUIMetricsService* service =
      WaapUIMetricsServiceFactory::GetForProfile(profile());
  ASSERT_TRUE(service);

  const base::TimeTicks paint_time = base::TimeTicks::Now();
  service->OnFirstContentfulPaint(paint_time);

  // For tests, startup temperature is undetermined.
  histogram_tester()->ExpectTotalCount(
      "InitialWebUI.Startup.ReloadButton.FirstContentfulPaint", 1);

  // Call a second time to ensure it's not recorded again.
  service->OnFirstContentfulPaint(paint_time);
  histogram_tester()->ExpectTotalCount(
      "InitialWebUI.Startup.ReloadButton.FirstContentfulPaint", 1);
}
#endif  // !BUILDFLAG(IS_CHROMEOS)

// Tests that the OnReloadButtonMousePressToNextPaint method records a
// histogram.
TEST_F(WaapUIMetricsServiceTest, OnReloadButtonMousePressToNextPaint) {
  WaapUIMetricsService* service =
      WaapUIMetricsServiceFactory::GetForProfile(profile());
  ASSERT_TRUE(service);

  base::HistogramTester histogram_tester;
  const auto start_ticks = base::TimeTicks::Now();
  const base::TimeDelta duration = base::Milliseconds(15);
  service->OnReloadButtonMousePressToNextPaint(start_ticks,
                                               start_ticks + duration);
  histogram_tester.ExpectUniqueTimeSample(
      "InitialWebUI.ReloadButton.MousePressToNextPaint", duration, 1);
}

// Tests that the OnReloadButtonMouseHoverToNextPaint method records a
// histogram.
TEST_F(WaapUIMetricsServiceTest, OnReloadButtonMouseHoverToNextPaint) {
  WaapUIMetricsService* service =
      WaapUIMetricsServiceFactory::GetForProfile(profile());
  ASSERT_TRUE(service);

  base::HistogramTester histogram_tester;
  const auto start_ticks = base::TimeTicks::Now();
  const base::TimeDelta latency = base::Milliseconds(10);
  service->OnReloadButtonMouseHoverToNextPaint(start_ticks,
                                               start_ticks + latency);
  histogram_tester.ExpectUniqueTimeSample(
      "InitialWebUI.ReloadButton.MouseHoverToNextPaint", latency, 1);
}

// Tests that the OnReloadButtonInput method records a histogram.
TEST_F(WaapUIMetricsServiceTest, OnReloadButtonInput) {
  WaapUIMetricsService* service =
      WaapUIMetricsServiceFactory::GetForProfile(profile());
  ASSERT_TRUE(service);

  base::HistogramTester histogram_tester;
  service->OnReloadButtonInput(
      WaapUIMetricsRecorder::ReloadButtonInputType::kMouseRelease);
  histogram_tester.ExpectBucketCount(
      "InitialWebUI.ReloadButton.InputCount",
      WaapUIMetricsRecorder::ReloadButtonInputType::kMouseRelease, 1);

  service->OnReloadButtonInput(
      WaapUIMetricsRecorder::ReloadButtonInputType::kKeyPress);
  histogram_tester.ExpectBucketCount(
      "InitialWebUI.ReloadButton.InputCount",
      WaapUIMetricsRecorder::ReloadButtonInputType::kKeyPress, 1);

  histogram_tester.ExpectTotalCount("InitialWebUI.ReloadButton.InputCount", 2);
}

// Tests that the OnReloadButtonInputToReload method records a histogram.
TEST_F(WaapUIMetricsServiceTest, OnReloadButtonInputToReload) {
  WaapUIMetricsService* service =
      WaapUIMetricsServiceFactory::GetForProfile(profile());
  ASSERT_TRUE(service);

  base::HistogramTester histogram_tester;
  const auto start_ticks = base::TimeTicks::Now();
  const base::TimeDelta latency = base::Milliseconds(10);
  service->OnReloadButtonInputToReload(
      start_ticks, start_ticks + latency,
      WaapUIMetricsRecorder::ReloadButtonInputType::kKeyPress);
  histogram_tester.ExpectUniqueTimeSample(
      "InitialWebUI.ReloadButton.InputToReload.KeyPress", latency, 1);
}

// Tests that the OnReloadButtonInputToStop method records a histogram.
TEST_F(WaapUIMetricsServiceTest, OnReloadButtonInputToStop) {
  WaapUIMetricsService* service =
      WaapUIMetricsServiceFactory::GetForProfile(profile());
  ASSERT_TRUE(service);

  base::HistogramTester histogram_tester;
  const auto start_ticks = base::TimeTicks::Now();
  const base::TimeDelta latency = base::Milliseconds(12);
  service->OnReloadButtonInputToStop(
      start_ticks, start_ticks + latency,
      WaapUIMetricsRecorder::ReloadButtonInputType::kMouseRelease);
  histogram_tester.ExpectUniqueTimeSample(
      "InitialWebUI.ReloadButton.InputToStop.MouseRelease", latency, 1);
}

// Tests that the OnReloadButtonInputToNextPaint method records a histogram.
TEST_F(WaapUIMetricsServiceTest, OnReloadButtonInputToNextPaint) {
  WaapUIMetricsService* service =
      WaapUIMetricsServiceFactory::GetForProfile(profile());
  ASSERT_TRUE(service);

  base::HistogramTester histogram_tester;
  const auto start_ticks = base::TimeTicks::Now();
  const base::TimeDelta latency = base::Milliseconds(14);
  service->OnReloadButtonInputToNextPaint(
      start_ticks, start_ticks + latency,
      WaapUIMetricsRecorder::ReloadButtonInputType::kKeyPress);
  histogram_tester.ExpectUniqueTimeSample(
      "InitialWebUI.ReloadButton.InputToNextPaint.KeyPress", latency, 1);
}

// Tests that the OnReloadButtonChangeVisibleModeToNextPaint method records a
// histogram.
TEST_F(WaapUIMetricsServiceTest, OnReloadButtonChangeVisibleModeToNextPaint) {
  WaapUIMetricsService* service =
      WaapUIMetricsServiceFactory::GetForProfile(profile());
  ASSERT_TRUE(service);

  base::HistogramTester histogram_tester;
  const auto start_ticks = base::TimeTicks::Now();
  const base::TimeDelta latency = base::Milliseconds(16);
  service->OnReloadButtonChangeVisibleModeToNextPaint(
      start_ticks, start_ticks + latency,
      WaapUIMetricsRecorder::ReloadButtonMode::kStop);
  histogram_tester.ExpectUniqueTimeSample(
      "InitialWebUI.ReloadButton.ChangeVisibleModeToNextPaintInStop", latency,
      1);
}