Add ICU message format support
[chromium-blink-merge.git] / chrome / browser / chromeos / device / input_service_proxy_browsertest.cc
blob4babbff94f68aff093f2155274cbd0d78ae5cba4
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.
5 #include "base/bind.h"
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;
19 namespace chromeos {
21 namespace {
23 const char kKeyboardId[] = "keyboard";
24 const char kMouseId[] = "mouse";
26 class InputServiceLinuxTestImpl : public InputServiceLinux {
27 public:
28 InputServiceLinuxTestImpl() {}
29 ~InputServiceLinuxTestImpl() override {}
31 void AddDeviceForTesting(const InputDeviceInfo& info) { AddDevice(info); }
32 void RemoveDeviceForTesting(const std::string& id) { RemoveDevice(id); }
34 private:
35 DISALLOW_COPY_AND_ASSIGN(InputServiceLinuxTestImpl);
38 class TestObserver : public InputServiceProxy::Observer {
39 public:
40 TestObserver()
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_)
46 return;
47 EXPECT_TRUE(Equals(expected_info_, info));
48 done_.Run();
51 void OnInputDeviceRemoved(const std::string& id) override {
52 if (!wait_for_device_removal_)
53 return;
54 EXPECT_EQ(expected_id_, id);
55 done_.Run();
58 void WaitForDeviceAddition(const InputDeviceInfo& info) {
59 base::RunLoop run;
60 expected_info_ = info;
61 wait_for_device_addition_ = true;
62 done_ = run.QuitClosure();
64 run.Run();
66 done_.Reset();
67 wait_for_device_addition_ = false;
70 void WaitForDeviceRemoval(const std::string& id) {
71 base::RunLoop run;
72 expected_id_ = id;
73 wait_for_device_removal_ = true;
74 done_ = run.QuitClosure();
76 run.Run();
78 done_.Reset();
79 wait_for_device_removal_ = false;
82 private:
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_;
101 base::Closure done_;
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()));
125 done.Run();
128 void OnGetKeyboard(const base::Closure& done,
129 bool success,
130 const InputDeviceInfo& info) {
131 EXPECT_TRUE(success);
132 EXPECT_EQ("keyboard", info.id);
133 EXPECT_TRUE(info.is_keyboard);
134 done.Run();
137 void OnGetMouse(const base::Closure& done,
138 bool success,
139 const InputDeviceInfo& /* info */) {
140 EXPECT_FALSE(success);
141 done.Run();
144 } // namespace
146 class InputServiceProxyTest : public InProcessBrowserTest {
147 public:
148 InputServiceProxyTest() {}
149 ~InputServiceProxyTest() override {}
151 private:
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;
172 mouse.id = kMouseId;
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);
181 base::RunLoop run;
182 proxy.GetDevices(base::Bind(&OnGetDevices, run.QuitClosure()));
183 run.Run();
186 BrowserThread::PostTask(
187 BrowserThread::FILE, FROM_HERE, base::Bind(&RemoveDevice, kMouseId));
188 observer.WaitForDeviceRemoval(kMouseId);
191 base::RunLoop run;
192 proxy.GetDeviceInfo(kKeyboardId,
193 base::Bind(&OnGetKeyboard, run.QuitClosure()));
194 run.Run();
198 base::RunLoop run;
199 proxy.GetDeviceInfo(kMouseId, base::Bind(&OnGetMouse, run.QuitClosure()));
200 run.Run();
203 proxy.RemoveObserver(&observer);
206 } // namespace chromeos