LCOV - code coverage report
Current view: top level - dom/gamepad - GamepadServiceTest.cpp (source / functions) Hit Total Coverage
Test: output.info Lines: 0 181 0.0 %
Date: 2017-07-14 16:53:18 Functions: 0 22 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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             : #include "GamepadServiceTest.h"
       8             : 
       9             : #include "mozilla/ErrorResult.h"
      10             : #include "mozilla/Unused.h"
      11             : 
      12             : #include "mozilla/dom/GamepadManager.h"
      13             : #include "mozilla/dom/GamepadPlatformService.h"
      14             : #include "mozilla/dom/GamepadServiceTestBinding.h"
      15             : #include "mozilla/dom/GamepadTestChannelChild.h"
      16             : 
      17             : #include "mozilla/ipc/BackgroundChild.h"
      18             : #include "mozilla/ipc/PBackgroundChild.h"
      19             : 
      20             : #include "mozilla/Unused.h"
      21             : 
      22             : #include "nsIObserver.h"
      23             : #include "nsIObserverService.h"
      24             : 
      25             : namespace mozilla {
      26             : namespace dom {
      27             : 
      28             : /*
      29             :  * Implementation of the test service. This is just to provide a simple binding
      30             :  * of the GamepadService to JavaScript via WebIDL so that we can write Mochitests
      31             :  * that add and remove fake gamepads, avoiding the platform-specific backends.
      32             :  */
      33             : 
      34             : NS_IMPL_CYCLE_COLLECTION_CLASS(GamepadServiceTest)
      35             : 
      36           0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(GamepadServiceTest,
      37             :                                                   DOMEventTargetHelper)
      38           0 :   NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mWindow)
      39           0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
      40             : 
      41           0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(GamepadServiceTest,
      42             :                                                 DOMEventTargetHelper)
      43           0 :   NS_IMPL_CYCLE_COLLECTION_UNLINK(mWindow)
      44           0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_END
      45             : 
      46           0 : NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(GamepadServiceTest)
      47           0 :   NS_INTERFACE_MAP_ENTRY(nsIIPCBackgroundChildCreateCallback)
      48           0 : NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
      49             : 
      50           0 : NS_IMPL_ADDREF_INHERITED(GamepadServiceTest, DOMEventTargetHelper)
      51           0 : NS_IMPL_RELEASE_INHERITED(GamepadServiceTest, DOMEventTargetHelper)
      52             : 
      53             : // static
      54             : already_AddRefed<GamepadServiceTest>
      55           0 : GamepadServiceTest::CreateTestService(nsPIDOMWindowInner* aWindow)
      56             : {
      57           0 :   MOZ_ASSERT(aWindow);
      58           0 :   RefPtr<GamepadServiceTest> service = new GamepadServiceTest(aWindow);
      59           0 :   service->InitPBackgroundActor();
      60           0 :   return service.forget();
      61             : }
      62             : 
      63             : void
      64           0 : GamepadServiceTest::Shutdown()
      65             : {
      66           0 :   MOZ_ASSERT(!mShuttingDown);
      67           0 :   mShuttingDown = true;
      68           0 :   DestroyPBackgroundActor();
      69           0 :   mWindow = nullptr;
      70           0 : }
      71             : 
      72           0 : GamepadServiceTest::GamepadServiceTest(nsPIDOMWindowInner* aWindow)
      73           0 :   : mService(GamepadManager::GetService()),
      74             :     mWindow(aWindow),
      75             :     mEventNumber(0),
      76             :     mShuttingDown(false),
      77           0 :     mChild(nullptr)
      78           0 : {}
      79             : 
      80           0 : GamepadServiceTest::~GamepadServiceTest() {}
      81             : 
      82             : void
      83           0 : GamepadServiceTest::InitPBackgroundActor()
      84             : {
      85           0 :   MOZ_ASSERT(!mChild);
      86           0 :   PBackgroundChild *actor = BackgroundChild::GetForCurrentThread();
      87             :   //Try to get the PBackground Child actor
      88           0 :   if (actor) {
      89           0 :     ActorCreated(actor);
      90             :   } else {
      91           0 :     Unused << BackgroundChild::GetOrCreateForCurrentThread(this);
      92             :   }
      93           0 : }
      94             : 
      95             : void
      96           0 : GamepadServiceTest::DestroyPBackgroundActor()
      97             : {
      98           0 :   if (mChild) {
      99             :     // If mChild exists, which means that IPDL channel
     100             :     // has been created, our pending operations should
     101             :     // be empty.
     102           0 :     MOZ_ASSERT(mPendingOperations.IsEmpty());
     103           0 :     mChild->SendShutdownChannel();
     104           0 :     mChild = nullptr;
     105             :   } else {
     106             :     // If the IPDL channel has not been created and we
     107             :     // want to destroy it now, just cancel all pending
     108             :     // operations.
     109           0 :     mPendingOperations.Clear();
     110             :   }
     111           0 : }
     112             : 
     113             : already_AddRefed<Promise>
     114           0 : GamepadServiceTest::AddGamepad(const nsAString& aID,
     115             :                                GamepadMappingType aMapping,
     116             :                                GamepadHand aHand,
     117             :                                uint32_t aNumButtons,
     118             :                                uint32_t aNumAxes,
     119             :                                uint32_t aNumHaptics,
     120             :                                ErrorResult& aRv)
     121             : {
     122           0 :   if (mShuttingDown) {
     123           0 :     return nullptr;
     124             :   }
     125             : 
     126           0 :   GamepadAdded a(nsString(aID), 0,
     127             :                  aMapping, aHand,
     128             :                  GamepadServiceType::Standard,
     129           0 :                  aNumButtons, aNumAxes, aNumHaptics);
     130           0 :   GamepadChangeEvent e(a);
     131           0 :   nsCOMPtr<nsIGlobalObject> go = do_QueryInterface(mWindow);
     132             : 
     133           0 :   RefPtr<Promise> p = Promise::Create(go, aRv);
     134           0 :   if (aRv.Failed()) {
     135           0 :     return nullptr;
     136             :   }
     137             : 
     138           0 :   uint32_t id = ++mEventNumber;
     139           0 :   if (mChild) {
     140           0 :     mChild->AddPromise(id, p);
     141           0 :     mChild->SendGamepadTestEvent(id, e);
     142             :   } else {
     143           0 :     PendingOperation op(id, e, p);
     144           0 :     mPendingOperations.AppendElement(op);
     145             :   }
     146           0 :   return p.forget();
     147             : }
     148             : 
     149             : void
     150           0 : GamepadServiceTest::RemoveGamepad(uint32_t aIndex)
     151             : {
     152           0 :   if (mShuttingDown) {
     153           0 :     return;
     154             :   }
     155             : 
     156           0 :   GamepadRemoved a(aIndex, GamepadServiceType::Standard);
     157           0 :   GamepadChangeEvent e(a);
     158             : 
     159           0 :   uint32_t id = ++mEventNumber;
     160           0 :   if (mChild) {
     161           0 :     mChild->SendGamepadTestEvent(id, e);
     162             :   } else {
     163           0 :     PendingOperation op(id, e);
     164           0 :     mPendingOperations.AppendElement(op);
     165             :   }
     166             : }
     167             : 
     168             : void
     169           0 : GamepadServiceTest::NewButtonEvent(uint32_t aIndex,
     170             :                                    uint32_t aButton,
     171             :                                    bool aTouched,
     172             :                                    bool aPressed)
     173             : {
     174           0 :   if (mShuttingDown) {
     175           0 :     return;
     176             :   }
     177             : 
     178             :   GamepadButtonInformation a(aIndex, GamepadServiceType::Standard,
     179           0 :                              aButton, aPressed ? 1.0 : 0, aPressed, aTouched);
     180           0 :   GamepadChangeEvent e(a);
     181             : 
     182           0 :   uint32_t id = ++mEventNumber;
     183           0 :   if (mChild) {
     184           0 :     mChild->SendGamepadTestEvent(id, e);
     185             :   } else {
     186           0 :     PendingOperation op(id, e);
     187           0 :     mPendingOperations.AppendElement(op);
     188             :   }
     189             : }
     190             : 
     191             : void
     192           0 : GamepadServiceTest::NewButtonValueEvent(uint32_t aIndex,
     193             :                                         uint32_t aButton,
     194             :                                         bool aPressed,
     195             :                                         bool aTouched,
     196             :                                         double aValue)
     197             : {
     198           0 :   if (mShuttingDown) {
     199           0 :     return;
     200             :   }
     201             : 
     202             :   GamepadButtonInformation a(aIndex, GamepadServiceType::Standard,
     203           0 :                              aButton, aValue, aPressed, aTouched);
     204           0 :   GamepadChangeEvent e(a);
     205             : 
     206           0 :   uint32_t id = ++mEventNumber;
     207           0 :   if (mChild) {
     208           0 :     mChild->SendGamepadTestEvent(id, e);
     209             :   } else {
     210           0 :     PendingOperation op(id, e);
     211           0 :     mPendingOperations.AppendElement(op);
     212             :   }
     213             : }
     214             : 
     215             : void
     216           0 : GamepadServiceTest::NewAxisMoveEvent(uint32_t aIndex,
     217             :                                      uint32_t aAxis,
     218             :                                      double aValue)
     219             : {
     220           0 :   if (mShuttingDown) {
     221           0 :     return;
     222             :   }
     223             : 
     224             :   GamepadAxisInformation a(aIndex, GamepadServiceType::Standard,
     225           0 :                            aAxis, aValue);
     226           0 :   GamepadChangeEvent e(a);
     227             : 
     228           0 :   uint32_t id = ++mEventNumber;
     229           0 :   if (mChild) {
     230           0 :     mChild->SendGamepadTestEvent(id, e);
     231             :   } else {
     232           0 :     PendingOperation op(id, e);
     233           0 :     mPendingOperations.AppendElement(op);
     234             :   }
     235             : }
     236             : 
     237             : void
     238           0 : GamepadServiceTest::NewPoseMove(uint32_t aIndex,
     239             :                                 const Nullable<Float32Array>& aOrient,
     240             :                                 const Nullable<Float32Array>& aPos,
     241             :                                 const Nullable<Float32Array>& aAngVelocity,
     242             :                                 const Nullable<Float32Array>& aAngAcceleration,
     243             :                                 const Nullable<Float32Array>& aLinVelocity,
     244             :                                 const Nullable<Float32Array>& aLinAcceleration)
     245             : {
     246           0 :   if (mShuttingDown) {
     247           0 :     return;
     248             :   }
     249             : 
     250           0 :   GamepadPoseState poseState;
     251           0 :   poseState.flags = GamepadCapabilityFlags::Cap_Orientation |
     252           0 :                     GamepadCapabilityFlags::Cap_Position |
     253           0 :                     GamepadCapabilityFlags::Cap_AngularAcceleration |
     254           0 :                     GamepadCapabilityFlags::Cap_LinearAcceleration;
     255           0 :   if (!aOrient.IsNull()) {
     256           0 :     const Float32Array& value = aOrient.Value();
     257           0 :     value.ComputeLengthAndData();
     258           0 :     MOZ_ASSERT(value.Length() == 4);
     259           0 :     poseState.orientation[0] = value.Data()[0];
     260           0 :     poseState.orientation[1] = value.Data()[1];
     261           0 :     poseState.orientation[2] = value.Data()[2];
     262           0 :     poseState.orientation[3] = value.Data()[3];
     263           0 :     poseState.isOrientationValid = true;
     264             :   }
     265           0 :   if (!aPos.IsNull()) {
     266           0 :     const Float32Array& value = aPos.Value();
     267           0 :     value.ComputeLengthAndData();
     268           0 :     MOZ_ASSERT(value.Length() == 3);
     269           0 :     poseState.position[0] = value.Data()[0];
     270           0 :     poseState.position[1] = value.Data()[1];
     271           0 :     poseState.position[2] = value.Data()[2];
     272           0 :     poseState.isPositionValid = true;
     273             :   }
     274           0 :   if (!aAngVelocity.IsNull()) {
     275           0 :     const Float32Array& value = aAngVelocity.Value();
     276           0 :     value.ComputeLengthAndData();
     277           0 :     MOZ_ASSERT(value.Length() == 3);
     278           0 :     poseState.angularVelocity[0] = value.Data()[0];
     279           0 :     poseState.angularVelocity[1] = value.Data()[1];
     280           0 :     poseState.angularVelocity[2] = value.Data()[2];
     281             :   }
     282           0 :   if (!aAngAcceleration.IsNull()) {
     283           0 :     const Float32Array& value = aAngAcceleration.Value();
     284           0 :     value.ComputeLengthAndData();
     285           0 :     MOZ_ASSERT(value.Length() == 3);
     286           0 :     poseState.angularAcceleration[0] = value.Data()[0];
     287           0 :     poseState.angularAcceleration[1] = value.Data()[1];
     288           0 :     poseState.angularAcceleration[2] = value.Data()[2];
     289             :   }
     290           0 :   if (!aLinVelocity.IsNull()) {
     291           0 :     const Float32Array& value = aLinVelocity.Value();
     292           0 :     value.ComputeLengthAndData();
     293           0 :     MOZ_ASSERT(value.Length() == 3);
     294           0 :     poseState.linearVelocity[0] = value.Data()[0];
     295           0 :     poseState.linearVelocity[1] = value.Data()[1];
     296           0 :     poseState.linearVelocity[2] = value.Data()[2];
     297             :   }
     298           0 :   if (!aLinAcceleration.IsNull()) {
     299           0 :     const Float32Array& value = aLinAcceleration.Value();
     300           0 :     value.ComputeLengthAndData();
     301           0 :     MOZ_ASSERT(value.Length() == 3);
     302           0 :     poseState.linearAcceleration[0] = value.Data()[0];
     303           0 :     poseState.linearAcceleration[1] = value.Data()[1];
     304           0 :     poseState.linearAcceleration[2] = value.Data()[2];
     305             :   }
     306             : 
     307             :   GamepadPoseInformation a(aIndex, GamepadServiceType::Standard,
     308           0 :                            poseState);
     309           0 :   GamepadChangeEvent e(a);
     310             : 
     311           0 :   uint32_t id = ++mEventNumber;
     312           0 :   if (mChild) {
     313           0 :     mChild->SendGamepadTestEvent(id, e);
     314             :   } else {
     315           0 :     PendingOperation op(id, e);
     316           0 :     mPendingOperations.AppendElement(op);
     317             :   }
     318             : }
     319             : 
     320             : void
     321           0 : GamepadServiceTest::FlushPendingOperations()
     322             : {
     323           0 :   for (uint32_t i=0; i < mPendingOperations.Length(); ++i) {
     324           0 :     PendingOperation op = mPendingOperations[i];
     325           0 :     if (op.mPromise) {
     326           0 :       mChild->AddPromise(op.mID, op.mPromise);
     327             :     }
     328           0 :     mChild->SendGamepadTestEvent(op.mID, op.mEvent);
     329             :   }
     330           0 :   mPendingOperations.Clear();
     331           0 : }
     332             : 
     333             : void
     334           0 : GamepadServiceTest::ActorCreated(PBackgroundChild* aActor)
     335             : {
     336           0 :   MOZ_ASSERT(aActor);
     337             :   // If we are shutting down, we don't need to create the
     338             :   // IPDL child/parent pair anymore.
     339           0 :   if (mShuttingDown) {
     340             :     // mPendingOperations should be cleared in
     341             :     // DestroyPBackgroundActor()
     342           0 :     MOZ_ASSERT(mPendingOperations.IsEmpty());
     343           0 :     return;
     344             :   }
     345             : 
     346           0 :   mChild = new GamepadTestChannelChild();
     347             :   PGamepadTestChannelChild* initedChild =
     348           0 :     aActor->SendPGamepadTestChannelConstructor(mChild);
     349           0 :   if (NS_WARN_IF(!initedChild)) {
     350           0 :     ActorFailed();
     351           0 :     return;
     352             :   }
     353           0 :   FlushPendingOperations();
     354             : }
     355             : 
     356             : void
     357           0 : GamepadServiceTest::ActorFailed()
     358             : {
     359           0 :   MOZ_CRASH("Failed to create background child actor!");
     360             : }
     361             : 
     362             : JSObject*
     363           0 : GamepadServiceTest::WrapObject(JSContext* aCx, JS::HandleObject aGivenProto)
     364             : {
     365           0 :   return GamepadServiceTestBinding::Wrap(aCx, this, aGivenProto);
     366             : }
     367             : 
     368             : } // dom
     369             : } // mozilla

Generated by: LCOV version 1.13