GamepadServiceTest.h (4640B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef mozilla_dom_GamepadServiceTest_h_ 8 #define mozilla_dom_GamepadServiceTest_h_ 9 10 #include "mozilla/DOMEventTargetHelper.h" 11 #include "mozilla/WeakPtr.h" 12 #include "mozilla/dom/GamepadBinding.h" 13 #include "mozilla/dom/GamepadHandle.h" 14 #include "mozilla/dom/TypedArray.h" 15 16 namespace mozilla::dom { 17 18 class GamepadChangeEvent; 19 class GamepadManager; 20 class GamepadTestChannelChild; 21 class Promise; 22 23 // Service for testing purposes 24 class GamepadServiceTest final : public DOMEventTargetHelper, 25 public SupportsWeakPtr { 26 public: 27 NS_DECL_ISUPPORTS_INHERITED 28 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(GamepadServiceTest, 29 DOMEventTargetHelper) 30 31 GamepadMappingType NoMapping() const { return GamepadMappingType::_empty; } 32 GamepadMappingType StandardMapping() const { 33 return GamepadMappingType::Standard; 34 } 35 GamepadHand NoHand() const { return GamepadHand::_empty; } 36 GamepadHand LeftHand() const { return GamepadHand::Left; } 37 GamepadHand RightHand() const { return GamepadHand::Right; } 38 39 // IPC receiver 40 void ReplyGamepadHandle(uint32_t aPromiseId, const GamepadHandle& aHandle); 41 42 // Methods from GamepadServiceTest.webidl 43 already_AddRefed<Promise> AddGamepad( 44 const nsAString& aID, GamepadMappingType aMapping, GamepadHand aHand, 45 uint32_t aNumButtons, uint32_t aNumAxes, uint32_t aNumHaptics, 46 uint32_t aNumLightIndicator, uint32_t aNumTouchEvents, ErrorResult& aRv); 47 48 already_AddRefed<Promise> RemoveGamepad(uint32_t aHandleSlot, 49 ErrorResult& aRv); 50 51 already_AddRefed<Promise> NewButtonEvent(uint32_t aHandleSlot, 52 uint32_t aButton, bool aPressed, 53 bool aTouched, ErrorResult& aRv); 54 55 already_AddRefed<Promise> NewButtonValueEvent(uint32_t aHandleSlot, 56 uint32_t aButton, bool aPressed, 57 bool aTouched, double aValue, 58 ErrorResult& aRv); 59 60 already_AddRefed<Promise> NewAxisMoveEvent(uint32_t aHandleSlot, 61 uint32_t aAxis, double aValue, 62 ErrorResult& aRv); 63 64 already_AddRefed<Promise> NewPoseMove( 65 uint32_t aHandleSlot, const Nullable<Float32Array>& aOrient, 66 const Nullable<Float32Array>& aPos, 67 const Nullable<Float32Array>& aAngVelocity, 68 const Nullable<Float32Array>& aAngAcceleration, 69 const Nullable<Float32Array>& aLinVelocity, 70 const Nullable<Float32Array>& aLinAcceleration, ErrorResult& aRv); 71 72 already_AddRefed<Promise> NewTouch(uint32_t aHandleSlot, 73 uint32_t aTouchArrayIndex, 74 uint32_t aTouchId, uint8_t aSurfaceId, 75 const Float32Array& aPos, 76 const Nullable<Float32Array>& aSurfDim, 77 ErrorResult& aRv); 78 79 void Shutdown(); 80 81 static already_AddRefed<GamepadServiceTest> CreateTestService( 82 nsPIDOMWindowInner* aWindow); 83 nsPIDOMWindowInner* GetParentObject() const { return mWindow; } 84 JSObject* WrapObject(JSContext* aCx, 85 JS::Handle<JSObject*> aGivenProto) override; 86 87 private: 88 // Hold a reference to the gamepad service so we don't have to worry about 89 // execution order in tests. 90 RefPtr<GamepadManager> mService; 91 nsCOMPtr<nsPIDOMWindowInner> mWindow; 92 uint32_t mEventNumber; 93 bool mShuttingDown; 94 95 // IPDL Channel for us to send test events to GamepadPlatformService, it 96 // will only be used in this singleton class and deleted during the IPDL 97 // shutdown chain 98 RefPtr<GamepadTestChannelChild> mChild; 99 nsTArray<GamepadHandle> mGamepadHandles; 100 101 nsRefPtrHashtable<nsUint32HashKey, dom::Promise> mPromiseList; 102 103 explicit GamepadServiceTest(nsPIDOMWindowInner* aWindow); 104 ~GamepadServiceTest(); 105 void InitPBackgroundActor(); 106 void DestroyPBackgroundActor(); 107 108 uint32_t AddGamepadHandle(GamepadHandle aHandle); 109 void RemoveGamepadHandle(uint32_t aHandleSlot); 110 GamepadHandle GetHandleInSlot(uint32_t aHandleSlot) const; 111 }; 112 113 } // namespace mozilla::dom 114 115 #endif