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

Side by Side Diff: extensions/browser/api/usb/usb_apitest.cc

Issue 1314273002: Manage UsbService lifetime in DeviceClient implementations. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
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/usb/usb_service_unittest.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 "chrome/browser/extensions/extension_apitest.h" 5 #include "chrome/browser/extensions/extension_apitest.h"
6 #include "content/public/browser/browser_thread.h" 6 #include "content/public/browser/browser_thread.h"
7 #include "content/public/test/test_utils.h" 7 #include "content/public/test/test_utils.h"
8 #include "device/core/device_client.h"
8 #include "device/usb/mock_usb_device.h" 9 #include "device/usb/mock_usb_device.h"
9 #include "device/usb/mock_usb_device_handle.h" 10 #include "device/usb/mock_usb_device_handle.h"
10 #include "device/usb/mock_usb_service.h" 11 #include "device/usb/mock_usb_service.h"
11 #include "extensions/browser/api/device_permissions_prompt.h" 12 #include "extensions/browser/api/device_permissions_prompt.h"
12 #include "extensions/browser/api/usb/usb_api.h" 13 #include "extensions/browser/api/usb/usb_api.h"
13 #include "extensions/shell/browser/shell_extensions_api_client.h" 14 #include "extensions/shell/browser/shell_extensions_api_client.h"
14 #include "extensions/shell/test/shell_apitest.h" 15 #include "extensions/shell/test/shell_apitest.h"
15 #include "extensions/test/extension_test_message_listener.h" 16 #include "extensions/test/extension_test_message_listener.h"
16 #include "net/base/io_buffer.h" 17 #include "net/base/io_buffer.h"
17 18
18 using testing::_; 19 using testing::_;
19 using testing::AnyNumber; 20 using testing::AnyNumber;
20 using testing::Invoke; 21 using testing::Invoke;
21 using testing::Return; 22 using testing::Return;
22 using content::BrowserThread; 23 using content::BrowserThread;
24 using device::DeviceClient;
23 using device::MockUsbDevice; 25 using device::MockUsbDevice;
24 using device::MockUsbDeviceHandle; 26 using device::MockUsbDeviceHandle;
25 using device::MockUsbService; 27 using device::MockUsbService;
26 using device::UsbConfigDescriptor; 28 using device::UsbConfigDescriptor;
27 using device::UsbDevice; 29 using device::UsbDevice;
28 using device::UsbDeviceHandle; 30 using device::UsbDeviceHandle;
29 using device::UsbEndpointDirection; 31 using device::UsbEndpointDirection;
30 using device::UsbInterfaceDescriptor; 32 using device::UsbInterfaceDescriptor;
31 using device::UsbService; 33 using device::UsbService;
32 34
(...skipping 28 matching lines...) Expand all
61 for (size_t i = 0; i < prompt()->GetDeviceCount(); ++i) { 63 for (size_t i = 0; i < prompt()->GetDeviceCount(); ++i) {
62 prompt()->GrantDevicePermission(i); 64 prompt()->GrantDevicePermission(i);
63 if (!prompt()->multiple()) { 65 if (!prompt()->multiple()) {
64 break; 66 break;
65 } 67 }
66 } 68 }
67 prompt()->Dismissed(); 69 prompt()->Dismissed();
68 } 70 }
69 }; 71 };
70 72
73 class TestDeviceClient : public DeviceClient {
74 public:
75 TestDeviceClient() : DeviceClient() {}
76 ~TestDeviceClient() override {}
77
78 MockUsbService& mock_usb_service() { return usb_service_; }
79
80 private:
81 UsbService* GetUsbService() override { return &usb_service_; }
82
83 MockUsbService usb_service_;
84 };
85
71 class TestExtensionsAPIClient : public ShellExtensionsAPIClient { 86 class TestExtensionsAPIClient : public ShellExtensionsAPIClient {
72 public: 87 public:
73 TestExtensionsAPIClient() : ShellExtensionsAPIClient() {} 88 TestExtensionsAPIClient() : ShellExtensionsAPIClient() {}
74 89
75 scoped_ptr<DevicePermissionsPrompt> CreateDevicePermissionsPrompt( 90 scoped_ptr<DevicePermissionsPrompt> CreateDevicePermissionsPrompt(
76 content::WebContents* web_contents) const override { 91 content::WebContents* web_contents) const override {
77 return make_scoped_ptr(new TestDevicePermissionsPrompt(web_contents)); 92 return make_scoped_ptr(new TestDevicePermissionsPrompt(web_contents));
78 } 93 }
79 }; 94 };
80 95
81 class UsbApiTest : public ShellApiTest { 96 class UsbApiTest : public ShellApiTest {
82 public: 97 public:
83 void SetUpOnMainThread() override { 98 void SetUpOnMainThread() override {
84 ShellApiTest::SetUpOnMainThread(); 99 ShellApiTest::SetUpOnMainThread();
85 100
86 mock_device_ = 101 mock_device_ =
87 new MockUsbDevice(0, 0, "Test Manufacturer", "Test Device", "ABC123"); 102 new MockUsbDevice(0, 0, "Test Manufacturer", "Test Device", "ABC123");
88 mock_device_handle_ = new MockUsbDeviceHandle(mock_device_.get()); 103 mock_device_handle_ = new MockUsbDeviceHandle(mock_device_.get());
89 EXPECT_CALL(*mock_device_.get(), Open(_)) 104 EXPECT_CALL(*mock_device_.get(), Open(_))
90 .WillRepeatedly(InvokeCallback<0>(mock_device_handle_)); 105 .WillRepeatedly(InvokeCallback<0>(mock_device_handle_));
91 mock_service_.reset(new MockUsbService()); 106 device_client_.reset(new TestDeviceClient());
92 mock_service_->AddDevice(mock_device_); 107 device_client_->mock_usb_service().AddDevice(mock_device_);
93 } 108 }
94 109
95 protected: 110 protected:
96 scoped_refptr<MockUsbDeviceHandle> mock_device_handle_; 111 scoped_refptr<MockUsbDeviceHandle> mock_device_handle_;
97 scoped_refptr<MockUsbDevice> mock_device_; 112 scoped_refptr<MockUsbDevice> mock_device_;
98 scoped_ptr<MockUsbService> mock_service_; 113 scoped_ptr<TestDeviceClient> device_client_;
99 }; 114 };
100 115
101 } // namespace 116 } // namespace
102 117
103 IN_PROC_BROWSER_TEST_F(UsbApiTest, DeviceHandling) { 118 IN_PROC_BROWSER_TEST_F(UsbApiTest, DeviceHandling) {
104 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(2); 119 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(2);
105 ASSERT_TRUE(RunAppTest("api_test/usb/device_handling")); 120 ASSERT_TRUE(RunAppTest("api_test/usb/device_handling"));
106 } 121 }
107 122
108 IN_PROC_BROWSER_TEST_F(UsbApiTest, ResetDevice) { 123 IN_PROC_BROWSER_TEST_F(UsbApiTest, ResetDevice) {
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
190 205
191 IN_PROC_BROWSER_TEST_F(UsbApiTest, OnDeviceAdded) { 206 IN_PROC_BROWSER_TEST_F(UsbApiTest, OnDeviceAdded) {
192 ExtensionTestMessageListener load_listener("loaded", false); 207 ExtensionTestMessageListener load_listener("loaded", false);
193 ExtensionTestMessageListener result_listener("success", false); 208 ExtensionTestMessageListener result_listener("success", false);
194 result_listener.set_failure_message("failure"); 209 result_listener.set_failure_message("failure");
195 210
196 ASSERT_TRUE(LoadApp("api_test/usb/add_event")); 211 ASSERT_TRUE(LoadApp("api_test/usb/add_event"));
197 ASSERT_TRUE(load_listener.WaitUntilSatisfied()); 212 ASSERT_TRUE(load_listener.WaitUntilSatisfied());
198 213
199 scoped_refptr<MockUsbDevice> device(new MockUsbDevice(0x18D1, 0x58F0)); 214 scoped_refptr<MockUsbDevice> device(new MockUsbDevice(0x18D1, 0x58F0));
200 mock_service_->AddDevice(device); 215 device_client_->mock_usb_service().AddDevice(device);
201 216
202 device = new MockUsbDevice(0x18D1, 0x58F1); 217 device = new MockUsbDevice(0x18D1, 0x58F1);
203 mock_service_->AddDevice(device); 218 device_client_->mock_usb_service().AddDevice(device);
204 219
205 ASSERT_TRUE(result_listener.WaitUntilSatisfied()); 220 ASSERT_TRUE(result_listener.WaitUntilSatisfied());
206 } 221 }
207 222
208 IN_PROC_BROWSER_TEST_F(UsbApiTest, OnDeviceRemoved) { 223 IN_PROC_BROWSER_TEST_F(UsbApiTest, OnDeviceRemoved) {
209 ExtensionTestMessageListener load_listener("loaded", false); 224 ExtensionTestMessageListener load_listener("loaded", false);
210 ExtensionTestMessageListener result_listener("success", false); 225 ExtensionTestMessageListener result_listener("success", false);
211 result_listener.set_failure_message("failure"); 226 result_listener.set_failure_message("failure");
212 227
213 ASSERT_TRUE(LoadApp("api_test/usb/remove_event")); 228 ASSERT_TRUE(LoadApp("api_test/usb/remove_event"));
214 ASSERT_TRUE(load_listener.WaitUntilSatisfied()); 229 ASSERT_TRUE(load_listener.WaitUntilSatisfied());
215 230
216 mock_service_->RemoveDevice(mock_device_); 231 device_client_->mock_usb_service().RemoveDevice(mock_device_);
217 ASSERT_TRUE(result_listener.WaitUntilSatisfied()); 232 ASSERT_TRUE(result_listener.WaitUntilSatisfied());
218 } 233 }
219 234
220 IN_PROC_BROWSER_TEST_F(UsbApiTest, GetUserSelectedDevices) { 235 IN_PROC_BROWSER_TEST_F(UsbApiTest, GetUserSelectedDevices) {
221 ExtensionTestMessageListener ready_listener("opened_device", false); 236 ExtensionTestMessageListener ready_listener("opened_device", false);
222 ExtensionTestMessageListener result_listener("success", false); 237 ExtensionTestMessageListener result_listener("success", false);
223 result_listener.set_failure_message("failure"); 238 result_listener.set_failure_message("failure");
224 239
225 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(1); 240 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(1);
226 241
227 TestExtensionsAPIClient test_api_client; 242 TestExtensionsAPIClient test_api_client;
228 ASSERT_TRUE(LoadApp("api_test/usb/get_user_selected_devices")); 243 ASSERT_TRUE(LoadApp("api_test/usb/get_user_selected_devices"));
229 ASSERT_TRUE(ready_listener.WaitUntilSatisfied()); 244 ASSERT_TRUE(ready_listener.WaitUntilSatisfied());
230 245
231 mock_service_->RemoveDevice(mock_device_); 246 device_client_->mock_usb_service().RemoveDevice(mock_device_);
232 ASSERT_TRUE(result_listener.WaitUntilSatisfied()); 247 ASSERT_TRUE(result_listener.WaitUntilSatisfied());
233 } 248 }
234 249
235 } // namespace extensions 250 } // namespace extensions
OLDNEW
« no previous file with comments | « device/usb/usb_service_unittest.cc ('k') | extensions/shell/browser/shell_device_client.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698