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.
6 #include "base/compiler_specific.h"
7 #include "base/macros.h"
8 #include "base/run_loop.h"
9 #include "chrome/browser/chromeos/device/input_service_proxy.h"
10 #include "chrome/test/base/in_process_browser_test.h"
11 #include "content/public/browser/browser_thread.h"
12 #include "device/hid/input_service_linux.h"
14 using content::BrowserThread
;
15 using device::InputServiceLinux
;
17 typedef InputServiceLinux::InputDeviceInfo InputDeviceInfo
;
23 const char kKeyboardId
[] = "keyboard";
24 const char kMouseId
[] = "mouse";
26 class InputServiceLinuxTestImpl
: public InputServiceLinux
{
28 InputServiceLinuxTestImpl() {}
29 ~InputServiceLinuxTestImpl() override
{}
31 void AddDeviceForTesting(const InputDeviceInfo
& info
) { AddDevice(info
); }
32 void RemoveDeviceForTesting(const std::string
& id
) { RemoveDevice(id
); }
35 DISALLOW_COPY_AND_ASSIGN(InputServiceLinuxTestImpl
);
38 class TestObserver
: public InputServiceProxy::Observer
{
41 : wait_for_device_addition_(false), wait_for_device_removal_(false) {}
42 ~TestObserver() override
{}
44 void OnInputDeviceAdded(const InputDeviceInfo
& info
) override
{
45 if (!wait_for_device_addition_
)
47 EXPECT_TRUE(Equals(expected_info_
, info
));
51 void OnInputDeviceRemoved(const std::string
& id
) override
{
52 if (!wait_for_device_removal_
)
54 EXPECT_EQ(expected_id_
, id
);
58 void WaitForDeviceAddition(const InputDeviceInfo
& info
) {
60 expected_info_
= info
;
61 wait_for_device_addition_
= true;
62 done_
= run
.QuitClosure();
67 wait_for_device_addition_
= false;
70 void WaitForDeviceRemoval(const std::string
& id
) {
73 wait_for_device_removal_
= true;
74 done_
= run
.QuitClosure();
79 wait_for_device_removal_
= false;
83 static bool Equals(const InputDeviceInfo
& lhs
, const InputDeviceInfo
& rhs
) {
84 return lhs
.id
== rhs
.id
&& lhs
.name
== rhs
.name
&&
85 lhs
.subsystem
== rhs
.subsystem
&&
86 lhs
.type
== rhs
.type
&&
87 lhs
.is_accelerometer
== rhs
.is_accelerometer
&&
88 lhs
.is_joystick
== rhs
.is_joystick
&& lhs
.is_key
== rhs
.is_key
&&
89 lhs
.is_keyboard
== rhs
.is_keyboard
&& lhs
.is_mouse
== rhs
.is_mouse
&&
90 lhs
.is_tablet
== rhs
.is_tablet
&&
91 lhs
.is_touchpad
== rhs
.is_touchpad
&&
92 lhs
.is_touchscreen
== rhs
.is_touchscreen
;
95 InputDeviceInfo expected_info_
;
96 std::string expected_id_
;
98 bool wait_for_device_addition_
;
99 bool wait_for_device_removal_
;
103 DISALLOW_COPY_AND_ASSIGN(TestObserver
);
106 void InitInputService() {
107 InputServiceLinux::SetForTesting(new InputServiceLinuxTestImpl());
110 void AddDevice(const InputDeviceInfo
& device
) {
111 InputServiceLinuxTestImpl
* service
=
112 static_cast<InputServiceLinuxTestImpl
*>(InputServiceLinux::GetInstance());
113 service
->AddDeviceForTesting(device
);
116 void RemoveDevice(const std::string
& id
) {
117 InputServiceLinuxTestImpl
* service
=
118 static_cast<InputServiceLinuxTestImpl
*>(InputServiceLinux::GetInstance());
119 service
->RemoveDeviceForTesting(id
);
122 void OnGetDevices(const base::Closure
& done
,
123 const std::vector
<InputDeviceInfo
>& devices
) {
124 EXPECT_EQ(2, static_cast<int>(devices
.size()));
128 void OnGetKeyboard(const base::Closure
& done
,
130 const InputDeviceInfo
& info
) {
131 EXPECT_TRUE(success
);
132 EXPECT_EQ("keyboard", info
.id
);
133 EXPECT_TRUE(info
.is_keyboard
);
137 void OnGetMouse(const base::Closure
& done
,
139 const InputDeviceInfo
& /* info */) {
140 EXPECT_FALSE(success
);
146 class InputServiceProxyTest
: public InProcessBrowserTest
{
148 InputServiceProxyTest() {}
149 ~InputServiceProxyTest() override
{}
152 DISALLOW_COPY_AND_ASSIGN(InputServiceProxyTest
);
155 IN_PROC_BROWSER_TEST_F(InputServiceProxyTest
, Simple
) {
156 BrowserThread::PostTask(
157 BrowserThread::FILE, FROM_HERE
, base::Bind(&InitInputService
));
158 InputServiceProxy proxy
;
159 TestObserver observer
;
160 proxy
.AddObserver(&observer
);
162 InputDeviceInfo keyboard
;
163 keyboard
.id
= kKeyboardId
;
164 keyboard
.subsystem
= InputServiceLinux::InputDeviceInfo::SUBSYSTEM_INPUT
;
165 keyboard
.type
= InputServiceLinux::InputDeviceInfo::TYPE_USB
;
166 keyboard
.is_keyboard
= true;
167 BrowserThread::PostTask(
168 BrowserThread::FILE, FROM_HERE
, base::Bind(&AddDevice
, keyboard
));
169 observer
.WaitForDeviceAddition(keyboard
);
171 InputDeviceInfo mouse
;
173 mouse
.subsystem
= InputServiceLinux::InputDeviceInfo::SUBSYSTEM_INPUT
;
174 mouse
.type
= InputServiceLinux::InputDeviceInfo::TYPE_BLUETOOTH
;
175 mouse
.is_mouse
= true;
176 BrowserThread::PostTask(
177 BrowserThread::FILE, FROM_HERE
, base::Bind(&AddDevice
, mouse
));
178 observer
.WaitForDeviceAddition(mouse
);
182 proxy
.GetDevices(base::Bind(&OnGetDevices
, run
.QuitClosure()));
186 BrowserThread::PostTask(
187 BrowserThread::FILE, FROM_HERE
, base::Bind(&RemoveDevice
, kMouseId
));
188 observer
.WaitForDeviceRemoval(kMouseId
);
192 proxy
.GetDeviceInfo(kKeyboardId
,
193 base::Bind(&OnGetKeyboard
, run
.QuitClosure()));
199 proxy
.GetDeviceInfo(kMouseId
, base::Bind(&OnGetMouse
, run
.QuitClosure()));
203 proxy
.RemoveObserver(&observer
);
206 } // namespace chromeos