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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
ash / system / input_device_settings / keyboard_modifier_metrics_recorder_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/keyboard_modifier_metrics_recorder.h"
#include <memory>
#include "ash/constants/ash_features.h"
#include "ash/public/cpp/ash_prefs.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shell.h"
#include "ash/test/ash_test_base.h"
#include "ash/test/ash_test_helper.h"
#include "base/containers/flat_map.h"
#include "base/memory/raw_ptr.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "components/account_id/account_id.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/testing_pref_service.h"
#include "ui/events/ash/mojom/modifier_key.mojom.h"
#include "ui/events/ash/pref_names.h"
namespace ash {
namespace {
constexpr char kUserEmail1[] = "example1@abc.com";
constexpr char kUserEmail2[] = "example2@abc.com";
// Table containing the list of modifier remapping prefs with their expected
// metric names and their matching default key. Used by the following test
// suites.
struct KeyboardModifierMetricsRecorderTestData {
std::string pref_name;
std::string changed_metric_name;
std::string started_metric_name;
ui::mojom::ModifierKey default_modifier_key;
} kKeyboardModifierMetricTestData[] = {
{prefs::kLanguageRemapAltKeyTo,
"ChromeOS.Settings.Keyboard.Modifiers.AltRemappedTo.Changed",
"ChromeOS.Settings.Keyboard.Modifiers.AltRemappedTo.Started",
ui::mojom::ModifierKey::kAlt},
{prefs::kLanguageRemapControlKeyTo,
"ChromeOS.Settings.Keyboard.Modifiers.ControlRemappedTo.Changed",
"ChromeOS.Settings.Keyboard.Modifiers.ControlRemappedTo.Started",
ui::mojom::ModifierKey::kControl},
{prefs::kLanguageRemapEscapeKeyTo,
"ChromeOS.Settings.Keyboard.Modifiers.EscapeRemappedTo.Changed",
"ChromeOS.Settings.Keyboard.Modifiers.EscapeRemappedTo.Started",
ui::mojom::ModifierKey::kEscape},
{prefs::kLanguageRemapBackspaceKeyTo,
"ChromeOS.Settings.Keyboard.Modifiers.BackspaceRemappedTo.Changed",
"ChromeOS.Settings.Keyboard.Modifiers.BackspaceRemappedTo.Started",
ui::mojom::ModifierKey::kBackspace},
{prefs::kLanguageRemapAssistantKeyTo,
"ChromeOS.Settings.Keyboard.Modifiers.AssistantRemappedTo.Changed",
"ChromeOS.Settings.Keyboard.Modifiers.AssistantRemappedTo.Started",
ui::mojom::ModifierKey::kAssistant},
{prefs::kLanguageRemapCapsLockKeyTo,
"ChromeOS.Settings.Keyboard.Modifiers.CapsLockRemappedTo.Changed",
"ChromeOS.Settings.Keyboard.Modifiers.CapsLockRemappedTo.Started",
ui::mojom::ModifierKey::kCapsLock},
{prefs::kLanguageRemapSearchKeyTo,
"ChromeOS.Settings.Keyboard.Modifiers.SearchRemappedTo.Changed",
"ChromeOS.Settings.Keyboard.Modifiers.SearchRemappedTo.Started",
ui::mojom::ModifierKey::kMeta},
{prefs::kLanguageRemapExternalMetaKeyTo,
"ChromeOS.Settings.Keyboard.Modifiers.ExternalMetaRemappedTo.Changed",
"ChromeOS.Settings.Keyboard.Modifiers.ExternalMetaRemappedTo.Started",
ui::mojom::ModifierKey::kMeta},
{prefs::kLanguageRemapExternalCommandKeyTo,
"ChromeOS.Settings.Keyboard.Modifiers.ExternalCommandRemappedTo.Changed",
"ChromeOS.Settings.Keyboard.Modifiers.ExternalCommandRemappedTo.Started",
ui::mojom::ModifierKey::kControl},
};
} // namespace
class KeyboardModifierMetricsRecorderTest : public AshTestBase {
public:
KeyboardModifierMetricsRecorderTest() = default;
KeyboardModifierMetricsRecorderTest(
const KeyboardModifierMetricsRecorderTest&) = delete;
KeyboardModifierMetricsRecorderTest& operator=(
const KeyboardModifierMetricsRecorderTest&) = delete;
~KeyboardModifierMetricsRecorderTest() override = default;
void SetUp() override {
feature_list_.InitAndDisableFeature(features::kInputDeviceSettingsSplit);
AshTestBase::SetUp();
ResetHistogramTester();
recorder_ = Shell::Get()->keyboard_modifier_metrics_recorder();
}
void TearDown() override {
histogram_tester_.reset();
AshTestBase::TearDown();
}
void ResetHistogramTester() {
histogram_tester_ = std::make_unique<base::HistogramTester>();
}
protected:
raw_ptr<KeyboardModifierMetricsRecorder, DanglingUntriaged> recorder_;
std::unique_ptr<base::HistogramTester> histogram_tester_;
base::test::ScopedFeatureList feature_list_;
};
class KeyboardModifierMetricsRecorderPrefChangedTest
: public KeyboardModifierMetricsRecorderTest,
public testing::WithParamInterface<
std::tuple<KeyboardModifierMetricsRecorderTestData, int, int>> {
void SetUp() override {
KeyboardModifierMetricsRecorderTest::SetUp();
int int_modifier_key_from, int_modifier_key_to;
std::tie(data_, int_modifier_key_from, int_modifier_key_to) = GetParam();
modifier_key_from_ =
static_cast<ui::mojom::ModifierKey>(int_modifier_key_from);
modifier_key_to_ = static_cast<ui::mojom::ModifierKey>(int_modifier_key_to);
pref_service_ = Shell::Get()->session_controller()->GetActivePrefService();
pref_service_->SetInteger(data_.pref_name,
static_cast<int>(data_.default_modifier_key));
ResetHistogramTester();
}
protected:
raw_ptr<PrefService, DanglingUntriaged> pref_service_;
KeyboardModifierMetricsRecorderTestData data_;
ui::mojom::ModifierKey modifier_key_from_;
ui::mojom::ModifierKey modifier_key_to_;
};
// Instantiates the test case with every combination of the modifiers in
// `kKeyboardModifierMetricTestData` and every combination of both a modifier
// to start with and a modifier to change to. Custom name generator is
// implemented to simplify searching through test results for failed cases.
INSTANTIATE_TEST_SUITE_P(
,
KeyboardModifierMetricsRecorderPrefChangedTest,
testing::Combine(
testing::ValuesIn(kKeyboardModifierMetricTestData),
testing::Range(static_cast<int>(ui::mojom::ModifierKey::kMinValue),
static_cast<int>(ui::mojom::ModifierKey::kAssistant) +
1),
testing::Range(static_cast<int>(ui::mojom::ModifierKey::kMinValue),
static_cast<int>(ui::mojom::ModifierKey::kAssistant) +
1)),
([](const testing::TestParamInfo<
KeyboardModifierMetricsRecorderPrefChangedTest::ParamType>& info) {
const auto& [data, int_modifier_key_from, int_modifier_key_to] =
info.param;
// Pref name must replace periods with underscores for gtest output.
std::string result;
base::ReplaceChars(data.pref_name, ".", "_", &result);
return base::StringPrintf("%s_%d_%d", result.c_str(),
int_modifier_key_from, int_modifier_key_to);
}));
TEST_P(KeyboardModifierMetricsRecorderPrefChangedTest, CheckChangedMetric) {
pref_service_->SetInteger(data_.pref_name,
static_cast<int>(modifier_key_from_));
if (modifier_key_from_ != data_.default_modifier_key) {
histogram_tester_->ExpectUniqueSample(data_.changed_metric_name,
modifier_key_from_, 1);
} else {
histogram_tester_->ExpectUniqueSample(data_.changed_metric_name,
modifier_key_from_, 0);
}
ResetHistogramTester();
pref_service_->SetInteger(data_.pref_name,
static_cast<int>(modifier_key_to_));
if (modifier_key_from_ != modifier_key_to_) {
histogram_tester_->ExpectUniqueSample(data_.changed_metric_name,
modifier_key_to_, 1);
} else {
histogram_tester_->ExpectUniqueSample(data_.changed_metric_name,
modifier_key_to_, 0);
}
}
class KeyboardModifierMetricsRecorderPrefStartedTest
: public KeyboardModifierMetricsRecorderTest,
public testing::WithParamInterface<
std::tuple<KeyboardModifierMetricsRecorderTestData, int>> {
void SetUp() override {
KeyboardModifierMetricsRecorderTest::SetUp();
int int_modifier_key;
std::tie(data_, int_modifier_key) = GetParam();
modifier_key_ = static_cast<ui::mojom::ModifierKey>(int_modifier_key);
ResetHistogramTester();
}
protected:
KeyboardModifierMetricsRecorderTestData data_;
ui::mojom::ModifierKey modifier_key_;
};
// Instantiates the test case with every combination of the modifiers in
// `kKeyboardModifierMetricTestData` and with every possible remapped value in
// `ui::mojom::ModifierKey`. A custom name generator is implemented to simplify
// searching through test results for failed cases.
INSTANTIATE_TEST_SUITE_P(
,
KeyboardModifierMetricsRecorderPrefStartedTest,
testing::Combine(
testing::ValuesIn(kKeyboardModifierMetricTestData),
// Only test from 0 to Assistant as those are the only modifiers
// supported pre settings split.
testing::Range(static_cast<int>(ui::mojom::ModifierKey::kMinValue),
static_cast<int>(ui::mojom::ModifierKey::kAssistant) +
1)),
([](const testing::TestParamInfo<
KeyboardModifierMetricsRecorderPrefStartedTest::ParamType>& info) {
const auto& [data, int_modifier_key] = info.param;
// Pref name must replace periods with underscores for gtest output.
std::string result;
base::ReplaceChars(data.pref_name, ".", "_", &result);
return base::StringPrintf("%s_%d", result.c_str(), int_modifier_key);
}));
TEST_P(KeyboardModifierMetricsRecorderPrefStartedTest, InitializeTest) {
// Initialize two pref services with the initial value of the metric.
const AccountId account_id1 = AccountId::FromUserEmail(kUserEmail1);
const AccountId account_id2 = AccountId::FromUserEmail(kUserEmail2);
std::unique_ptr<TestingPrefServiceSimple> pref_service1 =
std::make_unique<TestingPrefServiceSimple>();
ash::RegisterUserProfilePrefs(pref_service1->registry(), /*country=*/"",
true);
std::unique_ptr<TestingPrefServiceSimple> pref_service2 =
std::make_unique<TestingPrefServiceSimple>();
ash::RegisterUserProfilePrefs(pref_service2->registry(), /*country=*/"",
true);
pref_service1->SetInteger(data_.pref_name, static_cast<int>(modifier_key_));
pref_service2->SetInteger(data_.pref_name, static_cast<int>(modifier_key_));
ash_test_helper()->test_session_controller_client()->SetUserPrefService(
account_id1, std::move(pref_service1));
ash_test_helper()->test_session_controller_client()->SetUserPrefService(
account_id2, std::move(pref_service2));
ResetHistogramTester();
// Sign into first account and verify the metric is emitted.
SimulateUserLogin(account_id1);
if (modifier_key_ != data_.default_modifier_key) {
histogram_tester_->ExpectUniqueSample(data_.started_metric_name,
static_cast<int>(modifier_key_), 1);
} else {
histogram_tester_->ExpectUniqueSample(data_.started_metric_name,
static_cast<int>(modifier_key_), 0);
}
// Sign into second account and verify the metric is emitted.
SimulateUserLogin(account_id2);
if (modifier_key_ != data_.default_modifier_key) {
histogram_tester_->ExpectUniqueSample(data_.started_metric_name,
static_cast<int>(modifier_key_), 2);
} else {
histogram_tester_->ExpectUniqueSample(data_.started_metric_name,
static_cast<int>(modifier_key_), 0);
}
// Sign back into the first account and verify no more metrics are emitted.
SimulateUserLogin(account_id1);
if (modifier_key_ != data_.default_modifier_key) {
histogram_tester_->ExpectUniqueSample(data_.started_metric_name,
static_cast<int>(modifier_key_), 2);
} else {
histogram_tester_->ExpectUniqueSample(data_.started_metric_name,
static_cast<int>(modifier_key_), 0);
}
}
// Contains a list of modifier remappings to apply and then the expected hash
// value after a user signs in. If `expected_value` is empty, then no metric is
// expected.
struct KeyboardModifierMetricsRecorderHashTestData {
base::flat_map<std::string, ui::mojom::ModifierKey> modifier_remappings;
std::optional<int32_t> expected_value;
};
class KeyboardModifierMetricsRecorderHashTest
: public KeyboardModifierMetricsRecorderTest,
public testing::WithParamInterface<
KeyboardModifierMetricsRecorderHashTestData> {
void SetUp() override {
KeyboardModifierMetricsRecorderTest::SetUp();
data_ = GetParam();
ResetHistogramTester();
}
protected:
KeyboardModifierMetricsRecorderHashTestData data_;
};
// Contains lists of modifier remappings to apply before signing in the user and
// then the expected computed hash once the user signs in.
INSTANTIATE_TEST_SUITE_P(
,
KeyboardModifierMetricsRecorderHashTest,
testing::ValuesIn(std::vector<KeyboardModifierMetricsRecorderHashTestData>{
// With only default remappings, no metric is expected.
{{}, std::nullopt},
// All keys remapped to `ui::mojom::ModifierKey::kMeta` should hash to
// 0.
{{{::prefs::kLanguageRemapAltKeyTo, ui::mojom::ModifierKey::kMeta},
{::prefs::kLanguageRemapCapsLockKeyTo, ui::mojom::ModifierKey::kMeta},
{::prefs::kLanguageRemapBackspaceKeyTo,
ui::mojom::ModifierKey::kMeta},
{::prefs::kLanguageRemapEscapeKeyTo, ui::mojom::ModifierKey::kMeta},
{::prefs::kLanguageRemapControlKeyTo, ui::mojom::ModifierKey::kMeta},
{::prefs::kLanguageRemapAssistantKeyTo,
ui::mojom::ModifierKey::kMeta},
{::prefs::kLanguageRemapExternalMetaKeyTo,
ui::mojom::ModifierKey::kMeta},
{::prefs::kLanguageRemapExternalCommandKeyTo,
ui::mojom::ModifierKey::kMeta},
{::prefs::kLanguageRemapSearchKeyTo, ui::mojom::ModifierKey::kMeta}},
0x0},
// All keys remapped to `ui::mojom::ModifierKey::kBackspace` should hash
// to
// 0x6db6db6.
{{{::prefs::kLanguageRemapAltKeyTo, ui::mojom::ModifierKey::kBackspace},
{::prefs::kLanguageRemapCapsLockKeyTo,
ui::mojom::ModifierKey::kBackspace},
{::prefs::kLanguageRemapBackspaceKeyTo,
ui::mojom::ModifierKey::kBackspace},
{::prefs::kLanguageRemapEscapeKeyTo,
ui::mojom::ModifierKey::kBackspace},
{::prefs::kLanguageRemapControlKeyTo,
ui::mojom::ModifierKey::kBackspace},
{::prefs::kLanguageRemapAssistantKeyTo,
ui::mojom::ModifierKey::kBackspace},
{::prefs::kLanguageRemapExternalMetaKeyTo,
ui::mojom::ModifierKey::kBackspace},
{::prefs::kLanguageRemapExternalCommandKeyTo,
ui::mojom::ModifierKey::kBackspace},
{::prefs::kLanguageRemapSearchKeyTo,
ui::mojom::ModifierKey::kBackspace}},
0x6db6db6},
// Random assortment of keys with a manually computed hash.
{{{::prefs::kLanguageRemapAltKeyTo, ui::mojom::ModifierKey::kControl},
{::prefs::kLanguageRemapCapsLockKeyTo, ui::mojom::ModifierKey::kAlt},
{::prefs::kLanguageRemapBackspaceKeyTo,
ui::mojom::ModifierKey::kAssistant},
{::prefs::kLanguageRemapEscapeKeyTo, ui::mojom::ModifierKey::kVoid},
{::prefs::kLanguageRemapControlKeyTo, ui::mojom::ModifierKey::kMeta},
{::prefs::kLanguageRemapAssistantKeyTo, ui::mojom::ModifierKey::kAlt},
{::prefs::kLanguageRemapExternalMetaKeyTo,
ui::mojom::ModifierKey::kControl},
{::prefs::kLanguageRemapExternalCommandKeyTo,
ui::mojom::ModifierKey::kCapsLock},
{::prefs::kLanguageRemapSearchKeyTo, ui::mojom::ModifierKey::kAlt}},
0x4452ec1},
}));
TEST_P(KeyboardModifierMetricsRecorderHashTest, HashTest) {
// Initialize two pref services with the initial value of the metric.
const AccountId account_id1 = AccountId::FromUserEmail(kUserEmail1);
const AccountId account_id2 = AccountId::FromUserEmail(kUserEmail2);
std::unique_ptr<TestingPrefServiceSimple> pref_service1 =
std::make_unique<TestingPrefServiceSimple>();
ash::RegisterUserProfilePrefs(pref_service1->registry(), /*country=*/"",
true);
std::unique_ptr<TestingPrefServiceSimple> pref_service2 =
std::make_unique<TestingPrefServiceSimple>();
ash::RegisterUserProfilePrefs(pref_service2->registry(), /*country=*/"",
true);
for (const auto& [pref, remapping] : data_.modifier_remappings) {
pref_service1->SetInteger(pref, static_cast<int>(remapping));
pref_service2->SetInteger(pref, static_cast<int>(remapping));
}
ash_test_helper()->test_session_controller_client()->SetUserPrefService(
account_id1, std::move(pref_service1));
ash_test_helper()->test_session_controller_client()->SetUserPrefService(
account_id2, std::move(pref_service2));
ResetHistogramTester();
// Sign into first account and verify the metric is emitted.
SimulateUserLogin(account_id1);
if (data_.expected_value.has_value()) {
histogram_tester_->ExpectUniqueSample(
"ChromeOS.Settings.Keyboard.Modifiers.Hash",
data_.expected_value.value(), 1);
} else {
histogram_tester_->ExpectTotalCount(
"ChromeOS.Settings.Keyboard.Modifiers.Hash", 0);
}
// Sign into second account and verify the metric is emitted.
SimulateUserLogin(account_id2);
if (data_.expected_value.has_value()) {
histogram_tester_->ExpectUniqueSample(
"ChromeOS.Settings.Keyboard.Modifiers.Hash",
data_.expected_value.value(), 2);
} else {
histogram_tester_->ExpectTotalCount(
"ChromeOS.Settings.Keyboard.Modifiers.Hash", 0);
}
ResetHistogramTester();
// Sign back into first account and verify the metric is not emitted again.
SimulateUserLogin(account_id1);
histogram_tester_->ExpectTotalCount(
"ChromeOS.Settings.Keyboard.Modifiers.Hash", 0);
}
} // namespace ash