OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/api/usb/usb_api.h" | |
6 #include "chrome/browser/extensions/extension_apitest.h" | 5 #include "chrome/browser/extensions/extension_apitest.h" |
7 #include "chrome/browser/ui/browser.h" | 6 #include "chrome/browser/ui/browser.h" |
8 #include "components/usb_service/usb_service.h" | 7 #include "components/usb_service/usb_service.h" |
9 #include "content/public/browser/browser_thread.h" | 8 #include "content/public/browser/browser_thread.h" |
10 #include "content/public/test/test_utils.h" | 9 #include "content/public/test/test_utils.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 content::BrowserThread; | 17 using content::BrowserThread; |
18 using usb_service::UsbConfigDescriptor; | 18 using usb_service::UsbConfigDescriptor; |
19 using usb_service::UsbDevice; | 19 using usb_service::UsbDevice; |
20 using usb_service::UsbDeviceHandle; | 20 using usb_service::UsbDeviceHandle; |
21 using usb_service::UsbEndpointDirection; | 21 using usb_service::UsbEndpointDirection; |
22 using usb_service::UsbService; | 22 using usb_service::UsbService; |
23 using usb_service::UsbTransferCallback; | 23 using usb_service::UsbTransferCallback; |
24 | 24 |
25 namespace { | 25 namespace { |
26 | 26 |
27 ACTION_TEMPLATE(InvokeUsbTransferCallback, | 27 ACTION_TEMPLATE(InvokeUsbTransferCallback, |
28 HAS_1_TEMPLATE_PARAMS(int, k), | 28 HAS_1_TEMPLATE_PARAMS(int, k), |
29 AND_1_VALUE_PARAMS(p1)) { | 29 AND_1_VALUE_PARAMS(p1)) { |
30 ::std::tr1::get<k>(args).Run(p1, new net::IOBuffer(1), 1); | 30 ::std::tr1::get<k>(args).Run(p1, new net::IOBuffer(1), 1); |
31 } | 31 } |
32 | 32 |
33 // MSVC erroneously thinks that at least one of the arguments for the transfer | 33 // MSVC erroneously thinks that at least one of the arguments for the transfer |
34 // methods differ by const or volatility and emits a warning about the old | 34 // methods differ by const or volatility and emits a warning about the old |
35 // standards-noncompliant behaviour of their compiler. | 35 // standards-noncompliant behaviour of their compiler. |
36 #if defined(OS_WIN) | 36 #if defined(OS_WIN) |
37 #pragma warning(push) | 37 #pragma warning(push) |
38 #pragma warning(disable:4373) | 38 #pragma warning(disable : 4373) |
39 #endif | 39 #endif |
40 | 40 |
41 class MockUsbDeviceHandle : public UsbDeviceHandle { | 41 class MockUsbDeviceHandle : public UsbDeviceHandle { |
42 public: | 42 public: |
43 MockUsbDeviceHandle() : UsbDeviceHandle() {} | 43 MockUsbDeviceHandle() : UsbDeviceHandle() {} |
44 | 44 |
45 MOCK_METHOD0(Close, void()); | 45 MOCK_METHOD0(Close, void()); |
46 | 46 |
47 MOCK_METHOD10(ControlTransfer, void(const UsbEndpointDirection direction, | 47 MOCK_METHOD10(ControlTransfer, |
48 const TransferRequestType request_type, const TransferRecipient recipient, | 48 void(const UsbEndpointDirection direction, |
49 const uint8 request, const uint16 value, const uint16 index, | 49 const TransferRequestType request_type, |
50 net::IOBuffer* buffer, const size_t length, const unsigned int timeout, | 50 const TransferRecipient recipient, |
51 const UsbTransferCallback& callback)); | 51 const uint8 request, |
| 52 const uint16 value, |
| 53 const uint16 index, |
| 54 net::IOBuffer* buffer, |
| 55 const size_t length, |
| 56 const unsigned int timeout, |
| 57 const UsbTransferCallback& callback)); |
52 | 58 |
53 MOCK_METHOD6(BulkTransfer, void(const UsbEndpointDirection direction, | 59 MOCK_METHOD6(BulkTransfer, |
54 const uint8 endpoint, net::IOBuffer* buffer, const size_t length, | 60 void(const UsbEndpointDirection direction, |
55 const unsigned int timeout, const UsbTransferCallback& callback)); | 61 const uint8 endpoint, |
| 62 net::IOBuffer* buffer, |
| 63 const size_t length, |
| 64 const unsigned int timeout, |
| 65 const UsbTransferCallback& callback)); |
56 | 66 |
57 MOCK_METHOD6(InterruptTransfer, void(const UsbEndpointDirection direction, | 67 MOCK_METHOD6(InterruptTransfer, |
58 const uint8 endpoint, net::IOBuffer* buffer, const size_t length, | 68 void(const UsbEndpointDirection direction, |
59 const unsigned int timeout, const UsbTransferCallback& callback)); | 69 const uint8 endpoint, |
| 70 net::IOBuffer* buffer, |
| 71 const size_t length, |
| 72 const unsigned int timeout, |
| 73 const UsbTransferCallback& callback)); |
60 | 74 |
61 MOCK_METHOD8(IsochronousTransfer, void(const UsbEndpointDirection direction, | 75 MOCK_METHOD8(IsochronousTransfer, |
62 const uint8 endpoint, net::IOBuffer* buffer, const size_t length, | 76 void(const UsbEndpointDirection direction, |
63 const unsigned int packets, const unsigned int packet_length, | 77 const uint8 endpoint, |
64 const unsigned int timeout, const UsbTransferCallback& callback)); | 78 net::IOBuffer* buffer, |
| 79 const size_t length, |
| 80 const unsigned int packets, |
| 81 const unsigned int packet_length, |
| 82 const unsigned int timeout, |
| 83 const UsbTransferCallback& callback)); |
65 | 84 |
66 MOCK_METHOD0(ResetDevice, bool()); | 85 MOCK_METHOD0(ResetDevice, bool()); |
67 | 86 |
68 void set_device(UsbDevice* device) { device_ = device; } | 87 void set_device(UsbDevice* device) { device_ = device; } |
69 | 88 |
70 protected: | 89 protected: |
71 virtual ~MockUsbDeviceHandle() {} | 90 virtual ~MockUsbDeviceHandle() {} |
72 }; | 91 }; |
73 | 92 |
74 class MockUsbConfigDescriptor : public UsbConfigDescriptor { | 93 class MockUsbConfigDescriptor : public UsbConfigDescriptor { |
75 public: | 94 public: |
76 MOCK_CONST_METHOD0(GetNumInterfaces, size_t()); | 95 MOCK_CONST_METHOD0(GetNumInterfaces, size_t()); |
77 | 96 |
78 protected: | 97 protected: |
79 virtual ~MockUsbConfigDescriptor() {} | 98 virtual ~MockUsbConfigDescriptor() {} |
80 }; | 99 }; |
81 | 100 |
82 class MockUsbDevice : public UsbDevice { | 101 class MockUsbDevice : public UsbDevice { |
83 public: | 102 public: |
84 explicit MockUsbDevice(MockUsbDeviceHandle* mock_handle) | 103 explicit MockUsbDevice(MockUsbDeviceHandle* mock_handle) |
85 : UsbDevice(), | 104 : UsbDevice(), mock_handle_(mock_handle) { |
86 mock_handle_(mock_handle) { | |
87 mock_handle->set_device(this); | 105 mock_handle->set_device(this); |
88 } | 106 } |
89 | 107 |
90 virtual scoped_refptr<UsbDeviceHandle> Open() OVERRIDE { | 108 virtual scoped_refptr<UsbDeviceHandle> Open() OVERRIDE { |
91 return mock_handle_; | 109 return mock_handle_; |
92 } | 110 } |
93 | 111 |
94 virtual bool Close(scoped_refptr<UsbDeviceHandle> handle) OVERRIDE { | 112 virtual bool Close(scoped_refptr<UsbDeviceHandle> handle) OVERRIDE { |
95 EXPECT_TRUE(false) << "Should not be reached"; | 113 EXPECT_TRUE(false) << "Should not be reached"; |
96 return false; | 114 return false; |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
198 EXPECT_CALL(*mock_device_.get(), ListInterfaces()) | 216 EXPECT_CALL(*mock_device_.get(), ListInterfaces()) |
199 .WillOnce(Return(mock_descriptor)); | 217 .WillOnce(Return(mock_descriptor)); |
200 ASSERT_TRUE(RunExtensionTest("usb/list_interfaces")); | 218 ASSERT_TRUE(RunExtensionTest("usb/list_interfaces")); |
201 } | 219 } |
202 | 220 |
203 IN_PROC_BROWSER_TEST_F(UsbApiTest, TransferEvent) { | 221 IN_PROC_BROWSER_TEST_F(UsbApiTest, TransferEvent) { |
204 EXPECT_CALL(*mock_device_handle_.get(), | 222 EXPECT_CALL(*mock_device_handle_.get(), |
205 ControlTransfer(usb_service::USB_DIRECTION_OUTBOUND, | 223 ControlTransfer(usb_service::USB_DIRECTION_OUTBOUND, |
206 UsbDeviceHandle::STANDARD, | 224 UsbDeviceHandle::STANDARD, |
207 UsbDeviceHandle::DEVICE, | 225 UsbDeviceHandle::DEVICE, |
208 1, 2, 3, _, 1, _, _)) | 226 1, |
| 227 2, |
| 228 3, |
| 229 _, |
| 230 1, |
| 231 _, |
| 232 _)) |
209 .WillOnce( | 233 .WillOnce( |
210 InvokeUsbTransferCallback<9>(usb_service::USB_TRANSFER_COMPLETED)); | 234 InvokeUsbTransferCallback<9>(usb_service::USB_TRANSFER_COMPLETED)); |
211 EXPECT_CALL(*mock_device_handle_.get(), | 235 EXPECT_CALL(*mock_device_handle_.get(), |
212 BulkTransfer(usb_service::USB_DIRECTION_OUTBOUND, 1, _, 1, _, _)) | 236 BulkTransfer(usb_service::USB_DIRECTION_OUTBOUND, 1, _, 1, _, _)) |
213 .WillOnce( | 237 .WillOnce( |
214 InvokeUsbTransferCallback<5>(usb_service::USB_TRANSFER_COMPLETED)); | 238 InvokeUsbTransferCallback<5>(usb_service::USB_TRANSFER_COMPLETED)); |
215 EXPECT_CALL( | 239 EXPECT_CALL( |
216 *mock_device_handle_.get(), | 240 *mock_device_handle_.get(), |
217 InterruptTransfer(usb_service::USB_DIRECTION_OUTBOUND, 2, _, 1, _, _)) | 241 InterruptTransfer(usb_service::USB_DIRECTION_OUTBOUND, 2, _, 1, _, _)) |
218 .WillOnce( | 242 .WillOnce( |
(...skipping 23 matching lines...) Expand all Loading... |
242 .WillOnce( | 266 .WillOnce( |
243 InvokeUsbTransferCallback<5>(usb_service::USB_TRANSFER_TIMEOUT)); | 267 InvokeUsbTransferCallback<5>(usb_service::USB_TRANSFER_TIMEOUT)); |
244 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(AnyNumber()); | 268 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(AnyNumber()); |
245 ASSERT_TRUE(RunExtensionTest("usb/transfer_failure")); | 269 ASSERT_TRUE(RunExtensionTest("usb/transfer_failure")); |
246 } | 270 } |
247 | 271 |
248 IN_PROC_BROWSER_TEST_F(UsbApiTest, InvalidLengthTransfer) { | 272 IN_PROC_BROWSER_TEST_F(UsbApiTest, InvalidLengthTransfer) { |
249 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(AnyNumber()); | 273 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(AnyNumber()); |
250 ASSERT_TRUE(RunExtensionTest("usb/invalid_length_transfer")); | 274 ASSERT_TRUE(RunExtensionTest("usb/invalid_length_transfer")); |
251 } | 275 } |
OLD | NEW |