OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "ui/events/devices/x11/device_data_manager_x11.h" |
| 6 |
| 7 #include <vector> |
| 8 |
| 9 // Generically-named #defines from Xlib that conflict with symbols in GTest. |
| 10 #undef Bool |
| 11 #undef None |
| 12 |
| 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" |
| 19 |
| 20 namespace ui { |
| 21 namespace test { |
| 22 namespace { |
| 23 |
| 24 class TestInputDeviceObserver : public InputDeviceEventObserver { |
| 25 public: |
| 26 explicit TestInputDeviceObserver(DeviceDataManagerX11* manager) |
| 27 : manager_(manager), change_notified_(false) { |
| 28 if (manager_) |
| 29 manager_->AddObserver(this); |
| 30 } |
| 31 |
| 32 ~TestInputDeviceObserver() override { |
| 33 if (manager_) |
| 34 manager_->RemoveObserver(this); |
| 35 } |
| 36 |
| 37 // InputDeviceEventObserver implementation. |
| 38 void OnKeyboardDeviceConfigurationChanged() override { |
| 39 change_notified_ = true; |
| 40 } |
| 41 |
| 42 int change_notified() const { return change_notified_; } |
| 43 void Reset() { change_notified_ = false; } |
| 44 |
| 45 private: |
| 46 DeviceDataManager* manager_; |
| 47 bool change_notified_; |
| 48 |
| 49 DISALLOW_COPY_AND_ASSIGN(TestInputDeviceObserver); |
| 50 }; |
| 51 |
| 52 } // namespace |
| 53 |
| 54 class DeviceDataManagerX11Test : public testing::Test { |
| 55 public: |
| 56 DeviceDataManagerX11Test() {} |
| 57 ~DeviceDataManagerX11Test() override {} |
| 58 |
| 59 void SetUp() override { DeviceDataManagerX11::CreateInstance(); } |
| 60 |
| 61 void TearDown() override { |
| 62 SetKeyboardDevices(std::vector<KeyboardDevice>()); |
| 63 } |
| 64 |
| 65 virtual void SetKeyboardDevices(const std::vector<KeyboardDevice>& devices) { |
| 66 DeviceHotplugEventObserver* manager = DeviceDataManagerX11::GetInstance(); |
| 67 manager->OnKeyboardDevicesUpdated(devices); |
| 68 } |
| 69 |
| 70 private: |
| 71 DISALLOW_COPY_AND_ASSIGN(DeviceDataManagerX11Test); |
| 72 }; |
| 73 |
| 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()); |
| 88 observer.Reset(); |
| 89 // Disable the device, should be notified that the device list contains one |
| 90 // less device. |
| 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); |
| 97 observer.Reset(); |
| 98 // Reenable the device, should be notified that the device list contains one |
| 99 // more device. |
| 100 manager->EnableDevice(2); |
| 101 EXPECT_TRUE(observer.change_notified()); |
| 102 devices = manager->keyboard_devices(); |
| 103 EXPECT_EQ(keyboards.size(), devices.size()); |
| 104 } |
| 105 |
| 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()); |
| 119 observer.Reset(); |
| 120 // Disable the device, should be notified that the device list contains one |
| 121 // less device. |
| 122 manager->DisableDevice(1); |
| 123 EXPECT_TRUE(observer.change_notified()); |
| 124 devices = manager->keyboard_devices(); |
| 125 EXPECT_EQ(1u, devices.size()); |
| 126 observer.Reset(); |
| 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()); |
| 132 observer.Reset(); |
| 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()); |
| 138 observer.Reset(); |
| 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()); |
| 144 } |
| 145 |
| 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()); |
| 158 observer.Reset(); |
| 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()); |
| 164 observer.Reset(); |
| 165 // Unplug the disabled device. Should not be notified, since the active list |
| 166 // did not change. |
| 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()); |
| 178 } |
| 179 |
| 180 } // namespace test |
| 181 } // namespace ui |
OLD | NEW |