Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(252)

Side by Side Diff: ui/events/x/device_data_manager_x11_unittest.cc

Issue 618283003: Adds special support to the device manager for keyboards devices. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add checks for Daisy. Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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({});
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 TEST_F(DeviceDataManagerX11Test, UnblockOnDeviceUnplugged) {
111 DeviceDataManagerX11* manager = DeviceDataManagerX11::GetInstance();
112 TestInputDeviceObserver observer(manager);
113 std::vector<ui::KeyboardDevice> all_keyboards;
114 all_keyboards.push_back(
115 ui::KeyboardDevice(1, ui::InputDeviceType::INTERNAL, "Keyboard"));
116 all_keyboards.push_back(
117 ui::KeyboardDevice(2, ui::InputDeviceType::INTERNAL, "Keyboard"));
118 SetKeyboardDevices(all_keyboards);
119 EXPECT_TRUE(observer.change_notified());
120 std::vector<KeyboardDevice> devices = manager->keyboard_devices();
121 EXPECT_EQ(all_keyboards.size(), devices.size());
122 observer.Reset();
123 // Expect to be notified that the device is no longer available.
124 manager->DisableDevice(2);
125 EXPECT_TRUE(observer.change_notified());
126 devices = manager->keyboard_devices();
127 EXPECT_EQ(1u, devices.size());
128 observer.Reset();
129 // Unplug the disabled device. Should not be notified, since the active list
130 // did not change.
131 std::vector<ui::KeyboardDevice> subset_keyboards;
132 subset_keyboards.push_back(
133 ui::KeyboardDevice(1, ui::InputDeviceType::INTERNAL, "Keyboard"));
134 SetKeyboardDevices(subset_keyboards);
135 EXPECT_FALSE(observer.change_notified());
136 // Replug in the first device. Should be notified of the new device.
137 SetKeyboardDevices(all_keyboards);
138 EXPECT_TRUE(observer.change_notified());
139 devices = manager->keyboard_devices();
140 // Both devices now present.
141 EXPECT_EQ(2u, devices.size());
142 }
143
144 } // namespace test
145 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698