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/x/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/device_hotplug_event_observer.h" |
| 15 #include "ui/events/input_device.h" |
| 16 #include "ui/events/input_device_event_observer.h" |
| 17 #include "ui/events/keyboard_device.h" |
| 18 #include "ui/events/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 virtual ~TestInputDeviceObserver() { |
| 33 if (manager_) |
| 34 manager_->RemoveObserver(this); |
| 35 } |
| 36 |
| 37 // InputDeviceEventObserver implementation. |
| 38 virtual void OnTouchscreenDeviceConfigurationChanged() override {} |
| 39 virtual void OnKeyboardDeviceConfigurationChanged() override { |
| 40 change_notified_ = true; |
| 41 } |
| 42 |
| 43 int change_notified() const { return change_notified_; } |
| 44 void Reset() { change_notified_ = false; } |
| 45 |
| 46 private: |
| 47 DeviceDataManager* manager_; |
| 48 bool change_notified_; |
| 49 |
| 50 DISALLOW_COPY_AND_ASSIGN(TestInputDeviceObserver); |
| 51 }; |
| 52 |
| 53 } // namespace |
| 54 |
| 55 class DeviceDataManagerX11Test : public testing::Test { |
| 56 public: |
| 57 DeviceDataManagerX11Test() {} |
| 58 virtual ~DeviceDataManagerX11Test() {} |
| 59 |
| 60 virtual void SetUp() override { DeviceDataManagerX11::CreateInstance(); } |
| 61 |
| 62 virtual void TearDown() override { |
| 63 SetKeyboardDevices(std::vector<KeyboardDevice>()); |
| 64 } |
| 65 |
| 66 virtual void SetKeyboardDevices(const std::vector<KeyboardDevice>& devices) { |
| 67 DeviceHotplugEventObserver* manager = DeviceDataManagerX11::GetInstance(); |
| 68 manager->OnKeyboardDevicesUpdated(devices); |
| 69 } |
| 70 |
| 71 private: |
| 72 DISALLOW_COPY_AND_ASSIGN(DeviceDataManagerX11Test); |
| 73 }; |
| 74 |
| 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()); |
| 89 observer.Reset(); |
| 90 // Disable the device, should be notified that the device list contains one |
| 91 // less device. |
| 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); |
| 98 observer.Reset(); |
| 99 // Reenable the device, should be notified that the device list contains one |
| 100 // more device. |
| 101 manager->EnableDevice(2u); |
| 102 EXPECT_TRUE(observer.change_notified()); |
| 103 devices = manager->keyboard_devices(); |
| 104 EXPECT_EQ(keyboards.size(), devices.size()); |
| 105 } |
| 106 |
| 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()); |
| 120 observer.Reset(); |
| 121 // Disable the device, should be notified that the device list contains one |
| 122 // less device. |
| 123 manager->DisableDevice(1u); |
| 124 EXPECT_TRUE(observer.change_notified()); |
| 125 devices = manager->keyboard_devices(); |
| 126 EXPECT_EQ(1u, devices.size()); |
| 127 observer.Reset(); |
| 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()); |
| 133 observer.Reset(); |
| 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()); |
| 139 observer.Reset(); |
| 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()); |
| 145 } |
| 146 |
| 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()); |
| 159 observer.Reset(); |
| 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()); |
| 165 observer.Reset(); |
| 166 // Unplug the disabled device. Should not be notified, since the active list |
| 167 // did not change. |
| 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()); |
| 179 } |
| 180 |
| 181 } // namespace test |
| 182 } // namespace ui |
OLD | NEW |