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 "device/usb/usb_device_handle.h" | 5 #include "device/usb/usb_device_handle.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
14 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
| 15 #include "base/test/scoped_task_environment.h" |
15 #include "base/test/test_io_thread.h" | 16 #include "base/test/test_io_thread.h" |
16 #include "device/test/test_device_client.h" | 17 #include "device/test/test_device_client.h" |
17 #include "device/test/usb_test_gadget.h" | 18 #include "device/test/usb_test_gadget.h" |
18 #include "device/usb/usb_device.h" | 19 #include "device/usb/usb_device.h" |
19 #include "net/base/io_buffer.h" | 20 #include "net/base/io_buffer.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
21 | 22 |
22 namespace device { | 23 namespace device { |
23 | 24 |
24 namespace { | 25 namespace { |
25 | 26 |
26 class UsbDeviceHandleTest : public ::testing::Test { | 27 class UsbDeviceHandleTest : public ::testing::Test { |
27 public: | 28 public: |
28 void SetUp() override { | 29 UsbDeviceHandleTest() |
29 message_loop_.reset(new base::MessageLoopForUI); | 30 : io_thread_(base::TestIOThread::kAutoStart), |
30 io_thread_.reset(new base::TestIOThread(base::TestIOThread::kAutoStart)); | 31 scoped_task_environment_( |
31 device_client_.reset(new TestDeviceClient(io_thread_->task_runner())); | 32 base::test::ScopedTaskEnvironment::MainThreadType::UI), |
32 } | 33 device_client_(io_thread_.task_runner()) {} |
33 | 34 |
34 protected: | 35 protected: |
35 std::unique_ptr<base::TestIOThread> io_thread_; | 36 base::TestIOThread io_thread_; |
36 | 37 |
37 private: | 38 private: |
38 std::unique_ptr<base::MessageLoop> message_loop_; | 39 base::test::ScopedTaskEnvironment scoped_task_environment_; |
39 std::unique_ptr<TestDeviceClient> device_client_; | 40 TestDeviceClient device_client_; |
40 }; | 41 }; |
41 | 42 |
42 class TestOpenCallback { | 43 class TestOpenCallback { |
43 public: | 44 public: |
44 TestOpenCallback() | 45 TestOpenCallback() |
45 : callback_( | 46 : callback_( |
46 base::Bind(&TestOpenCallback::SetResult, base::Unretained(this))) {} | 47 base::Bind(&TestOpenCallback::SetResult, base::Unretained(this))) {} |
47 | 48 |
48 scoped_refptr<UsbDeviceHandle> WaitForResult() { | 49 scoped_refptr<UsbDeviceHandle> WaitForResult() { |
49 run_loop_.Run(); | 50 run_loop_.Run(); |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 handle->Close(); | 126 handle->Close(); |
126 quit_closure.Run(); | 127 quit_closure.Run(); |
127 } | 128 } |
128 | 129 |
129 TEST_F(UsbDeviceHandleTest, InterruptTransfer) { | 130 TEST_F(UsbDeviceHandleTest, InterruptTransfer) { |
130 if (!UsbTestGadget::IsTestEnabled()) { | 131 if (!UsbTestGadget::IsTestEnabled()) { |
131 return; | 132 return; |
132 } | 133 } |
133 | 134 |
134 std::unique_ptr<UsbTestGadget> gadget = | 135 std::unique_ptr<UsbTestGadget> gadget = |
135 UsbTestGadget::Claim(io_thread_->task_runner()); | 136 UsbTestGadget::Claim(io_thread_.task_runner()); |
136 ASSERT_TRUE(gadget.get()); | 137 ASSERT_TRUE(gadget.get()); |
137 ASSERT_TRUE(gadget->SetType(UsbTestGadget::ECHO)); | 138 ASSERT_TRUE(gadget->SetType(UsbTestGadget::ECHO)); |
138 | 139 |
139 TestOpenCallback open_device; | 140 TestOpenCallback open_device; |
140 gadget->GetDevice()->Open(open_device.callback()); | 141 gadget->GetDevice()->Open(open_device.callback()); |
141 scoped_refptr<UsbDeviceHandle> handle = open_device.WaitForResult(); | 142 scoped_refptr<UsbDeviceHandle> handle = open_device.WaitForResult(); |
142 ASSERT_TRUE(handle.get()); | 143 ASSERT_TRUE(handle.get()); |
143 | 144 |
144 TestResultCallback claim_interface; | 145 TestResultCallback claim_interface; |
145 handle->ClaimInterface(0, claim_interface.callback()); | 146 handle->ClaimInterface(0, claim_interface.callback()); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 | 194 |
194 handle->Close(); | 195 handle->Close(); |
195 } | 196 } |
196 | 197 |
197 TEST_F(UsbDeviceHandleTest, BulkTransfer) { | 198 TEST_F(UsbDeviceHandleTest, BulkTransfer) { |
198 if (!UsbTestGadget::IsTestEnabled()) { | 199 if (!UsbTestGadget::IsTestEnabled()) { |
199 return; | 200 return; |
200 } | 201 } |
201 | 202 |
202 std::unique_ptr<UsbTestGadget> gadget = | 203 std::unique_ptr<UsbTestGadget> gadget = |
203 UsbTestGadget::Claim(io_thread_->task_runner()); | 204 UsbTestGadget::Claim(io_thread_.task_runner()); |
204 ASSERT_TRUE(gadget.get()); | 205 ASSERT_TRUE(gadget.get()); |
205 ASSERT_TRUE(gadget->SetType(UsbTestGadget::ECHO)); | 206 ASSERT_TRUE(gadget->SetType(UsbTestGadget::ECHO)); |
206 | 207 |
207 TestOpenCallback open_device; | 208 TestOpenCallback open_device; |
208 gadget->GetDevice()->Open(open_device.callback()); | 209 gadget->GetDevice()->Open(open_device.callback()); |
209 scoped_refptr<UsbDeviceHandle> handle = open_device.WaitForResult(); | 210 scoped_refptr<UsbDeviceHandle> handle = open_device.WaitForResult(); |
210 ASSERT_TRUE(handle.get()); | 211 ASSERT_TRUE(handle.get()); |
211 | 212 |
212 TestResultCallback claim_interface; | 213 TestResultCallback claim_interface; |
213 handle->ClaimInterface(1, claim_interface.callback()); | 214 handle->ClaimInterface(1, claim_interface.callback()); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
261 ASSERT_TRUE(release_interface.WaitForResult()); | 262 ASSERT_TRUE(release_interface.WaitForResult()); |
262 | 263 |
263 handle->Close(); | 264 handle->Close(); |
264 } | 265 } |
265 | 266 |
266 TEST_F(UsbDeviceHandleTest, ControlTransfer) { | 267 TEST_F(UsbDeviceHandleTest, ControlTransfer) { |
267 if (!UsbTestGadget::IsTestEnabled()) | 268 if (!UsbTestGadget::IsTestEnabled()) |
268 return; | 269 return; |
269 | 270 |
270 std::unique_ptr<UsbTestGadget> gadget = | 271 std::unique_ptr<UsbTestGadget> gadget = |
271 UsbTestGadget::Claim(io_thread_->task_runner()); | 272 UsbTestGadget::Claim(io_thread_.task_runner()); |
272 ASSERT_TRUE(gadget.get()); | 273 ASSERT_TRUE(gadget.get()); |
273 | 274 |
274 TestOpenCallback open_device; | 275 TestOpenCallback open_device; |
275 gadget->GetDevice()->Open(open_device.callback()); | 276 gadget->GetDevice()->Open(open_device.callback()); |
276 scoped_refptr<UsbDeviceHandle> handle = open_device.WaitForResult(); | 277 scoped_refptr<UsbDeviceHandle> handle = open_device.WaitForResult(); |
277 ASSERT_TRUE(handle.get()); | 278 ASSERT_TRUE(handle.get()); |
278 | 279 |
279 scoped_refptr<net::IOBufferWithSize> buffer(new net::IOBufferWithSize(255)); | 280 scoped_refptr<net::IOBufferWithSize> buffer(new net::IOBufferWithSize(255)); |
280 TestCompletionCallback completion; | 281 TestCompletionCallback completion; |
281 handle->ControlTransfer( | 282 handle->ControlTransfer( |
(...skipping 11 matching lines...) Expand all Loading... |
293 | 294 |
294 handle->Close(); | 295 handle->Close(); |
295 } | 296 } |
296 | 297 |
297 TEST_F(UsbDeviceHandleTest, SetInterfaceAlternateSetting) { | 298 TEST_F(UsbDeviceHandleTest, SetInterfaceAlternateSetting) { |
298 if (!UsbTestGadget::IsTestEnabled()) { | 299 if (!UsbTestGadget::IsTestEnabled()) { |
299 return; | 300 return; |
300 } | 301 } |
301 | 302 |
302 std::unique_ptr<UsbTestGadget> gadget = | 303 std::unique_ptr<UsbTestGadget> gadget = |
303 UsbTestGadget::Claim(io_thread_->task_runner()); | 304 UsbTestGadget::Claim(io_thread_.task_runner()); |
304 ASSERT_TRUE(gadget.get()); | 305 ASSERT_TRUE(gadget.get()); |
305 ASSERT_TRUE(gadget->SetType(UsbTestGadget::ECHO)); | 306 ASSERT_TRUE(gadget->SetType(UsbTestGadget::ECHO)); |
306 | 307 |
307 TestOpenCallback open_device; | 308 TestOpenCallback open_device; |
308 gadget->GetDevice()->Open(open_device.callback()); | 309 gadget->GetDevice()->Open(open_device.callback()); |
309 scoped_refptr<UsbDeviceHandle> handle = open_device.WaitForResult(); | 310 scoped_refptr<UsbDeviceHandle> handle = open_device.WaitForResult(); |
310 ASSERT_TRUE(handle.get()); | 311 ASSERT_TRUE(handle.get()); |
311 | 312 |
312 TestResultCallback claim_interface; | 313 TestResultCallback claim_interface; |
313 handle->ClaimInterface(2, claim_interface.callback()); | 314 handle->ClaimInterface(2, claim_interface.callback()); |
314 ASSERT_TRUE(claim_interface.WaitForResult()); | 315 ASSERT_TRUE(claim_interface.WaitForResult()); |
315 | 316 |
316 TestResultCallback set_interface; | 317 TestResultCallback set_interface; |
317 handle->SetInterfaceAlternateSetting(2, 1, set_interface.callback()); | 318 handle->SetInterfaceAlternateSetting(2, 1, set_interface.callback()); |
318 ASSERT_TRUE(set_interface.WaitForResult()); | 319 ASSERT_TRUE(set_interface.WaitForResult()); |
319 | 320 |
320 TestResultCallback release_interface; | 321 TestResultCallback release_interface; |
321 handle->ReleaseInterface(2, release_interface.callback()); | 322 handle->ReleaseInterface(2, release_interface.callback()); |
322 ASSERT_TRUE(release_interface.WaitForResult()); | 323 ASSERT_TRUE(release_interface.WaitForResult()); |
323 | 324 |
324 handle->Close(); | 325 handle->Close(); |
325 } | 326 } |
326 | 327 |
327 TEST_F(UsbDeviceHandleTest, CancelOnClose) { | 328 TEST_F(UsbDeviceHandleTest, CancelOnClose) { |
328 if (!UsbTestGadget::IsTestEnabled()) { | 329 if (!UsbTestGadget::IsTestEnabled()) { |
329 return; | 330 return; |
330 } | 331 } |
331 | 332 |
332 std::unique_ptr<UsbTestGadget> gadget = | 333 std::unique_ptr<UsbTestGadget> gadget = |
333 UsbTestGadget::Claim(io_thread_->task_runner()); | 334 UsbTestGadget::Claim(io_thread_.task_runner()); |
334 ASSERT_TRUE(gadget.get()); | 335 ASSERT_TRUE(gadget.get()); |
335 ASSERT_TRUE(gadget->SetType(UsbTestGadget::ECHO)); | 336 ASSERT_TRUE(gadget->SetType(UsbTestGadget::ECHO)); |
336 | 337 |
337 TestOpenCallback open_device; | 338 TestOpenCallback open_device; |
338 gadget->GetDevice()->Open(open_device.callback()); | 339 gadget->GetDevice()->Open(open_device.callback()); |
339 scoped_refptr<UsbDeviceHandle> handle = open_device.WaitForResult(); | 340 scoped_refptr<UsbDeviceHandle> handle = open_device.WaitForResult(); |
340 ASSERT_TRUE(handle.get()); | 341 ASSERT_TRUE(handle.get()); |
341 | 342 |
342 TestResultCallback claim_interface; | 343 TestResultCallback claim_interface; |
343 handle->ClaimInterface(1, claim_interface.callback()); | 344 handle->ClaimInterface(1, claim_interface.callback()); |
(...skipping 10 matching lines...) Expand all Loading... |
354 completion.WaitForResult(); | 355 completion.WaitForResult(); |
355 ASSERT_EQ(UsbTransferStatus::CANCELLED, completion.status()); | 356 ASSERT_EQ(UsbTransferStatus::CANCELLED, completion.status()); |
356 } | 357 } |
357 | 358 |
358 TEST_F(UsbDeviceHandleTest, CancelOnDisconnect) { | 359 TEST_F(UsbDeviceHandleTest, CancelOnDisconnect) { |
359 if (!UsbTestGadget::IsTestEnabled()) { | 360 if (!UsbTestGadget::IsTestEnabled()) { |
360 return; | 361 return; |
361 } | 362 } |
362 | 363 |
363 std::unique_ptr<UsbTestGadget> gadget = | 364 std::unique_ptr<UsbTestGadget> gadget = |
364 UsbTestGadget::Claim(io_thread_->task_runner()); | 365 UsbTestGadget::Claim(io_thread_.task_runner()); |
365 ASSERT_TRUE(gadget.get()); | 366 ASSERT_TRUE(gadget.get()); |
366 ASSERT_TRUE(gadget->SetType(UsbTestGadget::ECHO)); | 367 ASSERT_TRUE(gadget->SetType(UsbTestGadget::ECHO)); |
367 | 368 |
368 TestOpenCallback open_device; | 369 TestOpenCallback open_device; |
369 gadget->GetDevice()->Open(open_device.callback()); | 370 gadget->GetDevice()->Open(open_device.callback()); |
370 scoped_refptr<UsbDeviceHandle> handle = open_device.WaitForResult(); | 371 scoped_refptr<UsbDeviceHandle> handle = open_device.WaitForResult(); |
371 ASSERT_TRUE(handle.get()); | 372 ASSERT_TRUE(handle.get()); |
372 | 373 |
373 TestResultCallback claim_interface; | 374 TestResultCallback claim_interface; |
374 handle->ClaimInterface(1, claim_interface.callback()); | 375 handle->ClaimInterface(1, claim_interface.callback()); |
(...skipping 12 matching lines...) Expand all Loading... |
387 | 388 |
388 handle->Close(); | 389 handle->Close(); |
389 } | 390 } |
390 | 391 |
391 TEST_F(UsbDeviceHandleTest, Timeout) { | 392 TEST_F(UsbDeviceHandleTest, Timeout) { |
392 if (!UsbTestGadget::IsTestEnabled()) { | 393 if (!UsbTestGadget::IsTestEnabled()) { |
393 return; | 394 return; |
394 } | 395 } |
395 | 396 |
396 std::unique_ptr<UsbTestGadget> gadget = | 397 std::unique_ptr<UsbTestGadget> gadget = |
397 UsbTestGadget::Claim(io_thread_->task_runner()); | 398 UsbTestGadget::Claim(io_thread_.task_runner()); |
398 ASSERT_TRUE(gadget.get()); | 399 ASSERT_TRUE(gadget.get()); |
399 ASSERT_TRUE(gadget->SetType(UsbTestGadget::ECHO)); | 400 ASSERT_TRUE(gadget->SetType(UsbTestGadget::ECHO)); |
400 | 401 |
401 TestOpenCallback open_device; | 402 TestOpenCallback open_device; |
402 gadget->GetDevice()->Open(open_device.callback()); | 403 gadget->GetDevice()->Open(open_device.callback()); |
403 scoped_refptr<UsbDeviceHandle> handle = open_device.WaitForResult(); | 404 scoped_refptr<UsbDeviceHandle> handle = open_device.WaitForResult(); |
404 ASSERT_TRUE(handle.get()); | 405 ASSERT_TRUE(handle.get()); |
405 | 406 |
406 TestResultCallback claim_interface; | 407 TestResultCallback claim_interface; |
407 handle->ClaimInterface(1, claim_interface.callback()); | 408 handle->ClaimInterface(1, claim_interface.callback()); |
(...skipping 10 matching lines...) Expand all Loading... |
418 ASSERT_EQ(UsbTransferStatus::TIMEOUT, completion.status()); | 419 ASSERT_EQ(UsbTransferStatus::TIMEOUT, completion.status()); |
419 | 420 |
420 handle->Close(); | 421 handle->Close(); |
421 } | 422 } |
422 | 423 |
423 TEST_F(UsbDeviceHandleTest, CloseReentrancy) { | 424 TEST_F(UsbDeviceHandleTest, CloseReentrancy) { |
424 if (!UsbTestGadget::IsTestEnabled()) | 425 if (!UsbTestGadget::IsTestEnabled()) |
425 return; | 426 return; |
426 | 427 |
427 std::unique_ptr<UsbTestGadget> gadget = | 428 std::unique_ptr<UsbTestGadget> gadget = |
428 UsbTestGadget::Claim(io_thread_->task_runner()); | 429 UsbTestGadget::Claim(io_thread_.task_runner()); |
429 ASSERT_TRUE(gadget.get()); | 430 ASSERT_TRUE(gadget.get()); |
430 ASSERT_TRUE(gadget->SetType(UsbTestGadget::ECHO)); | 431 ASSERT_TRUE(gadget->SetType(UsbTestGadget::ECHO)); |
431 | 432 |
432 TestOpenCallback open_device; | 433 TestOpenCallback open_device; |
433 gadget->GetDevice()->Open(open_device.callback()); | 434 gadget->GetDevice()->Open(open_device.callback()); |
434 scoped_refptr<UsbDeviceHandle> handle = open_device.WaitForResult(); | 435 scoped_refptr<UsbDeviceHandle> handle = open_device.WaitForResult(); |
435 ASSERT_TRUE(handle.get()); | 436 ASSERT_TRUE(handle.get()); |
436 | 437 |
437 TestResultCallback claim_interface; | 438 TestResultCallback claim_interface; |
438 handle->ClaimInterface(1, claim_interface.callback()); | 439 handle->ClaimInterface(1, claim_interface.callback()); |
439 ASSERT_TRUE(claim_interface.WaitForResult()); | 440 ASSERT_TRUE(claim_interface.WaitForResult()); |
440 | 441 |
441 base::RunLoop run_loop; | 442 base::RunLoop run_loop; |
442 auto buffer = base::MakeShared<net::IOBufferWithSize>(512); | 443 auto buffer = base::MakeShared<net::IOBufferWithSize>(512); |
443 handle->GenericTransfer( | 444 handle->GenericTransfer( |
444 UsbTransferDirection::INBOUND, 0x82, buffer.get(), buffer->size(), | 445 UsbTransferDirection::INBOUND, 0x82, buffer.get(), buffer->size(), |
445 10, // 10 millisecond timeout | 446 10, // 10 millisecond timeout |
446 base::Bind(&ExpectTimeoutAndClose, handle, run_loop.QuitClosure())); | 447 base::Bind(&ExpectTimeoutAndClose, handle, run_loop.QuitClosure())); |
447 // Drop handle so that the completion callback holds the last reference. | 448 // Drop handle so that the completion callback holds the last reference. |
448 handle = nullptr; | 449 handle = nullptr; |
449 run_loop.Run(); | 450 run_loop.Run(); |
450 } | 451 } |
451 | 452 |
452 } // namespace | 453 } // namespace |
453 | 454 |
454 } // namespace device | 455 } // namespace device |
OLD | NEW |