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
ash / system / input_device_settings / settings_updated_metrics_info_unittest.cc [blame]
// Copyright 2023 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/system/input_device_settings/settings_updated_metrics_info.h"
#include "base/json/values_util.h"
#include "base/time/time.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace ash {
namespace {
using TimePeriod = SettingsUpdatedMetricsInfo::TimePeriod;
using Category = SettingsUpdatedMetricsInfo::Category;
constexpr char kLocalFirstConnectionKey[] = "initial_connection_time";
constexpr char kCategoryKey[] = "category";
} // namespace
TEST(SettingsUpdatedMetricsInfoTest, ConvertToDict) {
{
const base::Time expected_time =
base::Time::FromDeltaSinceWindowsEpoch(base::Days(10000));
SettingsUpdatedMetricsInfo metrics_info(
SettingsUpdatedMetricsInfo::Category::kFirstEver, expected_time);
auto metrics_info_dict = metrics_info.ToDict();
auto category_int = metrics_info_dict.FindInt(kCategoryKey);
ASSERT_TRUE(category_int);
auto* time_value = metrics_info_dict.Find(kLocalFirstConnectionKey);
ASSERT_TRUE(time_value);
auto time_from_dict = base::ValueToTime(*time_value);
ASSERT_TRUE(time_from_dict);
EXPECT_EQ(expected_time, *time_from_dict);
EXPECT_EQ(SettingsUpdatedMetricsInfo::Category::kFirstEver,
static_cast<SettingsUpdatedMetricsInfo::Category>(*category_int));
}
{
const base::Time expected_time =
base::Time::FromDeltaSinceWindowsEpoch(base::Days(20000));
SettingsUpdatedMetricsInfo metrics_info(
SettingsUpdatedMetricsInfo::Category::kDefault, expected_time);
auto metrics_info_dict = metrics_info.ToDict();
auto category_int = metrics_info_dict.FindInt(kCategoryKey);
ASSERT_TRUE(category_int);
auto* time_value = metrics_info_dict.Find(kLocalFirstConnectionKey);
ASSERT_TRUE(time_value);
auto time_from_dict = base::ValueToTime(*time_value);
ASSERT_TRUE(time_from_dict);
EXPECT_EQ(expected_time, *time_from_dict);
EXPECT_EQ(SettingsUpdatedMetricsInfo::Category::kDefault,
static_cast<SettingsUpdatedMetricsInfo::Category>(*category_int));
}
{
const base::Time expected_time =
base::Time::FromDeltaSinceWindowsEpoch(base::Days(30000));
SettingsUpdatedMetricsInfo metrics_info(
SettingsUpdatedMetricsInfo::Category::kSynced, expected_time);
auto metrics_info_dict = metrics_info.ToDict();
auto category_int = metrics_info_dict.FindInt(kCategoryKey);
ASSERT_TRUE(category_int);
auto* time_value = metrics_info_dict.Find(kLocalFirstConnectionKey);
ASSERT_TRUE(time_value);
auto time_from_dict = base::ValueToTime(*time_value);
ASSERT_TRUE(time_from_dict);
EXPECT_EQ(expected_time, *time_from_dict);
EXPECT_EQ(SettingsUpdatedMetricsInfo::Category::kSynced,
static_cast<SettingsUpdatedMetricsInfo::Category>(*category_int));
}
}
TEST(SettingsUpdatedMetricsInfoTest, InvalidConversionMissingTime) {
base::Value::Dict dict;
dict.Set(kCategoryKey, static_cast<int>(Category::kFirstEver));
auto metrics_info = SettingsUpdatedMetricsInfo::FromDict(dict);
EXPECT_FALSE(metrics_info);
}
TEST(SettingsUpdatedMetricsInfoTest, InvalidConversionMissingCategory) {
base::Value::Dict dict;
dict.Set(kLocalFirstConnectionKey, base::TimeToValue(base::Time::Now()));
auto metrics_info = SettingsUpdatedMetricsInfo::FromDict(dict);
EXPECT_FALSE(metrics_info);
}
TEST(SettingsUpdatedMetricsInfoTest, ConversionNoPeriods) {
base::Value::Dict dict;
const base::Time expected_time =
base::Time::FromDeltaSinceWindowsEpoch(base::Days(10000));
dict.Set(kCategoryKey, static_cast<int>(Category::kFirstEver));
dict.Set(kLocalFirstConnectionKey, base::TimeToValue(expected_time));
auto metrics_info = SettingsUpdatedMetricsInfo::FromDict(dict);
ASSERT_TRUE(metrics_info);
EXPECT_EQ(Category::kFirstEver, metrics_info->category());
EXPECT_EQ(expected_time, metrics_info->initial_connection_time());
}
TEST(SettingsUpdatedMetricsInfoTest, ConversionInvalidCategory) {
base::Value::Dict dict;
const base::Time expected_time =
base::Time::FromDeltaSinceWindowsEpoch(base::Days(10000));
dict.Set(kCategoryKey, static_cast<int>(Category::kMaxValue) + 1);
dict.Set(kLocalFirstConnectionKey, base::TimeToValue(expected_time));
auto metrics_info = SettingsUpdatedMetricsInfo::FromDict(dict);
EXPECT_FALSE(metrics_info);
}
TEST(SettingsUpdatedMetricsInfoTest, ConversionCheckPeriodCounts) {
base::Value::Dict dict;
const base::Time expected_time =
base::Time::FromDeltaSinceWindowsEpoch(base::Days(10000));
dict.Set(kCategoryKey, static_cast<int>(Category::kMaxValue));
dict.Set(kLocalFirstConnectionKey, base::TimeToValue(expected_time));
dict.Set("one_hour", 1);
dict.Set("three_hours", 2);
dict.Set("one_day", 3);
dict.Set("three_days", 4);
dict.Set("one_week", 5);
auto metrics_info = SettingsUpdatedMetricsInfo::FromDict(dict);
ASSERT_TRUE(metrics_info);
EXPECT_EQ(1, metrics_info->GetCount(TimePeriod::kOneHour));
EXPECT_EQ(2, metrics_info->GetCount(TimePeriod::kThreeHours));
EXPECT_EQ(3, metrics_info->GetCount(TimePeriod::kOneDay));
EXPECT_EQ(4, metrics_info->GetCount(TimePeriod::kThreeDays));
EXPECT_EQ(5, metrics_info->GetCount(TimePeriod::kOneWeek));
}
TEST(SettingsUpdatedMetricsInfoTest, ConversionCheckPeriodCountsRoundTrip) {
base::Value::Dict dict;
const base::Time expected_time =
base::Time::FromDeltaSinceWindowsEpoch(base::Days(10000));
dict.Set(kCategoryKey, static_cast<int>(Category::kMaxValue));
dict.Set(kLocalFirstConnectionKey, base::TimeToValue(expected_time));
dict.Set("one_hour", 1);
dict.Set("three_hours", 2);
dict.Set("one_day", 3);
dict.Set("three_days", 4);
dict.Set("one_week", 5);
auto metrics_info = SettingsUpdatedMetricsInfo::FromDict(dict);
ASSERT_TRUE(metrics_info);
base::Value::Dict duplicate_dict = metrics_info->ToDict();
EXPECT_EQ(dict, duplicate_dict);
}
TEST(SettingsUpdatedMetricsInfoTest, ConversionCheckPeriodCountsAllZero) {
base::Value::Dict dict;
const base::Time expected_time =
base::Time::FromDeltaSinceWindowsEpoch(base::Days(10000));
dict.Set(kCategoryKey, static_cast<int>(Category::kMaxValue));
dict.Set(kLocalFirstConnectionKey, base::TimeToValue(expected_time));
auto metrics_info = SettingsUpdatedMetricsInfo::FromDict(dict);
ASSERT_TRUE(metrics_info);
EXPECT_EQ(0, metrics_info->GetCount(TimePeriod::kOneHour));
EXPECT_EQ(0, metrics_info->GetCount(TimePeriod::kThreeHours));
EXPECT_EQ(0, metrics_info->GetCount(TimePeriod::kOneDay));
EXPECT_EQ(0, metrics_info->GetCount(TimePeriod::kThreeDays));
EXPECT_EQ(0, metrics_info->GetCount(TimePeriod::kOneWeek));
}
TEST(SettingsUpdatedMetricsInfoTest, CheckCountUpdates) {
const base::Time start_time =
base::Time::FromDeltaSinceWindowsEpoch(base::Days(10000));
std::optional<TimePeriod> optional_period;
SettingsUpdatedMetricsInfo metrics_info(
SettingsUpdatedMetricsInfo::Category::kFirstEver, start_time);
// Update within one hour.
optional_period =
metrics_info.RecordSettingsUpdate(start_time + base::Minutes(10));
EXPECT_EQ(TimePeriod::kOneHour, *optional_period);
optional_period =
metrics_info.RecordSettingsUpdate(start_time + base::Minutes(30));
EXPECT_EQ(TimePeriod::kOneHour, *optional_period);
optional_period =
metrics_info.RecordSettingsUpdate(start_time + base::Minutes(50));
EXPECT_EQ(TimePeriod::kOneHour, *optional_period);
optional_period =
metrics_info.RecordSettingsUpdate(start_time + base::Minutes(59));
EXPECT_EQ(TimePeriod::kOneHour, *optional_period);
EXPECT_EQ(4, metrics_info.GetCount(TimePeriod::kOneHour));
EXPECT_EQ(0, metrics_info.GetCount(TimePeriod::kThreeHours));
// Update within one day.
optional_period = metrics_info.RecordSettingsUpdate(
start_time + base::Hours(3) + base::Minutes(10));
EXPECT_EQ(TimePeriod::kOneDay, *optional_period);
optional_period = metrics_info.RecordSettingsUpdate(
start_time + base::Hours(4) + base::Minutes(30));
EXPECT_EQ(TimePeriod::kOneDay, *optional_period);
optional_period = metrics_info.RecordSettingsUpdate(
start_time + base::Hours(23) + base::Minutes(50));
EXPECT_EQ(TimePeriod::kOneDay, *optional_period);
EXPECT_EQ(4, metrics_info.GetCount(TimePeriod::kOneHour));
EXPECT_EQ(0, metrics_info.GetCount(TimePeriod::kThreeHours));
EXPECT_EQ(3, metrics_info.GetCount(TimePeriod::kOneDay));
// Update within 3 days.
optional_period = metrics_info.RecordSettingsUpdate(
start_time + base::Days(2) + base::Minutes(10));
EXPECT_EQ(TimePeriod::kThreeDays, *optional_period);
EXPECT_EQ(4, metrics_info.GetCount(TimePeriod::kOneHour));
EXPECT_EQ(0, metrics_info.GetCount(TimePeriod::kThreeHours));
EXPECT_EQ(3, metrics_info.GetCount(TimePeriod::kOneDay));
EXPECT_EQ(1, metrics_info.GetCount(TimePeriod::kThreeDays));
// Update within 1 week.
optional_period = metrics_info.RecordSettingsUpdate(
start_time + base::Days(5) + base::Minutes(10));
EXPECT_EQ(TimePeriod::kOneWeek, *optional_period);
optional_period =
metrics_info.RecordSettingsUpdate(start_time + base::Days(6));
EXPECT_EQ(TimePeriod::kOneWeek, *optional_period);
EXPECT_EQ(4, metrics_info.GetCount(TimePeriod::kOneHour));
EXPECT_EQ(0, metrics_info.GetCount(TimePeriod::kThreeHours));
EXPECT_EQ(3, metrics_info.GetCount(TimePeriod::kOneDay));
EXPECT_EQ(1, metrics_info.GetCount(TimePeriod::kThreeDays));
EXPECT_EQ(2, metrics_info.GetCount(TimePeriod::kOneWeek));
// Update within 3 hours, done last to ensure the logic properly skips the
// various time chunks.
optional_period =
metrics_info.RecordSettingsUpdate(start_time + base::Hours(2));
EXPECT_EQ(SettingsUpdatedMetricsInfo::TimePeriod::kThreeHours,
*optional_period);
EXPECT_EQ(4, metrics_info.GetCount(TimePeriod::kOneHour));
EXPECT_EQ(1, metrics_info.GetCount(TimePeriod::kThreeHours));
EXPECT_EQ(3, metrics_info.GetCount(TimePeriod::kOneDay));
EXPECT_EQ(1, metrics_info.GetCount(TimePeriod::kThreeDays));
EXPECT_EQ(2, metrics_info.GetCount(TimePeriod::kOneWeek));
// If >1week, nothing changes and return std::nullopt.
optional_period =
metrics_info.RecordSettingsUpdate(start_time + base::Days(7));
EXPECT_FALSE(optional_period);
EXPECT_EQ(4, metrics_info.GetCount(TimePeriod::kOneHour));
EXPECT_EQ(1, metrics_info.GetCount(TimePeriod::kThreeHours));
EXPECT_EQ(3, metrics_info.GetCount(TimePeriod::kOneDay));
EXPECT_EQ(1, metrics_info.GetCount(TimePeriod::kThreeDays));
EXPECT_EQ(2, metrics_info.GetCount(TimePeriod::kOneWeek));
}
} // namespace ash