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

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: 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::UsbSuccessCallback;
28 using device::UsbTransferCallback; 29 using device::UsbTransferCallback;
29 30
30 namespace extensions { 31 namespace extensions {
31 32
32 namespace { 33 namespace {
33 34
35 ACTION_TEMPLATE(InvokeCallback,
36 HAS_1_TEMPLATE_PARAMS(int, k),
37 AND_1_VALUE_PARAMS(p1)) {
38 ::std::tr1::get<k>(args).Run(p1);
39 }
40
34 ACTION_TEMPLATE(InvokeUsbTransferCallback, 41 ACTION_TEMPLATE(InvokeUsbTransferCallback,
35 HAS_1_TEMPLATE_PARAMS(int, k), 42 HAS_1_TEMPLATE_PARAMS(int, k),
36 AND_1_VALUE_PARAMS(p1)) { 43 AND_1_VALUE_PARAMS(p1)) {
37 net::IOBuffer* io_buffer = new net::IOBuffer(1); 44 net::IOBuffer* io_buffer = new net::IOBuffer(1);
38 memset(io_buffer->data(), 0, 1); // Avoid uninitialized reads. 45 memset(io_buffer->data(), 0, 1); // Avoid uninitialized reads.
39 ::std::tr1::get<k>(args).Run(p1, io_buffer, 1); 46 ::std::tr1::get<k>(args).Run(p1, io_buffer, 1);
40 } 47 }
41 48
42 void RequestUsbAccess(int interface_id, 49 void RequestUsbAccess(int interface_id,
43 const base::Callback<void(bool success)>& callback) { 50 const base::Callback<void(bool success)>& callback) {
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
113 MOCK_METHOD8(IsochronousTransfer, 120 MOCK_METHOD8(IsochronousTransfer,
114 void(UsbEndpointDirection direction, 121 void(UsbEndpointDirection direction,
115 uint8 endpoint, 122 uint8 endpoint,
116 net::IOBuffer* buffer, 123 net::IOBuffer* buffer,
117 size_t length, 124 size_t length,
118 unsigned int packets, 125 unsigned int packets,
119 unsigned int packet_length, 126 unsigned int packet_length,
120 unsigned int timeout, 127 unsigned int timeout,
121 const UsbTransferCallback& callback)); 128 const UsbTransferCallback& callback));
122 129
123 MOCK_METHOD0(ResetDevice, bool()); 130 MOCK_METHOD1(ResetDevice, void(const UsbSuccessCallback& callback));
124 MOCK_METHOD2(GetStringDescriptor, bool(uint8_t, base::string16*)); 131 MOCK_METHOD2(GetStringDescriptor, bool(uint8_t, base::string16*));
125 MOCK_METHOD1(SetConfiguration, bool(int)); 132 MOCK_METHOD2(SetConfiguration,
126 MOCK_METHOD1(ClaimInterface, bool(int interface_number)); 133 void(int configuration_value,
134 const UsbSuccessCallback& callback));
135 MOCK_METHOD2(ClaimInterface,
136 void(int interface_number, const UsbSuccessCallback& callback));
127 MOCK_METHOD1(ReleaseInterface, bool(int interface_number)); 137 MOCK_METHOD1(ReleaseInterface, bool(int interface_number));
128 MOCK_METHOD2(SetInterfaceAlternateSetting, 138 MOCK_METHOD3(SetInterfaceAlternateSetting,
129 bool(int interface_number, int alternate_setting)); 139 void(int interface_number,
140 int alternate_setting,
141 const UsbSuccessCallback& callback));
130 142
131 virtual scoped_refptr<UsbDevice> GetDevice() const override { 143 virtual scoped_refptr<UsbDevice> GetDevice() const override {
132 return device_; 144 return device_;
133 } 145 }
134 146
135 void set_device(UsbDevice* device) { device_ = device; } 147 void set_device(UsbDevice* device) { device_ = device; }
136 148
137 protected: 149 protected:
138 UsbDevice* device_; 150 UsbDevice* device_;
139 151
140 virtual ~MockUsbDeviceHandle() {} 152 virtual ~MockUsbDeviceHandle() {}
141 }; 153 };
142 154
143 class MockUsbDevice : public UsbDevice { 155 class MockUsbDevice : public UsbDevice {
144 public: 156 public:
145 MockUsbDevice(uint16 vendor_id, uint16 product_id, uint32 unique_id) 157 MockUsbDevice(uint16 vendor_id, uint16 product_id, uint32 unique_id)
146 : UsbDevice(vendor_id, product_id, unique_id) {} 158 : UsbDevice(vendor_id,
159 product_id,
160 unique_id,
161 base::string16(),
162 base::string16(),
163 base::string16()) {}
147 164
148 MOCK_METHOD2(RequestUsbAccess, void(int, const base::Callback<void(bool)>&)); 165 MOCK_METHOD2(RequestUsbAccess, void(int, const UsbSuccessCallback&));
149 MOCK_METHOD0(Open, scoped_refptr<UsbDeviceHandle>()); 166 MOCK_METHOD1(Open, void(const OpenCallback&));
150 MOCK_METHOD1(Close, bool(scoped_refptr<UsbDeviceHandle>)); 167 MOCK_METHOD1(Close, bool(scoped_refptr<UsbDeviceHandle>));
151 MOCK_METHOD0(GetConfiguration, const device::UsbConfigDescriptor*()); 168 MOCK_METHOD0(GetConfiguration, const device::UsbConfigDescriptor*());
152 MOCK_METHOD1(GetManufacturer, bool(base::string16*));
153 MOCK_METHOD1(GetProduct, bool(base::string16*));
154 MOCK_METHOD1(GetSerialNumber, bool(base::string16*));
155 169
156 private: 170 private:
157 virtual ~MockUsbDevice() {} 171 virtual ~MockUsbDevice() {}
158 }; 172 };
159 173
160 class MockUsbService : public UsbService { 174 class MockUsbService : public UsbService {
161 public: 175 public:
162 explicit MockUsbService(scoped_refptr<UsbDevice> device) : device_(device) {} 176 explicit MockUsbService(scoped_refptr<UsbDevice> device) : device_(device) {}
163 177
164 // Public wrapper around the protected base class method. 178 // Public wrapper around the protected base class method.
165 void NotifyDeviceAdded(scoped_refptr<UsbDevice> device) { 179 void NotifyDeviceAdded(scoped_refptr<UsbDevice> device) {
166 UsbService::NotifyDeviceAdded(device); 180 UsbService::NotifyDeviceAdded(device);
167 } 181 }
168 182
169 // Public wrapper around the protected base class method. 183 // Public wrapper around the protected base class method.
170 void NotifyDeviceRemoved(scoped_refptr<UsbDevice> device) { 184 void NotifyDeviceRemoved(scoped_refptr<UsbDevice> device) {
171 UsbService::NotifyDeviceRemoved(device); 185 UsbService::NotifyDeviceRemoved(device);
172 } 186 }
173 187
174 protected: 188 protected:
175 scoped_refptr<UsbDevice> GetDeviceById(uint32 unique_id) override { 189 scoped_refptr<UsbDevice> GetDeviceById(uint32 unique_id) override {
176 EXPECT_EQ(unique_id, 0U); 190 EXPECT_EQ(unique_id, 0U);
177 return device_; 191 return device_;
178 } 192 }
179 193
180 void GetDevices(std::vector<scoped_refptr<UsbDevice>>* devices) override { 194 void GetDevices(const GetDevicesCallback& callback) override {
181 STLClearObject(devices); 195 std::vector<scoped_refptr<UsbDevice>> devices;
182 devices->push_back(device_); 196 devices.push_back(device_);
197 callback.Run(devices);
183 } 198 }
184 199
185 scoped_refptr<UsbDevice> device_; 200 scoped_refptr<UsbDevice> device_;
186 }; 201 };
187 202
188 class UsbApiTest : public ShellApiTest { 203 class UsbApiTest : public ShellApiTest {
189 public: 204 public:
190 void SetUpOnMainThread() override { 205 void SetUpOnMainThread() override {
191 ShellApiTest::SetUpOnMainThread(); 206 ShellApiTest::SetUpOnMainThread();
192 207
193 mock_device_ = new MockUsbDevice(0, 0, 0); 208 mock_device_ = new MockUsbDevice(0, 0, 0);
194 EXPECT_CALL(*mock_device_.get(), GetManufacturer(_))
195 .WillRepeatedly(Return(false));
196 EXPECT_CALL(*mock_device_.get(), GetProduct(_))
197 .WillRepeatedly(Return(false));
198 EXPECT_CALL(*mock_device_.get(), GetSerialNumber(_))
199 .WillRepeatedly(Return(false));
200
201 mock_device_handle_ = new MockUsbDeviceHandle(); 209 mock_device_handle_ = new MockUsbDeviceHandle();
202 mock_device_handle_->set_device(mock_device_.get()); 210 mock_device_handle_->set_device(mock_device_.get());
203 EXPECT_CALL(*mock_device_.get(), RequestUsbAccess(_, _)) 211 EXPECT_CALL(*mock_device_.get(), RequestUsbAccess(_, _))
204 .WillRepeatedly(Invoke(RequestUsbAccess)); 212 .WillRepeatedly(Invoke(RequestUsbAccess));
205 EXPECT_CALL(*mock_device_.get(), Open()) 213 EXPECT_CALL(*mock_device_.get(), Open(_))
206 .WillRepeatedly(Return(mock_device_handle_)); 214 .WillRepeatedly(InvokeCallback<0>(mock_device_handle_));
207 215
208 base::RunLoop run_loop; 216 mock_service_.reset(new MockUsbService(mock_device_));
209 BrowserThread::PostTaskAndReply(BrowserThread::FILE, FROM_HERE,
210 base::Bind(&UsbApiTest::SetUpService, this),
211 run_loop.QuitClosure());
212 run_loop.Run();
213 }
214
215 void SetUpService() {
216 mock_service_ = new MockUsbService(mock_device_);
217 UsbService::SetInstanceForTest(mock_service_);
218 }
219
220 void AddTestDevices() {
221 scoped_refptr<MockUsbDevice> device(new MockUsbDevice(0x18D1, 0x58F0, 1));
222 EXPECT_CALL(*device.get(), GetSerialNumber(_))
223 .WillRepeatedly(Return(false));
224 mock_service_->NotifyDeviceAdded(device);
225
226 device = new MockUsbDevice(0x18D1, 0x58F1, 2);
227 EXPECT_CALL(*device.get(), GetSerialNumber(_))
228 .WillRepeatedly(Return(false));
229 mock_service_->NotifyDeviceAdded(device);
230 } 217 }
231 218
232 protected: 219 protected:
233 scoped_refptr<MockUsbDeviceHandle> mock_device_handle_; 220 scoped_refptr<MockUsbDeviceHandle> mock_device_handle_;
234 scoped_refptr<MockUsbDevice> mock_device_; 221 scoped_refptr<MockUsbDevice> mock_device_;
235 MockUsbService* mock_service_; 222 scoped_ptr<MockUsbService> mock_service_;
236 }; 223 };
237 224
238 } // namespace 225 } // namespace
239 226
240 IN_PROC_BROWSER_TEST_F(UsbApiTest, DeviceHandling) { 227 IN_PROC_BROWSER_TEST_F(UsbApiTest, DeviceHandling) {
241 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(2); 228 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(2);
242 ASSERT_TRUE(RunAppTest("api_test/usb/device_handling")); 229 ASSERT_TRUE(RunAppTest("api_test/usb/device_handling"));
243 } 230 }
244 231
245 IN_PROC_BROWSER_TEST_F(UsbApiTest, ResetDevice) { 232 IN_PROC_BROWSER_TEST_F(UsbApiTest, ResetDevice) {
246 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(2); 233 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(2);
247 EXPECT_CALL(*mock_device_handle_.get(), ResetDevice()) 234 EXPECT_CALL(*mock_device_handle_.get(), ResetDevice(_))
248 .WillOnce(Return(true)) 235 .WillOnce(InvokeCallback<0>(true))
249 .WillOnce(Return(false)); 236 .WillOnce(InvokeCallback<0>(false));
250 EXPECT_CALL(*mock_device_handle_.get(), 237 EXPECT_CALL(*mock_device_handle_.get(),
251 InterruptTransfer(device::USB_DIRECTION_OUTBOUND, 2, _, 1, _, _)) 238 InterruptTransfer(device::USB_DIRECTION_OUTBOUND, 2, _, 1, _, _))
252 .WillOnce(InvokeUsbTransferCallback<5>(device::USB_TRANSFER_COMPLETED)); 239 .WillOnce(InvokeUsbTransferCallback<5>(device::USB_TRANSFER_COMPLETED));
253 ASSERT_TRUE(RunAppTest("api_test/usb/reset_device")); 240 ASSERT_TRUE(RunAppTest("api_test/usb/reset_device"));
254 } 241 }
255 242
256 IN_PROC_BROWSER_TEST_F(UsbApiTest, SetConfiguration) { 243 IN_PROC_BROWSER_TEST_F(UsbApiTest, SetConfiguration) {
257 UsbConfigDescriptor config_descriptor; 244 UsbConfigDescriptor config_descriptor;
258 EXPECT_CALL(*mock_device_handle_.get(), SetConfiguration(1)) 245 EXPECT_CALL(*mock_device_handle_.get(), SetConfiguration(1, _))
259 .WillOnce(Return(true)); 246 .WillOnce(InvokeCallback<1>(true));
260 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(1); 247 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(1);
261 EXPECT_CALL(*mock_device_.get(), GetConfiguration()) 248 EXPECT_CALL(*mock_device_.get(), GetConfiguration())
262 .WillOnce(Return(nullptr)) 249 .WillOnce(Return(nullptr))
263 .WillOnce(Return(&config_descriptor)); 250 .WillOnce(Return(&config_descriptor));
264 ASSERT_TRUE(RunAppTest("api_test/usb/set_configuration")); 251 ASSERT_TRUE(RunAppTest("api_test/usb/set_configuration"));
265 } 252 }
266 253
267 IN_PROC_BROWSER_TEST_F(UsbApiTest, ListInterfaces) { 254 IN_PROC_BROWSER_TEST_F(UsbApiTest, ListInterfaces) {
268 UsbConfigDescriptor config_descriptor; 255 UsbConfigDescriptor config_descriptor;
269 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(1); 256 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(1);
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
326 } 313 }
327 314
328 IN_PROC_BROWSER_TEST_F(UsbApiTest, OnDeviceAdded) { 315 IN_PROC_BROWSER_TEST_F(UsbApiTest, OnDeviceAdded) {
329 ExtensionTestMessageListener load_listener("loaded", false); 316 ExtensionTestMessageListener load_listener("loaded", false);
330 ExtensionTestMessageListener result_listener("success", false); 317 ExtensionTestMessageListener result_listener("success", false);
331 result_listener.set_failure_message("failure"); 318 result_listener.set_failure_message("failure");
332 319
333 ASSERT_TRUE(LoadApp("api_test/usb/add_event")); 320 ASSERT_TRUE(LoadApp("api_test/usb/add_event"));
334 ASSERT_TRUE(load_listener.WaitUntilSatisfied()); 321 ASSERT_TRUE(load_listener.WaitUntilSatisfied());
335 322
336 base::RunLoop run_loop; 323 scoped_refptr<MockUsbDevice> device(new MockUsbDevice(0x18D1, 0x58F0, 1));
337 BrowserThread::PostTaskAndReply( 324 mock_service_->NotifyDeviceAdded(device);
338 BrowserThread::FILE, FROM_HERE, 325
339 base::Bind(&UsbApiTest::AddTestDevices, base::Unretained(this)), 326 device = new MockUsbDevice(0x18D1, 0x58F1, 2);
340 run_loop.QuitClosure()); 327 mock_service_->NotifyDeviceAdded(device);
341 run_loop.Run();
342 328
343 ASSERT_TRUE(result_listener.WaitUntilSatisfied()); 329 ASSERT_TRUE(result_listener.WaitUntilSatisfied());
344 } 330 }
345 331
346 IN_PROC_BROWSER_TEST_F(UsbApiTest, OnDeviceRemoved) { 332 IN_PROC_BROWSER_TEST_F(UsbApiTest, OnDeviceRemoved) {
347 ExtensionTestMessageListener load_listener("loaded", false); 333 ExtensionTestMessageListener load_listener("loaded", false);
348 ExtensionTestMessageListener result_listener("success", false); 334 ExtensionTestMessageListener result_listener("success", false);
349 result_listener.set_failure_message("failure"); 335 result_listener.set_failure_message("failure");
350 336
351 ASSERT_TRUE(LoadApp("api_test/usb/remove_event")); 337 ASSERT_TRUE(LoadApp("api_test/usb/remove_event"));
352 ASSERT_TRUE(load_listener.WaitUntilSatisfied()); 338 ASSERT_TRUE(load_listener.WaitUntilSatisfied());
353 339
354 base::RunLoop run_loop; 340 mock_service_->NotifyDeviceRemoved(mock_device_);
355 BrowserThread::PostTaskAndReply(
356 BrowserThread::FILE, FROM_HERE,
357 base::Bind(&MockUsbService::NotifyDeviceRemoved,
358 base::Unretained(mock_service_), mock_device_),
359 run_loop.QuitClosure());
360 run_loop.Run();
361
362 ASSERT_TRUE(result_listener.WaitUntilSatisfied()); 341 ASSERT_TRUE(result_listener.WaitUntilSatisfied());
363 } 342 }
364 343
365 IN_PROC_BROWSER_TEST_F(UsbApiTest, GetUserSelectedDevices) { 344 IN_PROC_BROWSER_TEST_F(UsbApiTest, GetUserSelectedDevices) {
366 ExtensionTestMessageListener ready_listener("opened_device", false); 345 ExtensionTestMessageListener ready_listener("opened_device", false);
367 ExtensionTestMessageListener result_listener("success", false); 346 ExtensionTestMessageListener result_listener("success", false);
368 result_listener.set_failure_message("failure"); 347 result_listener.set_failure_message("failure");
369 348
370 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(1); 349 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(1);
371 350
372 TestExtensionsAPIClient test_api_client; 351 TestExtensionsAPIClient test_api_client;
373 ASSERT_TRUE(LoadApp("api_test/usb/get_user_selected_devices")); 352 ASSERT_TRUE(LoadApp("api_test/usb/get_user_selected_devices"));
374 ASSERT_TRUE(ready_listener.WaitUntilSatisfied()); 353 ASSERT_TRUE(ready_listener.WaitUntilSatisfied());
375 354
376 base::RunLoop run_loop; 355 mock_service_->NotifyDeviceRemoved(mock_device_);
377 BrowserThread::PostTaskAndReply(
378 BrowserThread::FILE, FROM_HERE,
379 base::Bind(&MockUsbService::NotifyDeviceRemoved,
380 base::Unretained(mock_service_), mock_device_),
381 run_loop.QuitClosure());
382 run_loop.Run();
383
384 ASSERT_TRUE(result_listener.WaitUntilSatisfied()); 356 ASSERT_TRUE(result_listener.WaitUntilSatisfied());
385 } 357 }
386 358
387 } // namespace extensions 359 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698