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
ash / webui / diagnostics_ui / diagnostics_metrics_message_handler_unittest.cc [blame]
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/webui/diagnostics_ui/diagnostics_metrics_message_handler.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "content/public/test/test_web_ui.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace ash::diagnostics::metrics {
namespace {
const base::TimeDelta kDefaultTimeDelta = base::Minutes(1);
// Handler names:
const char kRecordNavigation[] = "recordNavigation";
// Metrics:
const char kConnectivityOpenDurationMetric[] =
"ChromeOS.DiagnosticsUi.Connectivity.OpenDuration";
const char kInputOpenDurationMetric[] =
"ChromeOS.DiagnosticsUi.Input.OpenDuration";
const char kSystemOpenDurationMetric[] =
"ChromeOS.DiagnosticsUi.System.OpenDuration";
class DiagnosticsMetricsMessageHandlerTest : public testing::Test {
public:
DiagnosticsMetricsMessageHandlerTest()
: task_environment_(base::test::TaskEnvironment::TimeSource::MOCK_TIME),
web_ui_(),
histograms() {}
~DiagnosticsMetricsMessageHandlerTest() override = default;
void InitializeHandler(NavigationView view) {
handler_ = std::make_unique<DiagnosticsMetricsMessageHandler>(view);
handler_->SetWebUiForTesting(&web_ui_);
handler_->RegisterMessages();
}
void SendRecordNavigation(NavigationView from, NavigationView to) {
base::Value::List args;
args.Append(static_cast<int>(from));
args.Append(static_cast<int>(to));
web_ui_.HandleReceivedMessage(kRecordNavigation, args);
task_environment_.RunUntilIdle();
}
protected:
void AdvanceClock(base::TimeDelta delta) {
task_environment_.FastForwardBy(delta);
}
base::test::TaskEnvironment task_environment_;
content::TestWebUI web_ui_;
std::unique_ptr<DiagnosticsMetricsMessageHandler> handler_;
base::HistogramTester histograms;
};
} // namespace
TEST_F(DiagnosticsMetricsMessageHandlerTest, AbleToRegisterMessages) {
EXPECT_NO_FATAL_FAILURE(InitializeHandler(NavigationView::kInput));
}
TEST_F(DiagnosticsMetricsMessageHandlerTest, InitializedCorrectly) {
NavigationView expected_view = NavigationView::kSystem;
InitializeHandler(expected_view);
AdvanceClock(kDefaultTimeDelta);
EXPECT_EQ(expected_view, handler_->GetCurrentViewForTesting());
EXPECT_EQ(kDefaultTimeDelta,
handler_->GetElapsedNavigationTimeDeltaForTesting());
}
TEST_F(DiagnosticsMetricsMessageHandlerTest, HandleNoRecordNavigationMessage) {
NavigationView expected_initial_view = NavigationView::kSystem;
InitializeHandler(expected_initial_view);
AdvanceClock(kDefaultTimeDelta);
histograms.ExpectTotalCount(
ash::diagnostics::metrics::kConnectivityOpenDurationMetric,
/** count= */ 0);
histograms.ExpectTotalCount(
ash::diagnostics::metrics::kConnectivityOpenDurationMetric,
/** count= */ 0);
histograms.ExpectTotalCount(
ash::diagnostics::metrics::kInputOpenDurationMetric, /** count= */ 0);
EXPECT_EQ(expected_initial_view, handler_->GetCurrentViewForTesting());
EXPECT_EQ(kDefaultTimeDelta,
handler_->GetElapsedNavigationTimeDeltaForTesting());
// Application shutdown should trigger metrics.
handler_.reset();
task_environment_.RunUntilIdle();
histograms.ExpectTimeBucketCount(kSystemOpenDurationMetric,
/** elapsed_time */ kDefaultTimeDelta,
/** count= */ 1);
histograms.ExpectTotalCount(
ash::diagnostics::metrics::kSystemOpenDurationMetric, /** count= */ 1);
histograms.ExpectTotalCount(
ash::diagnostics::metrics::kConnectivityOpenDurationMetric,
/** count= */ 0);
histograms.ExpectTotalCount(
ash::diagnostics::metrics::kInputOpenDurationMetric, /** count= */ 0);
}
TEST_F(DiagnosticsMetricsMessageHandlerTest, HandleRecordNavigationMessage) {
NavigationView expected_initial_view = NavigationView::kSystem;
InitializeHandler(expected_initial_view);
EXPECT_EQ(expected_initial_view, handler_->GetCurrentViewForTesting());
AdvanceClock(kDefaultTimeDelta);
SendRecordNavigation(expected_initial_view, NavigationView::kConnectivity);
EXPECT_EQ(NavigationView::kConnectivity,
handler_->GetCurrentViewForTesting());
EXPECT_EQ(base::Minutes(0),
handler_->GetElapsedNavigationTimeDeltaForTesting());
}
TEST_F(DiagnosticsMetricsMessageHandlerTest,
HandleRecordNavigationMultipleEvents) {
NavigationView initial_view = NavigationView::kConnectivity;
InitializeHandler(initial_view);
AdvanceClock(kDefaultTimeDelta);
histograms.ExpectTotalCount(kSystemOpenDurationMetric, /** count= */ 0);
histograms.ExpectTotalCount(kConnectivityOpenDurationMetric, /** count= */ 0);
histograms.ExpectTotalCount(kInputOpenDurationMetric, /** count= */ 0);
EXPECT_EQ(NavigationView::kConnectivity,
handler_->GetCurrentViewForTesting());
EXPECT_EQ(kDefaultTimeDelta,
handler_->GetElapsedNavigationTimeDeltaForTesting());
// Subsequent record navigations will trigger metric recording.
SendRecordNavigation(NavigationView::kConnectivity, NavigationView::kInput);
AdvanceClock(kDefaultTimeDelta);
histograms.ExpectTimeBucketCount(kConnectivityOpenDurationMetric,
/** elapsed_time */ kDefaultTimeDelta,
/** count= */ 1);
EXPECT_EQ(NavigationView::kInput, handler_->GetCurrentViewForTesting());
EXPECT_EQ(kDefaultTimeDelta,
handler_->GetElapsedNavigationTimeDeltaForTesting());
SendRecordNavigation(NavigationView::kInput, NavigationView::kSystem);
AdvanceClock(kDefaultTimeDelta);
histograms.ExpectTimeBucketCount(kInputOpenDurationMetric,
/** elapsed_time */ kDefaultTimeDelta,
/** count= */ 1);
EXPECT_EQ(NavigationView::kSystem, handler_->GetCurrentViewForTesting());
EXPECT_EQ(kDefaultTimeDelta,
handler_->GetElapsedNavigationTimeDeltaForTesting());
// Navigating to a screen you viewed before also updates metrics.
SendRecordNavigation(NavigationView::kSystem, NavigationView::kConnectivity);
AdvanceClock(kDefaultTimeDelta);
histograms.ExpectTimeBucketCount(kSystemOpenDurationMetric,
/** elapsed_time */ kDefaultTimeDelta,
/** count= */ 1);
EXPECT_EQ(NavigationView::kConnectivity,
handler_->GetCurrentViewForTesting());
EXPECT_EQ(kDefaultTimeDelta,
handler_->GetElapsedNavigationTimeDeltaForTesting());
// Application shutdown should trigger metrics.
AdvanceClock(kDefaultTimeDelta);
handler_.reset();
histograms.ExpectTimeBucketCount(kConnectivityOpenDurationMetric,
/** elapsed_time */ kDefaultTimeDelta,
/** count= */ 1);
histograms.ExpectTotalCount(kSystemOpenDurationMetric, /** count= */ 1);
histograms.ExpectTotalCount(kConnectivityOpenDurationMetric, /** count= */ 2);
histograms.ExpectTotalCount(kInputOpenDurationMetric, /** count= */ 1);
}
TEST_F(DiagnosticsMetricsMessageHandlerTest,
HandleRecordNavigationWithoutArgs) {
base::Value::List args;
NavigationView expected_view = NavigationView::kSystem;
InitializeHandler(expected_view);
EXPECT_NO_FATAL_FAILURE(
web_ui_.HandleReceivedMessage(kRecordNavigation, args));
EXPECT_EQ(expected_view, handler_->GetCurrentViewForTesting());
}
TEST_F(DiagnosticsMetricsMessageHandlerTest, HandleRecordNavigationWithOneArg) {
base::Value::List args;
args.Append(0);
NavigationView expected_view = NavigationView::kSystem;
InitializeHandler(expected_view);
EXPECT_NO_FATAL_FAILURE(
web_ui_.HandleReceivedMessage(kRecordNavigation, args));
EXPECT_EQ(expected_view, handler_->GetCurrentViewForTesting());
}
TEST_F(DiagnosticsMetricsMessageHandlerTest,
HandleRecordNavigationWithInvalidArgs) {
base::Value::List args;
args.Append("0");
args.Append(base::Value());
NavigationView expected_view = NavigationView::kSystem;
InitializeHandler(expected_view);
EXPECT_NO_FATAL_FAILURE(
web_ui_.HandleReceivedMessage(kRecordNavigation, args));
EXPECT_EQ(expected_view, handler_->GetCurrentViewForTesting());
}
TEST_F(DiagnosticsMetricsMessageHandlerTest,
HandleRecordNavigationWithMatchingArgs) {
base::Value::List args;
args.Append(1);
args.Append(1);
NavigationView expected_view = NavigationView::kSystem;
InitializeHandler(expected_view);
EXPECT_NO_FATAL_FAILURE(
web_ui_.HandleReceivedMessage(kRecordNavigation, args));
EXPECT_EQ(expected_view, handler_->GetCurrentViewForTesting());
}
TEST_F(DiagnosticsMetricsMessageHandlerTest,
HandleRecordNavigationWithOutOfRangeArgs) {
base::Value::List args;
args.Append(-100);
args.Append(100);
NavigationView expected_view = NavigationView::kSystem;
InitializeHandler(expected_view);
EXPECT_NO_FATAL_FAILURE(
web_ui_.HandleReceivedMessage(kRecordNavigation, args));
EXPECT_EQ(expected_view, handler_->GetCurrentViewForTesting());
}
} // namespace ash::diagnostics::metrics