1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/events/devices/x11/device_data_manager_x11.h"
9 // Generically-named #defines from Xlib that conflict with symbols in GTest.
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "ui/events/devices/device_hotplug_event_observer.h"
15 #include "ui/events/devices/input_device.h"
16 #include "ui/events/devices/input_device_event_observer.h"
17 #include "ui/events/devices/keyboard_device.h"
18 #include "ui/events/devices/touchscreen_device.h"
24 class TestInputDeviceObserver
: public InputDeviceEventObserver
{
26 explicit TestInputDeviceObserver(DeviceDataManagerX11
* manager
)
27 : manager_(manager
), change_notified_(false) {
29 manager_
->AddObserver(this);
32 ~TestInputDeviceObserver() override
{
34 manager_
->RemoveObserver(this);
37 // InputDeviceEventObserver implementation.
38 void OnKeyboardDeviceConfigurationChanged() override
{
39 change_notified_
= true;
42 int change_notified() const { return change_notified_
; }
43 void Reset() { change_notified_
= false; }
46 DeviceDataManager
* manager_
;
47 bool change_notified_
;
49 DISALLOW_COPY_AND_ASSIGN(TestInputDeviceObserver
);
54 class DeviceDataManagerX11Test
: public testing::Test
{
56 DeviceDataManagerX11Test() {}
57 ~DeviceDataManagerX11Test() override
{}
59 void SetUp() override
{ DeviceDataManagerX11::CreateInstance(); }
61 void TearDown() override
{
62 SetKeyboardDevices(std::vector
<KeyboardDevice
>());
65 virtual void SetKeyboardDevices(const std::vector
<KeyboardDevice
>& devices
) {
66 DeviceHotplugEventObserver
* manager
= DeviceDataManagerX11::GetInstance();
67 manager
->OnKeyboardDevicesUpdated(devices
);
71 DISALLOW_COPY_AND_ASSIGN(DeviceDataManagerX11Test
);
74 // Tests that the the device data manager notifies observers when a device is
75 // disabled and re-enabled.
76 TEST_F(DeviceDataManagerX11Test
, NotifyOnDisable
) {
77 DeviceDataManagerX11
* manager
= DeviceDataManagerX11::GetInstance();
78 TestInputDeviceObserver
observer(manager
);
79 std::vector
<ui::KeyboardDevice
> keyboards
;
80 keyboards
.push_back(ui::KeyboardDevice(
81 1, ui::InputDeviceType::INPUT_DEVICE_INTERNAL
, "Keyboard"));
82 keyboards
.push_back(ui::KeyboardDevice(
83 2, ui::InputDeviceType::INPUT_DEVICE_INTERNAL
, "Keyboard"));
84 SetKeyboardDevices(keyboards
);
85 EXPECT_TRUE(observer
.change_notified());
86 std::vector
<KeyboardDevice
> devices
= manager
->keyboard_devices();
87 EXPECT_EQ(keyboards
.size(), devices
.size());
89 // Disable the device, should be notified that the device list contains one
91 manager
->DisableDevice(2);
92 EXPECT_TRUE(observer
.change_notified());
93 devices
= manager
->keyboard_devices();
94 EXPECT_EQ(1u, devices
.size());
95 KeyboardDevice device
= devices
.front();
96 EXPECT_EQ(1, device
.id
);
98 // Reenable the device, should be notified that the device list contains one
100 manager
->EnableDevice(2);
101 EXPECT_TRUE(observer
.change_notified());
102 devices
= manager
->keyboard_devices();
103 EXPECT_EQ(keyboards
.size(), devices
.size());
106 // Tests blocking multiple devices.
107 TEST_F(DeviceDataManagerX11Test
, TestMultipleDisable
) {
108 DeviceDataManagerX11
* manager
= DeviceDataManagerX11::GetInstance();
109 TestInputDeviceObserver
observer(manager
);
110 std::vector
<ui::KeyboardDevice
> keyboards
;
111 keyboards
.push_back(ui::KeyboardDevice(
112 1, ui::InputDeviceType::INPUT_DEVICE_INTERNAL
, "Keyboard"));
113 keyboards
.push_back(ui::KeyboardDevice(
114 2, ui::InputDeviceType::INPUT_DEVICE_INTERNAL
, "Keyboard"));
115 SetKeyboardDevices(keyboards
);
116 EXPECT_TRUE(observer
.change_notified());
117 std::vector
<KeyboardDevice
> devices
= manager
->keyboard_devices();
118 EXPECT_EQ(keyboards
.size(), devices
.size());
120 // Disable the device, should be notified that the device list contains one
122 manager
->DisableDevice(1);
123 EXPECT_TRUE(observer
.change_notified());
124 devices
= manager
->keyboard_devices();
125 EXPECT_EQ(1u, devices
.size());
127 // Disable the second device, should be notified that the device list empty.
128 manager
->DisableDevice(2);
129 EXPECT_TRUE(observer
.change_notified());
130 devices
= manager
->keyboard_devices();
131 EXPECT_EQ(0u, devices
.size());
133 // Enable the first device, should be notified that one device present.
134 manager
->EnableDevice(1);
135 EXPECT_TRUE(observer
.change_notified());
136 devices
= manager
->keyboard_devices();
137 EXPECT_EQ(1u, devices
.size());
139 // Enable the second device, should be notified that both devices present.
140 manager
->EnableDevice(2);
141 EXPECT_TRUE(observer
.change_notified());
142 devices
= manager
->keyboard_devices();
143 EXPECT_EQ(2u, devices
.size());
146 TEST_F(DeviceDataManagerX11Test
, UnblockOnDeviceUnplugged
) {
147 DeviceDataManagerX11
* manager
= DeviceDataManagerX11::GetInstance();
148 TestInputDeviceObserver
observer(manager
);
149 std::vector
<ui::KeyboardDevice
> all_keyboards
;
150 all_keyboards
.push_back(ui::KeyboardDevice(
151 1, ui::InputDeviceType::INPUT_DEVICE_INTERNAL
, "Keyboard"));
152 all_keyboards
.push_back(ui::KeyboardDevice(
153 2, ui::InputDeviceType::INPUT_DEVICE_INTERNAL
, "Keyboard"));
154 SetKeyboardDevices(all_keyboards
);
155 EXPECT_TRUE(observer
.change_notified());
156 std::vector
<KeyboardDevice
> devices
= manager
->keyboard_devices();
157 EXPECT_EQ(all_keyboards
.size(), devices
.size());
159 // Expect to be notified that the device is no longer available.
160 manager
->DisableDevice(2);
161 EXPECT_TRUE(observer
.change_notified());
162 devices
= manager
->keyboard_devices();
163 EXPECT_EQ(1u, devices
.size());
165 // Unplug the disabled device. Should not be notified, since the active list
167 std::vector
<ui::KeyboardDevice
> subset_keyboards
;
168 subset_keyboards
.push_back(ui::KeyboardDevice(
169 1, ui::InputDeviceType::INPUT_DEVICE_INTERNAL
, "Keyboard"));
170 SetKeyboardDevices(subset_keyboards
);
171 EXPECT_FALSE(observer
.change_notified());
172 // Replug in the first device. Should be notified of the new device.
173 SetKeyboardDevices(all_keyboards
);
174 EXPECT_TRUE(observer
.change_notified());
175 devices
= manager
->keyboard_devices();
176 // Both devices now present.
177 EXPECT_EQ(2u, devices
.size());