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

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

Issue 695613002: Move the USB API tests into app_shell_browsertests. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 1 month 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 "chrome/browser/ui/browser.h"
7 #include "content/public/browser/browser_thread.h" 6 #include "content/public/browser/browser_thread.h"
8 #include "content/public/test/test_utils.h" 7 #include "content/public/test/test_utils.h"
9 #include "device/usb/usb_service.h" 8 #include "device/usb/usb_service.h"
10 #include "extensions/browser/api/usb/usb_api.h" 9 #include "extensions/browser/api/usb/usb_api.h"
10 #include "extensions/shell/test/shell_apitest.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 testing::ReturnRef;
18 using content::BrowserThread; 18 using content::BrowserThread;
19 using device::UsbConfigDescriptor; 19 using device::UsbConfigDescriptor;
20 using device::UsbDevice; 20 using device::UsbDevice;
21 using device::UsbDeviceHandle; 21 using device::UsbDeviceHandle;
22 using device::UsbEndpointDirection; 22 using device::UsbEndpointDirection;
23 using device::UsbInterfaceDescriptor; 23 using device::UsbInterfaceDescriptor;
24 using device::UsbService; 24 using device::UsbService;
25 using device::UsbTransferCallback; 25 using device::UsbTransferCallback;
26 26
27 namespace extensions {
28
27 namespace { 29 namespace {
28 30
29 ACTION_TEMPLATE(InvokeUsbTransferCallback, 31 ACTION_TEMPLATE(InvokeUsbTransferCallback,
30 HAS_1_TEMPLATE_PARAMS(int, k), 32 HAS_1_TEMPLATE_PARAMS(int, k),
31 AND_1_VALUE_PARAMS(p1)) { 33 AND_1_VALUE_PARAMS(p1)) {
32 net::IOBuffer* io_buffer = new net::IOBuffer(1); 34 net::IOBuffer* io_buffer = new net::IOBuffer(1);
33 memset(io_buffer->data(), 0, 1); // Avoid uninitialized reads. 35 memset(io_buffer->data(), 0, 1); // Avoid uninitialized reads.
34 ::std::tr1::get<k>(args).Run(p1, io_buffer, 1); 36 ::std::tr1::get<k>(args).Run(p1, io_buffer, 1);
35 } 37 }
36 38
37 // MSVC erroneously thinks that at least one of the arguments for the transfer
38 // methods differ by const or volatility and emits a warning about the old
39 // standards-noncompliant behaviour of their compiler.
40 #if defined(OS_WIN)
41 #pragma warning(push)
42 #pragma warning(disable : 4373)
43 #endif
44
45 class MockUsbDeviceHandle : public UsbDeviceHandle { 39 class MockUsbDeviceHandle : public UsbDeviceHandle {
46 public: 40 public:
47 MockUsbDeviceHandle() : UsbDeviceHandle() {} 41 MockUsbDeviceHandle() : UsbDeviceHandle() {}
48 42
49 MOCK_METHOD0(Close, void()); 43 MOCK_METHOD0(Close, void());
50 44
51 MOCK_METHOD10(ControlTransfer, 45 MOCK_METHOD10(ControlTransfer,
52 void(const UsbEndpointDirection direction, 46 void(UsbEndpointDirection direction,
53 const TransferRequestType request_type, 47 TransferRequestType request_type,
54 const TransferRecipient recipient, 48 TransferRecipient recipient,
55 const uint8 request, 49 uint8 request,
56 const uint16 value, 50 uint16 value,
57 const uint16 index, 51 uint16 index,
58 net::IOBuffer* buffer, 52 net::IOBuffer* buffer,
59 const size_t length, 53 size_t length,
60 const unsigned int timeout, 54 unsigned int timeout,
61 const UsbTransferCallback& callback)); 55 const UsbTransferCallback& callback));
62 56
63 MOCK_METHOD6(BulkTransfer, 57 MOCK_METHOD6(BulkTransfer,
64 void(const UsbEndpointDirection direction, 58 void(UsbEndpointDirection direction,
65 const uint8 endpoint, 59 uint8 endpoint,
66 net::IOBuffer* buffer, 60 net::IOBuffer* buffer,
67 const size_t length, 61 size_t length,
68 const unsigned int timeout, 62 unsigned int timeout,
69 const UsbTransferCallback& callback)); 63 const UsbTransferCallback& callback));
70 64
71 MOCK_METHOD6(InterruptTransfer, 65 MOCK_METHOD6(InterruptTransfer,
72 void(const UsbEndpointDirection direction, 66 void(UsbEndpointDirection direction,
73 const uint8 endpoint, 67 uint8 endpoint,
74 net::IOBuffer* buffer, 68 net::IOBuffer* buffer,
75 const size_t length, 69 size_t length,
76 const unsigned int timeout, 70 unsigned int timeout,
77 const UsbTransferCallback& callback)); 71 const UsbTransferCallback& callback));
78 72
79 MOCK_METHOD8(IsochronousTransfer, 73 MOCK_METHOD8(IsochronousTransfer,
80 void(const UsbEndpointDirection direction, 74 void(UsbEndpointDirection direction,
81 const uint8 endpoint, 75 uint8 endpoint,
82 net::IOBuffer* buffer, 76 net::IOBuffer* buffer,
83 const size_t length, 77 size_t length,
84 const unsigned int packets, 78 unsigned int packets,
85 const unsigned int packet_length, 79 unsigned int packet_length,
86 const unsigned int timeout, 80 unsigned int timeout,
87 const UsbTransferCallback& callback)); 81 const UsbTransferCallback& callback));
88 82
89 MOCK_METHOD0(ResetDevice, bool()); 83 MOCK_METHOD0(ResetDevice, bool());
90 MOCK_METHOD2(GetStringDescriptor, bool(uint8_t, base::string16*)); 84 MOCK_METHOD2(GetStringDescriptor, bool(uint8_t, base::string16*));
91 MOCK_METHOD1(ClaimInterface, bool(const int interface_number)); 85 MOCK_METHOD1(ClaimInterface, bool(int interface_number));
92 MOCK_METHOD1(ReleaseInterface, bool(const int interface_number)); 86 MOCK_METHOD1(ReleaseInterface, bool(int interface_number));
93 MOCK_METHOD2(SetInterfaceAlternateSetting, 87 MOCK_METHOD2(SetInterfaceAlternateSetting,
94 bool(const int interface_number, const int alternate_setting)); 88 bool(int interface_number, int alternate_setting));
95 89
96 virtual scoped_refptr<UsbDevice> GetDevice() const override { 90 virtual scoped_refptr<UsbDevice> GetDevice() const override {
97 return device_; 91 return device_;
98 } 92 }
99 93
100 void set_device(UsbDevice* device) { device_ = device; } 94 void set_device(UsbDevice* device) { device_ = device; }
101 95
102 protected: 96 protected:
103 UsbDevice* device_; 97 UsbDevice* device_;
104 98
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
151 } 145 }
152 146
153 void GetDevices(std::vector<scoped_refptr<UsbDevice>>* devices) override { 147 void GetDevices(std::vector<scoped_refptr<UsbDevice>>* devices) override {
154 STLClearObject(devices); 148 STLClearObject(devices);
155 devices->push_back(device_); 149 devices->push_back(device_);
156 } 150 }
157 151
158 scoped_refptr<UsbDevice> device_; 152 scoped_refptr<UsbDevice> device_;
159 }; 153 };
160 154
161 #if defined(OS_WIN) 155 class UsbApiTest : public ShellApiTest {
162 #pragma warning(pop)
Yoyo Zhou 2014/10/30 21:36:24 Why this change?
Reilly Grant (use Gerrit) 2014/10/30 21:43:22 The pragma silenced a warning related to those con
163 #endif
164
165 class UsbApiTest : public ExtensionApiTest {
166 public: 156 public:
167 void SetUpOnMainThread() override { 157 void SetUpOnMainThread() override {
158 ShellApiTest::SetUpOnMainThread();
168 mock_device_handle_ = new MockUsbDeviceHandle(); 159 mock_device_handle_ = new MockUsbDeviceHandle();
169 mock_device_ = new MockUsbDevice(mock_device_handle_.get()); 160 mock_device_ = new MockUsbDevice(mock_device_handle_.get());
170 scoped_refptr<content::MessageLoopRunner> runner = 161 scoped_refptr<content::MessageLoopRunner> runner =
171 new content::MessageLoopRunner; 162 new content::MessageLoopRunner;
172 BrowserThread::PostTaskAndReply(BrowserThread::FILE, 163 BrowserThread::PostTaskAndReply(BrowserThread::FILE,
173 FROM_HERE, 164 FROM_HERE,
174 base::Bind(&UsbApiTest::SetUpService, this), 165 base::Bind(&UsbApiTest::SetUpService, this),
175 runner->QuitClosure()); 166 runner->QuitClosure());
176 runner->Run(); 167 runner->Run();
177 } 168 }
(...skipping 16 matching lines...) Expand all
194 185
195 protected: 186 protected:
196 scoped_refptr<MockUsbDeviceHandle> mock_device_handle_; 187 scoped_refptr<MockUsbDeviceHandle> mock_device_handle_;
197 scoped_refptr<MockUsbDevice> mock_device_; 188 scoped_refptr<MockUsbDevice> mock_device_;
198 }; 189 };
199 190
200 } // namespace 191 } // namespace
201 192
202 IN_PROC_BROWSER_TEST_F(UsbApiTest, DeviceHandling) { 193 IN_PROC_BROWSER_TEST_F(UsbApiTest, DeviceHandling) {
203 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(4); 194 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(4);
204 ASSERT_TRUE(RunExtensionTest("usb/device_handling")); 195 ASSERT_TRUE(RunAppTest("api_test/usb/device_handling"));
205 } 196 }
206 197
207 IN_PROC_BROWSER_TEST_F(UsbApiTest, ResetDevice) { 198 IN_PROC_BROWSER_TEST_F(UsbApiTest, ResetDevice) {
208 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(2); 199 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(2);
209 EXPECT_CALL(*mock_device_handle_.get(), ResetDevice()) 200 EXPECT_CALL(*mock_device_handle_.get(), ResetDevice())
210 .WillOnce(Return(true)) 201 .WillOnce(Return(true))
211 .WillOnce(Return(false)); 202 .WillOnce(Return(false));
212 EXPECT_CALL(*mock_device_handle_.get(), 203 EXPECT_CALL(*mock_device_handle_.get(),
213 InterruptTransfer(device::USB_DIRECTION_OUTBOUND, 2, _, 1, _, _)) 204 InterruptTransfer(device::USB_DIRECTION_OUTBOUND, 2, _, 1, _, _))
214 .WillOnce(InvokeUsbTransferCallback<5>(device::USB_TRANSFER_COMPLETED)); 205 .WillOnce(InvokeUsbTransferCallback<5>(device::USB_TRANSFER_COMPLETED));
215 ASSERT_TRUE(RunExtensionTest("usb/reset_device")); 206 ASSERT_TRUE(RunAppTest("api_test/usb/reset_device"));
216 } 207 }
217 208
218 IN_PROC_BROWSER_TEST_F(UsbApiTest, ListInterfaces) { 209 IN_PROC_BROWSER_TEST_F(UsbApiTest, ListInterfaces) {
219 UsbConfigDescriptor config_descriptor; 210 UsbConfigDescriptor config_descriptor;
220 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(AnyNumber()); 211 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(AnyNumber());
221 EXPECT_CALL(*mock_device_.get(), GetConfiguration()) 212 EXPECT_CALL(*mock_device_.get(), GetConfiguration())
222 .WillOnce(ReturnRef(config_descriptor)); 213 .WillOnce(ReturnRef(config_descriptor));
223 ASSERT_TRUE(RunExtensionTest("usb/list_interfaces")); 214 ASSERT_TRUE(RunAppTest("api_test/usb/list_interfaces"));
224 } 215 }
225 216
226 IN_PROC_BROWSER_TEST_F(UsbApiTest, TransferEvent) { 217 IN_PROC_BROWSER_TEST_F(UsbApiTest, TransferEvent) {
227 EXPECT_CALL(*mock_device_handle_.get(), 218 EXPECT_CALL(*mock_device_handle_.get(),
228 ControlTransfer(device::USB_DIRECTION_OUTBOUND, 219 ControlTransfer(device::USB_DIRECTION_OUTBOUND,
229 UsbDeviceHandle::STANDARD, 220 UsbDeviceHandle::STANDARD,
230 UsbDeviceHandle::DEVICE, 221 UsbDeviceHandle::DEVICE,
231 1, 222 1,
232 2, 223 2,
233 3, 224 3,
234 _, 225 _,
235 1, 226 1,
236 _, 227 _,
237 _)) 228 _))
238 .WillOnce(InvokeUsbTransferCallback<9>(device::USB_TRANSFER_COMPLETED)); 229 .WillOnce(InvokeUsbTransferCallback<9>(device::USB_TRANSFER_COMPLETED));
239 EXPECT_CALL(*mock_device_handle_.get(), 230 EXPECT_CALL(*mock_device_handle_.get(),
240 BulkTransfer(device::USB_DIRECTION_OUTBOUND, 1, _, 1, _, _)) 231 BulkTransfer(device::USB_DIRECTION_OUTBOUND, 1, _, 1, _, _))
241 .WillOnce(InvokeUsbTransferCallback<5>(device::USB_TRANSFER_COMPLETED)); 232 .WillOnce(InvokeUsbTransferCallback<5>(device::USB_TRANSFER_COMPLETED));
242 EXPECT_CALL(*mock_device_handle_.get(), 233 EXPECT_CALL(*mock_device_handle_.get(),
243 InterruptTransfer(device::USB_DIRECTION_OUTBOUND, 2, _, 1, _, _)) 234 InterruptTransfer(device::USB_DIRECTION_OUTBOUND, 2, _, 1, _, _))
244 .WillOnce(InvokeUsbTransferCallback<5>(device::USB_TRANSFER_COMPLETED)); 235 .WillOnce(InvokeUsbTransferCallback<5>(device::USB_TRANSFER_COMPLETED));
245 EXPECT_CALL( 236 EXPECT_CALL(
246 *mock_device_handle_.get(), 237 *mock_device_handle_.get(),
247 IsochronousTransfer(device::USB_DIRECTION_OUTBOUND, 3, _, 1, 1, 1, _, _)) 238 IsochronousTransfer(device::USB_DIRECTION_OUTBOUND, 3, _, 1, 1, 1, _, _))
248 .WillOnce(InvokeUsbTransferCallback<7>(device::USB_TRANSFER_COMPLETED)); 239 .WillOnce(InvokeUsbTransferCallback<7>(device::USB_TRANSFER_COMPLETED));
249 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(AnyNumber()); 240 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(AnyNumber());
250 ASSERT_TRUE(RunExtensionTest("usb/transfer_event")); 241 ASSERT_TRUE(RunAppTest("api_test/usb/transfer_event"));
251 } 242 }
252 243
253 IN_PROC_BROWSER_TEST_F(UsbApiTest, ZeroLengthTransfer) { 244 IN_PROC_BROWSER_TEST_F(UsbApiTest, ZeroLengthTransfer) {
254 EXPECT_CALL(*mock_device_handle_.get(), BulkTransfer(_, _, _, 0, _, _)) 245 EXPECT_CALL(*mock_device_handle_.get(), BulkTransfer(_, _, _, 0, _, _))
255 .WillOnce(InvokeUsbTransferCallback<5>(device::USB_TRANSFER_COMPLETED)); 246 .WillOnce(InvokeUsbTransferCallback<5>(device::USB_TRANSFER_COMPLETED));
256 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(AnyNumber()); 247 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(AnyNumber());
257 ASSERT_TRUE(RunExtensionTest("usb/zero_length_transfer")); 248 ASSERT_TRUE(RunAppTest("api_test/usb/zero_length_transfer"));
258 } 249 }
259 250
260 IN_PROC_BROWSER_TEST_F(UsbApiTest, TransferFailure) { 251 IN_PROC_BROWSER_TEST_F(UsbApiTest, TransferFailure) {
261 EXPECT_CALL(*mock_device_handle_.get(), BulkTransfer(_, _, _, _, _, _)) 252 EXPECT_CALL(*mock_device_handle_.get(), BulkTransfer(_, _, _, _, _, _))
262 .WillOnce(InvokeUsbTransferCallback<5>(device::USB_TRANSFER_COMPLETED)) 253 .WillOnce(InvokeUsbTransferCallback<5>(device::USB_TRANSFER_COMPLETED))
263 .WillOnce(InvokeUsbTransferCallback<5>(device::USB_TRANSFER_ERROR)) 254 .WillOnce(InvokeUsbTransferCallback<5>(device::USB_TRANSFER_ERROR))
264 .WillOnce(InvokeUsbTransferCallback<5>(device::USB_TRANSFER_TIMEOUT)); 255 .WillOnce(InvokeUsbTransferCallback<5>(device::USB_TRANSFER_TIMEOUT));
265 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(AnyNumber()); 256 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(AnyNumber());
266 ASSERT_TRUE(RunExtensionTest("usb/transfer_failure")); 257 ASSERT_TRUE(RunAppTest("api_test/usb/transfer_failure"));
267 } 258 }
268 259
269 IN_PROC_BROWSER_TEST_F(UsbApiTest, InvalidLengthTransfer) { 260 IN_PROC_BROWSER_TEST_F(UsbApiTest, InvalidLengthTransfer) {
270 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(AnyNumber()); 261 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(AnyNumber());
271 ASSERT_TRUE(RunExtensionTest("usb/invalid_length_transfer")); 262 ASSERT_TRUE(RunAppTest("api_test/usb/invalid_length_transfer"));
272 } 263 }
264
265 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/test/data/extensions/api_test/usb/zero_length_transfer/test.js ('k') | extensions/shell/app_shell.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698