summaryrefslogtreecommitdiffstats
path: root/dom/gamepad/GamepadServiceTest.h
blob: 3b88ff178a4a2255a626d09f56855e534953021f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef mozilla_dom_GamepadServiceTest_h_
#define mozilla_dom_GamepadServiceTest_h_

#include "mozilla/DOMEventTargetHelper.h"
#include "mozilla/dom/GamepadBinding.h"
#include "mozilla/dom/GamepadHandle.h"
#include "mozilla/dom/TypedArray.h"
#include "mozilla/WeakPtr.h"

namespace mozilla::dom {

class GamepadChangeEvent;
class GamepadManager;
class GamepadTestChannelChild;
class Promise;

// Service for testing purposes
class GamepadServiceTest final : public DOMEventTargetHelper,
                                 public SupportsWeakPtr {
 public:
  NS_DECL_ISUPPORTS_INHERITED
  NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(GamepadServiceTest,
                                           DOMEventTargetHelper)

  GamepadMappingType NoMapping() const { return GamepadMappingType::_empty; }
  GamepadMappingType StandardMapping() const {
    return GamepadMappingType::Standard;
  }
  GamepadHand NoHand() const { return GamepadHand::_empty; }
  GamepadHand LeftHand() const { return GamepadHand::Left; }
  GamepadHand RightHand() const { return GamepadHand::Right; }

  // IPC receiver
  void ReplyGamepadHandle(uint32_t aPromiseId, const GamepadHandle& aHandle);

  // Methods from GamepadServiceTest.webidl
  already_AddRefed<Promise> AddGamepad(
      const nsAString& aID, GamepadMappingType aMapping, GamepadHand aHand,
      uint32_t aNumButtons, uint32_t aNumAxes, uint32_t aNumHaptics,
      uint32_t aNumLightIndicator, uint32_t aNumTouchEvents, ErrorResult& aRv);

  already_AddRefed<Promise> RemoveGamepad(uint32_t aHandleSlot,
                                          ErrorResult& aRv);

  already_AddRefed<Promise> NewButtonEvent(uint32_t aHandleSlot,
                                           uint32_t aButton, bool aPressed,
                                           bool aTouched, ErrorResult& aRv);

  already_AddRefed<Promise> NewButtonValueEvent(uint32_t aHandleSlot,
                                                uint32_t aButton, bool aPressed,
                                                bool aTouched, double aValue,
                                                ErrorResult& aRv);

  already_AddRefed<Promise> NewAxisMoveEvent(uint32_t aHandleSlot,
                                             uint32_t aAxis, double aValue,
                                             ErrorResult& aRv);

  already_AddRefed<Promise> NewPoseMove(
      uint32_t aHandleSlot, const Nullable<Float32Array>& aOrient,
      const Nullable<Float32Array>& aPos,
      const Nullable<Float32Array>& aAngVelocity,
      const Nullable<Float32Array>& aAngAcceleration,
      const Nullable<Float32Array>& aLinVelocity,
      const Nullable<Float32Array>& aLinAcceleration, ErrorResult& aRv);

  already_AddRefed<Promise> NewTouch(uint32_t aHandleSlot,
                                     uint32_t aTouchArrayIndex,
                                     uint32_t aTouchId, uint8_t aSurfaceId,
                                     const Float32Array& aPos,
                                     const Nullable<Float32Array>& aSurfDim,
                                     ErrorResult& aRv);

  void Shutdown();

  static already_AddRefed<GamepadServiceTest> CreateTestService(
      nsPIDOMWindowInner* aWindow);
  nsPIDOMWindowInner* GetParentObject() const { return mWindow; }
  JSObject* WrapObject(JSContext* aCx,
                       JS::Handle<JSObject*> aGivenProto) override;

 private:
  // Hold a reference to the gamepad service so we don't have to worry about
  // execution order in tests.
  RefPtr<GamepadManager> mService;
  nsCOMPtr<nsPIDOMWindowInner> mWindow;
  uint32_t mEventNumber;
  bool mShuttingDown;

  // IPDL Channel for us to send test events to GamepadPlatformService, it
  // will only be used in this singleton class and deleted during the IPDL
  // shutdown chain
  RefPtr<GamepadTestChannelChild> mChild;
  nsTArray<GamepadHandle> mGamepadHandles;

  nsRefPtrHashtable<nsUint32HashKey, dom::Promise> mPromiseList;

  explicit GamepadServiceTest(nsPIDOMWindowInner* aWindow);
  ~GamepadServiceTest();
  void InitPBackgroundActor();
  void DestroyPBackgroundActor();

  uint32_t AddGamepadHandle(GamepadHandle aHandle);
  void RemoveGamepadHandle(uint32_t aHandleSlot);
  GamepadHandle GetHandleInSlot(uint32_t aHandleSlot) const;
};

}  // namespace mozilla::dom

#endif