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), |
| 28 change_notified_(false) { |
| 29 if (manager_) |
| 30 manager_->AddObserver(this); |
| 31 } |
| 32 |
| 33 virtual ~TestInputDeviceObserver() { |
| 34 if (manager_) |
| 35 manager_->RemoveObserver(this); |
| 36 } |
| 37 |
| 38 // InputDeviceEventObserver implementation. |
| 39 virtual void OnTouchscreenDeviceConfigurationChanged() override {} |
| 40 virtual void OnKeyboardDeviceConfigurationChanged() override { |
| 41 change_notified_ = true; |
| 42 } |
| 43 |
| 44 int change_notified() const { return change_notified_; } |
| 45 void Reset() { change_notified_ = false; } |
| 46 |
| 47 private: |
| 48 DeviceDataManager* manager_; |
| 49 bool change_notified_; |
| 50 |
| 51 DISALLOW_COPY_AND_ASSIGN(TestInputDeviceObserver); |
| 52 }; |
| 53 |
| 54 } // namespace |
| 55 |
| 56 class DeviceDataManagerX11Test : public testing::Test { |
| 57 public: |
| 58 DeviceDataManagerX11Test() {} |
| 59 virtual ~DeviceDataManagerX11Test() {} |
| 60 |
| 61 virtual void SetUp() override { |
| 62 DeviceDataManagerX11::CreateInstance(); |
| 63 } |
| 64 |
| 65 virtual void TearDown() override { |
| 66 SetKeyboardDevices(std::vector<KeyboardDevice>()); |
| 67 } |
| 68 |
| 69 virtual void SetKeyboardDevices(const std::vector<KeyboardDevice>& devices) { |
| 70 DeviceHotplugEventObserver* manager = DeviceDataManagerX11::GetInstance(); |
| 71 manager->OnKeyboardDevicesUpdated(devices); |
| 72 } |
| 73 |
| 74 private: |
| 75 DISALLOW_COPY_AND_ASSIGN(DeviceDataManagerX11Test); |
| 76 }; |
| 77 |
| 78 // Tests that the the device data manager notifies observers when a device is |
| 79 // disabled and re-enabled. |
| 80 TEST_F(DeviceDataManagerX11Test, NotifyOnDisable) { |
| 81 DeviceDataManagerX11* manager = DeviceDataManagerX11::GetInstance(); |
| 82 TestInputDeviceObserver observer(manager); |
| 83 std::vector<ui::KeyboardDevice> keyboards; |
| 84 keyboards.push_back( |
| 85 ui::KeyboardDevice(1, ui::InputDeviceType::INTERNAL, "Keyboard")); |
| 86 keyboards.push_back( |
| 87 ui::KeyboardDevice(2, ui::InputDeviceType::INTERNAL, "Keyboard")); |
| 88 SetKeyboardDevices(keyboards); |
| 89 EXPECT_TRUE(observer.change_notified()); |
| 90 std::vector<KeyboardDevice> devices = manager->keyboard_devices(); |
| 91 EXPECT_EQ(keyboards.size(), devices.size()); |
| 92 observer.Reset(); |
| 93 // Disable the device, should be notified that the device list contains one |
| 94 // less device. |
| 95 manager->DisableDevice(2); |
| 96 EXPECT_TRUE(observer.change_notified()); |
| 97 devices = manager->keyboard_devices(); |
| 98 EXPECT_EQ(1u, devices.size()); |
| 99 KeyboardDevice device = devices.front(); |
| 100 EXPECT_EQ(1, device.id); |
| 101 observer.Reset(); |
| 102 // Reenable the device, should be notified that the device list contains one |
| 103 // more device. |
| 104 manager->EnableDevice(2); |
| 105 EXPECT_TRUE(observer.change_notified()); |
| 106 devices = manager->keyboard_devices(); |
| 107 EXPECT_EQ(keyboards.size(), devices.size()); |
| 108 } |
| 109 |
| 110 // Tests blocking multiple devices. |
| 111 TEST_F(DeviceDataManagerX11Test, TestMultipleDisable) { |
| 112 DeviceDataManagerX11* manager = DeviceDataManagerX11::GetInstance(); |
| 113 TestInputDeviceObserver observer(manager); |
| 114 std::vector<ui::KeyboardDevice> keyboards; |
| 115 keyboards.push_back( |
| 116 ui::KeyboardDevice(1, ui::InputDeviceType::INTERNAL, "Keyboard")); |
| 117 keyboards.push_back( |
| 118 ui::KeyboardDevice(2, ui::InputDeviceType::INTERNAL, "Keyboard")); |
| 119 SetKeyboardDevices(keyboards); |
| 120 EXPECT_TRUE(observer.change_notified()); |
| 121 std::vector<KeyboardDevice> devices = manager->keyboard_devices(); |
| 122 EXPECT_EQ(keyboards.size(), devices.size()); |
| 123 observer.Reset(); |
| 124 // Disable the device, should be notified that the device list contains one |
| 125 // less device. |
| 126 manager->DisableDevice(1); |
| 127 EXPECT_TRUE(observer.change_notified()); |
| 128 devices = manager->keyboard_devices(); |
| 129 EXPECT_EQ(1u, devices.size()); |
| 130 observer.Reset(); |
| 131 // Disable the second device, should be notified that the device list empty. |
| 132 manager->DisableDevice(2); |
| 133 EXPECT_TRUE(observer.change_notified()); |
| 134 devices = manager->keyboard_devices(); |
| 135 EXPECT_EQ(0u, devices.size()); |
| 136 observer.Reset(); |
| 137 // Enable the first device, should be notified that one device present. |
| 138 manager->EnableDevice(1); |
| 139 EXPECT_TRUE(observer.change_notified()); |
| 140 devices = manager->keyboard_devices(); |
| 141 EXPECT_EQ(1u, devices.size()); |
| 142 observer.Reset(); |
| 143 // Enable the second device, should be notified that both devices present. |
| 144 manager->EnableDevice(2); |
| 145 EXPECT_TRUE(observer.change_notified()); |
| 146 devices = manager->keyboard_devices(); |
| 147 EXPECT_EQ(2u, devices.size()); |
| 148 } |
| 149 |
| 150 TEST_F(DeviceDataManagerX11Test, UnblockOnDeviceUnplugged) { |
| 151 DeviceDataManagerX11* manager = DeviceDataManagerX11::GetInstance(); |
| 152 TestInputDeviceObserver observer(manager); |
| 153 std::vector<ui::KeyboardDevice> all_keyboards; |
| 154 all_keyboards.push_back( |
| 155 ui::KeyboardDevice(1, ui::InputDeviceType::INTERNAL, "Keyboard")); |
| 156 all_keyboards.push_back( |
| 157 ui::KeyboardDevice(2, ui::InputDeviceType::INTERNAL, "Keyboard")); |
| 158 SetKeyboardDevices(all_keyboards); |
| 159 EXPECT_TRUE(observer.change_notified()); |
| 160 std::vector<KeyboardDevice> devices = manager->keyboard_devices(); |
| 161 EXPECT_EQ(all_keyboards.size(), devices.size()); |
| 162 observer.Reset(); |
| 163 // Expect to be notified that the device is no longer available. |
| 164 manager->DisableDevice(2); |
| 165 EXPECT_TRUE(observer.change_notified()); |
| 166 devices = manager->keyboard_devices(); |
| 167 EXPECT_EQ(1u, devices.size()); |
| 168 observer.Reset(); |
| 169 // Unplug the disabled device. Should not be notified, since the active list |
| 170 // did not change. |
| 171 std::vector<ui::KeyboardDevice> subset_keyboards; |
| 172 subset_keyboards.push_back( |
| 173 ui::KeyboardDevice(1, ui::InputDeviceType::INTERNAL, "Keyboard")); |
| 174 SetKeyboardDevices(subset_keyboards); |
| 175 EXPECT_FALSE(observer.change_notified()); |
| 176 // Replug in the first device. Should be notified of the new device. |
| 177 SetKeyboardDevices(all_keyboards); |
| 178 EXPECT_TRUE(observer.change_notified()); |
| 179 devices = manager->keyboard_devices(); |
| 180 // Both devices now present. |
| 181 EXPECT_EQ(2u, devices.size()); |
| 182 } |
| 183 |
| 184 } // namespace test |
| 185 } // namespace ui |
OLD | NEW |