cygprofile: increase timeouts to allow showing web contents
[chromium-blink-merge.git] / extensions / browser / api / usb / usb_apitest.cc
blobdebe02bde40d6183761cb9db097ac7d7c422dc0b
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
3 // found in the LICENSE file.
5 #include "chrome/browser/extensions/extension_apitest.h"
6 #include "content/public/browser/browser_thread.h"
7 #include "content/public/test/test_utils.h"
8 #include "device/core/device_client.h"
9 #include "device/usb/mock_usb_device.h"
10 #include "device/usb/mock_usb_device_handle.h"
11 #include "device/usb/mock_usb_service.h"
12 #include "extensions/browser/api/device_permissions_prompt.h"
13 #include "extensions/browser/api/usb/usb_api.h"
14 #include "extensions/shell/browser/shell_extensions_api_client.h"
15 #include "extensions/shell/test/shell_apitest.h"
16 #include "extensions/test/extension_test_message_listener.h"
17 #include "net/base/io_buffer.h"
19 using testing::_;
20 using testing::AnyNumber;
21 using testing::Invoke;
22 using testing::Return;
23 using content::BrowserThread;
24 using device::DeviceClient;
25 using device::MockUsbDevice;
26 using device::MockUsbDeviceHandle;
27 using device::MockUsbService;
28 using device::UsbConfigDescriptor;
29 using device::UsbDevice;
30 using device::UsbDeviceHandle;
31 using device::UsbEndpointDirection;
32 using device::UsbInterfaceDescriptor;
33 using device::UsbService;
35 namespace extensions {
37 namespace {
39 ACTION_TEMPLATE(InvokeCallback,
40 HAS_1_TEMPLATE_PARAMS(int, k),
41 AND_1_VALUE_PARAMS(p1)) {
42 ::std::tr1::get<k>(args).Run(p1);
45 ACTION_TEMPLATE(InvokeUsbTransferCallback,
46 HAS_1_TEMPLATE_PARAMS(int, k),
47 AND_1_VALUE_PARAMS(p1)) {
48 net::IOBuffer* io_buffer = new net::IOBuffer(1);
49 memset(io_buffer->data(), 0, 1); // Avoid uninitialized reads.
50 ::std::tr1::get<k>(args).Run(p1, io_buffer, 1);
53 class TestDevicePermissionsPrompt
54 : public DevicePermissionsPrompt,
55 public DevicePermissionsPrompt::Prompt::Observer {
56 public:
57 TestDevicePermissionsPrompt(content::WebContents* web_contents)
58 : DevicePermissionsPrompt(web_contents) {}
60 void ShowDialog() override { prompt()->SetObserver(this); }
62 void OnDevicesChanged() override {
63 for (size_t i = 0; i < prompt()->GetDeviceCount(); ++i) {
64 prompt()->GrantDevicePermission(i);
65 if (!prompt()->multiple()) {
66 break;
69 prompt()->Dismissed();
73 class TestDeviceClient : public DeviceClient {
74 public:
75 TestDeviceClient() : DeviceClient() {}
76 ~TestDeviceClient() override {}
78 MockUsbService& mock_usb_service() { return usb_service_; }
80 private:
81 UsbService* GetUsbService() override { return &usb_service_; }
83 MockUsbService usb_service_;
86 class TestExtensionsAPIClient : public ShellExtensionsAPIClient {
87 public:
88 TestExtensionsAPIClient() : ShellExtensionsAPIClient() {}
90 scoped_ptr<DevicePermissionsPrompt> CreateDevicePermissionsPrompt(
91 content::WebContents* web_contents) const override {
92 return make_scoped_ptr(new TestDevicePermissionsPrompt(web_contents));
96 class UsbApiTest : public ShellApiTest {
97 public:
98 void SetUpOnMainThread() override {
99 ShellApiTest::SetUpOnMainThread();
101 mock_device_ =
102 new MockUsbDevice(0, 0, "Test Manufacturer", "Test Device", "ABC123");
103 mock_device_handle_ = new MockUsbDeviceHandle(mock_device_.get());
104 EXPECT_CALL(*mock_device_.get(), Open(_))
105 .WillRepeatedly(InvokeCallback<0>(mock_device_handle_));
106 device_client_.reset(new TestDeviceClient());
107 device_client_->mock_usb_service().AddDevice(mock_device_);
110 protected:
111 scoped_refptr<MockUsbDeviceHandle> mock_device_handle_;
112 scoped_refptr<MockUsbDevice> mock_device_;
113 scoped_ptr<TestDeviceClient> device_client_;
116 } // namespace
118 IN_PROC_BROWSER_TEST_F(UsbApiTest, DeviceHandling) {
119 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(2);
120 ASSERT_TRUE(RunAppTest("api_test/usb/device_handling"));
123 IN_PROC_BROWSER_TEST_F(UsbApiTest, ResetDevice) {
124 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(2);
125 EXPECT_CALL(*mock_device_handle_.get(), ResetDevice(_))
126 .WillOnce(InvokeCallback<0>(true))
127 .WillOnce(InvokeCallback<0>(false));
128 EXPECT_CALL(*mock_device_handle_.get(),
129 GenericTransfer(device::USB_DIRECTION_OUTBOUND, 2, _, 1, _, _))
130 .WillOnce(InvokeUsbTransferCallback<5>(device::USB_TRANSFER_COMPLETED));
131 ASSERT_TRUE(RunAppTest("api_test/usb/reset_device"));
134 IN_PROC_BROWSER_TEST_F(UsbApiTest, SetConfiguration) {
135 UsbConfigDescriptor config_descriptor;
136 EXPECT_CALL(*mock_device_handle_.get(), SetConfiguration(1, _))
137 .WillOnce(InvokeCallback<1>(true));
138 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(1);
139 EXPECT_CALL(*mock_device_.get(), GetActiveConfiguration())
140 .WillOnce(Return(nullptr))
141 .WillOnce(Return(&config_descriptor));
142 ASSERT_TRUE(RunAppTest("api_test/usb/set_configuration"));
145 IN_PROC_BROWSER_TEST_F(UsbApiTest, ListInterfaces) {
146 UsbConfigDescriptor config_descriptor;
147 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(1);
148 EXPECT_CALL(*mock_device_.get(), GetActiveConfiguration())
149 .WillOnce(Return(&config_descriptor));
150 ASSERT_TRUE(RunAppTest("api_test/usb/list_interfaces"));
153 IN_PROC_BROWSER_TEST_F(UsbApiTest, TransferEvent) {
154 EXPECT_CALL(*mock_device_handle_.get(),
155 ControlTransfer(device::USB_DIRECTION_OUTBOUND,
156 UsbDeviceHandle::STANDARD,
157 UsbDeviceHandle::DEVICE,
165 .WillOnce(InvokeUsbTransferCallback<9>(device::USB_TRANSFER_COMPLETED));
166 EXPECT_CALL(*mock_device_handle_.get(),
167 GenericTransfer(device::USB_DIRECTION_OUTBOUND, 1, _, 1, _, _))
168 .WillOnce(InvokeUsbTransferCallback<5>(device::USB_TRANSFER_COMPLETED));
169 EXPECT_CALL(*mock_device_handle_.get(),
170 GenericTransfer(device::USB_DIRECTION_OUTBOUND, 2, _, 1, _, _))
171 .WillOnce(InvokeUsbTransferCallback<5>(device::USB_TRANSFER_COMPLETED));
172 EXPECT_CALL(
173 *mock_device_handle_.get(),
174 IsochronousTransfer(device::USB_DIRECTION_OUTBOUND, 3, _, 1, 1, 1, _, _))
175 .WillOnce(InvokeUsbTransferCallback<7>(device::USB_TRANSFER_COMPLETED));
176 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(AnyNumber());
177 ASSERT_TRUE(RunAppTest("api_test/usb/transfer_event"));
180 IN_PROC_BROWSER_TEST_F(UsbApiTest, ZeroLengthTransfer) {
181 EXPECT_CALL(*mock_device_handle_.get(), GenericTransfer(_, _, _, 0, _, _))
182 .WillOnce(InvokeUsbTransferCallback<5>(device::USB_TRANSFER_COMPLETED));
183 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(AnyNumber());
184 ASSERT_TRUE(RunAppTest("api_test/usb/zero_length_transfer"));
187 IN_PROC_BROWSER_TEST_F(UsbApiTest, TransferFailure) {
188 EXPECT_CALL(*mock_device_handle_.get(), GenericTransfer(_, _, _, _, _, _))
189 .WillOnce(InvokeUsbTransferCallback<5>(device::USB_TRANSFER_COMPLETED))
190 .WillOnce(InvokeUsbTransferCallback<5>(device::USB_TRANSFER_ERROR))
191 .WillOnce(InvokeUsbTransferCallback<5>(device::USB_TRANSFER_TIMEOUT));
192 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(AnyNumber());
193 ASSERT_TRUE(RunAppTest("api_test/usb/transfer_failure"));
196 IN_PROC_BROWSER_TEST_F(UsbApiTest, InvalidLengthTransfer) {
197 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(AnyNumber());
198 ASSERT_TRUE(RunAppTest("api_test/usb/invalid_length_transfer"));
201 IN_PROC_BROWSER_TEST_F(UsbApiTest, InvalidTimeout) {
202 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(AnyNumber());
203 ASSERT_TRUE(RunAppTest("api_test/usb/invalid_timeout"));
206 IN_PROC_BROWSER_TEST_F(UsbApiTest, OnDeviceAdded) {
207 ExtensionTestMessageListener load_listener("loaded", false);
208 ExtensionTestMessageListener result_listener("success", false);
209 result_listener.set_failure_message("failure");
211 ASSERT_TRUE(LoadApp("api_test/usb/add_event"));
212 ASSERT_TRUE(load_listener.WaitUntilSatisfied());
214 scoped_refptr<MockUsbDevice> device(new MockUsbDevice(0x18D1, 0x58F0));
215 device_client_->mock_usb_service().AddDevice(device);
217 device = new MockUsbDevice(0x18D1, 0x58F1);
218 device_client_->mock_usb_service().AddDevice(device);
220 ASSERT_TRUE(result_listener.WaitUntilSatisfied());
223 IN_PROC_BROWSER_TEST_F(UsbApiTest, OnDeviceRemoved) {
224 ExtensionTestMessageListener load_listener("loaded", false);
225 ExtensionTestMessageListener result_listener("success", false);
226 result_listener.set_failure_message("failure");
228 ASSERT_TRUE(LoadApp("api_test/usb/remove_event"));
229 ASSERT_TRUE(load_listener.WaitUntilSatisfied());
231 device_client_->mock_usb_service().RemoveDevice(mock_device_);
232 ASSERT_TRUE(result_listener.WaitUntilSatisfied());
235 IN_PROC_BROWSER_TEST_F(UsbApiTest, GetUserSelectedDevices) {
236 ExtensionTestMessageListener ready_listener("opened_device", false);
237 ExtensionTestMessageListener result_listener("success", false);
238 result_listener.set_failure_message("failure");
240 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(1);
242 TestExtensionsAPIClient test_api_client;
243 ASSERT_TRUE(LoadApp("api_test/usb/get_user_selected_devices"));
244 ASSERT_TRUE(ready_listener.WaitUntilSatisfied());
246 device_client_->mock_usb_service().RemoveDevice(mock_device_);
247 ASSERT_TRUE(result_listener.WaitUntilSatisfied());
250 } // namespace extensions