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 OnTouchscreenDeviceConfigurationChanged() override {}
39 void OnKeyboardDeviceConfigurationChanged() override {
40 change_notified_ = true;
43 int change_notified() const { return change_notified_; }
44 void Reset() { change_notified_ = false; }
47 DeviceDataManager* manager_;
48 bool change_notified_;
50 DISALLOW_COPY_AND_ASSIGN(TestInputDeviceObserver);
55 class DeviceDataManagerX11Test : public testing::Test {
57 DeviceDataManagerX11Test() {}
58 ~DeviceDataManagerX11Test() override {}
60 void SetUp() override { DeviceDataManagerX11::CreateInstance(); }
62 void TearDown() override {
63 SetKeyboardDevices(std::vector<KeyboardDevice>());
66 virtual void SetKeyboardDevices(const std::vector<KeyboardDevice>& devices) {
67 DeviceHotplugEventObserver* manager = DeviceDataManagerX11::GetInstance();
68 manager->OnKeyboardDevicesUpdated(devices);
72 DISALLOW_COPY_AND_ASSIGN(DeviceDataManagerX11Test);
75 // Tests that the the device data manager notifies observers when a device is
76 // disabled and re-enabled.
77 TEST_F(DeviceDataManagerX11Test, NotifyOnDisable) {
78 DeviceDataManagerX11* manager = DeviceDataManagerX11::GetInstance();
79 TestInputDeviceObserver observer(manager);
80 std::vector<ui::KeyboardDevice> keyboards;
81 keyboards.push_back(ui::KeyboardDevice(
82 1u, ui::InputDeviceType::INPUT_DEVICE_INTERNAL, "Keyboard"));
83 keyboards.push_back(ui::KeyboardDevice(
84 2u, ui::InputDeviceType::INPUT_DEVICE_INTERNAL, "Keyboard"));
85 SetKeyboardDevices(keyboards);
86 EXPECT_TRUE(observer.change_notified());
87 std::vector<KeyboardDevice> devices = manager->keyboard_devices();
88 EXPECT_EQ(keyboards.size(), devices.size());
90 // Disable the device, should be notified that the device list contains one
92 manager->DisableDevice(2u);
93 EXPECT_TRUE(observer.change_notified());
94 devices = manager->keyboard_devices();
95 EXPECT_EQ(1u, devices.size());
96 KeyboardDevice device = devices.front();
97 EXPECT_EQ(1u, device.id);
99 // Reenable the device, should be notified that the device list contains one
101 manager->EnableDevice(2u);
102 EXPECT_TRUE(observer.change_notified());
103 devices = manager->keyboard_devices();
104 EXPECT_EQ(keyboards.size(), devices.size());
107 // Tests blocking multiple devices.
108 TEST_F(DeviceDataManagerX11Test, TestMultipleDisable) {
109 DeviceDataManagerX11* manager = DeviceDataManagerX11::GetInstance();
110 TestInputDeviceObserver observer(manager);
111 std::vector<ui::KeyboardDevice> keyboards;
112 keyboards.push_back(ui::KeyboardDevice(
113 1u, ui::InputDeviceType::INPUT_DEVICE_INTERNAL, "Keyboard"));
114 keyboards.push_back(ui::KeyboardDevice(
115 2u, ui::InputDeviceType::INPUT_DEVICE_INTERNAL, "Keyboard"));
116 SetKeyboardDevices(keyboards);
117 EXPECT_TRUE(observer.change_notified());
118 std::vector<KeyboardDevice> devices = manager->keyboard_devices();
119 EXPECT_EQ(keyboards.size(), devices.size());
121 // Disable the device, should be notified that the device list contains one
123 manager->DisableDevice(1u);
124 EXPECT_TRUE(observer.change_notified());
125 devices = manager->keyboard_devices();
126 EXPECT_EQ(1u, devices.size());
128 // Disable the second device, should be notified that the device list empty.
129 manager->DisableDevice(2u);
130 EXPECT_TRUE(observer.change_notified());
131 devices = manager->keyboard_devices();
132 EXPECT_EQ(0u, devices.size());
134 // Enable the first device, should be notified that one device present.
135 manager->EnableDevice(1u);
136 EXPECT_TRUE(observer.change_notified());
137 devices = manager->keyboard_devices();
138 EXPECT_EQ(1u, devices.size());
140 // Enable the second device, should be notified that both devices present.
141 manager->EnableDevice(2u);
142 EXPECT_TRUE(observer.change_notified());
143 devices = manager->keyboard_devices();
144 EXPECT_EQ(2u, devices.size());
147 TEST_F(DeviceDataManagerX11Test, UnblockOnDeviceUnplugged) {
148 DeviceDataManagerX11* manager = DeviceDataManagerX11::GetInstance();
149 TestInputDeviceObserver observer(manager);
150 std::vector<ui::KeyboardDevice> all_keyboards;
151 all_keyboards.push_back(ui::KeyboardDevice(
152 1u, ui::InputDeviceType::INPUT_DEVICE_INTERNAL, "Keyboard"));
153 all_keyboards.push_back(ui::KeyboardDevice(
154 2u, ui::InputDeviceType::INPUT_DEVICE_INTERNAL, "Keyboard"));
155 SetKeyboardDevices(all_keyboards);
156 EXPECT_TRUE(observer.change_notified());
157 std::vector<KeyboardDevice> devices = manager->keyboard_devices();
158 EXPECT_EQ(all_keyboards.size(), devices.size());
160 // Expect to be notified that the device is no longer available.
161 manager->DisableDevice(2u);
162 EXPECT_TRUE(observer.change_notified());
163 devices = manager->keyboard_devices();
164 EXPECT_EQ(1u, devices.size());
166 // Unplug the disabled device. Should not be notified, since the active list
168 std::vector<ui::KeyboardDevice> subset_keyboards;
169 subset_keyboards.push_back(ui::KeyboardDevice(
170 1u, ui::InputDeviceType::INPUT_DEVICE_INTERNAL, "Keyboard"));
171 SetKeyboardDevices(subset_keyboards);
172 EXPECT_FALSE(observer.change_notified());
173 // Replug in the first device. Should be notified of the new device.
174 SetKeyboardDevices(all_keyboards);
175 EXPECT_TRUE(observer.change_notified());
176 devices = manager->keyboard_devices();
177 // Both devices now present.
178 EXPECT_EQ(2u, devices.size());