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/device_permissions_prompt.h" |
10 #include "extensions/browser/api/usb/usb_api.h" | 10 #include "extensions/browser/api/usb/usb_api.h" |
(...skipping 21 matching lines...) Expand all Loading... |
32 namespace { | 32 namespace { |
33 | 33 |
34 ACTION_TEMPLATE(InvokeUsbTransferCallback, | 34 ACTION_TEMPLATE(InvokeUsbTransferCallback, |
35 HAS_1_TEMPLATE_PARAMS(int, k), | 35 HAS_1_TEMPLATE_PARAMS(int, k), |
36 AND_1_VALUE_PARAMS(p1)) { | 36 AND_1_VALUE_PARAMS(p1)) { |
37 net::IOBuffer* io_buffer = new net::IOBuffer(1); | 37 net::IOBuffer* io_buffer = new net::IOBuffer(1); |
38 memset(io_buffer->data(), 0, 1); // Avoid uninitialized reads. | 38 memset(io_buffer->data(), 0, 1); // Avoid uninitialized reads. |
39 ::std::tr1::get<k>(args).Run(p1, io_buffer, 1); | 39 ::std::tr1::get<k>(args).Run(p1, io_buffer, 1); |
40 } | 40 } |
41 | 41 |
42 void RequestUsbAccess(int interface_id, | |
43 const base::Callback<void(bool success)>& callback) { | |
44 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(callback, true)); | |
45 } | |
46 | |
47 class TestDevicePermissionsPrompt | 42 class TestDevicePermissionsPrompt |
48 : public DevicePermissionsPrompt, | 43 : public DevicePermissionsPrompt, |
49 public DevicePermissionsPrompt::Prompt::Observer { | 44 public DevicePermissionsPrompt::Prompt::Observer { |
50 public: | 45 public: |
51 TestDevicePermissionsPrompt(content::WebContents* web_contents) | 46 TestDevicePermissionsPrompt(content::WebContents* web_contents) |
52 : DevicePermissionsPrompt(web_contents) {} | 47 : DevicePermissionsPrompt(web_contents) {} |
53 | 48 |
54 void ShowDialog() override { prompt()->SetObserver(this); } | 49 void ShowDialog() override { prompt()->SetObserver(this); } |
55 | 50 |
56 void OnDevicesChanged() override { | 51 void OnDevicesChanged() override { |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 UsbDevice* device_; | 133 UsbDevice* device_; |
139 | 134 |
140 virtual ~MockUsbDeviceHandle() {} | 135 virtual ~MockUsbDeviceHandle() {} |
141 }; | 136 }; |
142 | 137 |
143 class MockUsbDevice : public UsbDevice { | 138 class MockUsbDevice : public UsbDevice { |
144 public: | 139 public: |
145 MockUsbDevice(uint16 vendor_id, uint16 product_id, uint32 unique_id) | 140 MockUsbDevice(uint16 vendor_id, uint16 product_id, uint32 unique_id) |
146 : UsbDevice(vendor_id, product_id, unique_id) {} | 141 : UsbDevice(vendor_id, product_id, unique_id) {} |
147 | 142 |
148 MOCK_METHOD2(RequestUsbAccess, void(int, const base::Callback<void(bool)>&)); | |
149 MOCK_METHOD0(Open, scoped_refptr<UsbDeviceHandle>()); | 143 MOCK_METHOD0(Open, scoped_refptr<UsbDeviceHandle>()); |
150 MOCK_METHOD1(Close, bool(scoped_refptr<UsbDeviceHandle>)); | 144 MOCK_METHOD1(Close, bool(scoped_refptr<UsbDeviceHandle>)); |
151 MOCK_METHOD0(GetConfiguration, const device::UsbConfigDescriptor*()); | 145 MOCK_METHOD0(GetConfiguration, const device::UsbConfigDescriptor*()); |
152 MOCK_METHOD1(GetManufacturer, bool(base::string16*)); | 146 MOCK_METHOD1(GetManufacturer, bool(base::string16*)); |
153 MOCK_METHOD1(GetProduct, bool(base::string16*)); | 147 MOCK_METHOD1(GetProduct, bool(base::string16*)); |
154 MOCK_METHOD1(GetSerialNumber, bool(base::string16*)); | 148 MOCK_METHOD1(GetSerialNumber, bool(base::string16*)); |
155 | 149 |
156 private: | 150 private: |
157 virtual ~MockUsbDevice() {} | 151 virtual ~MockUsbDevice() {} |
158 }; | 152 }; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 mock_device_ = new MockUsbDevice(0, 0, 0); | 187 mock_device_ = new MockUsbDevice(0, 0, 0); |
194 EXPECT_CALL(*mock_device_.get(), GetManufacturer(_)) | 188 EXPECT_CALL(*mock_device_.get(), GetManufacturer(_)) |
195 .WillRepeatedly(Return(false)); | 189 .WillRepeatedly(Return(false)); |
196 EXPECT_CALL(*mock_device_.get(), GetProduct(_)) | 190 EXPECT_CALL(*mock_device_.get(), GetProduct(_)) |
197 .WillRepeatedly(Return(false)); | 191 .WillRepeatedly(Return(false)); |
198 EXPECT_CALL(*mock_device_.get(), GetSerialNumber(_)) | 192 EXPECT_CALL(*mock_device_.get(), GetSerialNumber(_)) |
199 .WillRepeatedly(Return(false)); | 193 .WillRepeatedly(Return(false)); |
200 | 194 |
201 mock_device_handle_ = new MockUsbDeviceHandle(); | 195 mock_device_handle_ = new MockUsbDeviceHandle(); |
202 mock_device_handle_->set_device(mock_device_.get()); | 196 mock_device_handle_->set_device(mock_device_.get()); |
203 EXPECT_CALL(*mock_device_.get(), RequestUsbAccess(_, _)) | |
204 .WillRepeatedly(Invoke(RequestUsbAccess)); | |
205 EXPECT_CALL(*mock_device_.get(), Open()) | 197 EXPECT_CALL(*mock_device_.get(), Open()) |
206 .WillRepeatedly(Return(mock_device_handle_)); | 198 .WillRepeatedly(Return(mock_device_handle_)); |
207 | 199 |
208 base::RunLoop run_loop; | 200 base::RunLoop run_loop; |
209 BrowserThread::PostTaskAndReply(BrowserThread::FILE, FROM_HERE, | 201 BrowserThread::PostTaskAndReply(BrowserThread::FILE, FROM_HERE, |
210 base::Bind(&UsbApiTest::SetUpService, this), | 202 base::Bind(&UsbApiTest::SetUpService, this), |
211 run_loop.QuitClosure()); | 203 run_loop.QuitClosure()); |
212 run_loop.Run(); | 204 run_loop.Run(); |
213 } | 205 } |
214 | 206 |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
378 BrowserThread::FILE, FROM_HERE, | 370 BrowserThread::FILE, FROM_HERE, |
379 base::Bind(&MockUsbService::NotifyDeviceRemoved, | 371 base::Bind(&MockUsbService::NotifyDeviceRemoved, |
380 base::Unretained(mock_service_), mock_device_), | 372 base::Unretained(mock_service_), mock_device_), |
381 run_loop.QuitClosure()); | 373 run_loop.QuitClosure()); |
382 run_loop.Run(); | 374 run_loop.Run(); |
383 | 375 |
384 ASSERT_TRUE(result_listener.WaitUntilSatisfied()); | 376 ASSERT_TRUE(result_listener.WaitUntilSatisfied()); |
385 } | 377 } |
386 | 378 |
387 } // namespace extensions | 379 } // namespace extensions |
OLD | NEW |