OLD | NEW |
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 "chrome/browser/ui/browser.h" | 6 #include "chrome/browser/ui/browser.h" |
7 #include "content/public/browser/browser_thread.h" | 7 #include "content/public/browser/browser_thread.h" |
8 #include "content/public/test/test_utils.h" | 8 #include "content/public/test/test_utils.h" |
9 #include "device/usb/usb_service.h" | 9 #include "device/usb/usb_service.h" |
10 #include "extensions/browser/api/usb/usb_api.h" | 10 #include "extensions/browser/api/usb/usb_api.h" |
11 #include "net/base/io_buffer.h" | 11 #include "net/base/io_buffer.h" |
12 #include "testing/gmock/include/gmock/gmock.h" | 12 #include "testing/gmock/include/gmock/gmock.h" |
13 | 13 |
14 using testing::AnyNumber; | 14 using testing::AnyNumber; |
15 using testing::_; | 15 using testing::_; |
16 using testing::Return; | 16 using testing::Return; |
| 17 using testing::ReturnRef; |
17 using content::BrowserThread; | 18 using content::BrowserThread; |
18 using device::UsbConfigDescriptor; | 19 using device::UsbConfigDescriptor; |
19 using device::UsbDevice; | 20 using device::UsbDevice; |
20 using device::UsbDeviceHandle; | 21 using device::UsbDeviceHandle; |
21 using device::UsbEndpointDirection; | 22 using device::UsbEndpointDirection; |
22 using device::UsbInterfaceDescriptor; | 23 using device::UsbInterfaceDescriptor; |
23 using device::UsbService; | 24 using device::UsbService; |
24 using device::UsbTransferCallback; | 25 using device::UsbTransferCallback; |
25 | 26 |
26 namespace { | 27 namespace { |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 } | 100 } |
100 | 101 |
101 void set_device(UsbDevice* device) { device_ = device; } | 102 void set_device(UsbDevice* device) { device_ = device; } |
102 | 103 |
103 protected: | 104 protected: |
104 UsbDevice* device_; | 105 UsbDevice* device_; |
105 | 106 |
106 virtual ~MockUsbDeviceHandle() {} | 107 virtual ~MockUsbDeviceHandle() {} |
107 }; | 108 }; |
108 | 109 |
109 class MockUsbConfigDescriptor : public UsbConfigDescriptor { | |
110 public: | |
111 MOCK_CONST_METHOD0(GetNumInterfaces, size_t()); | |
112 MOCK_CONST_METHOD1(GetInterface, | |
113 scoped_refptr<const UsbInterfaceDescriptor>(size_t index)); | |
114 | |
115 protected: | |
116 virtual ~MockUsbConfigDescriptor() {} | |
117 }; | |
118 | |
119 class MockUsbDevice : public UsbDevice { | 110 class MockUsbDevice : public UsbDevice { |
120 public: | 111 public: |
121 explicit MockUsbDevice(MockUsbDeviceHandle* mock_handle) | 112 explicit MockUsbDevice(MockUsbDeviceHandle* mock_handle) |
122 : UsbDevice(0, 0, 0), mock_handle_(mock_handle) { | 113 : UsbDevice(0, 0, 0), mock_handle_(mock_handle) { |
123 mock_handle->set_device(this); | 114 mock_handle->set_device(this); |
124 } | 115 } |
125 | 116 |
126 virtual scoped_refptr<UsbDeviceHandle> Open() OVERRIDE { | 117 virtual scoped_refptr<UsbDeviceHandle> Open() OVERRIDE { |
127 return mock_handle_; | 118 return mock_handle_; |
128 } | 119 } |
129 | 120 |
130 virtual bool Close(scoped_refptr<UsbDeviceHandle> handle) OVERRIDE { | 121 virtual bool Close(scoped_refptr<UsbDeviceHandle> handle) OVERRIDE { |
131 EXPECT_TRUE(false) << "Should not be reached"; | 122 EXPECT_TRUE(false) << "Should not be reached"; |
132 return false; | 123 return false; |
133 } | 124 } |
134 | 125 |
135 #if defined(OS_CHROMEOS) | 126 #if defined(OS_CHROMEOS) |
136 virtual void RequestUsbAccess( | 127 virtual void RequestUsbAccess( |
137 int interface_id, | 128 int interface_id, |
138 const base::Callback<void(bool success)>& callback) OVERRIDE { | 129 const base::Callback<void(bool success)>& callback) OVERRIDE { |
139 BrowserThread::PostTask( | 130 BrowserThread::PostTask( |
140 BrowserThread::FILE, FROM_HERE, base::Bind(callback, true)); | 131 BrowserThread::FILE, FROM_HERE, base::Bind(callback, true)); |
141 } | 132 } |
142 #endif // OS_CHROMEOS | 133 #endif // OS_CHROMEOS |
143 | 134 |
144 MOCK_METHOD0(ListInterfaces, scoped_refptr<UsbConfigDescriptor>()); | 135 MOCK_METHOD0(GetConfiguration, const UsbConfigDescriptor&()); |
145 | 136 |
146 private: | 137 private: |
147 MockUsbDeviceHandle* mock_handle_; | 138 MockUsbDeviceHandle* mock_handle_; |
148 virtual ~MockUsbDevice() {} | 139 virtual ~MockUsbDevice() {} |
149 }; | 140 }; |
150 | 141 |
151 class MockUsbService : public UsbService { | 142 class MockUsbService : public UsbService { |
152 public: | 143 public: |
153 explicit MockUsbService(scoped_refptr<UsbDevice> device) : device_(device) {} | 144 explicit MockUsbService(scoped_refptr<UsbDevice> device) : device_(device) {} |
154 | 145 |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
218 EXPECT_CALL(*mock_device_handle_.get(), ResetDevice()) | 209 EXPECT_CALL(*mock_device_handle_.get(), ResetDevice()) |
219 .WillOnce(Return(true)) | 210 .WillOnce(Return(true)) |
220 .WillOnce(Return(false)); | 211 .WillOnce(Return(false)); |
221 EXPECT_CALL(*mock_device_handle_.get(), | 212 EXPECT_CALL(*mock_device_handle_.get(), |
222 InterruptTransfer(device::USB_DIRECTION_OUTBOUND, 2, _, 1, _, _)) | 213 InterruptTransfer(device::USB_DIRECTION_OUTBOUND, 2, _, 1, _, _)) |
223 .WillOnce(InvokeUsbTransferCallback<5>(device::USB_TRANSFER_COMPLETED)); | 214 .WillOnce(InvokeUsbTransferCallback<5>(device::USB_TRANSFER_COMPLETED)); |
224 ASSERT_TRUE(RunExtensionTest("usb/reset_device")); | 215 ASSERT_TRUE(RunExtensionTest("usb/reset_device")); |
225 } | 216 } |
226 | 217 |
227 IN_PROC_BROWSER_TEST_F(UsbApiTest, ListInterfaces) { | 218 IN_PROC_BROWSER_TEST_F(UsbApiTest, ListInterfaces) { |
228 scoped_refptr<MockUsbConfigDescriptor> mock_descriptor = | 219 UsbConfigDescriptor config_descriptor; |
229 new MockUsbConfigDescriptor(); | |
230 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(AnyNumber()); | 220 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(AnyNumber()); |
231 EXPECT_CALL(*mock_descriptor.get(), GetNumInterfaces()).WillOnce(Return(0)); | 221 EXPECT_CALL(*mock_device_.get(), GetConfiguration()) |
232 EXPECT_CALL(*mock_device_.get(), ListInterfaces()) | 222 .WillOnce(ReturnRef(config_descriptor)); |
233 .WillOnce(Return(mock_descriptor)); | |
234 ASSERT_TRUE(RunExtensionTest("usb/list_interfaces")); | 223 ASSERT_TRUE(RunExtensionTest("usb/list_interfaces")); |
235 } | 224 } |
236 | 225 |
237 IN_PROC_BROWSER_TEST_F(UsbApiTest, TransferEvent) { | 226 IN_PROC_BROWSER_TEST_F(UsbApiTest, TransferEvent) { |
238 EXPECT_CALL(*mock_device_handle_.get(), | 227 EXPECT_CALL(*mock_device_handle_.get(), |
239 ControlTransfer(device::USB_DIRECTION_OUTBOUND, | 228 ControlTransfer(device::USB_DIRECTION_OUTBOUND, |
240 UsbDeviceHandle::STANDARD, | 229 UsbDeviceHandle::STANDARD, |
241 UsbDeviceHandle::DEVICE, | 230 UsbDeviceHandle::DEVICE, |
242 1, | 231 1, |
243 2, | 232 2, |
(...skipping 30 matching lines...) Expand all Loading... |
274 .WillOnce(InvokeUsbTransferCallback<5>(device::USB_TRANSFER_ERROR)) | 263 .WillOnce(InvokeUsbTransferCallback<5>(device::USB_TRANSFER_ERROR)) |
275 .WillOnce(InvokeUsbTransferCallback<5>(device::USB_TRANSFER_TIMEOUT)); | 264 .WillOnce(InvokeUsbTransferCallback<5>(device::USB_TRANSFER_TIMEOUT)); |
276 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(AnyNumber()); | 265 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(AnyNumber()); |
277 ASSERT_TRUE(RunExtensionTest("usb/transfer_failure")); | 266 ASSERT_TRUE(RunExtensionTest("usb/transfer_failure")); |
278 } | 267 } |
279 | 268 |
280 IN_PROC_BROWSER_TEST_F(UsbApiTest, InvalidLengthTransfer) { | 269 IN_PROC_BROWSER_TEST_F(UsbApiTest, InvalidLengthTransfer) { |
281 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(AnyNumber()); | 270 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(AnyNumber()); |
282 ASSERT_TRUE(RunExtensionTest("usb/invalid_length_transfer")); | 271 ASSERT_TRUE(RunExtensionTest("usb/invalid_length_transfer")); |
283 } | 272 } |
OLD | NEW |