gamepad_test_helpers.cc 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. #include "device/gamepad/gamepad_test_helpers.h"
  5. namespace device {
  6. MockGamepadDataFetcher::MockGamepadDataFetcher(const Gamepads& test_data)
  7. : test_data_(test_data),
  8. read_data_(base::WaitableEvent::ResetPolicy::AUTOMATIC,
  9. base::WaitableEvent::InitialState::NOT_SIGNALED) {}
  10. MockGamepadDataFetcher::~MockGamepadDataFetcher() = default;
  11. GamepadSource MockGamepadDataFetcher::source() {
  12. return GAMEPAD_SOURCE_TEST;
  13. }
  14. void MockGamepadDataFetcher::GetGamepadData(bool devices_changed_hint) {
  15. {
  16. base::AutoLock lock(lock_);
  17. for (size_t i = 0; i < Gamepads::kItemsLengthCap; ++i) {
  18. if (test_data_.items[i].connected) {
  19. PadState* pad = GetPadState(i);
  20. if (pad)
  21. memcpy(&pad->data, &test_data_.items[i], sizeof(Gamepad));
  22. }
  23. }
  24. }
  25. read_data_.Signal();
  26. }
  27. void MockGamepadDataFetcher::WaitForDataRead() {
  28. return read_data_.Wait();
  29. }
  30. void MockGamepadDataFetcher::WaitForDataReadAndCallbacksIssued() {
  31. // The provider will read the data on the background thread (setting the
  32. // event) and *then* will issue the callback on the client thread. Waiting for
  33. // it to read twice is a simple way to ensure that it was able to issue
  34. // callbacks for the first read (if it issued one).
  35. WaitForDataRead();
  36. WaitForDataRead();
  37. }
  38. void MockGamepadDataFetcher::SetTestData(const Gamepads& new_data) {
  39. base::AutoLock lock(lock_);
  40. test_data_ = new_data;
  41. }
  42. GamepadTestHelper::GamepadTestHelper() = default;
  43. GamepadTestHelper::~GamepadTestHelper() = default;
  44. GamepadServiceTestConstructor::GamepadServiceTestConstructor(
  45. const Gamepads& test_data) {
  46. data_fetcher_ = new MockGamepadDataFetcher(test_data);
  47. gamepad_service_ =
  48. new GamepadService(std::unique_ptr<GamepadDataFetcher>(data_fetcher_));
  49. }
  50. GamepadServiceTestConstructor::~GamepadServiceTestConstructor() {
  51. delete gamepad_service_;
  52. }
  53. } // namespace device