Finish refactoring of DomCodeToUsLayoutKeyboardCode().
[chromium-blink-merge.git] / extensions / browser / api / usb / usb_apitest.cc
blob0219b9d510ede9f05157514402b52dcb6d806b93
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/usb/mock_usb_device.h"
9 #include "device/usb/mock_usb_device_handle.h"
10 #include "device/usb/mock_usb_service.h"
11 #include "extensions/browser/api/device_permissions_prompt.h"
12 #include "extensions/browser/api/usb/usb_api.h"
13 #include "extensions/shell/browser/shell_extensions_api_client.h"
14 #include "extensions/shell/test/shell_apitest.h"
15 #include "extensions/test/extension_test_message_listener.h"
16 #include "net/base/io_buffer.h"
18 using testing::_;
19 using testing::AnyNumber;
20 using testing::Invoke;
21 using testing::Return;
22 using content::BrowserThread;
23 using device::MockUsbDevice;
24 using device::MockUsbDeviceHandle;
25 using device::MockUsbService;
26 using device::UsbConfigDescriptor;
27 using device::UsbDevice;
28 using device::UsbDeviceHandle;
29 using device::UsbEndpointDirection;
30 using device::UsbInterfaceDescriptor;
31 using device::UsbService;
33 namespace extensions {
35 namespace {
37 ACTION_TEMPLATE(InvokeCallback,
38 HAS_1_TEMPLATE_PARAMS(int, k),
39 AND_1_VALUE_PARAMS(p1)) {
40 ::std::tr1::get<k>(args).Run(p1);
43 ACTION_TEMPLATE(InvokeUsbTransferCallback,
44 HAS_1_TEMPLATE_PARAMS(int, k),
45 AND_1_VALUE_PARAMS(p1)) {
46 net::IOBuffer* io_buffer = new net::IOBuffer(1);
47 memset(io_buffer->data(), 0, 1); // Avoid uninitialized reads.
48 ::std::tr1::get<k>(args).Run(p1, io_buffer, 1);
51 class TestDevicePermissionsPrompt
52 : public DevicePermissionsPrompt,
53 public DevicePermissionsPrompt::Prompt::Observer {
54 public:
55 TestDevicePermissionsPrompt(content::WebContents* web_contents)
56 : DevicePermissionsPrompt(web_contents) {}
58 void ShowDialog() override { prompt()->SetObserver(this); }
60 void OnDevicesChanged() override {
61 for (size_t i = 0; i < prompt()->GetDeviceCount(); ++i) {
62 prompt()->GrantDevicePermission(i);
63 if (!prompt()->multiple()) {
64 break;
67 prompt()->Dismissed();
71 class TestExtensionsAPIClient : public ShellExtensionsAPIClient {
72 public:
73 TestExtensionsAPIClient() : ShellExtensionsAPIClient() {}
75 scoped_ptr<DevicePermissionsPrompt> CreateDevicePermissionsPrompt(
76 content::WebContents* web_contents) const override {
77 return make_scoped_ptr(new TestDevicePermissionsPrompt(web_contents));
81 class UsbApiTest : public ShellApiTest {
82 public:
83 void SetUpOnMainThread() override {
84 ShellApiTest::SetUpOnMainThread();
86 mock_device_ = new MockUsbDevice(0, 0);
87 mock_device_handle_ = new MockUsbDeviceHandle(mock_device_.get());
88 EXPECT_CALL(*mock_device_.get(), Open(_))
89 .WillRepeatedly(InvokeCallback<0>(mock_device_handle_));
90 mock_service_.reset(new MockUsbService());
91 mock_service_->AddDevice(mock_device_);
94 protected:
95 scoped_refptr<MockUsbDeviceHandle> mock_device_handle_;
96 scoped_refptr<MockUsbDevice> mock_device_;
97 scoped_ptr<MockUsbService> mock_service_;
100 } // namespace
102 IN_PROC_BROWSER_TEST_F(UsbApiTest, DeviceHandling) {
103 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(2);
104 ASSERT_TRUE(RunAppTest("api_test/usb/device_handling"));
107 IN_PROC_BROWSER_TEST_F(UsbApiTest, ResetDevice) {
108 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(2);
109 EXPECT_CALL(*mock_device_handle_.get(), ResetDevice(_))
110 .WillOnce(InvokeCallback<0>(true))
111 .WillOnce(InvokeCallback<0>(false));
112 EXPECT_CALL(*mock_device_handle_.get(),
113 InterruptTransfer(device::USB_DIRECTION_OUTBOUND, 2, _, 1, _, _))
114 .WillOnce(InvokeUsbTransferCallback<5>(device::USB_TRANSFER_COMPLETED));
115 ASSERT_TRUE(RunAppTest("api_test/usb/reset_device"));
118 IN_PROC_BROWSER_TEST_F(UsbApiTest, SetConfiguration) {
119 UsbConfigDescriptor config_descriptor;
120 EXPECT_CALL(*mock_device_handle_.get(), SetConfiguration(1, _))
121 .WillOnce(InvokeCallback<1>(true));
122 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(1);
123 EXPECT_CALL(*mock_device_.get(), GetConfiguration())
124 .WillOnce(Return(nullptr))
125 .WillOnce(Return(&config_descriptor));
126 ASSERT_TRUE(RunAppTest("api_test/usb/set_configuration"));
129 IN_PROC_BROWSER_TEST_F(UsbApiTest, ListInterfaces) {
130 UsbConfigDescriptor config_descriptor;
131 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(1);
132 EXPECT_CALL(*mock_device_.get(), GetConfiguration())
133 .WillOnce(Return(&config_descriptor));
134 ASSERT_TRUE(RunAppTest("api_test/usb/list_interfaces"));
137 IN_PROC_BROWSER_TEST_F(UsbApiTest, TransferEvent) {
138 EXPECT_CALL(*mock_device_handle_.get(),
139 ControlTransfer(device::USB_DIRECTION_OUTBOUND,
140 UsbDeviceHandle::STANDARD,
141 UsbDeviceHandle::DEVICE,
149 .WillOnce(InvokeUsbTransferCallback<9>(device::USB_TRANSFER_COMPLETED));
150 EXPECT_CALL(*mock_device_handle_.get(),
151 BulkTransfer(device::USB_DIRECTION_OUTBOUND, 1, _, 1, _, _))
152 .WillOnce(InvokeUsbTransferCallback<5>(device::USB_TRANSFER_COMPLETED));
153 EXPECT_CALL(*mock_device_handle_.get(),
154 InterruptTransfer(device::USB_DIRECTION_OUTBOUND, 2, _, 1, _, _))
155 .WillOnce(InvokeUsbTransferCallback<5>(device::USB_TRANSFER_COMPLETED));
156 EXPECT_CALL(
157 *mock_device_handle_.get(),
158 IsochronousTransfer(device::USB_DIRECTION_OUTBOUND, 3, _, 1, 1, 1, _, _))
159 .WillOnce(InvokeUsbTransferCallback<7>(device::USB_TRANSFER_COMPLETED));
160 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(AnyNumber());
161 ASSERT_TRUE(RunAppTest("api_test/usb/transfer_event"));
164 IN_PROC_BROWSER_TEST_F(UsbApiTest, ZeroLengthTransfer) {
165 EXPECT_CALL(*mock_device_handle_.get(), BulkTransfer(_, _, _, 0, _, _))
166 .WillOnce(InvokeUsbTransferCallback<5>(device::USB_TRANSFER_COMPLETED));
167 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(AnyNumber());
168 ASSERT_TRUE(RunAppTest("api_test/usb/zero_length_transfer"));
171 IN_PROC_BROWSER_TEST_F(UsbApiTest, TransferFailure) {
172 EXPECT_CALL(*mock_device_handle_.get(), BulkTransfer(_, _, _, _, _, _))
173 .WillOnce(InvokeUsbTransferCallback<5>(device::USB_TRANSFER_COMPLETED))
174 .WillOnce(InvokeUsbTransferCallback<5>(device::USB_TRANSFER_ERROR))
175 .WillOnce(InvokeUsbTransferCallback<5>(device::USB_TRANSFER_TIMEOUT));
176 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(AnyNumber());
177 ASSERT_TRUE(RunAppTest("api_test/usb/transfer_failure"));
180 IN_PROC_BROWSER_TEST_F(UsbApiTest, InvalidLengthTransfer) {
181 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(AnyNumber());
182 ASSERT_TRUE(RunAppTest("api_test/usb/invalid_length_transfer"));
185 IN_PROC_BROWSER_TEST_F(UsbApiTest, InvalidTimeout) {
186 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(AnyNumber());
187 ASSERT_TRUE(RunAppTest("api_test/usb/invalid_timeout"));
190 IN_PROC_BROWSER_TEST_F(UsbApiTest, OnDeviceAdded) {
191 ExtensionTestMessageListener load_listener("loaded", false);
192 ExtensionTestMessageListener result_listener("success", false);
193 result_listener.set_failure_message("failure");
195 ASSERT_TRUE(LoadApp("api_test/usb/add_event"));
196 ASSERT_TRUE(load_listener.WaitUntilSatisfied());
198 scoped_refptr<MockUsbDevice> device(new MockUsbDevice(0x18D1, 0x58F0));
199 mock_service_->AddDevice(device);
201 device = new MockUsbDevice(0x18D1, 0x58F1);
202 mock_service_->AddDevice(device);
204 ASSERT_TRUE(result_listener.WaitUntilSatisfied());
207 IN_PROC_BROWSER_TEST_F(UsbApiTest, OnDeviceRemoved) {
208 ExtensionTestMessageListener load_listener("loaded", false);
209 ExtensionTestMessageListener result_listener("success", false);
210 result_listener.set_failure_message("failure");
212 ASSERT_TRUE(LoadApp("api_test/usb/remove_event"));
213 ASSERT_TRUE(load_listener.WaitUntilSatisfied());
215 mock_service_->RemoveDevice(mock_device_);
216 ASSERT_TRUE(result_listener.WaitUntilSatisfied());
219 IN_PROC_BROWSER_TEST_F(UsbApiTest, GetUserSelectedDevices) {
220 ExtensionTestMessageListener ready_listener("opened_device", false);
221 ExtensionTestMessageListener result_listener("success", false);
222 result_listener.set_failure_message("failure");
224 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(1);
226 TestExtensionsAPIClient test_api_client;
227 ASSERT_TRUE(LoadApp("api_test/usb/get_user_selected_devices"));
228 ASSERT_TRUE(ready_listener.WaitUntilSatisfied());
230 mock_service_->RemoveDevice(mock_device_);
231 ASSERT_TRUE(result_listener.WaitUntilSatisfied());
234 } // namespace extensions