nintendo_data_fetcher_unittest.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Copyright 2019 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 "device/gamepad/nintendo_data_fetcher.h"
  5. #include <utility>
  6. #include "base/memory/raw_ptr.h"
  7. #include "base/memory/ref_counted_memory.h"
  8. #include "base/memory/scoped_refptr.h"
  9. #include "base/run_loop.h"
  10. #include "base/threading/thread.h"
  11. #include "build/build_config.h"
  12. #include "device/gamepad/gamepad_service.h"
  13. #include "services/device/device_service_test_base.h"
  14. #include "services/device/hid/hid_manager_impl.h"
  15. #include "services/device/public/cpp/test/mock_hid_service.h"
  16. #include "services/device/public/mojom/hid.mojom.h"
  17. #include "testing/gtest/include/gtest/gtest.h"
  18. namespace device {
  19. namespace {
  20. #if BUILDFLAG(IS_MAC)
  21. const uint64_t kTestDeviceId = 123;
  22. #elif BUILDFLAG(IS_WIN)
  23. const wchar_t kTestDeviceId[] = L"123";
  24. #else
  25. const char kTestDeviceId[] = "123";
  26. #endif
  27. const char kPhysicalDeviceId[] = "1";
  28. void BindHidManager(mojom::DeviceService* service,
  29. scoped_refptr<base::SequencedTaskRunner> task_runner,
  30. mojo::PendingReceiver<mojom::HidManager> receiver) {
  31. task_runner->PostTask(
  32. FROM_HERE,
  33. base::BindOnce(&mojom::DeviceService::BindHidManager,
  34. base::Unretained(service), std::move(receiver)));
  35. }
  36. } // namespace
  37. // Main test fixture
  38. class NintendoDataFetcherTest : public DeviceServiceTestBase {
  39. public:
  40. NintendoDataFetcherTest() = default;
  41. NintendoDataFetcherTest(const NintendoDataFetcherTest&) = delete;
  42. NintendoDataFetcherTest& operator=(const NintendoDataFetcherTest&) = delete;
  43. void SetUp() override {
  44. // Set up the fake HID service.
  45. auto mock_hid_service = std::make_unique<MockHidService>();
  46. mock_hid_service_ = mock_hid_service.get();
  47. mock_hid_service_->FirstEnumerationComplete();
  48. // Transfer the ownership of the |mock_hid_service| to HidManagerImpl.
  49. // It is safe to use |mock_hid_service_| in this test.
  50. HidManagerImpl::SetHidServiceForTesting(std::move(mock_hid_service));
  51. // Initialize the device service and pass a HidManager binder to
  52. // GamepadService.
  53. DeviceServiceTestBase::SetUp();
  54. GamepadService::GetInstance()->StartUp(
  55. base::BindRepeating(&BindHidManager, device_service(),
  56. base::SequencedTaskRunnerHandle::Get()));
  57. // Create the data fetcher and polling thread.
  58. auto fetcher = std::make_unique<NintendoDataFetcher>();
  59. fetcher_ = fetcher.get();
  60. auto polling_thread = std::make_unique<base::Thread>("polling thread");
  61. polling_thread_ = polling_thread.get();
  62. provider_ = std::make_unique<GamepadProvider>(
  63. /*connection_change_client=*/nullptr, std::move(fetcher),
  64. std::move(polling_thread));
  65. RunUntilIdle();
  66. }
  67. void TearDown() override {
  68. HidManagerImpl::SetHidServiceForTesting(nullptr);
  69. GamepadService::SetInstance(nullptr);
  70. }
  71. void RunUntilIdle() {
  72. base::RunLoop().RunUntilIdle();
  73. polling_thread_->FlushForTesting();
  74. }
  75. raw_ptr<MockHidService> mock_hid_service_;
  76. std::unique_ptr<GamepadProvider> provider_;
  77. raw_ptr<NintendoDataFetcher> fetcher_;
  78. raw_ptr<base::Thread> polling_thread_;
  79. };
  80. TEST_F(NintendoDataFetcherTest, UnsupportedDeviceIsIgnored) {
  81. // Simulate an unsupported, non-Nintendo HID device.
  82. auto collection = mojom::HidCollectionInfo::New();
  83. collection->usage = mojom::HidUsageAndPage::New(0, 0);
  84. auto device_info = base::MakeRefCounted<HidDeviceInfo>(
  85. kTestDeviceId, kPhysicalDeviceId, "interface id", /*vendor_id=*/0x1234,
  86. /*product_id=*/0xabcd, "Invalipad", /*serial_number=*/"",
  87. mojom::HidBusType::kHIDBusTypeUSB, std::move(collection),
  88. /*max_input_report_size=*/0, /*max_output_report_size=*/0,
  89. /*max_feature_report_size=*/0);
  90. // Add the device to the mock HID service. The HID service should notify the
  91. // data fetcher.
  92. mock_hid_service_->AddDevice(device_info);
  93. RunUntilIdle();
  94. // The device should not have been added to the internal device map.
  95. EXPECT_TRUE(fetcher_->GetControllersForTesting().empty());
  96. // Remove the device.
  97. mock_hid_service_->RemoveDevice(kTestDeviceId);
  98. RunUntilIdle();
  99. }
  100. TEST_F(NintendoDataFetcherTest, AddAndRemoveSwitchPro) {
  101. // Simulate a Switch Pro over USB.
  102. auto collection = mojom::HidCollectionInfo::New();
  103. collection->usage = mojom::HidUsageAndPage::New(0, 0);
  104. auto device_info = base::MakeRefCounted<HidDeviceInfo>(
  105. kTestDeviceId, kPhysicalDeviceId, "interface id", /*vendor_id=*/0x057e,
  106. /*product_id=*/0x2009, "Switch Pro Controller", /*serial_number=*/"",
  107. mojom::HidBusType::kHIDBusTypeUSB, std::move(collection),
  108. /*max_input_report_size=*/0, /*max_output_report_size=*/63,
  109. /*max_feature_report_size=*/0);
  110. // Add the device to the mock HID service. The HID service should notify the
  111. // data fetcher.
  112. mock_hid_service_->AddDevice(device_info);
  113. RunUntilIdle();
  114. // The fetcher should have added the device to its internal device map.
  115. EXPECT_EQ(fetcher_->GetControllersForTesting().size(), 1U);
  116. // Remove the device.
  117. mock_hid_service_->RemoveDevice(kTestDeviceId);
  118. RunUntilIdle();
  119. // Check that the device was removed.
  120. EXPECT_TRUE(fetcher_->GetControllersForTesting().empty());
  121. }
  122. } // namespace device