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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
ash / system / input_device_settings / pref_handlers / pointing_stick_pref_handler_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/public/mojom/input_device_settings.mojom-forward.h"
#include "ash/system/input_device_settings/pref_handlers/pointing_stick_pref_handler_impl.h"
#include "ash/constants/ash_features.h"
#include "ash/constants/ash_pref_names.h"
#include "ash/public/mojom/input_device_settings.mojom.h"
#include "ash/shell.h"
#include "ash/system/input_device_settings/input_device_settings_defaults.h"
#include "ash/system/input_device_settings/input_device_settings_pref_names.h"
#include "ash/system/input_device_settings/input_device_tracker.h"
#include "ash/system/input_device_settings/settings_updated_metrics_info.h"
#include "ash/test/ash_test_base.h"
#include "base/test/scoped_feature_list.h"
#include "components/account_id/account_id.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/testing_pref_service.h"
#include "components/user_manager/known_user.h"
namespace ash {
namespace {
const std::string kDictFakeKey = "fake_key";
const std::string kDictFakeValue = "fake_value";
const std::string kPointingStickKey1 = "device_key1";
const std::string kPointingStickKey2 = "device_key2";
constexpr char kUserEmail[] = "example@email.com";
constexpr char kUserEmail2[] = "example2@email.com";
const AccountId account_id_1 = AccountId::FromUserEmail(kUserEmail);
const AccountId account_id_2 = AccountId::FromUserEmail(kUserEmail2);
const int kTestSensitivity = 2;
const bool kTestSwapRight = false;
const bool kTestAccelerationEnabled = false;
const mojom::PointingStickSettings kPointingStickSettingsDefault(
/*swap_right=*/kDefaultSwapRight,
/*sensitivity=*/kDefaultSensitivity,
/*acceleration_enabled=*/kDefaultAccelerationEnabled);
const mojom::PointingStickSettings kPointingStickSettingsNotDefault(
/*swap_right=*/!kDefaultSwapRight,
/*sensitivity=*/1,
/*acceleration_enabled=*/!kDefaultAccelerationEnabled);
const mojom::PointingStickSettings kPointingStickSettings1(
/*swap_right=*/true,
/*sensitivity=*/1,
/*acceleration_enabled=*/false);
const mojom::PointingStickSettings kPointingStickSettings2(
/*swap_right=*/false,
/*sensitivity=*/3,
/*acceleration_enabled=*/true);
} // namespace
class PointingStickPrefHandlerTest : public AshTestBase {
public:
PointingStickPrefHandlerTest() = default;
PointingStickPrefHandlerTest(const PointingStickPrefHandlerTest&) = delete;
PointingStickPrefHandlerTest& operator=(const PointingStickPrefHandlerTest&) =
delete;
~PointingStickPrefHandlerTest() override = default;
// testing::Test:
void SetUp() override {
scoped_feature_list_.InitAndEnableFeature(
features::kInputDeviceSettingsSplit);
AshTestBase::SetUp();
InitializePrefService();
pref_handler_ = std::make_unique<PointingStickPrefHandlerImpl>();
}
void TearDown() override {
pref_handler_.reset();
AshTestBase::TearDown();
}
void InitializePrefService() {
local_state()->registry()->RegisterBooleanPref(
prefs::kOwnerPrimaryPointingStickButtonRight, /*default_value=*/false);
pref_service_ = std::make_unique<TestingPrefServiceSimple>();
pref_service_->registry()->RegisterDictionaryPref(
prefs::kPointingStickDeviceSettingsDictPref);
pref_service_->registry()->RegisterDictionaryPref(
prefs::kPointingStickInternalSettings);
pref_service_->registry()->RegisterDictionaryPref(
prefs::kPointingStickUpdateSettingsMetricInfo);
pref_service_->registry()->RegisterIntegerPref(
prefs::kPointingStickSensitivity, kDefaultSensitivity);
pref_service_->registry()->RegisterBooleanPref(
prefs::kPrimaryPointingStickButtonRight, kDefaultSwapRight);
pref_service_->registry()->RegisterBooleanPref(
prefs::kPointingStickAcceleration, kDefaultAccelerationEnabled);
pref_service_->SetUserPref(prefs::kPointingStickSensitivity,
base::Value(kTestSensitivity));
pref_service_->SetUserPref(prefs::kPrimaryPointingStickButtonRight,
base::Value(kTestSwapRight));
pref_service_->SetUserPref(prefs::kPointingStickAcceleration,
base::Value(kTestAccelerationEnabled));
}
void CheckPointingStickSettingsAndDictAreEqual(
const mojom::PointingStickSettings& settings,
const base::Value::Dict& settings_dict) {
const auto sensitivity =
settings_dict.FindInt(prefs::kPointingStickSettingSensitivity);
if (sensitivity.has_value()) {
EXPECT_EQ(settings.sensitivity, sensitivity);
} else {
EXPECT_EQ(settings.sensitivity, kDefaultSensitivity);
}
const auto swap_right =
settings_dict.FindBool(prefs::kPointingStickSettingSwapRight);
if (swap_right.has_value()) {
EXPECT_EQ(settings.swap_right, swap_right);
} else {
EXPECT_EQ(settings.swap_right, kDefaultSwapRight);
}
const auto acceleration_enabled =
settings_dict.FindBool(prefs::kPointingStickSettingAcceleration);
if (acceleration_enabled.has_value()) {
EXPECT_EQ(settings.acceleration_enabled, acceleration_enabled);
} else {
EXPECT_EQ(settings.acceleration_enabled, kDefaultAccelerationEnabled);
}
}
void CheckPointingStickSettingsAreSetToDefaultValues(
const mojom::PointingStickSettings& settings) {
EXPECT_EQ(kPointingStickSettingsDefault.swap_right, settings.swap_right);
EXPECT_EQ(kPointingStickSettingsDefault.sensitivity, settings.sensitivity);
EXPECT_EQ(kPointingStickSettingsDefault.acceleration_enabled,
settings.acceleration_enabled);
}
void CallUpdatePointingStickSettings(
const std::string& device_key,
const mojom::PointingStickSettings& settings,
bool is_external = true) {
mojom::PointingStickPtr pointing_stick = mojom::PointingStick::New();
pointing_stick->settings = settings.Clone();
pointing_stick->device_key = device_key;
pointing_stick->is_external = is_external;
pref_handler_->UpdatePointingStickSettings(pref_service_.get(),
*pointing_stick);
}
void CallUpdateLoginScreenPointingStickSettings(
const AccountId& account_id,
const std::string& device_key,
const mojom::PointingStickSettings& settings) {
mojom::PointingStickPtr pointing_stick = mojom::PointingStick::New();
pointing_stick->settings = settings.Clone();
pref_handler_->UpdateLoginScreenPointingStickSettings(
local_state(), account_id, *pointing_stick);
}
mojom::PointingStickSettingsPtr CallInitializePointingStickSettings(
const std::string& device_key,
bool is_external = true) {
mojom::PointingStickPtr pointing_stick = mojom::PointingStick::New();
pointing_stick->device_key = device_key;
pointing_stick->is_external = is_external;
pref_handler_->InitializePointingStickSettings(pref_service_.get(),
pointing_stick.get());
return std::move(pointing_stick->settings);
}
mojom::PointingStickSettingsPtr
CallInitializeLoginScreenPointingStickSettings(
const AccountId& account_id,
const mojom::PointingStick& pointing_stick) {
const auto pointing_stick_ptr = pointing_stick.Clone();
pref_handler_->InitializeLoginScreenPointingStickSettings(
local_state(), account_id, pointing_stick_ptr.get());
return std::move(pointing_stick_ptr->settings);
}
const base::Value::Dict* GetSettingsDict(const std::string& device_key,
bool is_external = true) {
if (!is_external) {
return &pref_service_->GetDict(prefs::kPointingStickInternalSettings);
}
const auto& devices_dict =
pref_service_->GetDict(prefs::kPointingStickDeviceSettingsDictPref);
EXPECT_EQ(1u, devices_dict.size());
const auto* settings_dict = devices_dict.FindDict(device_key);
EXPECT_NE(nullptr, settings_dict);
return settings_dict;
}
user_manager::KnownUser known_user() {
return user_manager::KnownUser(local_state());
}
bool HasInternalLoginScreenSettingsDict(AccountId account_id) {
const auto* dict = known_user().FindPath(
account_id, prefs::kPointingStickLoginScreenInternalSettingsPref);
return dict && dict->is_dict();
}
bool HasExternalLoginScreenSettingsDict(AccountId account_id) {
const auto* dict = known_user().FindPath(
account_id, prefs::kPointingStickLoginScreenExternalSettingsPref);
return dict && dict->is_dict();
}
base::Value::Dict GetInternalLoginScreenSettingsDict(AccountId account_id) {
return known_user()
.FindPath(account_id,
prefs::kPointingStickLoginScreenInternalSettingsPref)
->GetDict()
.Clone();
}
protected:
base::test::ScopedFeatureList scoped_feature_list_;
std::unique_ptr<PointingStickPrefHandlerImpl> pref_handler_;
std::unique_ptr<TestingPrefServiceSimple> pref_service_;
};
TEST_F(PointingStickPrefHandlerTest,
InitializeLoginScreenPointingStickSettings) {
mojom::PointingStick pointing_stick;
pointing_stick.device_key = kPointingStickKey1;
pointing_stick.is_external = false;
mojom::PointingStickSettingsPtr settings =
CallInitializeLoginScreenPointingStickSettings(account_id_1,
pointing_stick);
EXPECT_FALSE(HasInternalLoginScreenSettingsDict(account_id_1));
CheckPointingStickSettingsAreSetToDefaultValues(*settings);
}
TEST_F(PointingStickPrefHandlerTest, UpdateLoginScreenPointingStickSettings) {
mojom::PointingStick PointingStick;
PointingStick.device_key = kPointingStickKey1;
PointingStick.is_external = false;
mojom::PointingStickSettingsPtr settings =
CallInitializeLoginScreenPointingStickSettings(account_id_1,
PointingStick);
mojom::PointingStickSettings updated_settings = *settings;
updated_settings.swap_right = !updated_settings.swap_right;
updated_settings.acceleration_enabled =
!updated_settings.acceleration_enabled;
CallUpdateLoginScreenPointingStickSettings(account_id_1, kPointingStickKey1,
updated_settings);
const auto& updated_settings_dict =
GetInternalLoginScreenSettingsDict(account_id_1);
CheckPointingStickSettingsAndDictAreEqual(updated_settings,
updated_settings_dict);
}
TEST_F(PointingStickPrefHandlerTest,
LoginScreenPrefsNotPersistedWhenFlagIsDisabled) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndDisableFeature(features::kInputDeviceSettingsSplit);
mojom::PointingStick pointing_stick1;
pointing_stick1.device_key = kPointingStickKey1;
pointing_stick1.is_external = false;
mojom::PointingStick pointing_stick2;
pointing_stick2.device_key = kPointingStickKey2;
pointing_stick2.is_external = true;
CallInitializeLoginScreenPointingStickSettings(account_id_1, pointing_stick1);
CallInitializeLoginScreenPointingStickSettings(account_id_1, pointing_stick2);
EXPECT_FALSE(HasInternalLoginScreenSettingsDict(account_id_1));
EXPECT_FALSE(HasExternalLoginScreenSettingsDict(account_id_1));
}
TEST_F(PointingStickPrefHandlerTest, MultipleDevices) {
CallUpdatePointingStickSettings(kPointingStickKey1, kPointingStickSettings1);
CallUpdatePointingStickSettings(kPointingStickKey2, kPointingStickSettings2);
const auto& devices_dict =
pref_service_->GetDict(prefs::kPointingStickDeviceSettingsDictPref);
ASSERT_EQ(2u, devices_dict.size());
auto* settings_dict = devices_dict.FindDict(kPointingStickKey1);
ASSERT_NE(nullptr, settings_dict);
CheckPointingStickSettingsAndDictAreEqual(kPointingStickSettings1,
*settings_dict);
settings_dict = devices_dict.FindDict(kPointingStickKey2);
ASSERT_NE(nullptr, settings_dict);
CheckPointingStickSettingsAndDictAreEqual(kPointingStickSettings2,
*settings_dict);
}
TEST_F(PointingStickPrefHandlerTest, PreservesOldSettings) {
CallUpdatePointingStickSettings(kPointingStickKey1, kPointingStickSettings1);
auto devices_dict =
pref_service_->GetDict(prefs::kPointingStickDeviceSettingsDictPref)
.Clone();
auto* settings_dict = devices_dict.FindDict(kPointingStickKey1);
ASSERT_NE(nullptr, settings_dict);
// Set a fake key to simulate a setting being removed from 1 milestone to the
// next.
settings_dict->Set(kDictFakeKey, kDictFakeValue);
pref_service_->SetDict(prefs::kPointingStickDeviceSettingsDictPref,
std::move(devices_dict));
// Update the settings again and verify the fake key and value still exist.
CallUpdatePointingStickSettings(kPointingStickKey1, kPointingStickSettings1);
const auto& updated_devices_dict =
pref_service_->GetDict(prefs::kPointingStickDeviceSettingsDictPref);
const auto* updated_settings_dict =
updated_devices_dict.FindDict(kPointingStickKey1);
const std::string* value = updated_settings_dict->FindString(kDictFakeKey);
ASSERT_NE(nullptr, value);
EXPECT_EQ(kDictFakeValue, *value);
}
TEST_F(PointingStickPrefHandlerTest, LastUpdated) {
CallUpdatePointingStickSettings(kPointingStickKey1, kPointingStickSettings1,
/*is_external=*/true);
auto devices_dict =
pref_service_->GetDict(prefs::kPointingStickDeviceSettingsDictPref)
.Clone();
auto* settings_dict = devices_dict.FindDict(kPointingStickKey1);
ASSERT_NE(nullptr, settings_dict);
auto* time_stamp1 = settings_dict->Find(prefs::kLastUpdatedKey);
ASSERT_NE(nullptr, time_stamp1);
mojom::PointingStickSettingsPtr updated_settings =
kPointingStickSettings1.Clone();
updated_settings->swap_right = !updated_settings->swap_right;
CallUpdatePointingStickSettings(kPointingStickKey1, *updated_settings);
const auto& updated_devices_dict =
pref_service_->GetDict(prefs::kPointingStickDeviceSettingsDictPref);
const auto* updated_settings_dict =
updated_devices_dict.FindDict(kPointingStickKey1);
ASSERT_NE(nullptr, updated_settings_dict);
auto* updated_time_stamp1 =
updated_settings_dict->Find(prefs::kLastUpdatedKey);
ASSERT_NE(nullptr, updated_time_stamp1);
ASSERT_NE(time_stamp1, updated_time_stamp1);
}
TEST_F(PointingStickPrefHandlerTest, UpdateSettings) {
CallUpdatePointingStickSettings(kPointingStickKey1, kPointingStickSettings1);
CallUpdatePointingStickSettings(kPointingStickKey2, kPointingStickSettings2);
auto devices_dict =
pref_service_->GetDict(prefs::kPointingStickDeviceSettingsDictPref)
.Clone();
auto* settings_dict = devices_dict.FindDict(kPointingStickKey1);
ASSERT_NE(nullptr, settings_dict);
CheckPointingStickSettingsAndDictAreEqual(kPointingStickSettings1,
*settings_dict);
settings_dict = devices_dict.FindDict(kPointingStickKey2);
ASSERT_NE(nullptr, settings_dict);
CheckPointingStickSettingsAndDictAreEqual(kPointingStickSettings2,
*settings_dict);
mojom::PointingStickSettings updated_settings = kPointingStickSettings1;
updated_settings.swap_right = !updated_settings.swap_right;
// Update the settings again and verify the settings are updated in place.
CallUpdatePointingStickSettings(kPointingStickKey1, updated_settings);
const auto& updated_devices_dict =
pref_service_->GetDict(prefs::kPointingStickDeviceSettingsDictPref);
const auto* updated_settings_dict =
updated_devices_dict.FindDict(kPointingStickKey1);
ASSERT_NE(nullptr, updated_settings_dict);
CheckPointingStickSettingsAndDictAreEqual(updated_settings,
*updated_settings_dict);
// Verify other device remains unmodified.
const auto* unchanged_settings_dict =
updated_devices_dict.FindDict(kPointingStickKey2);
ASSERT_NE(nullptr, unchanged_settings_dict);
CheckPointingStickSettingsAndDictAreEqual(kPointingStickSettings2,
*unchanged_settings_dict);
}
TEST_F(PointingStickPrefHandlerTest, UpdateSettingsInternal) {
CallUpdatePointingStickSettings(kPointingStickKey1, kPointingStickSettings1,
/*is_external=*/false);
const auto& settings_dict =
pref_service_->GetDict(prefs::kPointingStickInternalSettings);
CheckPointingStickSettingsAndDictAreEqual(kPointingStickSettings1,
settings_dict);
mojom::PointingStickSettings updated_settings = kPointingStickSettings1;
updated_settings.swap_right = !updated_settings.swap_right;
// Update the settings again and verify the settings are updated in place.
CallUpdatePointingStickSettings(kPointingStickKey1, updated_settings,
/*is_external=*/false);
const auto& updated_settings_dict =
pref_service_->GetDict(prefs::kPointingStickInternalSettings);
CheckPointingStickSettingsAndDictAreEqual(updated_settings,
updated_settings_dict);
}
TEST_F(PointingStickPrefHandlerTest, NewSettingAddedRoundTrip) {
mojom::PointingStickSettings test_settings = kPointingStickSettings1;
test_settings.swap_right = !kDefaultSwapRight;
CallUpdatePointingStickSettings(kPointingStickKey1, test_settings);
auto devices_dict =
pref_service_->GetDict(prefs::kPointingStickDeviceSettingsDictPref)
.Clone();
auto* settings_dict = devices_dict.FindDict(kPointingStickKey1);
// Remove key from the dict to mock adding a new setting in the future.
settings_dict->Remove(prefs::kPointingStickSettingSwapRight);
pref_service_->SetDict(prefs::kPointingStickDeviceSettingsDictPref,
std::move(devices_dict));
// Initialize keyboard settings for the device and check that
// "new settings" match their default values.
mojom::PointingStickSettingsPtr settings =
CallInitializePointingStickSettings(kPointingStickKey1);
EXPECT_EQ(kDefaultSwapRight, settings->swap_right);
// Reset "new settings" to the values that match `test_settings` and check
// that the rest of the fields are equal.
settings->swap_right = !kDefaultSwapRight;
EXPECT_EQ(test_settings, *settings);
}
TEST_F(PointingStickPrefHandlerTest, NewSettingAddedRoundTripInternal) {
mojom::PointingStickSettings test_settings = kPointingStickSettings1;
test_settings.swap_right = !kDefaultSwapRight;
CallUpdatePointingStickSettings(kPointingStickKey1, test_settings,
/*is_external=*/false);
auto settings_dict =
pref_service_->GetDict(prefs::kPointingStickInternalSettings).Clone();
// Remove key from the dict to mock adding a new setting in the future.
settings_dict.Remove(prefs::kPointingStickSettingSwapRight);
pref_service_->SetDict(prefs::kPointingStickInternalSettings,
std::move(settings_dict));
// Initialize PointingStick settings for the device and check that
// "new settings" match their default values.
mojom::PointingStickSettingsPtr settings =
CallInitializePointingStickSettings(kPointingStickKey1,
/*is_external=*/false);
EXPECT_EQ(kDefaultReverseScrolling, settings->swap_right);
// Reset "new settings" to the values that match `test_settings` and check
// that the rest of the fields are equal.
settings->swap_right = !kDefaultReverseScrolling;
EXPECT_EQ(test_settings, *settings);
}
TEST_F(PointingStickPrefHandlerTest, DefaultSettingsWhenPrefServiceNull) {
mojom::PointingStick pointing_stick;
pointing_stick.device_key = kPointingStickKey1;
pref_handler_->InitializePointingStickSettings(nullptr, &pointing_stick);
EXPECT_EQ(kPointingStickSettingsDefault, *pointing_stick.settings);
}
TEST_F(PointingStickPrefHandlerTest, NewPointingStickDefaultSettings) {
mojom::PointingStickSettingsPtr settings =
CallInitializePointingStickSettings(kPointingStickKey1);
EXPECT_EQ(*settings, kPointingStickSettingsDefault);
settings = CallInitializePointingStickSettings(kPointingStickKey2);
EXPECT_EQ(*settings, kPointingStickSettingsDefault);
auto devices_dict =
pref_service_->GetDict(prefs::kPointingStickDeviceSettingsDictPref)
.Clone();
ASSERT_EQ(2u, devices_dict.size());
auto* settings_dict = devices_dict.FindDict(kPointingStickKey1);
ASSERT_NE(nullptr, settings_dict);
CheckPointingStickSettingsAndDictAreEqual(kPointingStickSettingsDefault,
*settings_dict);
settings_dict = devices_dict.FindDict(kPointingStickKey2);
ASSERT_NE(nullptr, settings_dict);
CheckPointingStickSettingsAndDictAreEqual(kPointingStickSettingsDefault,
*settings_dict);
}
TEST_F(PointingStickPrefHandlerTest, NewPointingStickDefaultSettingsInternal) {
mojom::PointingStickSettingsPtr settings =
CallInitializePointingStickSettings(kPointingStickKey1,
/*is_external=*/false);
EXPECT_EQ(*settings, kPointingStickSettingsDefault);
auto& settings_dict =
pref_service_->GetDict(prefs::kPointingStickInternalSettings);
CheckPointingStickSettingsAndDictAreEqual(kPointingStickSettingsDefault,
settings_dict);
}
TEST_F(PointingStickPrefHandlerTest,
PointingStickObserveredInTransitionPeriod) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndDisableFeature(features::kInputDeviceSettingsSplit);
mojom::PointingStick pointing_stick;
pointing_stick.device_key = kPointingStickKey1;
Shell::Get()->input_device_tracker()->OnPointingStickConnected(
pointing_stick);
// Initialize PointingStick settings for the device and check that the global
// prefs were used as defaults.
mojom::PointingStickSettingsPtr settings =
CallInitializePointingStickSettings(pointing_stick.device_key);
ASSERT_EQ(settings->sensitivity, kTestSensitivity);
ASSERT_EQ(settings->swap_right, kTestSwapRight);
ASSERT_EQ(settings->acceleration_enabled, kTestAccelerationEnabled);
}
TEST_F(PointingStickPrefHandlerTest,
TransitionPeriodSettingsPersistedWhenUserChosen) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndDisableFeature(features::kInputDeviceSettingsSplit);
mojom::PointingStick pointing_stick;
pointing_stick.device_key = kPointingStickKey1;
Shell::Get()->input_device_tracker()->OnPointingStickConnected(
pointing_stick);
pref_service_->SetUserPref(prefs::kPointingStickSensitivity,
base::Value(kDefaultSensitivity));
pref_service_->SetUserPref(prefs::kPrimaryPointingStickButtonRight,
base::Value(kDefaultSwapRight));
pref_service_->SetUserPref(prefs::kPointingStickAcceleration,
base::Value(kDefaultAccelerationEnabled));
mojom::PointingStickSettingsPtr settings =
CallInitializePointingStickSettings(pointing_stick.device_key);
EXPECT_EQ(kPointingStickSettingsDefault, *settings);
const auto* settings_dict = GetSettingsDict(pointing_stick.device_key);
EXPECT_TRUE(settings_dict->contains(prefs::kPointingStickSettingSensitivity));
EXPECT_TRUE(
settings_dict->contains(prefs::kPointingStickSettingAcceleration));
EXPECT_TRUE(settings_dict->contains(prefs::kPointingStickSettingSwapRight));
CheckPointingStickSettingsAndDictAreEqual(kPointingStickSettingsDefault,
*settings_dict);
}
TEST_F(PointingStickPrefHandlerTest, DefaultNotPersistedUntilUpdated) {
CallUpdatePointingStickSettings(kPointingStickKey1,
kPointingStickSettingsDefault);
const auto* settings_dict = GetSettingsDict(kPointingStickKey1);
EXPECT_FALSE(
settings_dict->contains(prefs::kPointingStickSettingSensitivity));
EXPECT_FALSE(
settings_dict->contains(prefs::kPointingStickSettingAcceleration));
EXPECT_FALSE(settings_dict->contains(prefs::kPointingStickSettingSwapRight));
CheckPointingStickSettingsAndDictAreEqual(kPointingStickSettingsDefault,
*settings_dict);
CallUpdatePointingStickSettings(kPointingStickKey1,
kPointingStickSettingsNotDefault);
settings_dict = GetSettingsDict(kPointingStickKey1);
EXPECT_TRUE(settings_dict->contains(prefs::kPointingStickSettingSensitivity));
EXPECT_TRUE(
settings_dict->contains(prefs::kPointingStickSettingAcceleration));
EXPECT_TRUE(settings_dict->contains(prefs::kPointingStickSettingSwapRight));
CheckPointingStickSettingsAndDictAreEqual(kPointingStickSettingsNotDefault,
*settings_dict);
CallUpdatePointingStickSettings(kPointingStickKey1,
kPointingStickSettingsDefault);
settings_dict = GetSettingsDict(kPointingStickKey1);
EXPECT_TRUE(settings_dict->contains(prefs::kPointingStickSettingSensitivity));
EXPECT_TRUE(
settings_dict->contains(prefs::kPointingStickSettingAcceleration));
EXPECT_TRUE(settings_dict->contains(prefs::kPointingStickSettingSwapRight));
CheckPointingStickSettingsAndDictAreEqual(kPointingStickSettingsDefault,
*settings_dict);
}
TEST_F(PointingStickPrefHandlerTest, SettingsUpdateMetricTest) {
const auto settings1 =
CallInitializePointingStickSettings(kPointingStickKey1);
// When its the first device of the type the category should be kFirstEver.
{
const auto& metric_dict =
pref_service_->GetDict(prefs::kPointingStickUpdateSettingsMetricInfo);
ASSERT_TRUE(metric_dict.contains(kPointingStickKey1));
auto metrics_info = SettingsUpdatedMetricsInfo::FromDict(
*metric_dict.FindDict(kPointingStickKey1));
ASSERT_TRUE(metrics_info);
EXPECT_EQ(SettingsUpdatedMetricsInfo::Category::kFirstEver,
metrics_info->category());
}
// When its taken from synced prefs on a different device, category should
// match.
{
auto devices_dict =
pref_service_->GetDict(prefs::kPointingStickDeviceSettingsDictPref)
.Clone();
devices_dict.Set(kPointingStickKey2, base::Value::Dict());
pref_service_->SetDict(prefs::kPointingStickDeviceSettingsDictPref,
std::move(devices_dict));
CallInitializePointingStickSettings(kPointingStickKey2);
const auto& metric_dict =
pref_service_->GetDict(prefs::kPointingStickUpdateSettingsMetricInfo);
ASSERT_TRUE(metric_dict.contains(kPointingStickKey2));
auto metrics_info = SettingsUpdatedMetricsInfo::FromDict(
*metric_dict.FindDict(kPointingStickKey2));
ASSERT_TRUE(metrics_info);
EXPECT_EQ(SettingsUpdatedMetricsInfo::Category::kSynced,
metrics_info->category());
}
}
class PointingStickSettingsPrefConversionTest
: public PointingStickPrefHandlerTest,
public testing::WithParamInterface<
std::tuple<std::string, mojom::PointingStickSettings>> {
public:
PointingStickSettingsPrefConversionTest() = default;
PointingStickSettingsPrefConversionTest(
const PointingStickSettingsPrefConversionTest&) = delete;
PointingStickSettingsPrefConversionTest& operator=(
const PointingStickSettingsPrefConversionTest&) = delete;
~PointingStickSettingsPrefConversionTest() override = default;
// testing::Test:
void SetUp() override {
PointingStickPrefHandlerTest::SetUp();
std::tie(device_key_, settings_) = GetParam();
}
protected:
std::string device_key_;
mojom::PointingStickSettings settings_;
};
INSTANTIATE_TEST_SUITE_P(
// Empty to simplify gtest output
,
PointingStickSettingsPrefConversionTest,
testing::Combine(testing::Values(kPointingStickKey1, kPointingStickKey2),
testing::Values(kPointingStickSettings1,
kPointingStickSettings2)));
TEST_P(PointingStickSettingsPrefConversionTest, CheckConversion) {
CallUpdatePointingStickSettings(device_key_, settings_);
const auto& devices_dict =
pref_service_->GetDict(prefs::kPointingStickDeviceSettingsDictPref);
ASSERT_EQ(1u, devices_dict.size());
auto* settings_dict = devices_dict.FindDict(device_key_);
ASSERT_NE(nullptr, settings_dict);
CheckPointingStickSettingsAndDictAreEqual(settings_, *settings_dict);
}
TEST_P(PointingStickSettingsPrefConversionTest, CheckConversionInternal) {
CallUpdatePointingStickSettings(device_key_, settings_,
/*is_external=*/false);
const auto* settings_dict =
GetSettingsDict(device_key_, /*is_external=*/false);
CheckPointingStickSettingsAndDictAreEqual(settings_, *settings_dict);
}
} // namespace ash