2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2015-04-24 01:05:29 +03:00
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#include "GamepadServiceTest.h"
|
2016-06-28 01:26:00 +03:00
|
|
|
|
|
|
|
#include "mozilla/ErrorResult.h"
|
2016-08-23 07:09:32 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2016-06-28 01:26:00 +03:00
|
|
|
|
2016-06-28 01:25:00 +03:00
|
|
|
#include "mozilla/dom/GamepadManager.h"
|
|
|
|
#include "mozilla/dom/GamepadPlatformService.h"
|
2016-06-28 01:26:00 +03:00
|
|
|
#include "mozilla/dom/GamepadServiceTestBinding.h"
|
|
|
|
#include "mozilla/dom/GamepadTestChannelChild.h"
|
|
|
|
|
|
|
|
#include "mozilla/ipc/BackgroundChild.h"
|
|
|
|
#include "mozilla/ipc/PBackgroundChild.h"
|
|
|
|
|
2016-08-23 07:09:32 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2015-04-24 01:05:29 +03:00
|
|
|
|
2016-06-28 01:26:00 +03:00
|
|
|
#include "nsIObserver.h"
|
|
|
|
#include "nsIObserverService.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2015-04-24 01:05:29 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Implementation of the test service. This is just to provide a simple binding
|
2016-06-28 01:26:00 +03:00
|
|
|
* of the GamepadService to JavaScript via WebIDL so that we can write Mochitests
|
2015-04-24 01:05:29 +03:00
|
|
|
* that add and remove fake gamepads, avoiding the platform-specific backends.
|
|
|
|
*/
|
|
|
|
|
2017-10-19 21:41:10 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(GamepadServiceTest, DOMEventTargetHelper,
|
|
|
|
mWindow)
|
2016-06-28 01:26:00 +03:00
|
|
|
|
2017-08-30 02:02:48 +03:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(GamepadServiceTest)
|
2016-06-28 01:26:00 +03:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(GamepadServiceTest, DOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(GamepadServiceTest, DOMEventTargetHelper)
|
2015-04-24 01:05:29 +03:00
|
|
|
|
|
|
|
// static
|
|
|
|
already_AddRefed<GamepadServiceTest>
|
2016-06-28 01:26:00 +03:00
|
|
|
GamepadServiceTest::CreateTestService(nsPIDOMWindowInner* aWindow)
|
2015-04-24 01:05:29 +03:00
|
|
|
{
|
2016-06-28 01:26:00 +03:00
|
|
|
MOZ_ASSERT(aWindow);
|
|
|
|
RefPtr<GamepadServiceTest> service = new GamepadServiceTest(aWindow);
|
|
|
|
service->InitPBackgroundActor();
|
2015-04-24 01:05:29 +03:00
|
|
|
return service.forget();
|
|
|
|
}
|
|
|
|
|
2016-06-28 01:26:00 +03:00
|
|
|
void
|
|
|
|
GamepadServiceTest::Shutdown()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mShuttingDown);
|
|
|
|
mShuttingDown = true;
|
|
|
|
DestroyPBackgroundActor();
|
|
|
|
mWindow = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
GamepadServiceTest::GamepadServiceTest(nsPIDOMWindowInner* aWindow)
|
|
|
|
: mService(GamepadManager::GetService()),
|
|
|
|
mWindow(aWindow),
|
|
|
|
mEventNumber(0),
|
|
|
|
mShuttingDown(false),
|
|
|
|
mChild(nullptr)
|
|
|
|
{}
|
|
|
|
|
|
|
|
GamepadServiceTest::~GamepadServiceTest() {}
|
|
|
|
|
|
|
|
void
|
|
|
|
GamepadServiceTest::InitPBackgroundActor()
|
2015-04-24 01:05:29 +03:00
|
|
|
{
|
2016-06-28 01:26:00 +03:00
|
|
|
MOZ_ASSERT(!mChild);
|
2017-10-24 13:02:40 +03:00
|
|
|
|
|
|
|
PBackgroundChild* actor = BackgroundChild::GetOrCreateForCurrentThread();
|
|
|
|
if (NS_WARN_IF(!actor)) {
|
|
|
|
MOZ_CRASH("Failed to create a PBackgroundChild actor!");
|
|
|
|
}
|
|
|
|
|
|
|
|
mChild = new GamepadTestChannelChild();
|
|
|
|
PGamepadTestChannelChild* initedChild =
|
|
|
|
actor->SendPGamepadTestChannelConstructor(mChild);
|
|
|
|
if (NS_WARN_IF(!initedChild)) {
|
|
|
|
MOZ_CRASH("Failed to create a PBackgroundChild actor!");
|
2016-06-28 01:26:00 +03:00
|
|
|
}
|
2015-04-24 01:05:29 +03:00
|
|
|
}
|
|
|
|
|
2016-06-28 01:26:00 +03:00
|
|
|
void
|
|
|
|
GamepadServiceTest::DestroyPBackgroundActor()
|
2015-04-24 01:05:29 +03:00
|
|
|
{
|
2017-10-24 13:02:40 +03:00
|
|
|
mChild->SendShutdownChannel();
|
|
|
|
mChild = nullptr;
|
2015-04-24 01:05:29 +03:00
|
|
|
}
|
|
|
|
|
2016-06-28 01:26:00 +03:00
|
|
|
already_AddRefed<Promise>
|
|
|
|
GamepadServiceTest::AddGamepad(const nsAString& aID,
|
2016-12-09 02:00:44 +03:00
|
|
|
GamepadMappingType aMapping,
|
2017-02-02 10:00:51 +03:00
|
|
|
GamepadHand aHand,
|
2015-04-24 01:05:29 +03:00
|
|
|
uint32_t aNumButtons,
|
|
|
|
uint32_t aNumAxes,
|
2017-02-02 10:00:51 +03:00
|
|
|
uint32_t aNumHaptics,
|
2016-06-28 01:26:00 +03:00
|
|
|
ErrorResult& aRv)
|
2015-04-24 01:05:29 +03:00
|
|
|
{
|
2016-06-28 01:26:00 +03:00
|
|
|
if (mShuttingDown) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-07-14 11:55:52 +03:00
|
|
|
// Only VR controllers has displayID, we give 0 to the general gamepads.
|
2017-07-17 06:44:39 +03:00
|
|
|
GamepadAdded a(nsString(aID),
|
|
|
|
aMapping, aHand, 0,
|
2017-02-02 10:00:51 +03:00
|
|
|
aNumButtons, aNumAxes, aNumHaptics);
|
2017-07-17 06:44:39 +03:00
|
|
|
GamepadChangeEventBody body(a);
|
|
|
|
GamepadChangeEvent e(0, GamepadServiceType::Standard, body);
|
2016-06-28 01:26:00 +03:00
|
|
|
|
2018-02-23 01:28:39 +03:00
|
|
|
RefPtr<Promise> p = Promise::Create(mWindow->AsGlobal(), aRv);
|
2016-06-28 01:26:00 +03:00
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t id = ++mEventNumber;
|
2017-10-24 13:02:40 +03:00
|
|
|
|
|
|
|
mChild->AddPromise(id, p);
|
|
|
|
mChild->SendGamepadTestEvent(id, e);
|
|
|
|
|
2016-06-28 01:26:00 +03:00
|
|
|
return p.forget();
|
2015-04-24 01:05:29 +03:00
|
|
|
}
|
|
|
|
|
2016-06-28 01:26:00 +03:00
|
|
|
void
|
|
|
|
GamepadServiceTest::RemoveGamepad(uint32_t aIndex)
|
2015-04-24 01:05:29 +03:00
|
|
|
{
|
2016-06-28 01:26:00 +03:00
|
|
|
if (mShuttingDown) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-07-17 06:44:39 +03:00
|
|
|
GamepadRemoved a;
|
|
|
|
GamepadChangeEventBody body(a);
|
|
|
|
GamepadChangeEvent e(aIndex, GamepadServiceType::Standard, body);
|
2016-06-28 01:26:00 +03:00
|
|
|
|
|
|
|
uint32_t id = ++mEventNumber;
|
2017-10-24 13:02:40 +03:00
|
|
|
mChild->SendGamepadTestEvent(id, e);
|
2015-04-24 01:05:29 +03:00
|
|
|
}
|
|
|
|
|
2016-06-28 01:26:00 +03:00
|
|
|
void
|
|
|
|
GamepadServiceTest::NewButtonEvent(uint32_t aIndex,
|
|
|
|
uint32_t aButton,
|
2017-04-07 04:35:13 +03:00
|
|
|
bool aTouched,
|
2016-06-28 01:26:00 +03:00
|
|
|
bool aPressed)
|
2015-04-24 01:05:29 +03:00
|
|
|
{
|
2016-06-28 01:26:00 +03:00
|
|
|
if (mShuttingDown) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-07-17 06:44:39 +03:00
|
|
|
GamepadButtonInformation a(aButton, aPressed ? 1.0 : 0, aPressed, aTouched);
|
|
|
|
GamepadChangeEventBody body(a);
|
|
|
|
GamepadChangeEvent e(aIndex, GamepadServiceType::Standard, body);
|
2016-06-28 01:26:00 +03:00
|
|
|
|
|
|
|
uint32_t id = ++mEventNumber;
|
2017-10-24 13:02:40 +03:00
|
|
|
mChild->SendGamepadTestEvent(id, e);
|
2016-06-28 01:26:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GamepadServiceTest::NewButtonValueEvent(uint32_t aIndex,
|
|
|
|
uint32_t aButton,
|
|
|
|
bool aPressed,
|
2017-04-07 04:35:13 +03:00
|
|
|
bool aTouched,
|
2016-06-28 01:26:00 +03:00
|
|
|
double aValue)
|
|
|
|
{
|
|
|
|
if (mShuttingDown) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-07-17 06:44:39 +03:00
|
|
|
GamepadButtonInformation a(aButton, aValue, aPressed, aTouched);
|
|
|
|
GamepadChangeEventBody body(a);
|
|
|
|
GamepadChangeEvent e(aIndex, GamepadServiceType::Standard, body);
|
2016-06-28 01:26:00 +03:00
|
|
|
|
|
|
|
uint32_t id = ++mEventNumber;
|
2017-10-24 13:02:40 +03:00
|
|
|
mChild->SendGamepadTestEvent(id, e);
|
2016-06-28 01:26:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GamepadServiceTest::NewAxisMoveEvent(uint32_t aIndex,
|
|
|
|
uint32_t aAxis,
|
|
|
|
double aValue)
|
|
|
|
{
|
|
|
|
if (mShuttingDown) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-07-17 06:44:39 +03:00
|
|
|
GamepadAxisInformation a(aAxis, aValue);
|
|
|
|
GamepadChangeEventBody body(a);
|
|
|
|
GamepadChangeEvent e(aIndex, GamepadServiceType::Standard, body);
|
2016-06-28 01:26:00 +03:00
|
|
|
|
|
|
|
uint32_t id = ++mEventNumber;
|
2017-10-24 13:02:40 +03:00
|
|
|
mChild->SendGamepadTestEvent(id, e);
|
2016-06-28 01:26:00 +03:00
|
|
|
}
|
|
|
|
|
2017-02-02 10:00:51 +03:00
|
|
|
void
|
|
|
|
GamepadServiceTest::NewPoseMove(uint32_t aIndex,
|
|
|
|
const Nullable<Float32Array>& aOrient,
|
|
|
|
const Nullable<Float32Array>& aPos,
|
|
|
|
const Nullable<Float32Array>& aAngVelocity,
|
|
|
|
const Nullable<Float32Array>& aAngAcceleration,
|
|
|
|
const Nullable<Float32Array>& aLinVelocity,
|
|
|
|
const Nullable<Float32Array>& aLinAcceleration)
|
|
|
|
{
|
|
|
|
if (mShuttingDown) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
GamepadPoseState poseState;
|
|
|
|
poseState.flags = GamepadCapabilityFlags::Cap_Orientation |
|
|
|
|
GamepadCapabilityFlags::Cap_Position |
|
|
|
|
GamepadCapabilityFlags::Cap_AngularAcceleration |
|
|
|
|
GamepadCapabilityFlags::Cap_LinearAcceleration;
|
|
|
|
if (!aOrient.IsNull()) {
|
|
|
|
const Float32Array& value = aOrient.Value();
|
|
|
|
value.ComputeLengthAndData();
|
|
|
|
MOZ_ASSERT(value.Length() == 4);
|
|
|
|
poseState.orientation[0] = value.Data()[0];
|
|
|
|
poseState.orientation[1] = value.Data()[1];
|
|
|
|
poseState.orientation[2] = value.Data()[2];
|
|
|
|
poseState.orientation[3] = value.Data()[3];
|
2017-04-26 08:17:25 +03:00
|
|
|
poseState.isOrientationValid = true;
|
2017-02-02 10:00:51 +03:00
|
|
|
}
|
|
|
|
if (!aPos.IsNull()) {
|
|
|
|
const Float32Array& value = aPos.Value();
|
|
|
|
value.ComputeLengthAndData();
|
|
|
|
MOZ_ASSERT(value.Length() == 3);
|
|
|
|
poseState.position[0] = value.Data()[0];
|
|
|
|
poseState.position[1] = value.Data()[1];
|
|
|
|
poseState.position[2] = value.Data()[2];
|
2017-04-26 08:17:25 +03:00
|
|
|
poseState.isPositionValid = true;
|
2017-02-02 10:00:51 +03:00
|
|
|
}
|
|
|
|
if (!aAngVelocity.IsNull()) {
|
|
|
|
const Float32Array& value = aAngVelocity.Value();
|
|
|
|
value.ComputeLengthAndData();
|
|
|
|
MOZ_ASSERT(value.Length() == 3);
|
|
|
|
poseState.angularVelocity[0] = value.Data()[0];
|
|
|
|
poseState.angularVelocity[1] = value.Data()[1];
|
|
|
|
poseState.angularVelocity[2] = value.Data()[2];
|
|
|
|
}
|
|
|
|
if (!aAngAcceleration.IsNull()) {
|
|
|
|
const Float32Array& value = aAngAcceleration.Value();
|
|
|
|
value.ComputeLengthAndData();
|
|
|
|
MOZ_ASSERT(value.Length() == 3);
|
|
|
|
poseState.angularAcceleration[0] = value.Data()[0];
|
|
|
|
poseState.angularAcceleration[1] = value.Data()[1];
|
|
|
|
poseState.angularAcceleration[2] = value.Data()[2];
|
|
|
|
}
|
|
|
|
if (!aLinVelocity.IsNull()) {
|
|
|
|
const Float32Array& value = aLinVelocity.Value();
|
|
|
|
value.ComputeLengthAndData();
|
|
|
|
MOZ_ASSERT(value.Length() == 3);
|
|
|
|
poseState.linearVelocity[0] = value.Data()[0];
|
|
|
|
poseState.linearVelocity[1] = value.Data()[1];
|
|
|
|
poseState.linearVelocity[2] = value.Data()[2];
|
|
|
|
}
|
|
|
|
if (!aLinAcceleration.IsNull()) {
|
|
|
|
const Float32Array& value = aLinAcceleration.Value();
|
|
|
|
value.ComputeLengthAndData();
|
|
|
|
MOZ_ASSERT(value.Length() == 3);
|
|
|
|
poseState.linearAcceleration[0] = value.Data()[0];
|
|
|
|
poseState.linearAcceleration[1] = value.Data()[1];
|
|
|
|
poseState.linearAcceleration[2] = value.Data()[2];
|
|
|
|
}
|
|
|
|
|
2017-07-17 06:44:39 +03:00
|
|
|
GamepadPoseInformation a(poseState);
|
|
|
|
GamepadChangeEventBody body(a);
|
|
|
|
GamepadChangeEvent e(aIndex, GamepadServiceType::Standard, body);
|
2017-02-02 10:00:51 +03:00
|
|
|
|
|
|
|
uint32_t id = ++mEventNumber;
|
2017-10-24 13:02:40 +03:00
|
|
|
mChild->SendGamepadTestEvent(id, e);
|
2015-04-24 01:05:29 +03:00
|
|
|
}
|
|
|
|
|
2016-06-28 01:26:00 +03:00
|
|
|
JSObject*
|
|
|
|
GamepadServiceTest::WrapObject(JSContext* aCx, JS::HandleObject aGivenProto)
|
2015-04-24 01:05:29 +03:00
|
|
|
{
|
2018-06-26 00:20:54 +03:00
|
|
|
return GamepadServiceTest_Binding::Wrap(aCx, this, aGivenProto);
|
2015-04-24 01:05:29 +03:00
|
|
|
}
|
|
|
|
|
2016-06-28 01:26:00 +03:00
|
|
|
} // dom
|
|
|
|
} // mozilla
|