usb_service_unittest.cc 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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
  3. // found in the LICENSE file.
  4. #include <memory>
  5. #include "base/bind.h"
  6. #include "base/run_loop.h"
  7. #include "base/strings/utf_string_conversions.h"
  8. #include "base/test/scoped_feature_list.h"
  9. #include "base/test/task_environment.h"
  10. #include "base/test/test_io_thread.h"
  11. #include "build/build_config.h"
  12. #include "device/base/features.h"
  13. #include "services/device/test/usb_test_gadget.h"
  14. #include "services/device/usb/usb_device.h"
  15. #include "services/device/usb/usb_device_handle.h"
  16. #include "services/device/usb/usb_service.h"
  17. #include "testing/gtest/include/gtest/gtest.h"
  18. namespace device {
  19. namespace {
  20. class UsbServiceTest : public ::testing::Test {
  21. public:
  22. UsbServiceTest()
  23. : task_environment_(base::test::TaskEnvironment::MainThreadType::UI),
  24. usb_service_(UsbService::Create()),
  25. io_thread_(base::TestIOThread::kAutoStart) {}
  26. protected:
  27. base::test::TaskEnvironment task_environment_;
  28. std::unique_ptr<UsbService> usb_service_;
  29. base::TestIOThread io_thread_;
  30. };
  31. void OnGetDevices(base::OnceClosure quit_closure,
  32. const std::vector<scoped_refptr<UsbDevice>>& devices) {
  33. // Since there's no guarantee that any devices are connected at the moment
  34. // this test doesn't assume anything about the result but it at least verifies
  35. // that devices can be enumerated without the application crashing.
  36. std::move(quit_closure).Run();
  37. }
  38. } // namespace
  39. TEST_F(UsbServiceTest, GetDevices) {
  40. // The USB service is not available on all platforms.
  41. if (usb_service_) {
  42. base::RunLoop loop;
  43. usb_service_->GetDevices(base::BindOnce(&OnGetDevices, loop.QuitClosure()));
  44. loop.Run();
  45. }
  46. }
  47. #if BUILDFLAG(IS_MAC)
  48. TEST_F(UsbServiceTest, GetDevicesNewBackend) {
  49. base::test::ScopedFeatureList features;
  50. features.InitAndEnableFeature(device::kNewUsbBackend);
  51. // The USB service is not available on all platforms.
  52. if (usb_service_) {
  53. base::RunLoop loop;
  54. usb_service_->GetDevices(base::BindOnce(&OnGetDevices, loop.QuitClosure()));
  55. loop.Run();
  56. }
  57. }
  58. #endif // BUILDFLAG(IS_MAC)
  59. TEST_F(UsbServiceTest, ClaimGadget) {
  60. if (!UsbTestGadget::IsTestEnabled() || !usb_service_)
  61. return;
  62. std::unique_ptr<UsbTestGadget> gadget =
  63. UsbTestGadget::Claim(usb_service_.get(), io_thread_.task_runner());
  64. ASSERT_TRUE(gadget);
  65. scoped_refptr<UsbDevice> device = gadget->GetDevice();
  66. ASSERT_EQ("Google Inc.", base::UTF16ToUTF8(device->manufacturer_string()));
  67. ASSERT_EQ("Test Gadget (default state)",
  68. base::UTF16ToUTF8(device->product_string()));
  69. }
  70. TEST_F(UsbServiceTest, DisconnectAndReconnect) {
  71. if (!UsbTestGadget::IsTestEnabled() || !usb_service_)
  72. return;
  73. std::unique_ptr<UsbTestGadget> gadget =
  74. UsbTestGadget::Claim(usb_service_.get(), io_thread_.task_runner());
  75. ASSERT_TRUE(gadget);
  76. ASSERT_TRUE(gadget->Disconnect());
  77. ASSERT_TRUE(gadget->Reconnect());
  78. }
  79. } // namespace device