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
ash / system / input_device_settings / input_device_settings_dispatcher_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/input_device_settings_dispatcher.h"
#include "ash/constants/ash_features.h"
#include "ash/events/event_rewriter_controller_impl.h"
#include "ash/public/cpp/input_device_settings_controller.h"
#include "ash/public/mojom/input_device_settings.mojom.h"
#include "ash/shell.h"
#include "ash/test/ash_test_base.h"
#include "base/test/scoped_feature_list.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "ui/events/devices/device_data_manager_test_api.h"
#include "ui/events/devices/haptic_touchpad_effects.h"
#include "ui/events/devices/input_device.h"
#include "ui/events/devices/stylus_state.h"
#include "ui/events/keycodes/dom/dom_code.h"
#include "ui/events/keycodes/keyboard_codes_posix.h"
#include "ui/ozone/public/stub_input_controller.h"
namespace ash {
namespace {
constexpr int kMouseId = 1;
constexpr int kPointingStickId = 2;
constexpr int kTouchpadId = 3;
class MockInputController : public ui::StubInputController {
public:
MOCK_METHOD(void,
SetTouchpadSensitivity,
(std::optional<int> device_id, int value),
(override));
MOCK_METHOD(void,
SetTouchpadScrollSensitivity,
(std::optional<int> device_id, int value),
(override));
MOCK_METHOD(void,
SetTouchpadHapticFeedback,
(std::optional<int> device_id, bool enabled),
(override));
MOCK_METHOD(void,
SetTouchpadHapticClickSensitivity,
(std::optional<int> device_id, int value),
(override));
MOCK_METHOD(void,
SetTapToClick,
(std::optional<int> device_id, bool enabled),
(override));
MOCK_METHOD(void,
SetTapDragging,
(std::optional<int> device_id, bool enabled),
(override));
MOCK_METHOD(void,
SetNaturalScroll,
(std::optional<int> device_id, bool enabled),
(override));
MOCK_METHOD(void,
SetMouseSensitivity,
(std::optional<int> device_id, int value),
(override));
MOCK_METHOD(void,
SetMouseScrollSensitivity,
(std::optional<int> device_id, int value),
(override));
MOCK_METHOD(void,
SetMouseReverseScroll,
(std::optional<int> device_id, bool enabled),
(override));
MOCK_METHOD(void,
SetMouseAcceleration,
(std::optional<int> device_id, bool enabled),
(override));
MOCK_METHOD(void,
SetMouseScrollAcceleration,
(std::optional<int> device_id, bool enabled),
(override));
MOCK_METHOD(void,
SetPointingStickSensitivity,
(std::optional<int> device_id, int value),
(override));
MOCK_METHOD(void,
SetPointingStickAcceleration,
(std::optional<int> device_id, bool enabled),
(override));
MOCK_METHOD(void,
SetTouchpadAcceleration,
(std::optional<int> device_id, bool enabled),
(override));
MOCK_METHOD(void,
SetTouchpadScrollAcceleration,
(std::optional<int> device_id, bool enabled),
(override));
MOCK_METHOD(void,
SetPrimaryButtonRight,
(std::optional<int> device_id, bool right),
(override));
MOCK_METHOD(void,
SetPointingStickPrimaryButtonRight,
(std::optional<int> device_id, bool right),
(override));
MOCK_METHOD(void,
BlockModifiersOnDevices,
(std::vector<int> device_ids),
(override));
MOCK_METHOD(std::unique_ptr<ui::ScopedDisableInputDevices>,
DisableInputDevices,
(),
(override));
MOCK_METHOD(bool, AreInputDevicesEnabled, (), (const override));
};
const ui::InputDevice CreateInputDevice(int id,
uint16_t vendor,
uint16_t product) {
return ui::InputDevice(id, ui::INPUT_DEVICE_USB, "kDeviceName", "",
base::FilePath(), vendor, product, 0);
}
} // namespace
class InputDeviceSettingsDispatcherTest : public AshTestBase {
public:
InputDeviceSettingsDispatcherTest() = default;
InputDeviceSettingsDispatcherTest(const InputDeviceSettingsDispatcherTest&) =
delete;
InputDeviceSettingsDispatcherTest& operator=(
const InputDeviceSettingsDispatcherTest&) = delete;
~InputDeviceSettingsDispatcherTest() override = default;
// testing::Test:
void SetUp() override {
scoped_feature_list_.InitWithFeatures({features::kInputDeviceSettingsSplit,
features::kPeripheralCustomization},
{});
AshTestBase::SetUp();
Shell::Get()->event_rewriter_controller()->Initialize(nullptr, nullptr);
controller_ = std::make_unique<MockInputController>();
dispatcher_ =
std::make_unique<InputDeviceSettingsDispatcher>(controller_.get());
ON_CALL(*controller_, BlockModifiersOnDevices)
.WillByDefault(testing::Invoke([&](std::vector<int> device_ids) {
device_ids_to_block_modifiers_ = std::move(device_ids);
}));
}
void TearDown() override {
// Dispatcher must be destroyed before the mock controller as the dispatcher
// depends on the controller.
dispatcher_.reset();
controller_.reset();
AshTestBase::TearDown();
}
protected:
std::unique_ptr<InputDeviceSettingsDispatcher> dispatcher_;
std::unique_ptr<MockInputController> controller_;
base::test::ScopedFeatureList scoped_feature_list_;
std::vector<int> device_ids_to_block_modifiers_;
};
TEST_F(InputDeviceSettingsDispatcherTest, MouseTest) {
mojom::Mouse mouse;
mouse.id = kMouseId;
mouse.settings = mojom::MouseSettings::New();
auto& settings = *mouse.settings;
settings.acceleration_enabled = false;
settings.reverse_scrolling = false;
settings.swap_right = false;
settings.scroll_sensitivity = 3;
settings.sensitivity = 3;
constexpr std::optional<int> mouse_id = kMouseId;
EXPECT_CALL(*controller_, SetMouseSensitivity(mouse_id, settings.sensitivity))
.Times(2);
EXPECT_CALL(*controller_,
SetMouseReverseScroll(mouse_id, settings.reverse_scrolling))
.Times(2);
EXPECT_CALL(*controller_, SetMouseScrollAcceleration(
mouse_id, settings.scroll_acceleration))
.Times(2);
EXPECT_CALL(*controller_,
SetMouseScrollSensitivity(mouse_id, settings.scroll_sensitivity))
.Times(2);
EXPECT_CALL(*controller_,
SetMouseAcceleration(mouse_id, settings.acceleration_enabled))
.Times(2);
EXPECT_CALL(*controller_,
SetPrimaryButtonRight(mouse_id, settings.swap_right))
.Times(2);
dispatcher_->OnMouseConnected(mouse);
dispatcher_->OnMouseSettingsUpdated(mouse);
}
TEST_F(InputDeviceSettingsDispatcherTest, PointingStickTest) {
mojom::PointingStick pointing_stick;
pointing_stick.id = kPointingStickId;
pointing_stick.settings = mojom::PointingStickSettings::New();
auto& settings = *pointing_stick.settings;
settings.acceleration_enabled = false;
settings.swap_right = false;
settings.sensitivity = 3;
constexpr std::optional<int> pointing_stick_id = kPointingStickId;
EXPECT_CALL(*controller_, SetPointingStickSensitivity(pointing_stick_id,
settings.sensitivity))
.Times(2);
EXPECT_CALL(*controller_,
SetPointingStickAcceleration(pointing_stick_id,
settings.acceleration_enabled))
.Times(2);
EXPECT_CALL(*controller_, SetPointingStickPrimaryButtonRight(
pointing_stick_id, settings.swap_right))
.Times(2);
dispatcher_->OnPointingStickConnected(pointing_stick);
dispatcher_->OnPointingStickSettingsUpdated(pointing_stick);
}
TEST_F(InputDeviceSettingsDispatcherTest, TouchpadTest) {
mojom::Touchpad touchpad;
touchpad.id = kTouchpadId;
touchpad.settings = mojom::TouchpadSettings::New();
auto& settings = *touchpad.settings;
settings.acceleration_enabled = false;
settings.haptic_enabled = false;
settings.haptic_sensitivity = 1;
settings.scroll_sensitivity = 3;
settings.reverse_scrolling = false;
settings.tap_dragging_enabled = false;
settings.tap_to_click_enabled = false;
settings.sensitivity = 5;
constexpr std::optional<int> touchpad_id = kTouchpadId;
EXPECT_CALL(*controller_,
SetTouchpadSensitivity(touchpad_id, settings.sensitivity))
.Times(2);
EXPECT_CALL(*controller_, SetTouchpadScrollSensitivity(
touchpad_id, settings.scroll_sensitivity))
.Times(2);
EXPECT_CALL(*controller_, SetTouchpadAcceleration(
touchpad_id, settings.acceleration_enabled))
.Times(2);
EXPECT_CALL(*controller_, SetTouchpadScrollAcceleration(
touchpad_id, settings.scroll_acceleration))
.Times(2);
EXPECT_CALL(*controller_, SetTouchpadHapticClickSensitivity(
touchpad_id, settings.haptic_sensitivity))
.Times(2);
EXPECT_CALL(*controller_,
SetTouchpadHapticFeedback(touchpad_id, settings.haptic_enabled))
.Times(2);
EXPECT_CALL(*controller_,
SetTapToClick(touchpad_id, settings.tap_to_click_enabled))
.Times(2);
EXPECT_CALL(*controller_,
SetNaturalScroll(touchpad_id, settings.reverse_scrolling))
.Times(2);
EXPECT_CALL(*controller_,
SetTapDragging(touchpad_id, settings.tap_dragging_enabled))
.Times(2);
dispatcher_->OnTouchpadConnected(touchpad);
dispatcher_->OnTouchpadSettingsUpdated(touchpad);
}
TEST_F(InputDeviceSettingsDispatcherTest, DuplicateIdsBlockModifiers) {
// Use VID/PID for mouse with modifier blocking issue.
auto duplicate_1_1 = CreateInputDevice(0, 0x046d, 0xb019);
auto duplicate_1_2 = CreateInputDevice(1, 0x046d, 0xb019);
auto duplicate_1_3 = CreateInputDevice(2, 0x046d, 0xb019);
// Use VID/PID without modifier blocking issue.
auto duplicate_2_1 = CreateInputDevice(3, 0x046d, 0xb034);
auto duplicate_2_2 = CreateInputDevice(4, 0x046d, 0xb034);
auto duplicate_2_3 = CreateInputDevice(5, 0x046d, 0xb034);
ui::DeviceDataManagerTestApi().SetMouseDevices(
{duplicate_1_1, duplicate_2_1});
ui::DeviceDataManagerTestApi().SetGraphicsTabletDevices(
{duplicate_1_2, duplicate_2_2});
ui::DeviceDataManagerTestApi().SetUncategorizedDevices({duplicate_1_3});
ui::DeviceDataManagerTestApi().SetKeyboardDevices(
{ui::KeyboardDevice(duplicate_2_3)});
EXPECT_TRUE(device_ids_to_block_modifiers_.empty());
auto* input_device_settings_controller =
Shell::Get()->input_device_settings_controller();
// After starting to observe, all device ids with same vid/pid as
// `duplicate_1_1` should be blocked.
input_device_settings_controller->StartObservingButtons(duplicate_1_1.id);
EXPECT_EQ(
(std::vector<int>{duplicate_1_1.id, duplicate_1_2.id, duplicate_1_3.id}),
device_ids_to_block_modifiers_);
// Add mouse button to configure.
input_device_settings_controller->OnMouseButtonPressed(
duplicate_1_1.id, *mojom::Button::NewVkey(ui::VKEY_TAB));
input_device_settings_controller->StopObservingButtons();
EXPECT_TRUE(device_ids_to_block_modifiers_.empty());
auto mouse_settings =
input_device_settings_controller->GetMouseSettings(duplicate_1_1.id)
->Clone();
mouse_settings->button_remappings.back()->remapping_action =
mojom::RemappingAction::NewStaticShortcutAction(
mojom::StaticShortcutAction::kDisable);
input_device_settings_controller->SetMouseSettings(duplicate_1_1.id,
std::move(mouse_settings));
// After configuring the button to an action, block modifiers from the device.
EXPECT_EQ(
(std::vector<int>{duplicate_1_1.id, duplicate_1_2.id, duplicate_1_3.id}),
device_ids_to_block_modifiers_);
mouse_settings =
input_device_settings_controller->GetMouseSettings(duplicate_1_1.id)
->Clone();
mouse_settings->button_remappings.back()->remapping_action = nullptr;
input_device_settings_controller->SetMouseSettings(duplicate_1_1.id,
std::move(mouse_settings));
// After settings back to no remapping action, modifiers should not be
// blocked.
EXPECT_TRUE(device_ids_to_block_modifiers_.empty());
}
TEST_F(InputDeviceSettingsDispatcherTest, DuplicateIdsDontBlockModifiers) {
// Use VID/PID for mouse with modifier blocking issue.
auto duplicate_1_1 = CreateInputDevice(0, 0x046d, 0xb019);
auto duplicate_1_2 = CreateInputDevice(1, 0x046d, 0xb019);
auto duplicate_1_3 = CreateInputDevice(2, 0x046d, 0xb019);
// Use VID/PID without modifier blocking issue.
auto duplicate_2_1 = CreateInputDevice(3, 0x046d, 0xb034);
auto duplicate_2_2 = CreateInputDevice(4, 0x046d, 0xb034);
auto duplicate_2_3 = CreateInputDevice(5, 0x046d, 0xb034);
ui::DeviceDataManagerTestApi().SetMouseDevices(
{duplicate_1_1, duplicate_2_1});
ui::DeviceDataManagerTestApi().SetGraphicsTabletDevices(
{duplicate_1_2, duplicate_2_2});
ui::DeviceDataManagerTestApi().SetUncategorizedDevices({duplicate_1_3});
ui::DeviceDataManagerTestApi().SetKeyboardDevices(
{ui::KeyboardDevice(duplicate_2_3)});
EXPECT_TRUE(device_ids_to_block_modifiers_.empty());
auto* input_device_settings_controller =
Shell::Get()->input_device_settings_controller();
// After starting to observe, nothing should be blocked as `duplicate_2_1`
// does not require modifier blocking.
input_device_settings_controller->StartObservingButtons(duplicate_2_1.id);
EXPECT_TRUE(device_ids_to_block_modifiers_.empty());
// Add mouse button to configure.
input_device_settings_controller->OnMouseButtonPressed(
duplicate_2_1.id, *mojom::Button::NewVkey(ui::VKEY_TAB));
input_device_settings_controller->StopObservingButtons();
EXPECT_TRUE(device_ids_to_block_modifiers_.empty());
auto mouse_settings =
input_device_settings_controller->GetMouseSettings(duplicate_2_1.id)
->Clone();
mouse_settings->button_remappings.back()->remapping_action =
mojom::RemappingAction::NewStaticShortcutAction(
mojom::StaticShortcutAction::kDisable);
input_device_settings_controller->SetMouseSettings(duplicate_2_1.id,
std::move(mouse_settings));
// After configuring the button to an action, no modifiers should be blocked
// still.
EXPECT_TRUE(device_ids_to_block_modifiers_.empty());
mouse_settings =
input_device_settings_controller->GetMouseSettings(duplicate_2_1.id)
->Clone();
mouse_settings->button_remappings.back()->remapping_action = nullptr;
input_device_settings_controller->SetMouseSettings(duplicate_2_1.id,
std::move(mouse_settings));
// After settings back to no remapping action, modifiers should not be
// blocked.
EXPECT_TRUE(device_ids_to_block_modifiers_.empty());
}
} // namespace ash