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

Side by Side Diff: extensions/browser/api/hid/hid_apitest.cc

Issue 1312993008: Manage HidService lifetime in DeviceClient implementations. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@remove_usb_service_get_instance
Patch Set: Created 5 years, 3 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
« no previous file with comments | « device/test/test_device_client.cc ('k') | extensions/shell/browser/shell_device_client.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/bind.h" 5 #include "base/bind.h"
6 #include "base/run_loop.h" 6 #include "base/run_loop.h"
7 #include "base/thread_task_runner_handle.h" 7 #include "base/thread_task_runner_handle.h"
8 #include "device/core/device_client.h"
8 #include "device/hid/hid_collection_info.h" 9 #include "device/hid/hid_collection_info.h"
9 #include "device/hid/hid_connection.h" 10 #include "device/hid/hid_connection.h"
10 #include "device/hid/hid_device_info.h" 11 #include "device/hid/hid_device_info.h"
11 #include "device/hid/hid_service.h" 12 #include "device/hid/hid_service.h"
12 #include "device/hid/hid_usage_and_page.h" 13 #include "device/hid/hid_usage_and_page.h"
13 #include "extensions/browser/api/device_permissions_prompt.h" 14 #include "extensions/browser/api/device_permissions_prompt.h"
14 #include "extensions/shell/browser/shell_extensions_api_client.h" 15 #include "extensions/shell/browser/shell_extensions_api_client.h"
15 #include "extensions/shell/test/shell_apitest.h" 16 #include "extensions/shell/test/shell_apitest.h"
16 #include "extensions/test/extension_test_message_listener.h" 17 #include "extensions/test/extension_test_message_listener.h"
17 #include "net/base/io_buffer.h" 18 #include "net/base/io_buffer.h"
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after
161 HidService::AddDevice(new HidDeviceInfo(device_id, vendor_id, product_id, 162 HidService::AddDevice(new HidDeviceInfo(device_id, vendor_id, product_id,
162 "Test Device", "A", kHIDBusTypeUSB, 163 "Test Device", "A", kHIDBusTypeUSB,
163 report_descriptor)); 164 report_descriptor));
164 } 165 }
165 166
166 void RemoveDevice(const HidDeviceId& device_id) { 167 void RemoveDevice(const HidDeviceId& device_id) {
167 HidService::RemoveDevice(device_id); 168 HidService::RemoveDevice(device_id);
168 } 169 }
169 }; 170 };
170 171
172 class TestDeviceClient : public DeviceClient {
173 public:
174 TestDeviceClient() : DeviceClient() {}
175 ~TestDeviceClient() override {}
176
177 MockHidService& mock_service() { return hid_service_; }
178
179 private:
180 HidService* GetHidService() override { return &hid_service_; }
181
182 MockHidService hid_service_;
183 };
184
171 } // namespace device 185 } // namespace device
172 186
173 namespace extensions { 187 namespace extensions {
174 188
175 class TestDevicePermissionsPrompt 189 class TestDevicePermissionsPrompt
176 : public DevicePermissionsPrompt, 190 : public DevicePermissionsPrompt,
177 public DevicePermissionsPrompt::Prompt::Observer { 191 public DevicePermissionsPrompt::Prompt::Observer {
178 public: 192 public:
179 TestDevicePermissionsPrompt(content::WebContents* web_contents) 193 TestDevicePermissionsPrompt(content::WebContents* web_contents)
180 : DevicePermissionsPrompt(web_contents) {} 194 : DevicePermissionsPrompt(web_contents) {}
(...skipping 20 matching lines...) Expand all
201 scoped_ptr<DevicePermissionsPrompt> CreateDevicePermissionsPrompt( 215 scoped_ptr<DevicePermissionsPrompt> CreateDevicePermissionsPrompt(
202 content::WebContents* web_contents) const override { 216 content::WebContents* web_contents) const override {
203 return make_scoped_ptr(new TestDevicePermissionsPrompt(web_contents)); 217 return make_scoped_ptr(new TestDevicePermissionsPrompt(web_contents));
204 } 218 }
205 }; 219 };
206 220
207 class HidApiTest : public ShellApiTest { 221 class HidApiTest : public ShellApiTest {
208 public: 222 public:
209 void SetUpOnMainThread() override { 223 void SetUpOnMainThread() override {
210 ShellApiTest::SetUpOnMainThread(); 224 ShellApiTest::SetUpOnMainThread();
211 hid_service_ = new device::MockHidService(); 225 device_client_.reset(new device::TestDeviceClient());
212 HidService::SetInstanceForTest(hid_service_);
213 } 226 }
214 227
215 protected: 228 protected:
216 device::MockHidService* hid_service_; 229 scoped_ptr<device::TestDeviceClient> device_client_;
217 }; 230 };
218 231
219 IN_PROC_BROWSER_TEST_F(HidApiTest, HidApp) { 232 IN_PROC_BROWSER_TEST_F(HidApiTest, HidApp) {
220 ASSERT_TRUE(RunAppTest("api_test/hid/api")) << message_; 233 ASSERT_TRUE(RunAppTest("api_test/hid/api")) << message_;
221 } 234 }
222 235
223 IN_PROC_BROWSER_TEST_F(HidApiTest, OnDeviceAdded) { 236 IN_PROC_BROWSER_TEST_F(HidApiTest, OnDeviceAdded) {
224 ExtensionTestMessageListener load_listener("loaded", false); 237 ExtensionTestMessageListener load_listener("loaded", false);
225 ExtensionTestMessageListener result_listener("success", false); 238 ExtensionTestMessageListener result_listener("success", false);
226 result_listener.set_failure_message("failure"); 239 result_listener.set_failure_message("failure");
227 240
228 ASSERT_TRUE(LoadApp("api_test/hid/add_event")); 241 ASSERT_TRUE(LoadApp("api_test/hid/add_event"));
229 ASSERT_TRUE(load_listener.WaitUntilSatisfied()); 242 ASSERT_TRUE(load_listener.WaitUntilSatisfied());
230 243
231 // Add a blocked device first so that the test will fail if a notification is 244 // Add a blocked device first so that the test will fail if a notification is
232 // received. 245 // received.
233 hid_service_->AddDevice(kTestDeviceIds[3], 0x18D1, 0x58F1, false); 246 device_client_->mock_service().AddDevice(kTestDeviceIds[3], 0x18D1, 0x58F1,
234 hid_service_->AddDevice(kTestDeviceIds[4], 0x18D1, 0x58F0, false); 247 false);
248 device_client_->mock_service().AddDevice(kTestDeviceIds[4], 0x18D1, 0x58F0,
249 false);
235 ASSERT_TRUE(result_listener.WaitUntilSatisfied()); 250 ASSERT_TRUE(result_listener.WaitUntilSatisfied());
236 EXPECT_EQ("success", result_listener.message()); 251 EXPECT_EQ("success", result_listener.message());
237 } 252 }
238 253
239 IN_PROC_BROWSER_TEST_F(HidApiTest, OnDeviceRemoved) { 254 IN_PROC_BROWSER_TEST_F(HidApiTest, OnDeviceRemoved) {
240 ExtensionTestMessageListener load_listener("loaded", false); 255 ExtensionTestMessageListener load_listener("loaded", false);
241 ExtensionTestMessageListener result_listener("success", false); 256 ExtensionTestMessageListener result_listener("success", false);
242 result_listener.set_failure_message("failure"); 257 result_listener.set_failure_message("failure");
243 258
244 ASSERT_TRUE(LoadApp("api_test/hid/remove_event")); 259 ASSERT_TRUE(LoadApp("api_test/hid/remove_event"));
245 ASSERT_TRUE(load_listener.WaitUntilSatisfied()); 260 ASSERT_TRUE(load_listener.WaitUntilSatisfied());
246 261
247 // Device C was not returned by chrome.hid.getDevices, the app will not get 262 // Device C was not returned by chrome.hid.getDevices, the app will not get
248 // a notification. 263 // a notification.
249 hid_service_->RemoveDevice(kTestDeviceIds[2]); 264 device_client_->mock_service().RemoveDevice(kTestDeviceIds[2]);
250 // Device A was returned, the app will get a notification. 265 // Device A was returned, the app will get a notification.
251 hid_service_->RemoveDevice(kTestDeviceIds[0]); 266 device_client_->mock_service().RemoveDevice(kTestDeviceIds[0]);
252 ASSERT_TRUE(result_listener.WaitUntilSatisfied()); 267 ASSERT_TRUE(result_listener.WaitUntilSatisfied());
253 EXPECT_EQ("success", result_listener.message()); 268 EXPECT_EQ("success", result_listener.message());
254 } 269 }
255 270
256 IN_PROC_BROWSER_TEST_F(HidApiTest, GetUserSelectedDevices) { 271 IN_PROC_BROWSER_TEST_F(HidApiTest, GetUserSelectedDevices) {
257 ExtensionTestMessageListener open_listener("opened_device", false); 272 ExtensionTestMessageListener open_listener("opened_device", false);
258 273
259 TestExtensionsAPIClient test_api_client; 274 TestExtensionsAPIClient test_api_client;
260 ASSERT_TRUE(LoadApp("api_test/hid/get_user_selected_devices")); 275 ASSERT_TRUE(LoadApp("api_test/hid/get_user_selected_devices"));
261 ASSERT_TRUE(open_listener.WaitUntilSatisfied()); 276 ASSERT_TRUE(open_listener.WaitUntilSatisfied());
262 277
263 ExtensionTestMessageListener remove_listener("removed", false); 278 ExtensionTestMessageListener remove_listener("removed", false);
264 hid_service_->RemoveDevice(kTestDeviceIds[0]); 279 device_client_->mock_service().RemoveDevice(kTestDeviceIds[0]);
265 ASSERT_TRUE(remove_listener.WaitUntilSatisfied()); 280 ASSERT_TRUE(remove_listener.WaitUntilSatisfied());
266 281
267 ExtensionTestMessageListener add_listener("added", false); 282 ExtensionTestMessageListener add_listener("added", false);
268 hid_service_->AddDevice(kTestDeviceIds[0], 0x18D1, 0x58F0, true); 283 device_client_->mock_service().AddDevice(kTestDeviceIds[0], 0x18D1, 0x58F0,
284 true);
269 ASSERT_TRUE(add_listener.WaitUntilSatisfied()); 285 ASSERT_TRUE(add_listener.WaitUntilSatisfied());
270 } 286 }
271 287
272 } // namespace extensions 288 } // namespace extensions
OLDNEW
« no previous file with comments | « device/test/test_device_client.cc ('k') | extensions/shell/browser/shell_device_client.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698