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 <memory> | 5 #include <memory> |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/message_loop/message_loop.h" | |
9 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
10 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
11 #include "base/test/scoped_feature_list.h" | 10 #include "base/test/scoped_feature_list.h" |
| 11 #include "base/test/scoped_task_environment.h" |
12 #include "base/test/test_io_thread.h" | 12 #include "base/test/test_io_thread.h" |
13 #include "device/base/features.h" | 13 #include "device/base/features.h" |
14 #include "device/test/test_device_client.h" | 14 #include "device/test/test_device_client.h" |
15 #include "device/test/usb_test_gadget.h" | 15 #include "device/test/usb_test_gadget.h" |
16 #include "device/usb/usb_device.h" | 16 #include "device/usb/usb_device.h" |
17 #include "device/usb/usb_device_handle.h" | 17 #include "device/usb/usb_device_handle.h" |
18 #include "device/usb/usb_service.h" | 18 #include "device/usb/usb_service.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
20 | 20 |
21 namespace device { | 21 namespace device { |
22 | 22 |
23 namespace { | 23 namespace { |
24 | 24 |
25 class UsbServiceTest : public ::testing::Test { | 25 class UsbServiceTest : public ::testing::Test { |
26 public: | 26 public: |
| 27 UsbServiceTest() |
| 28 : scoped_task_environment_( |
| 29 base::test::ScopedTaskEnvironment::MainThreadType::UI), |
| 30 io_thread_(base::TestIOThread::kAutoStart) {} |
| 31 |
27 void SetUp() override { | 32 void SetUp() override { |
28 message_loop_.reset(new base::MessageLoopForUI); | 33 device_client_.reset(new TestDeviceClient(io_thread_.task_runner())); |
29 io_thread_.reset(new base::TestIOThread(base::TestIOThread::kAutoStart)); | |
30 device_client_.reset(new TestDeviceClient(io_thread_->task_runner())); | |
31 } | 34 } |
32 | 35 |
33 protected: | 36 protected: |
34 std::unique_ptr<base::MessageLoop> message_loop_; | 37 base::test::ScopedTaskEnvironment scoped_task_environment_; |
35 std::unique_ptr<base::TestIOThread> io_thread_; | 38 base::TestIOThread io_thread_; |
36 std::unique_ptr<TestDeviceClient> device_client_; | 39 std::unique_ptr<TestDeviceClient> device_client_; |
37 }; | 40 }; |
38 | 41 |
39 void OnGetDevices(const base::Closure& quit_closure, | 42 void OnGetDevices(const base::Closure& quit_closure, |
40 const std::vector<scoped_refptr<UsbDevice>>& devices) { | 43 const std::vector<scoped_refptr<UsbDevice>>& devices) { |
41 quit_closure.Run(); | 44 quit_closure.Run(); |
42 } | 45 } |
43 | 46 |
44 void OnOpen(scoped_refptr<UsbDeviceHandle>* output, | 47 void OnOpen(scoped_refptr<UsbDeviceHandle>* output, |
45 const base::Closure& quit_closure, | 48 const base::Closure& quit_closure, |
(...skipping 24 matching lines...) Expand all Loading... |
70 service->GetDevices(base::Bind(&OnGetDevices, loop.QuitClosure())); | 73 service->GetDevices(base::Bind(&OnGetDevices, loop.QuitClosure())); |
71 loop.Run(); | 74 loop.Run(); |
72 } | 75 } |
73 } | 76 } |
74 #endif // defined(OS_WIN) | 77 #endif // defined(OS_WIN) |
75 | 78 |
76 TEST_F(UsbServiceTest, ClaimGadget) { | 79 TEST_F(UsbServiceTest, ClaimGadget) { |
77 if (!UsbTestGadget::IsTestEnabled()) return; | 80 if (!UsbTestGadget::IsTestEnabled()) return; |
78 | 81 |
79 std::unique_ptr<UsbTestGadget> gadget = | 82 std::unique_ptr<UsbTestGadget> gadget = |
80 UsbTestGadget::Claim(io_thread_->task_runner()); | 83 UsbTestGadget::Claim(io_thread_.task_runner()); |
81 ASSERT_TRUE(gadget); | 84 ASSERT_TRUE(gadget); |
82 | 85 |
83 scoped_refptr<UsbDevice> device = gadget->GetDevice(); | 86 scoped_refptr<UsbDevice> device = gadget->GetDevice(); |
84 ASSERT_EQ("Google Inc.", base::UTF16ToUTF8(device->manufacturer_string())); | 87 ASSERT_EQ("Google Inc.", base::UTF16ToUTF8(device->manufacturer_string())); |
85 ASSERT_EQ("Test Gadget (default state)", | 88 ASSERT_EQ("Test Gadget (default state)", |
86 base::UTF16ToUTF8(device->product_string())); | 89 base::UTF16ToUTF8(device->product_string())); |
87 } | 90 } |
88 | 91 |
89 TEST_F(UsbServiceTest, DisconnectAndReconnect) { | 92 TEST_F(UsbServiceTest, DisconnectAndReconnect) { |
90 if (!UsbTestGadget::IsTestEnabled()) return; | 93 if (!UsbTestGadget::IsTestEnabled()) return; |
91 | 94 |
92 std::unique_ptr<UsbTestGadget> gadget = | 95 std::unique_ptr<UsbTestGadget> gadget = |
93 UsbTestGadget::Claim(io_thread_->task_runner()); | 96 UsbTestGadget::Claim(io_thread_.task_runner()); |
94 ASSERT_TRUE(gadget); | 97 ASSERT_TRUE(gadget); |
95 ASSERT_TRUE(gadget->Disconnect()); | 98 ASSERT_TRUE(gadget->Disconnect()); |
96 ASSERT_TRUE(gadget->Reconnect()); | 99 ASSERT_TRUE(gadget->Reconnect()); |
97 } | 100 } |
98 | 101 |
99 TEST_F(UsbServiceTest, Shutdown) { | 102 TEST_F(UsbServiceTest, Shutdown) { |
100 if (!UsbTestGadget::IsTestEnabled()) | 103 if (!UsbTestGadget::IsTestEnabled()) |
101 return; | 104 return; |
102 | 105 |
103 std::unique_ptr<UsbTestGadget> gadget = | 106 std::unique_ptr<UsbTestGadget> gadget = |
104 UsbTestGadget::Claim(io_thread_->task_runner()); | 107 UsbTestGadget::Claim(io_thread_.task_runner()); |
105 ASSERT_TRUE(gadget); | 108 ASSERT_TRUE(gadget); |
106 | 109 |
107 base::RunLoop loop; | 110 base::RunLoop loop; |
108 scoped_refptr<UsbDeviceHandle> device_handle; | 111 scoped_refptr<UsbDeviceHandle> device_handle; |
109 gadget->GetDevice()->Open( | 112 gadget->GetDevice()->Open( |
110 base::Bind(&OnOpen, &device_handle, loop.QuitClosure())); | 113 base::Bind(&OnOpen, &device_handle, loop.QuitClosure())); |
111 loop.Run(); | 114 loop.Run(); |
112 ASSERT_TRUE(device_handle); | 115 ASSERT_TRUE(device_handle); |
113 | 116 |
114 // Shut down the USB service while the device handle is still open. | 117 // Shut down the USB service while the device handle is still open. |
115 device_client_.reset(); | 118 device_client_.reset(); |
116 EXPECT_FALSE(device_handle->GetDevice()); | 119 EXPECT_FALSE(device_handle->GetDevice()); |
117 } | 120 } |
118 | 121 |
119 } // namespace | 122 } // namespace |
120 | 123 |
121 } // namespace device | 124 } // namespace device |
OLD | NEW |