| 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 "content/public/browser/browser_thread.h" | 6 #include "content/public/browser/browser_thread.h" |
| 7 #include "content/public/test/test_utils.h" | 7 #include "content/public/test/test_utils.h" |
| 8 #include "device/usb/usb_service.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" |
| 9 #include "extensions/browser/api/device_permissions_prompt.h" | 11 #include "extensions/browser/api/device_permissions_prompt.h" |
| 10 #include "extensions/browser/api/usb/usb_api.h" | 12 #include "extensions/browser/api/usb/usb_api.h" |
| 11 #include "extensions/shell/browser/shell_extensions_api_client.h" | 13 #include "extensions/shell/browser/shell_extensions_api_client.h" |
| 12 #include "extensions/shell/test/shell_apitest.h" | 14 #include "extensions/shell/test/shell_apitest.h" |
| 13 #include "extensions/test/extension_test_message_listener.h" | 15 #include "extensions/test/extension_test_message_listener.h" |
| 14 #include "net/base/io_buffer.h" | 16 #include "net/base/io_buffer.h" |
| 15 #include "testing/gmock/include/gmock/gmock.h" | |
| 16 | 17 |
| 17 using testing::_; | 18 using testing::_; |
| 18 using testing::AnyNumber; | 19 using testing::AnyNumber; |
| 19 using testing::Invoke; | 20 using testing::Invoke; |
| 20 using testing::Return; | 21 using testing::Return; |
| 21 using content::BrowserThread; | 22 using content::BrowserThread; |
| 23 using device::MockUsbDevice; |
| 24 using device::MockUsbDeviceHandle; |
| 25 using device::MockUsbService; |
| 22 using device::UsbConfigDescriptor; | 26 using device::UsbConfigDescriptor; |
| 23 using device::UsbDevice; | 27 using device::UsbDevice; |
| 24 using device::UsbDeviceHandle; | 28 using device::UsbDeviceHandle; |
| 25 using device::UsbEndpointDirection; | 29 using device::UsbEndpointDirection; |
| 26 using device::UsbInterfaceDescriptor; | 30 using device::UsbInterfaceDescriptor; |
| 27 using device::UsbService; | 31 using device::UsbService; |
| 28 | 32 |
| 29 namespace extensions { | 33 namespace extensions { |
| 30 | 34 |
| 31 namespace { | 35 namespace { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 class TestExtensionsAPIClient : public ShellExtensionsAPIClient { | 71 class TestExtensionsAPIClient : public ShellExtensionsAPIClient { |
| 68 public: | 72 public: |
| 69 TestExtensionsAPIClient() : ShellExtensionsAPIClient() {} | 73 TestExtensionsAPIClient() : ShellExtensionsAPIClient() {} |
| 70 | 74 |
| 71 scoped_ptr<DevicePermissionsPrompt> CreateDevicePermissionsPrompt( | 75 scoped_ptr<DevicePermissionsPrompt> CreateDevicePermissionsPrompt( |
| 72 content::WebContents* web_contents) const override { | 76 content::WebContents* web_contents) const override { |
| 73 return make_scoped_ptr(new TestDevicePermissionsPrompt(web_contents)); | 77 return make_scoped_ptr(new TestDevicePermissionsPrompt(web_contents)); |
| 74 } | 78 } |
| 75 }; | 79 }; |
| 76 | 80 |
| 77 class MockUsbDeviceHandle : public UsbDeviceHandle { | |
| 78 public: | |
| 79 MockUsbDeviceHandle() : UsbDeviceHandle() {} | |
| 80 | |
| 81 MOCK_METHOD0(Close, void()); | |
| 82 | |
| 83 MOCK_METHOD10(ControlTransfer, | |
| 84 void(UsbEndpointDirection direction, | |
| 85 TransferRequestType request_type, | |
| 86 TransferRecipient recipient, | |
| 87 uint8 request, | |
| 88 uint16 value, | |
| 89 uint16 index, | |
| 90 scoped_refptr<net::IOBuffer> buffer, | |
| 91 size_t length, | |
| 92 unsigned int timeout, | |
| 93 const TransferCallback& callback)); | |
| 94 | |
| 95 MOCK_METHOD6(BulkTransfer, | |
| 96 void(UsbEndpointDirection direction, | |
| 97 uint8 endpoint, | |
| 98 scoped_refptr<net::IOBuffer> buffer, | |
| 99 size_t length, | |
| 100 unsigned int timeout, | |
| 101 const TransferCallback& callback)); | |
| 102 | |
| 103 MOCK_METHOD6(InterruptTransfer, | |
| 104 void(UsbEndpointDirection direction, | |
| 105 uint8 endpoint, | |
| 106 scoped_refptr<net::IOBuffer> buffer, | |
| 107 size_t length, | |
| 108 unsigned int timeout, | |
| 109 const TransferCallback& callback)); | |
| 110 | |
| 111 MOCK_METHOD8(IsochronousTransfer, | |
| 112 void(UsbEndpointDirection direction, | |
| 113 uint8 endpoint, | |
| 114 scoped_refptr<net::IOBuffer> buffer, | |
| 115 size_t length, | |
| 116 unsigned int packets, | |
| 117 unsigned int packet_length, | |
| 118 unsigned int timeout, | |
| 119 const TransferCallback& callback)); | |
| 120 | |
| 121 MOCK_METHOD1(ResetDevice, void(const ResultCallback& callback)); | |
| 122 MOCK_METHOD2(GetStringDescriptor, bool(uint8_t, base::string16*)); | |
| 123 MOCK_METHOD2(SetConfiguration, | |
| 124 void(int configuration_value, const ResultCallback& callback)); | |
| 125 MOCK_METHOD2(ClaimInterface, | |
| 126 void(int interface_number, const ResultCallback& callback)); | |
| 127 MOCK_METHOD1(ReleaseInterface, bool(int interface_number)); | |
| 128 MOCK_METHOD3(SetInterfaceAlternateSetting, | |
| 129 void(int interface_number, | |
| 130 int alternate_setting, | |
| 131 const ResultCallback& callback)); | |
| 132 | |
| 133 scoped_refptr<UsbDevice> GetDevice() const override { return device_; } | |
| 134 | |
| 135 void set_device(UsbDevice* device) { device_ = device; } | |
| 136 | |
| 137 protected: | |
| 138 UsbDevice* device_; | |
| 139 | |
| 140 virtual ~MockUsbDeviceHandle() {} | |
| 141 }; | |
| 142 | |
| 143 class MockUsbDevice : public UsbDevice { | |
| 144 public: | |
| 145 MockUsbDevice(uint16 vendor_id, uint16 product_id, uint32 unique_id) | |
| 146 : UsbDevice(vendor_id, | |
| 147 product_id, | |
| 148 unique_id, | |
| 149 base::string16(), | |
| 150 base::string16(), | |
| 151 base::string16()) {} | |
| 152 | |
| 153 MOCK_METHOD1(Open, void(const OpenCallback&)); | |
| 154 MOCK_METHOD1(Close, bool(scoped_refptr<UsbDeviceHandle>)); | |
| 155 MOCK_METHOD0(GetConfiguration, const device::UsbConfigDescriptor*()); | |
| 156 | |
| 157 private: | |
| 158 virtual ~MockUsbDevice() {} | |
| 159 }; | |
| 160 | |
| 161 class MockUsbService : public UsbService { | |
| 162 public: | |
| 163 explicit MockUsbService(scoped_refptr<UsbDevice> device) : device_(device) {} | |
| 164 | |
| 165 // Public wrapper around the protected base class method. | |
| 166 void NotifyDeviceAdded(scoped_refptr<UsbDevice> device) { | |
| 167 UsbService::NotifyDeviceAdded(device); | |
| 168 } | |
| 169 | |
| 170 // Public wrapper around the protected base class method. | |
| 171 void NotifyDeviceRemoved(scoped_refptr<UsbDevice> device) { | |
| 172 UsbService::NotifyDeviceRemoved(device); | |
| 173 } | |
| 174 | |
| 175 protected: | |
| 176 scoped_refptr<UsbDevice> GetDeviceById(uint32 unique_id) override { | |
| 177 EXPECT_EQ(unique_id, 0U); | |
| 178 return device_; | |
| 179 } | |
| 180 | |
| 181 void GetDevices(const GetDevicesCallback& callback) override { | |
| 182 std::vector<scoped_refptr<UsbDevice>> devices; | |
| 183 devices.push_back(device_); | |
| 184 callback.Run(devices); | |
| 185 } | |
| 186 | |
| 187 scoped_refptr<UsbDevice> device_; | |
| 188 }; | |
| 189 | |
| 190 class UsbApiTest : public ShellApiTest { | 81 class UsbApiTest : public ShellApiTest { |
| 191 public: | 82 public: |
| 192 void SetUpOnMainThread() override { | 83 void SetUpOnMainThread() override { |
| 193 ShellApiTest::SetUpOnMainThread(); | 84 ShellApiTest::SetUpOnMainThread(); |
| 194 | 85 |
| 195 mock_device_ = new MockUsbDevice(0, 0, 0); | 86 mock_device_ = new MockUsbDevice(0, 0); |
| 196 mock_device_handle_ = new MockUsbDeviceHandle(); | 87 mock_device_handle_ = new MockUsbDeviceHandle(mock_device_.get()); |
| 197 mock_device_handle_->set_device(mock_device_.get()); | |
| 198 EXPECT_CALL(*mock_device_.get(), Open(_)) | 88 EXPECT_CALL(*mock_device_.get(), Open(_)) |
| 199 .WillRepeatedly(InvokeCallback<0>(mock_device_handle_)); | 89 .WillRepeatedly(InvokeCallback<0>(mock_device_handle_)); |
| 200 mock_service_.reset(new MockUsbService(mock_device_)); | 90 mock_service_.reset(new MockUsbService()); |
| 91 mock_service_->AddDevice(mock_device_); |
| 201 } | 92 } |
| 202 | 93 |
| 203 protected: | 94 protected: |
| 204 scoped_refptr<MockUsbDeviceHandle> mock_device_handle_; | 95 scoped_refptr<MockUsbDeviceHandle> mock_device_handle_; |
| 205 scoped_refptr<MockUsbDevice> mock_device_; | 96 scoped_refptr<MockUsbDevice> mock_device_; |
| 206 scoped_ptr<MockUsbService> mock_service_; | 97 scoped_ptr<MockUsbService> mock_service_; |
| 207 }; | 98 }; |
| 208 | 99 |
| 209 } // namespace | 100 } // namespace |
| 210 | 101 |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 297 } | 188 } |
| 298 | 189 |
| 299 IN_PROC_BROWSER_TEST_F(UsbApiTest, OnDeviceAdded) { | 190 IN_PROC_BROWSER_TEST_F(UsbApiTest, OnDeviceAdded) { |
| 300 ExtensionTestMessageListener load_listener("loaded", false); | 191 ExtensionTestMessageListener load_listener("loaded", false); |
| 301 ExtensionTestMessageListener result_listener("success", false); | 192 ExtensionTestMessageListener result_listener("success", false); |
| 302 result_listener.set_failure_message("failure"); | 193 result_listener.set_failure_message("failure"); |
| 303 | 194 |
| 304 ASSERT_TRUE(LoadApp("api_test/usb/add_event")); | 195 ASSERT_TRUE(LoadApp("api_test/usb/add_event")); |
| 305 ASSERT_TRUE(load_listener.WaitUntilSatisfied()); | 196 ASSERT_TRUE(load_listener.WaitUntilSatisfied()); |
| 306 | 197 |
| 307 scoped_refptr<MockUsbDevice> device(new MockUsbDevice(0x18D1, 0x58F0, 1)); | 198 scoped_refptr<MockUsbDevice> device(new MockUsbDevice(0x18D1, 0x58F0)); |
| 308 mock_service_->NotifyDeviceAdded(device); | 199 mock_service_->AddDevice(device); |
| 309 | 200 |
| 310 device = new MockUsbDevice(0x18D1, 0x58F1, 2); | 201 device = new MockUsbDevice(0x18D1, 0x58F1); |
| 311 mock_service_->NotifyDeviceAdded(device); | 202 mock_service_->AddDevice(device); |
| 312 | 203 |
| 313 ASSERT_TRUE(result_listener.WaitUntilSatisfied()); | 204 ASSERT_TRUE(result_listener.WaitUntilSatisfied()); |
| 314 } | 205 } |
| 315 | 206 |
| 316 IN_PROC_BROWSER_TEST_F(UsbApiTest, OnDeviceRemoved) { | 207 IN_PROC_BROWSER_TEST_F(UsbApiTest, OnDeviceRemoved) { |
| 317 ExtensionTestMessageListener load_listener("loaded", false); | 208 ExtensionTestMessageListener load_listener("loaded", false); |
| 318 ExtensionTestMessageListener result_listener("success", false); | 209 ExtensionTestMessageListener result_listener("success", false); |
| 319 result_listener.set_failure_message("failure"); | 210 result_listener.set_failure_message("failure"); |
| 320 | 211 |
| 321 ASSERT_TRUE(LoadApp("api_test/usb/remove_event")); | 212 ASSERT_TRUE(LoadApp("api_test/usb/remove_event")); |
| 322 ASSERT_TRUE(load_listener.WaitUntilSatisfied()); | 213 ASSERT_TRUE(load_listener.WaitUntilSatisfied()); |
| 323 | 214 |
| 324 mock_service_->NotifyDeviceRemoved(mock_device_); | 215 mock_service_->RemoveDevice(mock_device_); |
| 325 ASSERT_TRUE(result_listener.WaitUntilSatisfied()); | 216 ASSERT_TRUE(result_listener.WaitUntilSatisfied()); |
| 326 } | 217 } |
| 327 | 218 |
| 328 IN_PROC_BROWSER_TEST_F(UsbApiTest, GetUserSelectedDevices) { | 219 IN_PROC_BROWSER_TEST_F(UsbApiTest, GetUserSelectedDevices) { |
| 329 ExtensionTestMessageListener ready_listener("opened_device", false); | 220 ExtensionTestMessageListener ready_listener("opened_device", false); |
| 330 ExtensionTestMessageListener result_listener("success", false); | 221 ExtensionTestMessageListener result_listener("success", false); |
| 331 result_listener.set_failure_message("failure"); | 222 result_listener.set_failure_message("failure"); |
| 332 | 223 |
| 333 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(1); | 224 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(1); |
| 334 | 225 |
| 335 TestExtensionsAPIClient test_api_client; | 226 TestExtensionsAPIClient test_api_client; |
| 336 ASSERT_TRUE(LoadApp("api_test/usb/get_user_selected_devices")); | 227 ASSERT_TRUE(LoadApp("api_test/usb/get_user_selected_devices")); |
| 337 ASSERT_TRUE(ready_listener.WaitUntilSatisfied()); | 228 ASSERT_TRUE(ready_listener.WaitUntilSatisfied()); |
| 338 | 229 |
| 339 mock_service_->NotifyDeviceRemoved(mock_device_); | 230 mock_service_->RemoveDevice(mock_device_); |
| 340 ASSERT_TRUE(result_listener.WaitUntilSatisfied()); | 231 ASSERT_TRUE(result_listener.WaitUntilSatisfied()); |
| 341 } | 232 } |
| 342 | 233 |
| 343 } // namespace extensions | 234 } // namespace extensions |
| OLD | NEW |