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

Side by Side Diff: services/ui/input_devices/input_device_unittests.cc

Issue 2196563004: Add tests for InputDeviceServer/InputDeviceClient. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 4 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 2016 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 "base/bind.h"
6 #include "base/command_line.h"
7 #include "base/macros.h"
8 #include "base/run_loop.h"
9 #include "base/test/test_mock_time_task_runner.h"
10 #include "services/ui/input_devices/input_device_server.h"
11 #include "services/ui/public/cpp/input_devices/input_device_client.h"
12 #include "services/ui/public/interfaces/input_devices/input_device_server.mojom. h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "ui/events/devices/device_data_manager.h"
15 #include "ui/events/devices/device_hotplug_event_observer.h"
16
17 namespace ui {
18
19 namespace {
20
21 // Helper to AsVector items into a std::vector<T> needed for tests.
22 template <class T>
23 std::vector<T> AsVector(std::initializer_list<T> input) {
24 return std::vector<T>(input);
25 }
26
27 // TestClient that doesn't register itself as the InputDeviceManager.
28 class TestClient : public InputDeviceClient {
sadrul 2016/08/02 15:16:14 TestInputDeviceClient
kylechar 2016/08/02 17:46:57 Done.
29 public:
30 TestClient() : InputDeviceClient(false) {}
31 ~TestClient() override {}
32
33 mojom::InputDeviceObserverMojoPtr GetIntefacePtr() {
34 return InputDeviceClient::GetIntefacePtr();
35 }
36
37 private:
38 DISALLOW_COPY_AND_ASSIGN(TestClient);
39 };
40
41 class InputDeviceTest : public testing::Test {
42 public:
43 InputDeviceTest() {}
44 ~InputDeviceTest() override {}
45
46 void RunUntilIdle() { task_runner_->RunUntilIdle(); }
47
48 void AddClientAsObserver(TestClient* client) {
49 server_.AddObserver(client->GetIntefacePtr());
50 }
51
52 DeviceHotplugEventObserver* GetHotplugObs() {
sadrul 2016/08/02 15:16:14 Observer
kylechar 2016/08/02 17:46:57 Done.
53 return DeviceDataManager::GetInstance();
54 }
55
56 // testing::Test:
57 void SetUp() override {
58 task_runner_ = make_scoped_refptr(new base::TestMockTimeTaskRunner(
59 base::Time::Now(), base::TimeTicks::Now()));
60 message_loop_.SetTaskRunner(task_runner_);
61
62 DeviceDataManager::CreateInstance();
63 server_.RegisterAsObserver();
64 }
65
66 void TearDown() override { DeviceDataManager::DeleteInstance(); }
67
68 private:
69 base::MessageLoop message_loop_;
70 scoped_refptr<base::TestMockTimeTaskRunner> task_runner_;
71 InputDeviceServer server_;
72
73 DISALLOW_COPY_AND_ASSIGN(InputDeviceTest);
74 };
75
76 } // namespace
77
78 TEST_F(InputDeviceTest, DeviceListsComplete) {
79 TestClient client;
80 AddClientAsObserver(&client);
81
82 RunUntilIdle();
83 EXPECT_FALSE(client.AreDeviceListsComplete());
84
85 // Observers should get notification when we mark device lists complete.
86 GetHotplugObs()->OnDeviceListsComplete();
87
88 RunUntilIdle();
89 EXPECT_TRUE(client.AreDeviceListsComplete());
90 }
91
92 TEST_F(InputDeviceTest, DeviceListsCompleteTwoClients) {
93 TestClient client1;
94 AddClientAsObserver(&client1);
95
96 TestClient client2;
97 AddClientAsObserver(&client2);
98
99 RunUntilIdle();
100 EXPECT_FALSE(client1.AreDeviceListsComplete());
101 EXPECT_FALSE(client2.AreDeviceListsComplete());
102
103 GetHotplugObs()->OnDeviceListsComplete();
104
105 RunUntilIdle();
106 EXPECT_TRUE(client1.AreDeviceListsComplete());
107 EXPECT_TRUE(client2.AreDeviceListsComplete());
108 }
109
110 TEST_F(InputDeviceTest, AddDevices) {
111 const TouchscreenDevice touchscreen(100, INPUT_DEVICE_INTERNAL, "Touchscreen",
112 gfx::Size(2600, 1700), 3);
113
114 TestClient client;
115 AddClientAsObserver(&client);
116
117 // Add keyboard and mark device lists complete.
118 GetHotplugObs()->OnTouchscreenDevicesUpdated(AsVector({touchscreen}));
119 GetHotplugObs()->OnDeviceListsComplete();
120
121 RunUntilIdle();
122 EXPECT_TRUE(client.AreDeviceListsComplete());
123 EXPECT_EQ(1u, client.GetTouchscreenDevices().size());
124 EXPECT_EQ(0u, client.GetKeyboardDevices().size());
125 EXPECT_EQ(0u, client.GetMouseDevices().size());
126 EXPECT_EQ(0u, client.GetTouchpadDevices().size());
127 }
128
129 TEST_F(InputDeviceTest, AddDeviceAfterComplete) {
130 const InputDevice keyboard1(100, INPUT_DEVICE_INTERNAL, "Keyboard1");
131 const InputDevice keyboard2(200, INPUT_DEVICE_EXTERNAL, "Keyboard2");
132 const InputDevice mouse(300, INPUT_DEVICE_EXTERNAL, "Mouse");
133
134 TestClient client;
135 AddClientAsObserver(&client);
136
137 // Add mouse and mark device lists complete.
138 GetHotplugObs()->OnKeyboardDevicesUpdated(AsVector({keyboard1}));
139 GetHotplugObs()->OnDeviceListsComplete();
140
141 RunUntilIdle();
142 EXPECT_TRUE(client.AreDeviceListsComplete());
143 EXPECT_EQ(1lu, client.GetKeyboardDevices().size());
144
145 // Add a second keyboard and a mouse.
146 GetHotplugObs()->OnMouseDevicesUpdated(AsVector({mouse}));
147 GetHotplugObs()->OnKeyboardDevicesUpdated(AsVector({keyboard1, keyboard2}));
148
149 RunUntilIdle();
150 EXPECT_EQ(0u, client.GetTouchscreenDevices().size());
151 EXPECT_EQ(2u, client.GetKeyboardDevices().size());
152 EXPECT_EQ(1u, client.GetMouseDevices().size());
153 EXPECT_EQ(0u, client.GetTouchpadDevices().size());
154 }
155
156 TEST_F(InputDeviceTest, AddThenRemoveDevice) {
157 const InputDevice mouse(100, INPUT_DEVICE_INTERNAL, "Mouse");
158
159 TestClient client;
160 AddClientAsObserver(&client);
161
162 // Add mouse and mark device lists complete.
163 GetHotplugObs()->OnMouseDevicesUpdated(AsVector({mouse}));
164 GetHotplugObs()->OnDeviceListsComplete();
165
166 RunUntilIdle();
167 EXPECT_TRUE(client.AreDeviceListsComplete());
168 EXPECT_EQ(1u, client.GetMouseDevices().size());
169
170 // Remove mouse device.
171 GetHotplugObs()->OnMouseDevicesUpdated(AsVector<InputDevice>({}));
172
173 RunUntilIdle();
174 EXPECT_EQ(0u, client.GetMouseDevices().size());
175 }
176
177 TEST_F(InputDeviceTest, CheckClientDeviceFields) {
178 const InputDevice touchpad(100, INPUT_DEVICE_INTERNAL, "Touchpad");
179
180 TestClient client;
181 AddClientAsObserver(&client);
182
183 // Add touchpad and mark device lists complete.
184 GetHotplugObs()->OnTouchpadDevicesUpdated(AsVector({touchpad}));
185 GetHotplugObs()->OnDeviceListsComplete();
186
187 RunUntilIdle();
188 EXPECT_TRUE(client.AreDeviceListsComplete());
189 EXPECT_EQ(1u, client.GetTouchpadDevices().size());
190
191 // Check the touchpad fields match.
192 const InputDevice& output = client.GetTouchpadDevices()[0];
193 EXPECT_EQ(touchpad.id, output.id);
194 EXPECT_EQ(touchpad.type, output.type);
195 EXPECT_EQ(touchpad.name, output.name);
196 }
197
198 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698