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/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "base/run_loop.h" |
8 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
9 #include "base/test/test_io_thread.h" | 11 #include "base/test/test_io_thread.h" |
10 #include "device/test/test_device_client.h" | 12 #include "device/test/test_device_client.h" |
11 #include "device/test/usb_test_gadget.h" | 13 #include "device/test/usb_test_gadget.h" |
12 #include "device/usb/usb_device.h" | 14 #include "device/usb/usb_device.h" |
13 #include "device/usb/usb_device_handle.h" | 15 #include "device/usb/usb_service.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
15 | 17 |
16 namespace device { | 18 namespace device { |
17 | 19 |
18 namespace { | 20 namespace { |
19 | 21 |
20 class UsbServiceTest : public ::testing::Test { | 22 class UsbServiceTest : public ::testing::Test { |
21 public: | 23 public: |
22 void SetUp() override { | 24 void SetUp() override { |
23 message_loop_.reset(new base::MessageLoopForUI); | 25 message_loop_.reset(new base::MessageLoopForUI); |
24 io_thread_.reset(new base::TestIOThread(base::TestIOThread::kAutoStart)); | 26 io_thread_.reset(new base::TestIOThread(base::TestIOThread::kAutoStart)); |
25 device_client_.reset(new TestDeviceClient(io_thread_->task_runner())); | 27 device_client_.reset(new TestDeviceClient(io_thread_->task_runner())); |
26 } | 28 } |
27 | 29 |
28 protected: | 30 protected: |
29 std::unique_ptr<base::MessageLoop> message_loop_; | 31 std::unique_ptr<base::MessageLoop> message_loop_; |
30 std::unique_ptr<base::TestIOThread> io_thread_; | 32 std::unique_ptr<base::TestIOThread> io_thread_; |
31 std::unique_ptr<TestDeviceClient> device_client_; | 33 std::unique_ptr<TestDeviceClient> device_client_; |
32 }; | 34 }; |
33 | 35 |
| 36 void OnGetDevices(const base::Closure& quit_closure, |
| 37 const std::vector<scoped_refptr<UsbDevice>>& devices) { |
| 38 quit_closure.Run(); |
| 39 } |
| 40 |
| 41 TEST_F(UsbServiceTest, GetDevices) { |
| 42 // Since there's no guarantee that any devices are connected at the moment |
| 43 // this test doesn't assume anything about the result but it at least verifies |
| 44 // that devices can be enumerated without the application crashing. |
| 45 UsbService* service = device_client_->GetUsbService(); |
| 46 if (service) { |
| 47 base::RunLoop loop; |
| 48 service->GetDevices(base::Bind(&OnGetDevices, loop.QuitClosure())); |
| 49 loop.Run(); |
| 50 } |
| 51 } |
| 52 |
34 TEST_F(UsbServiceTest, ClaimGadget) { | 53 TEST_F(UsbServiceTest, ClaimGadget) { |
35 if (!UsbTestGadget::IsTestEnabled()) return; | 54 if (!UsbTestGadget::IsTestEnabled()) return; |
36 | 55 |
37 std::unique_ptr<UsbTestGadget> gadget = | 56 std::unique_ptr<UsbTestGadget> gadget = |
38 UsbTestGadget::Claim(io_thread_->task_runner()); | 57 UsbTestGadget::Claim(io_thread_->task_runner()); |
39 ASSERT_TRUE(gadget.get()); | 58 ASSERT_TRUE(gadget.get()); |
40 | 59 |
41 scoped_refptr<UsbDevice> device = gadget->GetDevice(); | 60 scoped_refptr<UsbDevice> device = gadget->GetDevice(); |
42 ASSERT_EQ("Google Inc.", base::UTF16ToUTF8(device->manufacturer_string())); | 61 ASSERT_EQ("Google Inc.", base::UTF16ToUTF8(device->manufacturer_string())); |
43 ASSERT_EQ("Test Gadget (default state)", | 62 ASSERT_EQ("Test Gadget (default state)", |
44 base::UTF16ToUTF8(device->product_string())); | 63 base::UTF16ToUTF8(device->product_string())); |
45 } | 64 } |
46 | 65 |
47 TEST_F(UsbServiceTest, DisconnectAndReconnect) { | 66 TEST_F(UsbServiceTest, DisconnectAndReconnect) { |
48 if (!UsbTestGadget::IsTestEnabled()) return; | 67 if (!UsbTestGadget::IsTestEnabled()) return; |
49 | 68 |
50 std::unique_ptr<UsbTestGadget> gadget = | 69 std::unique_ptr<UsbTestGadget> gadget = |
51 UsbTestGadget::Claim(io_thread_->task_runner()); | 70 UsbTestGadget::Claim(io_thread_->task_runner()); |
52 ASSERT_TRUE(gadget.get()); | 71 ASSERT_TRUE(gadget.get()); |
53 ASSERT_TRUE(gadget->Disconnect()); | 72 ASSERT_TRUE(gadget->Disconnect()); |
54 ASSERT_TRUE(gadget->Reconnect()); | 73 ASSERT_TRUE(gadget->Reconnect()); |
55 } | 74 } |
56 | 75 |
57 } // namespace | 76 } // namespace |
58 | 77 |
59 } // namespace device | 78 } // namespace device |
OLD | NEW |