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/. */
7 #ifndef mozilla_dom_VRServiceTest_h_
8 #define mozilla_dom_VRServiceTest_h_
10 #include "mozilla/DOMEventTargetHelper.h"
11 #include "mozilla/dom/TypedArray.h"
12 #include "mozilla/dom/VRServiceTestBinding.h"
18 enum class VRDisplayCapabilityFlags
: uint16_t;
19 enum class VRPuppet_Command
: uint64_t;
22 enum class GamepadCapabilityFlags
: uint16_t;
24 class VRMockDisplay final
: public DOMEventTargetHelper
{
26 NS_DECL_ISUPPORTS_INHERITED
27 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(VRMockDisplay
, DOMEventTargetHelper
)
29 explicit VRMockDisplay(VRServiceTest
* aVRServiceTest
);
34 void SetConnected(bool aConnected
);
35 bool Connected() const;
36 void SetMounted(bool aMounted
);
38 void SetCapPosition(bool aEnabled
);
39 bool CapPosition() const;
40 void SetCapOrientation(bool aEnabled
);
41 bool CapOrientation() const;
42 void SetCapPresent(bool aEnabled
);
43 bool CapPresent() const;
44 void SetCapExternal(bool aEnabled
);
45 bool CapExternal() const;
46 void SetCapAngularAcceleration(bool aEnabled
);
47 bool CapAngularAcceleration() const;
48 void SetCapLinearAcceleration(bool aEnabled
);
49 bool CapLinearAcceleration() const;
50 void SetCapStageParameters(bool aEnabled
);
51 bool CapStageParameters() const;
52 void SetCapMountDetection(bool aEnabled
);
53 bool CapMountDetection() const;
54 void SetCapPositionEmulated(bool aEnabled
);
55 bool CapPositionEmulated() const;
56 void SetEyeFOV(VREye aEye
, double aUpDegree
, double aRightDegree
,
57 double aDownDegree
, double aLeftDegree
);
58 void SetEyeOffset(VREye aEye
, double aOffsetX
, double aOffsetY
,
60 void SetEyeResolution(uint32_t aRenderWidth
, uint32_t aRenderHeight
);
61 void SetStageSize(double aWidth
, double aHeight
);
62 void SetSittingToStandingTransform(const Float32Array
& aTransform
,
64 void SetPose(const Nullable
<Float32Array
>& aPosition
,
65 const Nullable
<Float32Array
>& aLinearVelocity
,
66 const Nullable
<Float32Array
>& aLinearAcceleration
,
67 const Nullable
<Float32Array
>& aOrientation
,
68 const Nullable
<Float32Array
>& aAngularVelocity
,
69 const Nullable
<Float32Array
>& aAngularAcceleration
,
71 virtual JSObject
* WrapObject(JSContext
* aCx
,
72 JS::Handle
<JSObject
*> aGivenProto
) override
;
75 ~VRMockDisplay() = default;
76 gfx::VRDisplayState
& DisplayState() const;
77 gfx::VRHMDSensorState
& SensorState() const;
78 void SetCapFlag(gfx::VRDisplayCapabilityFlags aFlag
, bool aEnabled
);
79 bool GetCapFlag(gfx::VRDisplayCapabilityFlags aFlag
) const;
81 RefPtr
<VRServiceTest
> mVRServiceTest
;
84 class VRMockController
: public DOMEventTargetHelper
{
86 NS_DECL_ISUPPORTS_INHERITED
87 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(VRMockController
,
90 VRMockController(VRServiceTest
* aVRServiceTest
, uint32_t aControllerIdx
);
93 void SetHand(GamepadHand aHand
);
94 GamepadHand
Hand() const;
95 void SetCapPosition(bool aEnabled
);
96 bool CapPosition() const;
97 void SetCapOrientation(bool aEnabled
);
98 bool CapOrientation() const;
99 void SetCapAngularAcceleration(bool aEnabled
);
100 bool CapAngularAcceleration() const;
101 void SetCapLinearAcceleration(bool aEnabled
);
102 bool CapLinearAcceleration() const;
103 void SetAxisCount(uint32_t aCount
);
104 uint32_t AxisCount() const;
105 void SetButtonCount(uint32_t aCount
);
106 uint32_t ButtonCount() const;
107 void SetHapticCount(uint32_t aCount
);
108 uint32_t HapticCount() const;
109 void SetButtonPressed(uint32_t aButtonIdx
, bool aPressed
);
110 void SetButtonTouched(uint32_t aButtonIdx
, bool aTouched
);
111 void SetButtonTrigger(uint32_t aButtonIdx
, double aTrigger
);
112 void SetAxisValue(uint32_t aAxisIdx
, double aValue
);
113 void SetPose(const Nullable
<Float32Array
>& aPosition
,
114 const Nullable
<Float32Array
>& aLinearVelocity
,
115 const Nullable
<Float32Array
>& aLinearAcceleration
,
116 const Nullable
<Float32Array
>& aOrientation
,
117 const Nullable
<Float32Array
>& aAngularVelocity
,
118 const Nullable
<Float32Array
>& aAngularAcceleration
,
121 virtual JSObject
* WrapObject(JSContext
* aCx
,
122 JS::Handle
<JSObject
*> aGivenProto
) override
;
125 ~VRMockController() = default;
126 gfx::VRControllerState
& ControllerState() const;
127 void SetCapFlag(GamepadCapabilityFlags aFlag
, bool aEnabled
);
128 bool GetCapFlag(GamepadCapabilityFlags aFlag
) const;
129 RefPtr
<VRServiceTest
> mVRServiceTest
;
130 uint32_t mControllerIdx
;
133 class VRServiceTest final
: public DOMEventTargetHelper
{
135 NS_DECL_ISUPPORTS_INHERITED
136 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(VRServiceTest
, DOMEventTargetHelper
)
141 void ClearController(uint32_t aControllerIdx
);
144 already_AddRefed
<Promise
> Run(ErrorResult
& aRv
);
145 already_AddRefed
<Promise
> Reset(ErrorResult
& aRv
);
146 VRMockDisplay
* GetVRDisplay();
147 VRMockController
* GetVRController(uint32_t aControllerIdx
, ErrorResult
& aRv
);
148 void Timeout(uint32_t aDuration
);
149 void Wait(uint32_t aDuration
);
151 void WaitPresentationStart();
152 void WaitPresentationEnd();
153 void WaitHapticIntensity(uint32_t aControllerIdx
, uint32_t aHapticIdx
,
154 double aIntensity
, ErrorResult
& aRv
);
156 void AcknowledgeFrame();
163 void AddCommand(uint64_t aCommand
);
164 static already_AddRefed
<VRServiceTest
> CreateTestService(
165 nsPIDOMWindowInner
* aWindow
);
166 virtual JSObject
* WrapObject(JSContext
* aCx
,
167 JS::Handle
<JSObject
*> aGivenProto
) override
;
168 gfx::VRSystemState
& SystemState();
171 explicit VRServiceTest(nsPIDOMWindowInner
* aWindow
);
172 ~VRServiceTest() = default;
175 RefPtr
<VRMockDisplay
> mDisplay
;
176 nsTArray
<RefPtr
<VRMockController
>> mControllers
;
177 nsCOMPtr
<nsPIDOMWindowInner
> mWindow
;
178 // mPendingState records the state of the emulated VR hardware, including
179 // changes that have not yet been committed to the command buffer.
180 gfx::VRSystemState mPendingState
;
181 // mEncodedState records the state of the emulate VR hardware at the end
182 // of the last committed transaction, submitted with VRServiceTest::Commit().
183 // mPendingState represents the resulting state if all of the commands in
184 // mCommandBuffer have been played back.
185 gfx::VRSystemState mEncodedState
;
186 // mCommandBuffer encodes a sequence of steps to be executed asynchronously by
187 // the simulated VR hardware. The steps are encoded as a stream of uint64's,
188 // using the format described in gfx/vr/VRPuppetCommandBuffer.h
189 // mCommandBuffer includes only complete transactions, which will be played
190 // back such that multiple values in VRSystemState will be updated atomically.
191 // When the command buffer is submitted to the PuppetSession, with
192 // VRServiceTest::Run(), it is cleared to ensure that the commands are not
193 // sent redundantly in subsequent VRServicetest::Run() calls.
194 // VRServiceTest::Commit() will perform a binary comparison of mPendingState
195 // and mEncodedState to determine what instructions need to be appended to
197 // VRServiceTest::Reset() will effectively cancel all transactions and clear
198 // mCommandBuffer before submitting the reset request to the PuppetSession.
199 nsTArray
<uint64_t> mCommandBuffer
;
204 } // namespace mozilla
206 #endif // mozilla_dom_VRServiceTest_h_