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" |
| 6 |
5 #include <stddef.h> | 7 #include <stddef.h> |
6 | 8 |
| 9 #include <memory> |
| 10 |
7 #include "base/bind.h" | 11 #include "base/bind.h" |
8 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
9 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
10 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
11 #include "base/test/test_io_thread.h" | 15 #include "base/test/test_io_thread.h" |
12 #include "device/test/test_device_client.h" | 16 #include "device/test/test_device_client.h" |
13 #include "device/test/usb_test_gadget.h" | 17 #include "device/test/usb_test_gadget.h" |
14 #include "device/usb/usb_device.h" | 18 #include "device/usb/usb_device.h" |
15 #include "device/usb/usb_device_handle.h" | |
16 #include "net/base/io_buffer.h" | 19 #include "net/base/io_buffer.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
18 | 21 |
19 namespace device { | 22 namespace device { |
20 | 23 |
21 namespace { | 24 namespace { |
22 | 25 |
23 class UsbDeviceHandleTest : public ::testing::Test { | 26 class UsbDeviceHandleTest : public ::testing::Test { |
24 public: | 27 public: |
25 void SetUp() override { | 28 void SetUp() override { |
26 message_loop_.reset(new base::MessageLoopForUI); | 29 message_loop_.reset(new base::MessageLoopForUI); |
27 io_thread_.reset(new base::TestIOThread(base::TestIOThread::kAutoStart)); | 30 io_thread_.reset(new base::TestIOThread(base::TestIOThread::kAutoStart)); |
28 device_client_.reset(new TestDeviceClient(io_thread_->task_runner())); | 31 device_client_.reset(new TestDeviceClient(io_thread_->task_runner())); |
29 } | 32 } |
30 | 33 |
31 protected: | 34 protected: |
32 scoped_ptr<base::TestIOThread> io_thread_; | 35 std::unique_ptr<base::TestIOThread> io_thread_; |
33 | 36 |
34 private: | 37 private: |
35 scoped_ptr<base::MessageLoop> message_loop_; | 38 std::unique_ptr<base::MessageLoop> message_loop_; |
36 scoped_ptr<TestDeviceClient> device_client_; | 39 std::unique_ptr<TestDeviceClient> device_client_; |
37 }; | 40 }; |
38 | 41 |
39 class TestOpenCallback { | 42 class TestOpenCallback { |
40 public: | 43 public: |
41 TestOpenCallback() | 44 TestOpenCallback() |
42 : callback_( | 45 : callback_( |
43 base::Bind(&TestOpenCallback::SetResult, base::Unretained(this))) {} | 46 base::Bind(&TestOpenCallback::SetResult, base::Unretained(this))) {} |
44 | 47 |
45 scoped_refptr<UsbDeviceHandle> WaitForResult() { | 48 scoped_refptr<UsbDeviceHandle> WaitForResult() { |
46 run_loop_.Run(); | 49 run_loop_.Run(); |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 base::RunLoop run_loop_; | 114 base::RunLoop run_loop_; |
112 UsbTransferStatus status_; | 115 UsbTransferStatus status_; |
113 size_t transferred_; | 116 size_t transferred_; |
114 }; | 117 }; |
115 | 118 |
116 TEST_F(UsbDeviceHandleTest, InterruptTransfer) { | 119 TEST_F(UsbDeviceHandleTest, InterruptTransfer) { |
117 if (!UsbTestGadget::IsTestEnabled()) { | 120 if (!UsbTestGadget::IsTestEnabled()) { |
118 return; | 121 return; |
119 } | 122 } |
120 | 123 |
121 scoped_ptr<UsbTestGadget> gadget = | 124 std::unique_ptr<UsbTestGadget> gadget = |
122 UsbTestGadget::Claim(io_thread_->task_runner()); | 125 UsbTestGadget::Claim(io_thread_->task_runner()); |
123 ASSERT_TRUE(gadget.get()); | 126 ASSERT_TRUE(gadget.get()); |
124 ASSERT_TRUE(gadget->SetType(UsbTestGadget::ECHO)); | 127 ASSERT_TRUE(gadget->SetType(UsbTestGadget::ECHO)); |
125 | 128 |
126 TestOpenCallback open_device; | 129 TestOpenCallback open_device; |
127 gadget->GetDevice()->Open(open_device.callback()); | 130 gadget->GetDevice()->Open(open_device.callback()); |
128 scoped_refptr<UsbDeviceHandle> handle = open_device.WaitForResult(); | 131 scoped_refptr<UsbDeviceHandle> handle = open_device.WaitForResult(); |
129 ASSERT_TRUE(handle.get()); | 132 ASSERT_TRUE(handle.get()); |
130 | 133 |
131 TestResultCallback claim_interface; | 134 TestResultCallback claim_interface; |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 ASSERT_TRUE(release_interface.WaitForResult()); | 182 ASSERT_TRUE(release_interface.WaitForResult()); |
180 | 183 |
181 handle->Close(); | 184 handle->Close(); |
182 } | 185 } |
183 | 186 |
184 TEST_F(UsbDeviceHandleTest, BulkTransfer) { | 187 TEST_F(UsbDeviceHandleTest, BulkTransfer) { |
185 if (!UsbTestGadget::IsTestEnabled()) { | 188 if (!UsbTestGadget::IsTestEnabled()) { |
186 return; | 189 return; |
187 } | 190 } |
188 | 191 |
189 scoped_ptr<UsbTestGadget> gadget = | 192 std::unique_ptr<UsbTestGadget> gadget = |
190 UsbTestGadget::Claim(io_thread_->task_runner()); | 193 UsbTestGadget::Claim(io_thread_->task_runner()); |
191 ASSERT_TRUE(gadget.get()); | 194 ASSERT_TRUE(gadget.get()); |
192 ASSERT_TRUE(gadget->SetType(UsbTestGadget::ECHO)); | 195 ASSERT_TRUE(gadget->SetType(UsbTestGadget::ECHO)); |
193 | 196 |
194 TestOpenCallback open_device; | 197 TestOpenCallback open_device; |
195 gadget->GetDevice()->Open(open_device.callback()); | 198 gadget->GetDevice()->Open(open_device.callback()); |
196 scoped_refptr<UsbDeviceHandle> handle = open_device.WaitForResult(); | 199 scoped_refptr<UsbDeviceHandle> handle = open_device.WaitForResult(); |
197 ASSERT_TRUE(handle.get()); | 200 ASSERT_TRUE(handle.get()); |
198 | 201 |
199 TestResultCallback claim_interface; | 202 TestResultCallback claim_interface; |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
248 ASSERT_TRUE(release_interface.WaitForResult()); | 251 ASSERT_TRUE(release_interface.WaitForResult()); |
249 | 252 |
250 handle->Close(); | 253 handle->Close(); |
251 } | 254 } |
252 | 255 |
253 TEST_F(UsbDeviceHandleTest, SetInterfaceAlternateSetting) { | 256 TEST_F(UsbDeviceHandleTest, SetInterfaceAlternateSetting) { |
254 if (!UsbTestGadget::IsTestEnabled()) { | 257 if (!UsbTestGadget::IsTestEnabled()) { |
255 return; | 258 return; |
256 } | 259 } |
257 | 260 |
258 scoped_ptr<UsbTestGadget> gadget = | 261 std::unique_ptr<UsbTestGadget> gadget = |
259 UsbTestGadget::Claim(io_thread_->task_runner()); | 262 UsbTestGadget::Claim(io_thread_->task_runner()); |
260 ASSERT_TRUE(gadget.get()); | 263 ASSERT_TRUE(gadget.get()); |
261 ASSERT_TRUE(gadget->SetType(UsbTestGadget::ECHO)); | 264 ASSERT_TRUE(gadget->SetType(UsbTestGadget::ECHO)); |
262 | 265 |
263 TestOpenCallback open_device; | 266 TestOpenCallback open_device; |
264 gadget->GetDevice()->Open(open_device.callback()); | 267 gadget->GetDevice()->Open(open_device.callback()); |
265 scoped_refptr<UsbDeviceHandle> handle = open_device.WaitForResult(); | 268 scoped_refptr<UsbDeviceHandle> handle = open_device.WaitForResult(); |
266 ASSERT_TRUE(handle.get()); | 269 ASSERT_TRUE(handle.get()); |
267 | 270 |
268 TestResultCallback claim_interface; | 271 TestResultCallback claim_interface; |
269 handle->ClaimInterface(2, claim_interface.callback()); | 272 handle->ClaimInterface(2, claim_interface.callback()); |
270 ASSERT_TRUE(claim_interface.WaitForResult()); | 273 ASSERT_TRUE(claim_interface.WaitForResult()); |
271 | 274 |
272 TestResultCallback set_interface; | 275 TestResultCallback set_interface; |
273 handle->SetInterfaceAlternateSetting(2, 1, set_interface.callback()); | 276 handle->SetInterfaceAlternateSetting(2, 1, set_interface.callback()); |
274 ASSERT_TRUE(set_interface.WaitForResult()); | 277 ASSERT_TRUE(set_interface.WaitForResult()); |
275 | 278 |
276 TestResultCallback release_interface; | 279 TestResultCallback release_interface; |
277 handle->ReleaseInterface(2, release_interface.callback()); | 280 handle->ReleaseInterface(2, release_interface.callback()); |
278 ASSERT_TRUE(release_interface.WaitForResult()); | 281 ASSERT_TRUE(release_interface.WaitForResult()); |
279 | 282 |
280 handle->Close(); | 283 handle->Close(); |
281 } | 284 } |
282 | 285 |
283 } // namespace | 286 } // namespace |
284 | 287 |
285 } // namespace device | 288 } // namespace device |
OLD | NEW |