| 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/usb_service.h" |
| 9 #include "extensions/browser/api/device_permissions_prompt.h" |
| 9 #include "extensions/browser/api/usb/usb_api.h" | 10 #include "extensions/browser/api/usb/usb_api.h" |
| 11 #include "extensions/shell/browser/shell_extensions_api_client.h" |
| 10 #include "extensions/shell/test/shell_apitest.h" | 12 #include "extensions/shell/test/shell_apitest.h" |
| 11 #include "extensions/test/extension_test_message_listener.h" | 13 #include "extensions/test/extension_test_message_listener.h" |
| 12 #include "net/base/io_buffer.h" | 14 #include "net/base/io_buffer.h" |
| 13 #include "testing/gmock/include/gmock/gmock.h" | 15 #include "testing/gmock/include/gmock/gmock.h" |
| 14 | 16 |
| 15 using testing::_; | 17 using testing::_; |
| 16 using testing::AnyNumber; | 18 using testing::AnyNumber; |
| 17 using testing::Invoke; | 19 using testing::Invoke; |
| 18 using testing::Return; | 20 using testing::Return; |
| 19 using content::BrowserThread; | 21 using content::BrowserThread; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 35 net::IOBuffer* io_buffer = new net::IOBuffer(1); | 37 net::IOBuffer* io_buffer = new net::IOBuffer(1); |
| 36 memset(io_buffer->data(), 0, 1); // Avoid uninitialized reads. | 38 memset(io_buffer->data(), 0, 1); // Avoid uninitialized reads. |
| 37 ::std::tr1::get<k>(args).Run(p1, io_buffer, 1); | 39 ::std::tr1::get<k>(args).Run(p1, io_buffer, 1); |
| 38 } | 40 } |
| 39 | 41 |
| 40 void RequestUsbAccess(int interface_id, | 42 void RequestUsbAccess(int interface_id, |
| 41 const base::Callback<void(bool success)>& callback) { | 43 const base::Callback<void(bool success)>& callback) { |
| 42 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(callback, true)); | 44 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(callback, true)); |
| 43 } | 45 } |
| 44 | 46 |
| 47 class TestDevicePermissionsPrompt |
| 48 : public DevicePermissionsPrompt, |
| 49 public DevicePermissionsPrompt::Prompt::Observer { |
| 50 public: |
| 51 TestDevicePermissionsPrompt(content::WebContents* web_contents) |
| 52 : DevicePermissionsPrompt(web_contents) {} |
| 53 |
| 54 void ShowDialog() override { prompt()->SetObserver(this); } |
| 55 |
| 56 void OnDevicesChanged() override { |
| 57 std::vector<scoped_refptr<UsbDevice>> devices; |
| 58 for (size_t i = 0; i < prompt()->GetDeviceCount(); ++i) { |
| 59 prompt()->GrantDevicePermission(i); |
| 60 devices.push_back(prompt()->GetDevice(i)); |
| 61 if (!prompt()->multiple()) { |
| 62 break; |
| 63 } |
| 64 } |
| 65 delegate()->OnUsbDevicesChosen(devices); |
| 66 } |
| 67 }; |
| 68 |
| 69 class TestExtensionsAPIClient : public ShellExtensionsAPIClient { |
| 70 public: |
| 71 TestExtensionsAPIClient() : ShellExtensionsAPIClient() {} |
| 72 |
| 73 scoped_ptr<DevicePermissionsPrompt> CreateDevicePermissionsPrompt( |
| 74 content::WebContents* web_contents) const override { |
| 75 return make_scoped_ptr(new TestDevicePermissionsPrompt(web_contents)); |
| 76 } |
| 77 }; |
| 78 |
| 45 class MockUsbDeviceHandle : public UsbDeviceHandle { | 79 class MockUsbDeviceHandle : public UsbDeviceHandle { |
| 46 public: | 80 public: |
| 47 MockUsbDeviceHandle() : UsbDeviceHandle() {} | 81 MockUsbDeviceHandle() : UsbDeviceHandle() {} |
| 48 | 82 |
| 49 MOCK_METHOD0(Close, void()); | 83 MOCK_METHOD0(Close, void()); |
| 50 | 84 |
| 51 MOCK_METHOD10(ControlTransfer, | 85 MOCK_METHOD10(ControlTransfer, |
| 52 void(UsbEndpointDirection direction, | 86 void(UsbEndpointDirection direction, |
| 53 TransferRequestType request_type, | 87 TransferRequestType request_type, |
| 54 TransferRecipient recipient, | 88 TransferRecipient recipient, |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 | 184 |
| 151 scoped_refptr<UsbDevice> device_; | 185 scoped_refptr<UsbDevice> device_; |
| 152 }; | 186 }; |
| 153 | 187 |
| 154 class UsbApiTest : public ShellApiTest { | 188 class UsbApiTest : public ShellApiTest { |
| 155 public: | 189 public: |
| 156 void SetUpOnMainThread() override { | 190 void SetUpOnMainThread() override { |
| 157 ShellApiTest::SetUpOnMainThread(); | 191 ShellApiTest::SetUpOnMainThread(); |
| 158 | 192 |
| 159 mock_device_ = new MockUsbDevice(0, 0, 0); | 193 mock_device_ = new MockUsbDevice(0, 0, 0); |
| 194 EXPECT_CALL(*mock_device_.get(), GetManufacturer(_)) |
| 195 .WillRepeatedly(Return(false)); |
| 196 EXPECT_CALL(*mock_device_.get(), GetProduct(_)) |
| 197 .WillRepeatedly(Return(false)); |
| 160 EXPECT_CALL(*mock_device_.get(), GetSerialNumber(_)) | 198 EXPECT_CALL(*mock_device_.get(), GetSerialNumber(_)) |
| 161 .WillRepeatedly(Return(false)); | 199 .WillRepeatedly(Return(false)); |
| 162 | 200 |
| 163 mock_device_handle_ = new MockUsbDeviceHandle(); | 201 mock_device_handle_ = new MockUsbDeviceHandle(); |
| 164 mock_device_handle_->set_device(mock_device_.get()); | 202 mock_device_handle_->set_device(mock_device_.get()); |
| 165 EXPECT_CALL(*mock_device_.get(), RequestUsbAccess(_, _)) | 203 EXPECT_CALL(*mock_device_.get(), RequestUsbAccess(_, _)) |
| 166 .WillRepeatedly(Invoke(RequestUsbAccess)); | 204 .WillRepeatedly(Invoke(RequestUsbAccess)); |
| 167 EXPECT_CALL(*mock_device_.get(), Open()) | 205 EXPECT_CALL(*mock_device_.get(), Open()) |
| 168 .WillRepeatedly(Return(mock_device_handle_)); | 206 .WillRepeatedly(Return(mock_device_handle_)); |
| 169 | 207 |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 291 ASSERT_TRUE(load_listener.WaitUntilSatisfied()); | 329 ASSERT_TRUE(load_listener.WaitUntilSatisfied()); |
| 292 | 330 |
| 293 base::RunLoop run_loop; | 331 base::RunLoop run_loop; |
| 294 BrowserThread::PostTaskAndReply( | 332 BrowserThread::PostTaskAndReply( |
| 295 BrowserThread::FILE, FROM_HERE, | 333 BrowserThread::FILE, FROM_HERE, |
| 296 base::Bind(&UsbApiTest::AddTestDevices, base::Unretained(this)), | 334 base::Bind(&UsbApiTest::AddTestDevices, base::Unretained(this)), |
| 297 run_loop.QuitClosure()); | 335 run_loop.QuitClosure()); |
| 298 run_loop.Run(); | 336 run_loop.Run(); |
| 299 | 337 |
| 300 ASSERT_TRUE(result_listener.WaitUntilSatisfied()); | 338 ASSERT_TRUE(result_listener.WaitUntilSatisfied()); |
| 301 ASSERT_EQ("success", result_listener.message()); | |
| 302 } | 339 } |
| 303 | 340 |
| 304 IN_PROC_BROWSER_TEST_F(UsbApiTest, OnDeviceRemoved) { | 341 IN_PROC_BROWSER_TEST_F(UsbApiTest, OnDeviceRemoved) { |
| 305 ExtensionTestMessageListener load_listener("loaded", false); | 342 ExtensionTestMessageListener load_listener("loaded", false); |
| 306 ExtensionTestMessageListener result_listener("success", false); | 343 ExtensionTestMessageListener result_listener("success", false); |
| 307 result_listener.set_failure_message("failure"); | 344 result_listener.set_failure_message("failure"); |
| 308 | 345 |
| 309 ASSERT_TRUE(LoadApp("api_test/usb/remove_event")); | 346 ASSERT_TRUE(LoadApp("api_test/usb/remove_event")); |
| 310 ASSERT_TRUE(load_listener.WaitUntilSatisfied()); | 347 ASSERT_TRUE(load_listener.WaitUntilSatisfied()); |
| 311 | 348 |
| 312 base::RunLoop run_loop; | 349 base::RunLoop run_loop; |
| 313 BrowserThread::PostTaskAndReply( | 350 BrowserThread::PostTaskAndReply( |
| 314 BrowserThread::FILE, FROM_HERE, | 351 BrowserThread::FILE, FROM_HERE, |
| 315 base::Bind(&MockUsbService::NotifyDeviceRemoved, | 352 base::Bind(&MockUsbService::NotifyDeviceRemoved, |
| 316 base::Unretained(mock_service_), mock_device_), | 353 base::Unretained(mock_service_), mock_device_), |
| 317 run_loop.QuitClosure()); | 354 run_loop.QuitClosure()); |
| 318 run_loop.Run(); | 355 run_loop.Run(); |
| 319 | 356 |
| 320 ASSERT_TRUE(result_listener.WaitUntilSatisfied()); | 357 ASSERT_TRUE(result_listener.WaitUntilSatisfied()); |
| 321 ASSERT_EQ("success", result_listener.message()); | 358 } |
| 359 |
| 360 IN_PROC_BROWSER_TEST_F(UsbApiTest, GetUserSelectedDevices) { |
| 361 ExtensionTestMessageListener ready_listener("opened_device", false); |
| 362 ExtensionTestMessageListener result_listener("success", false); |
| 363 result_listener.set_failure_message("failure"); |
| 364 |
| 365 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(1); |
| 366 |
| 367 TestExtensionsAPIClient test_api_client; |
| 368 ASSERT_TRUE(LoadApp("api_test/usb/get_user_selected_devices")); |
| 369 ASSERT_TRUE(ready_listener.WaitUntilSatisfied()); |
| 370 |
| 371 base::RunLoop run_loop; |
| 372 BrowserThread::PostTaskAndReply( |
| 373 BrowserThread::FILE, FROM_HERE, |
| 374 base::Bind(&MockUsbService::NotifyDeviceRemoved, |
| 375 base::Unretained(mock_service_), mock_device_), |
| 376 run_loop.QuitClosure()); |
| 377 run_loop.Run(); |
| 378 |
| 379 ASSERT_TRUE(result_listener.WaitUntilSatisfied()); |
| 322 } | 380 } |
| 323 | 381 |
| 324 } // namespace extensions | 382 } // namespace extensions |
| OLD | NEW |