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

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

Issue 980023002: Move device/usb classes from the FILE thread to UI thread. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed first round of rocket@ feedback. Created 5 years, 8 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
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/usb/usb_service.h" 8 #include "device/usb/usb_service.h"
9 #include "extensions/browser/api/device_permissions_prompt.h" 9 #include "extensions/browser/api/device_permissions_prompt.h"
10 #include "extensions/browser/api/usb/usb_api.h" 10 #include "extensions/browser/api/usb/usb_api.h"
11 #include "extensions/shell/browser/shell_extensions_api_client.h" 11 #include "extensions/shell/browser/shell_extensions_api_client.h"
12 #include "extensions/shell/test/shell_apitest.h" 12 #include "extensions/shell/test/shell_apitest.h"
13 #include "extensions/test/extension_test_message_listener.h" 13 #include "extensions/test/extension_test_message_listener.h"
14 #include "net/base/io_buffer.h" 14 #include "net/base/io_buffer.h"
15 #include "testing/gmock/include/gmock/gmock.h" 15 #include "testing/gmock/include/gmock/gmock.h"
16 16
17 using testing::_; 17 using testing::_;
18 using testing::AnyNumber; 18 using testing::AnyNumber;
19 using testing::Invoke; 19 using testing::Invoke;
20 using testing::Return; 20 using testing::Return;
21 using content::BrowserThread; 21 using content::BrowserThread;
22 using device::UsbConfigDescriptor; 22 using device::UsbConfigDescriptor;
23 using device::UsbDevice; 23 using device::UsbDevice;
24 using device::UsbDeviceHandle; 24 using device::UsbDeviceHandle;
25 using device::UsbEndpointDirection; 25 using device::UsbEndpointDirection;
26 using device::UsbInterfaceDescriptor; 26 using device::UsbInterfaceDescriptor;
27 using device::UsbService; 27 using device::UsbService;
28 using device::UsbTransferCallback;
29 28
30 namespace extensions { 29 namespace extensions {
31 30
32 namespace { 31 namespace {
33 32
33 ACTION_TEMPLATE(InvokeCallback,
34 HAS_1_TEMPLATE_PARAMS(int, k),
35 AND_1_VALUE_PARAMS(p1)) {
36 ::std::tr1::get<k>(args).Run(p1);
37 }
38
34 ACTION_TEMPLATE(InvokeUsbTransferCallback, 39 ACTION_TEMPLATE(InvokeUsbTransferCallback,
35 HAS_1_TEMPLATE_PARAMS(int, k), 40 HAS_1_TEMPLATE_PARAMS(int, k),
36 AND_1_VALUE_PARAMS(p1)) { 41 AND_1_VALUE_PARAMS(p1)) {
37 net::IOBuffer* io_buffer = new net::IOBuffer(1); 42 net::IOBuffer* io_buffer = new net::IOBuffer(1);
38 memset(io_buffer->data(), 0, 1); // Avoid uninitialized reads. 43 memset(io_buffer->data(), 0, 1); // Avoid uninitialized reads.
39 ::std::tr1::get<k>(args).Run(p1, io_buffer, 1); 44 ::std::tr1::get<k>(args).Run(p1, io_buffer, 1);
40 } 45 }
41 46
42 class TestDevicePermissionsPrompt 47 class TestDevicePermissionsPrompt
43 : public DevicePermissionsPrompt, 48 : public DevicePermissionsPrompt,
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
80 MOCK_METHOD10(ControlTransfer, 85 MOCK_METHOD10(ControlTransfer,
81 void(UsbEndpointDirection direction, 86 void(UsbEndpointDirection direction,
82 TransferRequestType request_type, 87 TransferRequestType request_type,
83 TransferRecipient recipient, 88 TransferRecipient recipient,
84 uint8 request, 89 uint8 request,
85 uint16 value, 90 uint16 value,
86 uint16 index, 91 uint16 index,
87 net::IOBuffer* buffer, 92 net::IOBuffer* buffer,
88 size_t length, 93 size_t length,
89 unsigned int timeout, 94 unsigned int timeout,
90 const UsbTransferCallback& callback)); 95 const TransferCallback& callback));
91 96
92 MOCK_METHOD6(BulkTransfer, 97 MOCK_METHOD6(BulkTransfer,
93 void(UsbEndpointDirection direction, 98 void(UsbEndpointDirection direction,
94 uint8 endpoint, 99 uint8 endpoint,
95 net::IOBuffer* buffer, 100 net::IOBuffer* buffer,
96 size_t length, 101 size_t length,
97 unsigned int timeout, 102 unsigned int timeout,
98 const UsbTransferCallback& callback)); 103 const TransferCallback& callback));
99 104
100 MOCK_METHOD6(InterruptTransfer, 105 MOCK_METHOD6(InterruptTransfer,
101 void(UsbEndpointDirection direction, 106 void(UsbEndpointDirection direction,
102 uint8 endpoint, 107 uint8 endpoint,
103 net::IOBuffer* buffer, 108 net::IOBuffer* buffer,
104 size_t length, 109 size_t length,
105 unsigned int timeout, 110 unsigned int timeout,
106 const UsbTransferCallback& callback)); 111 const TransferCallback& callback));
107 112
108 MOCK_METHOD8(IsochronousTransfer, 113 MOCK_METHOD8(IsochronousTransfer,
109 void(UsbEndpointDirection direction, 114 void(UsbEndpointDirection direction,
110 uint8 endpoint, 115 uint8 endpoint,
111 net::IOBuffer* buffer, 116 net::IOBuffer* buffer,
112 size_t length, 117 size_t length,
113 unsigned int packets, 118 unsigned int packets,
114 unsigned int packet_length, 119 unsigned int packet_length,
115 unsigned int timeout, 120 unsigned int timeout,
116 const UsbTransferCallback& callback)); 121 const TransferCallback& callback));
117 122
118 MOCK_METHOD0(ResetDevice, bool()); 123 MOCK_METHOD1(ResetDevice, void(const ResultCallback& callback));
119 MOCK_METHOD2(GetStringDescriptor, bool(uint8_t, base::string16*)); 124 MOCK_METHOD2(GetStringDescriptor, bool(uint8_t, base::string16*));
120 MOCK_METHOD1(SetConfiguration, bool(int)); 125 MOCK_METHOD2(SetConfiguration,
121 MOCK_METHOD1(ClaimInterface, bool(int interface_number)); 126 void(int configuration_value, const ResultCallback& callback));
127 MOCK_METHOD2(ClaimInterface,
128 void(int interface_number, const ResultCallback& callback));
122 MOCK_METHOD1(ReleaseInterface, bool(int interface_number)); 129 MOCK_METHOD1(ReleaseInterface, bool(int interface_number));
123 MOCK_METHOD2(SetInterfaceAlternateSetting, 130 MOCK_METHOD3(SetInterfaceAlternateSetting,
124 bool(int interface_number, int alternate_setting)); 131 void(int interface_number,
132 int alternate_setting,
133 const ResultCallback& callback));
125 134
126 virtual scoped_refptr<UsbDevice> GetDevice() const override { 135 virtual scoped_refptr<UsbDevice> GetDevice() const override {
127 return device_; 136 return device_;
128 } 137 }
129 138
130 void set_device(UsbDevice* device) { device_ = device; } 139 void set_device(UsbDevice* device) { device_ = device; }
131 140
132 protected: 141 protected:
133 UsbDevice* device_; 142 UsbDevice* device_;
134 143
135 virtual ~MockUsbDeviceHandle() {} 144 virtual ~MockUsbDeviceHandle() {}
136 }; 145 };
137 146
138 class MockUsbDevice : public UsbDevice { 147 class MockUsbDevice : public UsbDevice {
139 public: 148 public:
140 MockUsbDevice(uint16 vendor_id, uint16 product_id, uint32 unique_id) 149 MockUsbDevice(uint16 vendor_id, uint16 product_id, uint32 unique_id)
141 : UsbDevice(vendor_id, product_id, unique_id) {} 150 : UsbDevice(vendor_id,
151 product_id,
152 unique_id,
153 base::string16(),
154 base::string16(),
155 base::string16()) {}
142 156
143 MOCK_METHOD0(Open, scoped_refptr<UsbDeviceHandle>()); 157 MOCK_METHOD1(Open, void(const OpenCallback&));
144 MOCK_METHOD1(Close, bool(scoped_refptr<UsbDeviceHandle>)); 158 MOCK_METHOD1(Close, bool(scoped_refptr<UsbDeviceHandle>));
145 MOCK_METHOD0(GetConfiguration, const device::UsbConfigDescriptor*()); 159 MOCK_METHOD0(GetConfiguration, const device::UsbConfigDescriptor*());
146 MOCK_METHOD1(GetManufacturer, bool(base::string16*));
147 MOCK_METHOD1(GetProduct, bool(base::string16*));
148 MOCK_METHOD1(GetSerialNumber, bool(base::string16*));
149 160
150 private: 161 private:
151 virtual ~MockUsbDevice() {} 162 virtual ~MockUsbDevice() {}
152 }; 163 };
153 164
154 class MockUsbService : public UsbService { 165 class MockUsbService : public UsbService {
155 public: 166 public:
156 explicit MockUsbService(scoped_refptr<UsbDevice> device) : device_(device) {} 167 explicit MockUsbService(scoped_refptr<UsbDevice> device) : device_(device) {}
157 168
158 // Public wrapper around the protected base class method. 169 // Public wrapper around the protected base class method.
159 void NotifyDeviceAdded(scoped_refptr<UsbDevice> device) { 170 void NotifyDeviceAdded(scoped_refptr<UsbDevice> device) {
160 UsbService::NotifyDeviceAdded(device); 171 UsbService::NotifyDeviceAdded(device);
161 } 172 }
162 173
163 // Public wrapper around the protected base class method. 174 // Public wrapper around the protected base class method.
164 void NotifyDeviceRemoved(scoped_refptr<UsbDevice> device) { 175 void NotifyDeviceRemoved(scoped_refptr<UsbDevice> device) {
165 UsbService::NotifyDeviceRemoved(device); 176 UsbService::NotifyDeviceRemoved(device);
166 } 177 }
167 178
168 protected: 179 protected:
169 scoped_refptr<UsbDevice> GetDeviceById(uint32 unique_id) override { 180 scoped_refptr<UsbDevice> GetDeviceById(uint32 unique_id) override {
170 EXPECT_EQ(unique_id, 0U); 181 EXPECT_EQ(unique_id, 0U);
171 return device_; 182 return device_;
172 } 183 }
173 184
174 void GetDevices(std::vector<scoped_refptr<UsbDevice>>* devices) override { 185 void GetDevices(const GetDevicesCallback& callback) override {
175 STLClearObject(devices); 186 std::vector<scoped_refptr<UsbDevice>> devices;
176 devices->push_back(device_); 187 devices.push_back(device_);
188 callback.Run(devices);
177 } 189 }
178 190
179 scoped_refptr<UsbDevice> device_; 191 scoped_refptr<UsbDevice> device_;
180 }; 192 };
181 193
182 class UsbApiTest : public ShellApiTest { 194 class UsbApiTest : public ShellApiTest {
183 public: 195 public:
184 void SetUpOnMainThread() override { 196 void SetUpOnMainThread() override {
185 ShellApiTest::SetUpOnMainThread(); 197 ShellApiTest::SetUpOnMainThread();
186 198
187 mock_device_ = new MockUsbDevice(0, 0, 0); 199 mock_device_ = new MockUsbDevice(0, 0, 0);
188 EXPECT_CALL(*mock_device_.get(), GetManufacturer(_))
189 .WillRepeatedly(Return(false));
190 EXPECT_CALL(*mock_device_.get(), GetProduct(_))
191 .WillRepeatedly(Return(false));
192 EXPECT_CALL(*mock_device_.get(), GetSerialNumber(_))
193 .WillRepeatedly(Return(false));
194
195 mock_device_handle_ = new MockUsbDeviceHandle(); 200 mock_device_handle_ = new MockUsbDeviceHandle();
196 mock_device_handle_->set_device(mock_device_.get()); 201 mock_device_handle_->set_device(mock_device_.get());
197 EXPECT_CALL(*mock_device_.get(), Open()) 202 EXPECT_CALL(*mock_device_.get(), Open(_))
198 .WillRepeatedly(Return(mock_device_handle_)); 203 .WillRepeatedly(InvokeCallback<0>(mock_device_handle_));
199 204 mock_service_.reset(new MockUsbService(mock_device_));
200 base::RunLoop run_loop;
201 BrowserThread::PostTaskAndReply(BrowserThread::FILE, FROM_HERE,
202 base::Bind(&UsbApiTest::SetUpService, this),
203 run_loop.QuitClosure());
204 run_loop.Run();
205 }
206
207 void SetUpService() {
208 mock_service_ = new MockUsbService(mock_device_);
209 UsbService::SetInstanceForTest(mock_service_);
210 }
211
212 void AddTestDevices() {
213 scoped_refptr<MockUsbDevice> device(new MockUsbDevice(0x18D1, 0x58F0, 1));
214 EXPECT_CALL(*device.get(), GetSerialNumber(_))
215 .WillRepeatedly(Return(false));
216 mock_service_->NotifyDeviceAdded(device);
217
218 device = new MockUsbDevice(0x18D1, 0x58F1, 2);
219 EXPECT_CALL(*device.get(), GetSerialNumber(_))
220 .WillRepeatedly(Return(false));
221 mock_service_->NotifyDeviceAdded(device);
222 } 205 }
223 206
224 protected: 207 protected:
225 scoped_refptr<MockUsbDeviceHandle> mock_device_handle_; 208 scoped_refptr<MockUsbDeviceHandle> mock_device_handle_;
226 scoped_refptr<MockUsbDevice> mock_device_; 209 scoped_refptr<MockUsbDevice> mock_device_;
227 MockUsbService* mock_service_; 210 scoped_ptr<MockUsbService> mock_service_;
228 }; 211 };
229 212
230 } // namespace 213 } // namespace
231 214
232 IN_PROC_BROWSER_TEST_F(UsbApiTest, DeviceHandling) { 215 IN_PROC_BROWSER_TEST_F(UsbApiTest, DeviceHandling) {
233 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(2); 216 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(2);
234 ASSERT_TRUE(RunAppTest("api_test/usb/device_handling")); 217 ASSERT_TRUE(RunAppTest("api_test/usb/device_handling"));
235 } 218 }
236 219
237 IN_PROC_BROWSER_TEST_F(UsbApiTest, ResetDevice) { 220 IN_PROC_BROWSER_TEST_F(UsbApiTest, ResetDevice) {
238 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(2); 221 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(2);
239 EXPECT_CALL(*mock_device_handle_.get(), ResetDevice()) 222 EXPECT_CALL(*mock_device_handle_.get(), ResetDevice(_))
240 .WillOnce(Return(true)) 223 .WillOnce(InvokeCallback<0>(true))
241 .WillOnce(Return(false)); 224 .WillOnce(InvokeCallback<0>(false));
242 EXPECT_CALL(*mock_device_handle_.get(), 225 EXPECT_CALL(*mock_device_handle_.get(),
243 InterruptTransfer(device::USB_DIRECTION_OUTBOUND, 2, _, 1, _, _)) 226 InterruptTransfer(device::USB_DIRECTION_OUTBOUND, 2, _, 1, _, _))
244 .WillOnce(InvokeUsbTransferCallback<5>(device::USB_TRANSFER_COMPLETED)); 227 .WillOnce(InvokeUsbTransferCallback<5>(device::USB_TRANSFER_COMPLETED));
245 ASSERT_TRUE(RunAppTest("api_test/usb/reset_device")); 228 ASSERT_TRUE(RunAppTest("api_test/usb/reset_device"));
246 } 229 }
247 230
248 IN_PROC_BROWSER_TEST_F(UsbApiTest, SetConfiguration) { 231 IN_PROC_BROWSER_TEST_F(UsbApiTest, SetConfiguration) {
249 UsbConfigDescriptor config_descriptor; 232 UsbConfigDescriptor config_descriptor;
250 EXPECT_CALL(*mock_device_handle_.get(), SetConfiguration(1)) 233 EXPECT_CALL(*mock_device_handle_.get(), SetConfiguration(1, _))
251 .WillOnce(Return(true)); 234 .WillOnce(InvokeCallback<1>(true));
252 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(1); 235 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(1);
253 EXPECT_CALL(*mock_device_.get(), GetConfiguration()) 236 EXPECT_CALL(*mock_device_.get(), GetConfiguration())
254 .WillOnce(Return(nullptr)) 237 .WillOnce(Return(nullptr))
255 .WillOnce(Return(&config_descriptor)); 238 .WillOnce(Return(&config_descriptor));
256 ASSERT_TRUE(RunAppTest("api_test/usb/set_configuration")); 239 ASSERT_TRUE(RunAppTest("api_test/usb/set_configuration"));
257 } 240 }
258 241
259 IN_PROC_BROWSER_TEST_F(UsbApiTest, ListInterfaces) { 242 IN_PROC_BROWSER_TEST_F(UsbApiTest, ListInterfaces) {
260 UsbConfigDescriptor config_descriptor; 243 UsbConfigDescriptor config_descriptor;
261 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(1); 244 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(1);
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
318 } 301 }
319 302
320 IN_PROC_BROWSER_TEST_F(UsbApiTest, OnDeviceAdded) { 303 IN_PROC_BROWSER_TEST_F(UsbApiTest, OnDeviceAdded) {
321 ExtensionTestMessageListener load_listener("loaded", false); 304 ExtensionTestMessageListener load_listener("loaded", false);
322 ExtensionTestMessageListener result_listener("success", false); 305 ExtensionTestMessageListener result_listener("success", false);
323 result_listener.set_failure_message("failure"); 306 result_listener.set_failure_message("failure");
324 307
325 ASSERT_TRUE(LoadApp("api_test/usb/add_event")); 308 ASSERT_TRUE(LoadApp("api_test/usb/add_event"));
326 ASSERT_TRUE(load_listener.WaitUntilSatisfied()); 309 ASSERT_TRUE(load_listener.WaitUntilSatisfied());
327 310
328 base::RunLoop run_loop; 311 scoped_refptr<MockUsbDevice> device(new MockUsbDevice(0x18D1, 0x58F0, 1));
329 BrowserThread::PostTaskAndReply( 312 mock_service_->NotifyDeviceAdded(device);
330 BrowserThread::FILE, FROM_HERE, 313
331 base::Bind(&UsbApiTest::AddTestDevices, base::Unretained(this)), 314 device = new MockUsbDevice(0x18D1, 0x58F1, 2);
332 run_loop.QuitClosure()); 315 mock_service_->NotifyDeviceAdded(device);
333 run_loop.Run();
334 316
335 ASSERT_TRUE(result_listener.WaitUntilSatisfied()); 317 ASSERT_TRUE(result_listener.WaitUntilSatisfied());
336 } 318 }
337 319
338 IN_PROC_BROWSER_TEST_F(UsbApiTest, OnDeviceRemoved) { 320 IN_PROC_BROWSER_TEST_F(UsbApiTest, OnDeviceRemoved) {
339 ExtensionTestMessageListener load_listener("loaded", false); 321 ExtensionTestMessageListener load_listener("loaded", false);
340 ExtensionTestMessageListener result_listener("success", false); 322 ExtensionTestMessageListener result_listener("success", false);
341 result_listener.set_failure_message("failure"); 323 result_listener.set_failure_message("failure");
342 324
343 ASSERT_TRUE(LoadApp("api_test/usb/remove_event")); 325 ASSERT_TRUE(LoadApp("api_test/usb/remove_event"));
344 ASSERT_TRUE(load_listener.WaitUntilSatisfied()); 326 ASSERT_TRUE(load_listener.WaitUntilSatisfied());
345 327
346 base::RunLoop run_loop; 328 mock_service_->NotifyDeviceRemoved(mock_device_);
347 BrowserThread::PostTaskAndReply(
348 BrowserThread::FILE, FROM_HERE,
349 base::Bind(&MockUsbService::NotifyDeviceRemoved,
350 base::Unretained(mock_service_), mock_device_),
351 run_loop.QuitClosure());
352 run_loop.Run();
353
354 ASSERT_TRUE(result_listener.WaitUntilSatisfied()); 329 ASSERT_TRUE(result_listener.WaitUntilSatisfied());
355 } 330 }
356 331
357 IN_PROC_BROWSER_TEST_F(UsbApiTest, GetUserSelectedDevices) { 332 IN_PROC_BROWSER_TEST_F(UsbApiTest, GetUserSelectedDevices) {
358 ExtensionTestMessageListener ready_listener("opened_device", false); 333 ExtensionTestMessageListener ready_listener("opened_device", false);
359 ExtensionTestMessageListener result_listener("success", false); 334 ExtensionTestMessageListener result_listener("success", false);
360 result_listener.set_failure_message("failure"); 335 result_listener.set_failure_message("failure");
361 336
362 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(1); 337 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(1);
363 338
364 TestExtensionsAPIClient test_api_client; 339 TestExtensionsAPIClient test_api_client;
365 ASSERT_TRUE(LoadApp("api_test/usb/get_user_selected_devices")); 340 ASSERT_TRUE(LoadApp("api_test/usb/get_user_selected_devices"));
366 ASSERT_TRUE(ready_listener.WaitUntilSatisfied()); 341 ASSERT_TRUE(ready_listener.WaitUntilSatisfied());
367 342
368 base::RunLoop run_loop; 343 mock_service_->NotifyDeviceRemoved(mock_device_);
369 BrowserThread::PostTaskAndReply(
370 BrowserThread::FILE, FROM_HERE,
371 base::Bind(&MockUsbService::NotifyDeviceRemoved,
372 base::Unretained(mock_service_), mock_device_),
373 run_loop.QuitClosure());
374 run_loop.Run();
375
376 ASSERT_TRUE(result_listener.WaitUntilSatisfied()); 344 ASSERT_TRUE(result_listener.WaitUntilSatisfied());
377 } 345 }
378 346
379 } // namespace extensions 347 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698