gamepad_test_helpers.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright (c) 2012 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. #ifndef DEVICE_GAMEPAD_GAMEPAD_TEST_HELPERS_H_
  5. #define DEVICE_GAMEPAD_GAMEPAD_TEST_HELPERS_H_
  6. #include "base/memory/raw_ptr.h"
  7. #include "base/synchronization/lock.h"
  8. #include "base/synchronization/waitable_event.h"
  9. #include "base/test/task_environment.h"
  10. #include "device/gamepad/gamepad_data_fetcher.h"
  11. #include "device/gamepad/gamepad_service.h"
  12. #include "device/gamepad/gamepad_shared_buffer.h"
  13. #include "device/gamepad/public/cpp/gamepads.h"
  14. namespace device {
  15. // Data fetcher that returns canned data for the gamepad provider.
  16. class MockGamepadDataFetcher : public GamepadDataFetcher {
  17. public:
  18. // Initializes the fetcher with the given gamepad data, which will be
  19. // returned when the provider queries us.
  20. explicit MockGamepadDataFetcher(const Gamepads& test_data);
  21. MockGamepadDataFetcher(const MockGamepadDataFetcher&) = delete;
  22. MockGamepadDataFetcher& operator=(const MockGamepadDataFetcher&) = delete;
  23. ~MockGamepadDataFetcher() override;
  24. GamepadSource source() override;
  25. // GamepadDataFetcher.
  26. void GetGamepadData(bool devices_changed_hint) override;
  27. // Blocks the current thread until the GamepadProvider reads from this
  28. // fetcher on the background thread.
  29. void WaitForDataRead();
  30. // Blocks the current thread until the GamepadProvider reads from this
  31. // fetcher on the background thread and issued all callbacks related to the
  32. // read on the client's thread.
  33. void WaitForDataReadAndCallbacksIssued();
  34. // Updates the test data.
  35. void SetTestData(const Gamepads& new_data);
  36. private:
  37. base::Lock lock_;
  38. Gamepads test_data_;
  39. base::WaitableEvent read_data_;
  40. };
  41. // Base class for the other test helpers. This just sets up the system monitor.
  42. class GamepadTestHelper {
  43. public:
  44. GamepadTestHelper();
  45. GamepadTestHelper(const GamepadTestHelper&) = delete;
  46. GamepadTestHelper& operator=(const GamepadTestHelper&) = delete;
  47. virtual ~GamepadTestHelper();
  48. private:
  49. // This must be constructed before the system monitor.
  50. base::test::SingleThreadTaskEnvironment task_environment_;
  51. };
  52. // Constructs a GamepadService with a mock data source. This bypasses the
  53. // global singleton for the gamepad service.
  54. class GamepadServiceTestConstructor : public GamepadTestHelper {
  55. public:
  56. explicit GamepadServiceTestConstructor(const Gamepads& test_data);
  57. GamepadServiceTestConstructor(const GamepadServiceTestConstructor&) = delete;
  58. GamepadServiceTestConstructor& operator=(
  59. const GamepadServiceTestConstructor&) = delete;
  60. ~GamepadServiceTestConstructor() override;
  61. GamepadService* gamepad_service() { return gamepad_service_; }
  62. MockGamepadDataFetcher* data_fetcher() { return data_fetcher_; }
  63. private:
  64. // Owning pointer (can't be a scoped_ptr due to private destructor).
  65. raw_ptr<GamepadService> gamepad_service_;
  66. // Pointer owned by the provider (which is owned by the gamepad service).
  67. raw_ptr<MockGamepadDataFetcher> data_fetcher_;
  68. };
  69. } // namespace device
  70. #endif // DEVICE_GAMEPAD_GAMEPAD_TEST_HELPERS_H_