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/usb/usb_api.h" | 9 #include "extensions/browser/api/usb/usb_api.h" |
10 #include "extensions/shell/test/shell_apitest.h" | 10 #include "extensions/shell/test/shell_apitest.h" |
| 11 #include "extensions/test/extension_test_message_listener.h" |
11 #include "net/base/io_buffer.h" | 12 #include "net/base/io_buffer.h" |
12 #include "testing/gmock/include/gmock/gmock.h" | 13 #include "testing/gmock/include/gmock/gmock.h" |
13 | 14 |
14 using testing::_; | 15 using testing::_; |
15 using testing::AnyNumber; | 16 using testing::AnyNumber; |
16 using testing::Invoke; | 17 using testing::Invoke; |
17 using testing::Return; | 18 using testing::Return; |
18 using content::BrowserThread; | 19 using content::BrowserThread; |
19 using device::UsbConfigDescriptor; | 20 using device::UsbConfigDescriptor; |
20 using device::UsbDevice; | 21 using device::UsbDevice; |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 MOCK_METHOD1(GetSerialNumber, bool(base::string16*)); | 120 MOCK_METHOD1(GetSerialNumber, bool(base::string16*)); |
120 | 121 |
121 private: | 122 private: |
122 virtual ~MockUsbDevice() {} | 123 virtual ~MockUsbDevice() {} |
123 }; | 124 }; |
124 | 125 |
125 class MockUsbService : public UsbService { | 126 class MockUsbService : public UsbService { |
126 public: | 127 public: |
127 explicit MockUsbService(scoped_refptr<UsbDevice> device) : device_(device) {} | 128 explicit MockUsbService(scoped_refptr<UsbDevice> device) : device_(device) {} |
128 | 129 |
| 130 // Public wrapper around the protected base class method. |
| 131 void NotifyDeviceAdded(scoped_refptr<UsbDevice> device) { |
| 132 UsbService::NotifyDeviceAdded(device); |
| 133 } |
| 134 |
| 135 // Public wrapper around the protected base class method. |
| 136 void NotifyDeviceRemoved(scoped_refptr<UsbDevice> device) { |
| 137 UsbService::NotifyDeviceRemoved(device); |
| 138 } |
| 139 |
129 protected: | 140 protected: |
130 scoped_refptr<UsbDevice> GetDeviceById(uint32 unique_id) override { | 141 scoped_refptr<UsbDevice> GetDeviceById(uint32 unique_id) override { |
131 EXPECT_EQ(unique_id, 0U); | 142 EXPECT_EQ(unique_id, 0U); |
132 return device_; | 143 return device_; |
133 } | 144 } |
134 | 145 |
135 void GetDevices(std::vector<scoped_refptr<UsbDevice>>* devices) override { | 146 void GetDevices(std::vector<scoped_refptr<UsbDevice>>* devices) override { |
136 STLClearObject(devices); | 147 STLClearObject(devices); |
137 devices->push_back(device_); | 148 devices->push_back(device_); |
138 } | 149 } |
139 | 150 |
140 scoped_refptr<UsbDevice> device_; | 151 scoped_refptr<UsbDevice> device_; |
141 }; | 152 }; |
142 | 153 |
143 class UsbApiTest : public ShellApiTest { | 154 class UsbApiTest : public ShellApiTest { |
144 public: | 155 public: |
145 void SetUpOnMainThread() override { | 156 void SetUpOnMainThread() override { |
146 ShellApiTest::SetUpOnMainThread(); | 157 ShellApiTest::SetUpOnMainThread(); |
| 158 |
| 159 mock_device_ = new MockUsbDevice(0, 0, 0); |
| 160 EXPECT_CALL(*mock_device_.get(), GetSerialNumber(_)) |
| 161 .WillRepeatedly(Return(false)); |
| 162 |
147 mock_device_handle_ = new MockUsbDeviceHandle(); | 163 mock_device_handle_ = new MockUsbDeviceHandle(); |
148 mock_device_ = new MockUsbDevice(0, 0, 0); | |
149 mock_device_handle_->set_device(mock_device_.get()); | 164 mock_device_handle_->set_device(mock_device_.get()); |
150 EXPECT_CALL(*mock_device_.get(), RequestUsbAccess(_, _)) | 165 EXPECT_CALL(*mock_device_.get(), RequestUsbAccess(_, _)) |
151 .WillRepeatedly(Invoke(RequestUsbAccess)); | 166 .WillRepeatedly(Invoke(RequestUsbAccess)); |
152 EXPECT_CALL(*mock_device_.get(), Open()) | 167 EXPECT_CALL(*mock_device_.get(), Open()) |
153 .WillRepeatedly(Return(mock_device_handle_)); | 168 .WillRepeatedly(Return(mock_device_handle_)); |
154 | 169 |
155 base::RunLoop run_loop; | 170 base::RunLoop run_loop; |
156 BrowserThread::PostTaskAndReply(BrowserThread::FILE, FROM_HERE, | 171 BrowserThread::PostTaskAndReply(BrowserThread::FILE, FROM_HERE, |
157 base::Bind(&UsbApiTest::SetUpService, this), | 172 base::Bind(&UsbApiTest::SetUpService, this), |
158 run_loop.QuitClosure()); | 173 run_loop.QuitClosure()); |
159 run_loop.Run(); | 174 run_loop.Run(); |
160 } | 175 } |
161 | 176 |
162 void SetUpService() { | 177 void SetUpService() { |
163 UsbService::SetInstanceForTest(new MockUsbService(mock_device_)); | 178 mock_service_ = new MockUsbService(mock_device_); |
| 179 UsbService::SetInstanceForTest(mock_service_); |
164 } | 180 } |
165 | 181 |
166 void TearDownOnMainThread() override { | 182 void AddTestDevices() { |
167 UsbService* service = NULL; | 183 scoped_refptr<MockUsbDevice> device(new MockUsbDevice(0x18D1, 0x58F0, 1)); |
168 base::RunLoop run_loop; | 184 EXPECT_CALL(*device.get(), GetSerialNumber(_)) |
169 BrowserThread::PostTaskAndReply( | 185 .WillRepeatedly(Return(false)); |
170 BrowserThread::FILE, FROM_HERE, | 186 mock_service_->NotifyDeviceAdded(device); |
171 base::Bind(&UsbService::SetInstanceForTest, service), | 187 |
172 run_loop.QuitClosure()); | 188 device = new MockUsbDevice(0x18D1, 0x58F1, 2); |
173 run_loop.Run(); | 189 EXPECT_CALL(*device.get(), GetSerialNumber(_)) |
| 190 .WillRepeatedly(Return(false)); |
| 191 mock_service_->NotifyDeviceAdded(device); |
174 } | 192 } |
175 | 193 |
176 protected: | 194 protected: |
177 scoped_refptr<MockUsbDeviceHandle> mock_device_handle_; | 195 scoped_refptr<MockUsbDeviceHandle> mock_device_handle_; |
178 scoped_refptr<MockUsbDevice> mock_device_; | 196 scoped_refptr<MockUsbDevice> mock_device_; |
| 197 MockUsbService* mock_service_; |
179 }; | 198 }; |
180 | 199 |
181 } // namespace | 200 } // namespace |
182 | 201 |
183 IN_PROC_BROWSER_TEST_F(UsbApiTest, DeviceHandling) { | 202 IN_PROC_BROWSER_TEST_F(UsbApiTest, DeviceHandling) { |
184 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(4); | 203 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(4); |
185 ASSERT_TRUE(RunAppTest("api_test/usb/device_handling")); | 204 ASSERT_TRUE(RunAppTest("api_test/usb/device_handling")); |
186 } | 205 } |
187 | 206 |
188 IN_PROC_BROWSER_TEST_F(UsbApiTest, ResetDevice) { | 207 IN_PROC_BROWSER_TEST_F(UsbApiTest, ResetDevice) { |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
256 .WillOnce(InvokeUsbTransferCallback<5>(device::USB_TRANSFER_TIMEOUT)); | 275 .WillOnce(InvokeUsbTransferCallback<5>(device::USB_TRANSFER_TIMEOUT)); |
257 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(AnyNumber()); | 276 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(AnyNumber()); |
258 ASSERT_TRUE(RunAppTest("api_test/usb/transfer_failure")); | 277 ASSERT_TRUE(RunAppTest("api_test/usb/transfer_failure")); |
259 } | 278 } |
260 | 279 |
261 IN_PROC_BROWSER_TEST_F(UsbApiTest, InvalidLengthTransfer) { | 280 IN_PROC_BROWSER_TEST_F(UsbApiTest, InvalidLengthTransfer) { |
262 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(AnyNumber()); | 281 EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(AnyNumber()); |
263 ASSERT_TRUE(RunAppTest("api_test/usb/invalid_length_transfer")); | 282 ASSERT_TRUE(RunAppTest("api_test/usb/invalid_length_transfer")); |
264 } | 283 } |
265 | 284 |
| 285 IN_PROC_BROWSER_TEST_F(UsbApiTest, OnDeviceAdded) { |
| 286 ExtensionTestMessageListener load_listener("loaded", false); |
| 287 ExtensionTestMessageListener result_listener("success", false); |
| 288 result_listener.set_failure_message("failure"); |
| 289 |
| 290 ASSERT_TRUE(LoadApp("api_test/usb/add_event")); |
| 291 ASSERT_TRUE(load_listener.WaitUntilSatisfied()); |
| 292 |
| 293 base::RunLoop run_loop; |
| 294 BrowserThread::PostTaskAndReply( |
| 295 BrowserThread::FILE, FROM_HERE, |
| 296 base::Bind(&UsbApiTest::AddTestDevices, base::Unretained(this)), |
| 297 run_loop.QuitClosure()); |
| 298 run_loop.Run(); |
| 299 |
| 300 ASSERT_TRUE(result_listener.WaitUntilSatisfied()); |
| 301 ASSERT_EQ("success", result_listener.message()); |
| 302 } |
| 303 |
| 304 IN_PROC_BROWSER_TEST_F(UsbApiTest, OnDeviceRemoved) { |
| 305 ExtensionTestMessageListener load_listener("loaded", false); |
| 306 ExtensionTestMessageListener result_listener("success", false); |
| 307 result_listener.set_failure_message("failure"); |
| 308 |
| 309 ASSERT_TRUE(LoadApp("api_test/usb/remove_event")); |
| 310 ASSERT_TRUE(load_listener.WaitUntilSatisfied()); |
| 311 |
| 312 base::RunLoop run_loop; |
| 313 BrowserThread::PostTaskAndReply( |
| 314 BrowserThread::FILE, FROM_HERE, |
| 315 base::Bind(&MockUsbService::NotifyDeviceRemoved, |
| 316 base::Unretained(mock_service_), mock_device_), |
| 317 run_loop.QuitClosure()); |
| 318 run_loop.Run(); |
| 319 |
| 320 ASSERT_TRUE(result_listener.WaitUntilSatisfied()); |
| 321 ASSERT_EQ("success", result_listener.message()); |
| 322 } |
| 323 |
266 } // namespace extensions | 324 } // namespace extensions |
OLD | NEW |