| 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" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| 11 #include "base/test/scoped_feature_list.h" | 11 #include "base/test/scoped_feature_list.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_service.h" | 18 #include "device/usb/usb_service.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 20 |
| 20 namespace device { | 21 namespace device { |
| 21 | 22 |
| 22 namespace { | 23 namespace { |
| 23 | 24 |
| 24 class UsbServiceTest : public ::testing::Test { | 25 class UsbServiceTest : public ::testing::Test { |
| 25 public: | 26 public: |
| 26 void SetUp() override { | 27 void SetUp() override { |
| 27 message_loop_.reset(new base::MessageLoopForUI); | 28 message_loop_.reset(new base::MessageLoopForUI); |
| 28 io_thread_.reset(new base::TestIOThread(base::TestIOThread::kAutoStart)); | 29 io_thread_.reset(new base::TestIOThread(base::TestIOThread::kAutoStart)); |
| 29 device_client_.reset(new TestDeviceClient(io_thread_->task_runner())); | 30 device_client_.reset(new TestDeviceClient(io_thread_->task_runner())); |
| 30 } | 31 } |
| 31 | 32 |
| 32 protected: | 33 protected: |
| 33 std::unique_ptr<base::MessageLoop> message_loop_; | 34 std::unique_ptr<base::MessageLoop> message_loop_; |
| 34 std::unique_ptr<base::TestIOThread> io_thread_; | 35 std::unique_ptr<base::TestIOThread> io_thread_; |
| 35 std::unique_ptr<TestDeviceClient> device_client_; | 36 std::unique_ptr<TestDeviceClient> device_client_; |
| 36 }; | 37 }; |
| 37 | 38 |
| 38 void OnGetDevices(const base::Closure& quit_closure, | 39 void OnGetDevices(const base::Closure& quit_closure, |
| 39 const std::vector<scoped_refptr<UsbDevice>>& devices) { | 40 const std::vector<scoped_refptr<UsbDevice>>& devices) { |
| 40 quit_closure.Run(); | 41 quit_closure.Run(); |
| 41 } | 42 } |
| 42 | 43 |
| 44 void OnOpen(scoped_refptr<UsbDeviceHandle>* output, |
| 45 const base::Closure& quit_closure, |
| 46 scoped_refptr<UsbDeviceHandle> input) { |
| 47 *output = input; |
| 48 quit_closure.Run(); |
| 49 } |
| 50 |
| 43 TEST_F(UsbServiceTest, GetDevices) { | 51 TEST_F(UsbServiceTest, GetDevices) { |
| 44 // Since there's no guarantee that any devices are connected at the moment | 52 // Since there's no guarantee that any devices are connected at the moment |
| 45 // this test doesn't assume anything about the result but it at least verifies | 53 // this test doesn't assume anything about the result but it at least verifies |
| 46 // that devices can be enumerated without the application crashing. | 54 // that devices can be enumerated without the application crashing. |
| 47 UsbService* service = device_client_->GetUsbService(); | 55 UsbService* service = device_client_->GetUsbService(); |
| 48 if (service) { | 56 if (service) { |
| 49 base::RunLoop loop; | 57 base::RunLoop loop; |
| 50 service->GetDevices(base::Bind(&OnGetDevices, loop.QuitClosure())); | 58 service->GetDevices(base::Bind(&OnGetDevices, loop.QuitClosure())); |
| 51 loop.Run(); | 59 loop.Run(); |
| 52 } | 60 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 63 loop.Run(); | 71 loop.Run(); |
| 64 } | 72 } |
| 65 } | 73 } |
| 66 #endif // defined(OS_WIN) | 74 #endif // defined(OS_WIN) |
| 67 | 75 |
| 68 TEST_F(UsbServiceTest, ClaimGadget) { | 76 TEST_F(UsbServiceTest, ClaimGadget) { |
| 69 if (!UsbTestGadget::IsTestEnabled()) return; | 77 if (!UsbTestGadget::IsTestEnabled()) return; |
| 70 | 78 |
| 71 std::unique_ptr<UsbTestGadget> gadget = | 79 std::unique_ptr<UsbTestGadget> gadget = |
| 72 UsbTestGadget::Claim(io_thread_->task_runner()); | 80 UsbTestGadget::Claim(io_thread_->task_runner()); |
| 73 ASSERT_TRUE(gadget.get()); | 81 ASSERT_TRUE(gadget); |
| 74 | 82 |
| 75 scoped_refptr<UsbDevice> device = gadget->GetDevice(); | 83 scoped_refptr<UsbDevice> device = gadget->GetDevice(); |
| 76 ASSERT_EQ("Google Inc.", base::UTF16ToUTF8(device->manufacturer_string())); | 84 ASSERT_EQ("Google Inc.", base::UTF16ToUTF8(device->manufacturer_string())); |
| 77 ASSERT_EQ("Test Gadget (default state)", | 85 ASSERT_EQ("Test Gadget (default state)", |
| 78 base::UTF16ToUTF8(device->product_string())); | 86 base::UTF16ToUTF8(device->product_string())); |
| 79 } | 87 } |
| 80 | 88 |
| 81 TEST_F(UsbServiceTest, DisconnectAndReconnect) { | 89 TEST_F(UsbServiceTest, DisconnectAndReconnect) { |
| 82 if (!UsbTestGadget::IsTestEnabled()) return; | 90 if (!UsbTestGadget::IsTestEnabled()) return; |
| 83 | 91 |
| 84 std::unique_ptr<UsbTestGadget> gadget = | 92 std::unique_ptr<UsbTestGadget> gadget = |
| 85 UsbTestGadget::Claim(io_thread_->task_runner()); | 93 UsbTestGadget::Claim(io_thread_->task_runner()); |
| 86 ASSERT_TRUE(gadget.get()); | 94 ASSERT_TRUE(gadget); |
| 87 ASSERT_TRUE(gadget->Disconnect()); | 95 ASSERT_TRUE(gadget->Disconnect()); |
| 88 ASSERT_TRUE(gadget->Reconnect()); | 96 ASSERT_TRUE(gadget->Reconnect()); |
| 89 } | 97 } |
| 90 | 98 |
| 99 TEST_F(UsbServiceTest, Shutdown) { |
| 100 if (!UsbTestGadget::IsTestEnabled()) |
| 101 return; |
| 102 |
| 103 std::unique_ptr<UsbTestGadget> gadget = |
| 104 UsbTestGadget::Claim(io_thread_->task_runner()); |
| 105 ASSERT_TRUE(gadget); |
| 106 |
| 107 base::RunLoop loop; |
| 108 scoped_refptr<UsbDeviceHandle> device_handle; |
| 109 gadget->GetDevice()->Open( |
| 110 base::Bind(&OnOpen, &device_handle, loop.QuitClosure())); |
| 111 loop.Run(); |
| 112 ASSERT_TRUE(device_handle); |
| 113 |
| 114 // Shut down the USB service while the device handle is still open. |
| 115 device_client_.reset(); |
| 116 EXPECT_FALSE(device_handle->GetDevice()); |
| 117 } |
| 118 |
| 91 } // namespace | 119 } // namespace |
| 92 | 120 |
| 93 } // namespace device | 121 } // namespace device |
| OLD | NEW |