GamepadServiceTest.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  4. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef mozilla_dom_GamepadServiceTest_h_
  6. #define mozilla_dom_GamepadServiceTest_h_
  7. #include "nsIIPCBackgroundChildCreateCallback.h"
  8. #include "mozilla/DOMEventTargetHelper.h"
  9. #include "mozilla/dom/GamepadBinding.h"
  10. namespace mozilla {
  11. namespace dom {
  12. class GamepadChangeEvent;
  13. class GamepadManager;
  14. class GamepadTestChannelChild;
  15. class Promise;
  16. // Service for testing purposes
  17. class GamepadServiceTest final : public DOMEventTargetHelper,
  18. public nsIIPCBackgroundChildCreateCallback
  19. {
  20. public:
  21. NS_DECL_NSIIPCBACKGROUNDCHILDCREATECALLBACK
  22. NS_DECL_ISUPPORTS_INHERITED
  23. NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(GamepadServiceTest,
  24. DOMEventTargetHelper)
  25. uint32_t NoMapping() const { return 0; }
  26. uint32_t StandardMapping() const { return 1; }
  27. already_AddRefed<Promise> AddGamepad(const nsAString& aID,
  28. uint32_t aMapping,
  29. uint32_t aNumButtons,
  30. uint32_t aNumAxes,
  31. ErrorResult& aRv);
  32. void RemoveGamepad(uint32_t aIndex);
  33. void NewButtonEvent(uint32_t aIndex, uint32_t aButton, bool aPressed);
  34. void NewButtonValueEvent(uint32_t aIndex, uint32_t aButton, bool aPressed, double aValue);
  35. void NewAxisMoveEvent(uint32_t aIndex, uint32_t aAxis, double aValue);
  36. void Shutdown();
  37. static already_AddRefed<GamepadServiceTest> CreateTestService(nsPIDOMWindowInner* aWindow);
  38. nsPIDOMWindowInner* GetParentObject() const { return mWindow; }
  39. JSObject* WrapObject(JSContext* aCx, JS::HandleObject aGivenProto) override;
  40. private:
  41. // We need to asynchronously create IPDL channel, it is possible that
  42. // we send commands before the channel is created, so we have to buffer
  43. // them until the channel is created in that case.
  44. struct PendingOperation {
  45. explicit PendingOperation(const uint32_t& aID,
  46. const GamepadChangeEvent& aEvent,
  47. Promise* aPromise = nullptr)
  48. : mID(aID), mEvent(aEvent), mPromise(aPromise) {}
  49. uint32_t mID;
  50. const GamepadChangeEvent& mEvent;
  51. RefPtr<Promise> mPromise;
  52. };
  53. // Hold a reference to the gamepad service so we don't have to worry about
  54. // execution order in tests.
  55. RefPtr<GamepadManager> mService;
  56. nsCOMPtr<nsPIDOMWindowInner> mWindow;
  57. nsTArray<PendingOperation> mPendingOperations;
  58. uint32_t mEventNumber;
  59. bool mShuttingDown;
  60. // IPDL Channel for us to send test events to GamepadPlatformService, it
  61. // will only be used in this singleton class and deleted during the IPDL
  62. // shutdown chain
  63. GamepadTestChannelChild* MOZ_NON_OWNING_REF mChild;
  64. explicit GamepadServiceTest(nsPIDOMWindowInner* aWindow);
  65. ~GamepadServiceTest();
  66. void InitPBackgroundActor();
  67. void DestroyPBackgroundActor();
  68. void FlushPendingOperations();
  69. };
  70. } // namespace dom
  71. } // namespace mozilla
  72. #endif