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
364
365
366
367
368
369
ash / metrics / stylus_metrics_recorder_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/metrics/stylus_metrics_recorder.h"
#include <memory>
#include <set>
#include <string>
#include "ash/system/power/peripheral_battery_listener.h"
#include "ash/system/power/peripheral_battery_tests.h"
#include "ash/test/ash_test_base.h"
#include "base/containers/contains.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/metrics/histogram_tester.h"
#include "testing/gmock/include/gmock/gmock.h"
using BatteryInfo = ash::PeripheralBatteryListener::BatteryInfo;
namespace ash {
namespace {
const char kHistogramGarageSessionMetric[] =
"ChromeOS.FeatureUsage.StylusDetachedFromGarageSession";
const char kHistogramGarageSessionUsetimeMetric[] =
"ChromeOS.FeatureUsage.StylusDetachedFromGarageSession.Usetime";
const char kHistogramDockSessionMetric[] =
"ChromeOS.FeatureUsage.StylusDetachedFromDockSession";
const char kHistogramDockSessionUsetimeMetric[] =
"ChromeOS.FeatureUsage.StylusDetachedFromDockSession.Usetime";
const char kHistogramGarageOrDockSessionMetric[] =
"ChromeOS.FeatureUsage.StylusDetachedFromGarageOrDockSession";
const char kHistogramGarageOrDockSessionUsetimeMetric[] =
"ChromeOS.FeatureUsage.StylusDetachedFromGarageOrDockSession.Usetime";
enum class StylusChargingStyle { kDock, kGarage };
std::string BatteryKey(StylusChargingStyle style) {
return (style == StylusChargingStyle::kGarage) ? "garaged-stylus-charger"
: "docked-stylus-charger";
}
// Test fixture for the StylusMetricsRecorder class.
class StylusMetricsRecorderTest : public AshTestBase {
public:
StylusMetricsRecorderTest()
: AshTestBase(base::test::TaskEnvironment::TimeSource::MOCK_TIME) {}
StylusMetricsRecorderTest(const StylusMetricsRecorderTest&) = delete;
StylusMetricsRecorderTest& operator=(const StylusMetricsRecorderTest&) =
delete;
~StylusMetricsRecorderTest() override = default;
// AshTestBase:
void SetUp() override {
AshTestBase::SetUp();
stylus_metrics_recorder_ = std::make_unique<ash::StylusMetricsRecorder>();
histogram_tester_ = std::make_unique<base::HistogramTester>();
}
void TearDown() override {
stylus_metrics_recorder_.reset();
AshTestBase::TearDown();
}
base::TimeTicks NowTicks() { return task_environment()->NowTicks(); }
void AdvanceClock(base::TimeDelta delta) {
task_environment()->AdvanceClock(delta);
}
BatteryInfo ConstructBatteryInfo(
StylusChargingStyle style,
BatteryInfo::ChargeStatus charge_status,
bool battery_report_eligible = true,
BatteryInfo::PeripheralType type =
BatteryInfo::PeripheralType::kStylusViaCharger) {
const std::string key = BatteryKey(style);
const int level = 50;
const std::u16string name = base::ASCIIToUTF16("name:" + key);
const std::string btaddr = "";
return BatteryInfo(key, name, level, battery_report_eligible, NowTicks(),
type, charge_status, btaddr);
}
void SetChargerState(StylusChargingStyle style,
BatteryInfo::ChargeStatus charge_status,
bool battery_report_eligible = true,
BatteryInfo::PeripheralType type =
BatteryInfo::PeripheralType::kStylusViaCharger) {
const BatteryInfo info = ConstructBatteryInfo(
style, charge_status, battery_report_eligible, type);
if (!base::Contains(known_batteries_, info.key)) {
stylus_metrics_recorder_->OnAddingBattery(info);
known_batteries_.insert(info.key);
}
stylus_metrics_recorder_->OnUpdatedBatteryLevel(info);
}
void RemoveCharger(StylusChargingStyle style,
BatteryInfo::ChargeStatus charge_status,
bool battery_report_eligible = true,
BatteryInfo::PeripheralType type =
BatteryInfo::PeripheralType::kStylusViaCharger) {
const BatteryInfo info = ConstructBatteryInfo(
style, charge_status, battery_report_eligible, type);
if (base::Contains(known_batteries_, info.key)) {
stylus_metrics_recorder_->OnRemovingBattery(info);
known_batteries_.erase(info.key);
}
}
protected:
// The test target.
std::unique_ptr<StylusMetricsRecorder> stylus_metrics_recorder_;
// Used to verify recorded data.
std::unique_ptr<base::HistogramTester> histogram_tester_;
// Track whether batteries are known or need to be added.
std::set<std::string> known_batteries_;
};
} // namespace
// Verifies that histogram is not recorded when no events are received.
TEST_F(StylusMetricsRecorderTest, Baseline) {
histogram_tester_->ExpectTotalCount(kHistogramGarageSessionMetric, 0);
histogram_tester_->ExpectTotalCount(kHistogramDockSessionMetric, 0);
histogram_tester_->ExpectTotalCount(
kHistogramGarageOrDockSessionUsetimeMetric, 0);
histogram_tester_->ExpectTotalCount(kHistogramGarageSessionUsetimeMetric, 0);
histogram_tester_->ExpectTotalCount(kHistogramDockSessionUsetimeMetric, 0);
histogram_tester_->ExpectTotalCount(
kHistogramGarageOrDockSessionUsetimeMetric, 0);
}
TEST_F(StylusMetricsRecorderTest, BaselineStayInGarage) {
const base::TimeDelta kTimeSpentCharging = base::Minutes(5);
SetChargerState(StylusChargingStyle::kGarage,
BatteryInfo::ChargeStatus::kCharging);
AdvanceClock(kTimeSpentCharging);
// By removing the battery, we force the stylus_metrics_recorder to close out
// the session.
RemoveCharger(StylusChargingStyle::kGarage,
BatteryInfo::ChargeStatus::kCharging);
histogram_tester_->ExpectTotalCount(kHistogramGarageSessionMetric, 0);
histogram_tester_->ExpectTotalCount(kHistogramDockSessionMetric, 0);
histogram_tester_->ExpectTotalCount(kHistogramGarageOrDockSessionMetric, 0);
histogram_tester_->ExpectTotalCount(kHistogramGarageSessionUsetimeMetric, 0);
histogram_tester_->ExpectTotalCount(kHistogramDockSessionUsetimeMetric, 0);
histogram_tester_->ExpectTotalCount(
kHistogramGarageOrDockSessionUsetimeMetric, 0);
}
TEST_F(StylusMetricsRecorderTest, BaselineStayInDock) {
const base::TimeDelta kTimeSpentCharging = base::Minutes(5);
SetChargerState(StylusChargingStyle::kDock,
BatteryInfo::ChargeStatus::kCharging);
AdvanceClock(kTimeSpentCharging);
// By removing the battery, we force the stylus_metrics_recorder to close out
// the session.
RemoveCharger(StylusChargingStyle::kDock,
BatteryInfo::ChargeStatus::kCharging);
histogram_tester_->ExpectTotalCount(kHistogramGarageSessionMetric, 0);
histogram_tester_->ExpectTotalCount(kHistogramDockSessionMetric, 0);
histogram_tester_->ExpectTotalCount(kHistogramGarageOrDockSessionMetric, 0);
histogram_tester_->ExpectTotalCount(kHistogramGarageSessionUsetimeMetric, 0);
histogram_tester_->ExpectTotalCount(kHistogramDockSessionUsetimeMetric, 0);
histogram_tester_->ExpectTotalCount(
kHistogramGarageOrDockSessionUsetimeMetric, 0);
}
TEST_F(StylusMetricsRecorderTest, RemovedFromGarage) {
const base::TimeDelta kTimeSpentInUse = base::Minutes(5);
const base::TimeDelta kTimeSpentCharging = base::Minutes(1);
SetChargerState(StylusChargingStyle::kGarage,
BatteryInfo::ChargeStatus::kDischarging);
AdvanceClock(kTimeSpentInUse);
// By removing the battery, we force the stylus_metrics_recorder to close out
// the session.
SetChargerState(StylusChargingStyle::kGarage,
BatteryInfo::ChargeStatus::kCharging);
// Step time further when we're back on charge, to make sure this time is not
// counted
AdvanceClock(kTimeSpentCharging);
RemoveCharger(StylusChargingStyle::kGarage,
BatteryInfo::ChargeStatus::kCharging);
histogram_tester_->ExpectTotalCount(kHistogramDockSessionMetric, 0);
histogram_tester_->ExpectBucketCount(
kHistogramGarageSessionMetric,
static_cast<int>(
feature_usage::FeatureUsageMetrics::Event::kUsedWithSuccess),
1);
histogram_tester_->ExpectBucketCount(
kHistogramGarageSessionMetric,
static_cast<int>(feature_usage::FeatureUsageMetrics::Event::kEnabled), 1);
histogram_tester_->ExpectBucketCount(
kHistogramGarageSessionMetric,
static_cast<int>(feature_usage::FeatureUsageMetrics::Event::kEligible),
1);
histogram_tester_->ExpectBucketCount(
kHistogramGarageOrDockSessionMetric,
static_cast<int>(
feature_usage::FeatureUsageMetrics::Event::kUsedWithSuccess),
1);
histogram_tester_->ExpectBucketCount(
kHistogramGarageOrDockSessionMetric,
static_cast<int>(feature_usage::FeatureUsageMetrics::Event::kEnabled), 1);
histogram_tester_->ExpectBucketCount(
kHistogramGarageOrDockSessionMetric,
static_cast<int>(feature_usage::FeatureUsageMetrics::Event::kEligible),
1);
histogram_tester_->ExpectTimeBucketCount(kHistogramGarageSessionUsetimeMetric,
kTimeSpentInUse, 1);
histogram_tester_->ExpectTimeBucketCount(
kHistogramGarageOrDockSessionUsetimeMetric, kTimeSpentInUse, 1);
}
TEST_F(StylusMetricsRecorderTest, RemovedFromDock) {
const base::TimeDelta kTimeSpentInUse = base::Minutes(5);
const base::TimeDelta kTimeSpentCharging = base::Minutes(1);
SetChargerState(StylusChargingStyle::kDock,
BatteryInfo::ChargeStatus::kDischarging);
AdvanceClock(kTimeSpentInUse);
// By removing the battery, we force the stylus_metrics_recorder to close out
// the session.
SetChargerState(StylusChargingStyle::kDock,
BatteryInfo::ChargeStatus::kCharging);
// Step time further when we're back on charge, to make sure this time is not
// counted
AdvanceClock(kTimeSpentCharging);
RemoveCharger(StylusChargingStyle::kDock,
BatteryInfo::ChargeStatus::kCharging);
histogram_tester_->ExpectTotalCount(kHistogramGarageSessionMetric, 0);
histogram_tester_->ExpectBucketCount(
kHistogramDockSessionMetric,
static_cast<int>(
feature_usage::FeatureUsageMetrics::Event::kUsedWithSuccess),
1);
histogram_tester_->ExpectBucketCount(
kHistogramDockSessionMetric,
static_cast<int>(feature_usage::FeatureUsageMetrics::Event::kEnabled), 1);
histogram_tester_->ExpectBucketCount(
kHistogramDockSessionMetric,
static_cast<int>(feature_usage::FeatureUsageMetrics::Event::kEligible),
1);
histogram_tester_->ExpectBucketCount(
kHistogramGarageOrDockSessionMetric,
static_cast<int>(
feature_usage::FeatureUsageMetrics::Event::kUsedWithSuccess),
1);
histogram_tester_->ExpectBucketCount(
kHistogramGarageOrDockSessionMetric,
static_cast<int>(feature_usage::FeatureUsageMetrics::Event::kEnabled), 1);
histogram_tester_->ExpectBucketCount(
kHistogramGarageOrDockSessionMetric,
static_cast<int>(feature_usage::FeatureUsageMetrics::Event::kEligible),
1);
histogram_tester_->ExpectTimeBucketCount(kHistogramDockSessionUsetimeMetric,
kTimeSpentInUse, 1);
histogram_tester_->ExpectTimeBucketCount(
kHistogramGarageOrDockSessionUsetimeMetric, kTimeSpentInUse, 1);
}
TEST_F(StylusMetricsRecorderTest, ShutdownWhileStylusRemoved) {
const base::TimeDelta kTimeSpentInUse = base::Minutes(5);
SetChargerState(StylusChargingStyle::kGarage,
BatteryInfo::ChargeStatus::kDischarging);
AdvanceClock(kTimeSpentInUse);
// By removing the battery, we force the stylus_metrics_recorder to close out
// the session.
SetChargerState(StylusChargingStyle::kGarage,
BatteryInfo::ChargeStatus::kCharging);
// Just destroy the recorder, without replacing the stylus; we should still
// see the time recorded
stylus_metrics_recorder_.reset();
histogram_tester_->ExpectBucketCount(
kHistogramGarageSessionMetric,
static_cast<int>(
feature_usage::FeatureUsageMetrics::Event::kUsedWithSuccess),
1);
histogram_tester_->ExpectTimeBucketCount(kHistogramGarageSessionUsetimeMetric,
kTimeSpentInUse, 1);
}
TEST_F(StylusMetricsRecorderTest, StylusUsageOverMultipleDays) {
const base::TimeDelta kTimeSpentInUse = base::Hours(48);
SetChargerState(StylusChargingStyle::kGarage,
BatteryInfo::ChargeStatus::kDischarging);
AdvanceClock(kTimeSpentInUse);
RemoveCharger(StylusChargingStyle::kGarage,
BatteryInfo::ChargeStatus::kCharging);
histogram_tester_->ExpectBucketCount(
kHistogramGarageSessionMetric,
static_cast<int>(
feature_usage::FeatureUsageMetrics::Event::kUsedWithSuccess),
1);
histogram_tester_->ExpectTimeBucketCount(kHistogramGarageSessionUsetimeMetric,
kTimeSpentInUse, 1);
}
TEST_F(StylusMetricsRecorderTest, StylusChargeSequencing) {
const base::TimeDelta kTimeSpentTrickleCharging = base::Minutes(1);
const base::TimeDelta kTimeSpentCharging = base::Minutes(60);
const base::TimeDelta kTimeSpentFull = base::Minutes(5);
const base::TimeDelta kTimeSpentDischarging = base::Minutes(60);
const int kCycles = 2;
// Initial state, stylus is garage, charging, not in use
SetChargerState(StylusChargingStyle::kGarage,
BatteryInfo::ChargeStatus::kCharging);
for (int cycle = 0; cycle < kCycles; cycle++) {
SetChargerState(StylusChargingStyle::kGarage,
BatteryInfo::ChargeStatus::kCharging);
AdvanceClock(kTimeSpentTrickleCharging);
SetChargerState(StylusChargingStyle::kGarage,
BatteryInfo::ChargeStatus::kCharging);
AdvanceClock(kTimeSpentCharging);
SetChargerState(StylusChargingStyle::kGarage,
BatteryInfo::ChargeStatus::kFull);
AdvanceClock(kTimeSpentFull);
// Stylus is removed from garage when it starts discharging
SetChargerState(StylusChargingStyle::kGarage,
BatteryInfo::ChargeStatus::kDischarging);
AdvanceClock(kTimeSpentDischarging);
}
// Final state, same as initial
SetChargerState(StylusChargingStyle::kGarage,
BatteryInfo::ChargeStatus::kCharging);
histogram_tester_->ExpectBucketCount(
kHistogramGarageSessionMetric,
static_cast<int>(
feature_usage::FeatureUsageMetrics::Event::kUsedWithSuccess),
kCycles);
histogram_tester_->ExpectTimeBucketCount(kHistogramGarageSessionUsetimeMetric,
kTimeSpentDischarging, kCycles);
}
} // namespace ash