Bug 852944 - Update mochitests to reflect new Gamepad API
authorKyle Machulis <kyle@nonpolynomial.com>
Wed, 25 Feb 2015 16:09:16 -0800
changeset 399226 9d64c0b034d6a6f8e477cc1144459a61a5f66136
parent 399225 d1a19308b5bc089853681324a440d643b640d0f5
child 399227 a69f80ee5334f7b96b56258116eb0442c1c9c6e4
push id55872
push userkmachulis@mozilla.com
push dateThu, 26 Feb 2015 00:10:46 +0000
treeherdertry@a69f80ee5334 [default view] [failures only]
bugs852944
milestone39.0a1
Bug 852944 - Update mochitests to reflect new Gamepad API
dom/gamepad/GamepadService.cpp
dom/gamepad/GamepadService.h
dom/gamepad/GamepadServiceTest.cpp
dom/gamepad/GamepadServiceTest.h
dom/gamepad/moz.build
dom/interfaces/gamepad/nsIGamepadServiceTest.idl
dom/tests/mochitest/gamepad/test_check_timestamp.html
dom/tests/mochitest/gamepad/test_gamepad.html
dom/tests/mochitest/gamepad/test_navigator_gamepads.html
layout/build/nsLayoutModule.cpp
--- a/dom/gamepad/GamepadService.cpp
+++ b/dom/gamepad/GamepadService.cpp
@@ -559,77 +559,10 @@ GamepadService::Update(const GamepadChan
   } else if (aEvent.type() == GamepadChangeEvent::TGamepadAxisInformation) {
     const GamepadAxisInformation& a = aEvent.get_GamepadAxisInformation();
     NewAxisMoveEvent(a.index(), a.axis(), a.value());
   } else {
     MOZ_CRASH("We shouldn't be here!");
   }
 }
 
-/*
- * Implementation of the test service. This is just to provide a simple binding
- * of the GamepadService to JavaScript via XPCOM so that we can write Mochitests
- * that add and remove fake gamepads, avoiding the platform-specific backends.
- */
-NS_IMPL_ISUPPORTS(GamepadServiceTest, nsIGamepadServiceTest)
-
-GamepadServiceTest* GamepadServiceTest::sSingleton = nullptr;
-
-// static
-already_AddRefed<GamepadServiceTest>
-GamepadServiceTest::CreateService()
-{
-  if (sSingleton == nullptr) {
-    sSingleton = new GamepadServiceTest();
-  }
-  nsRefPtr<GamepadServiceTest> service = sSingleton;
-  return service.forget();
-}
-
-GamepadServiceTest::GamepadServiceTest()
-{
-  /* member initializers and constructor code */
-  nsRefPtr<GamepadService> service = GamepadService::GetService();
-}
-
-/* uint32_t addGamepad (in string id, in unsigned long mapping, in unsigned long numButtons, in unsigned long numAxes); */
-NS_IMETHODIMP GamepadServiceTest::AddGamepad(const char* aID,
-                                             uint32_t aMapping,
-                                             uint32_t aNumButtons,
-                                             uint32_t aNumAxes,
-                                             uint32_t* aRetval)
-{
-  *aRetval = gGamepadServiceSingleton->AddGamepad(aID,
-                                                  static_cast<GamepadMappingType>(aMapping),
-                                                  aNumButtons,
-                                                  aNumAxes);
-  return NS_OK;
-}
-
-/* void removeGamepad (in uint32_t index); */
-NS_IMETHODIMP GamepadServiceTest::RemoveGamepad(uint32_t aIndex)
-{
-  gGamepadServiceSingleton->RemoveGamepad(aIndex);
-  return NS_OK;
-}
-
-/* void newButtonEvent (in uint32_t index, in uint32_t button,
-   in boolean pressed); */
-NS_IMETHODIMP GamepadServiceTest::NewButtonEvent(uint32_t aIndex,
-                                                 uint32_t aButton,
-                                                 bool aPressed)
-{
-  gGamepadServiceSingleton->NewButtonEvent(aIndex, aButton, aPressed);
-  return NS_OK;
-}
-
-/* void newAxisMoveEvent (in uint32_t index, in uint32_t axis,
-   in double value); */
-NS_IMETHODIMP GamepadServiceTest::NewAxisMoveEvent(uint32_t aIndex,
-                                                   uint32_t aAxis,
-                                                   double aValue)
-{
-  gGamepadServiceSingleton->NewAxisMoveEvent(aIndex, aAxis, aValue);
-  return NS_OK;
-}
-
 } // namespace dom
 } // namespace mozilla
--- a/dom/gamepad/GamepadService.h
+++ b/dom/gamepad/GamepadService.h
@@ -129,33 +129,11 @@ class GamepadService : public nsIObserve
   // to each window.
   nsRefPtrHashtable<nsUint32HashKey, mozilla::dom::Gamepad > mGamepads;
   // Inner windows that are listening for gamepad events.
   // has been sent to that window.
   nsTArray<nsRefPtr<nsGlobalWindow> > mListeners;
   nsCOMPtr<nsITimer> mTimer;
 };
 
-// Service for testing purposes
-class GamepadServiceTest : public nsIGamepadServiceTest
-{
-public:
-  NS_DECL_ISUPPORTS
-  NS_DECL_NSIGAMEPADSERVICETEST
-
-  GamepadServiceTest();
-
-  static already_AddRefed<GamepadServiceTest> CreateService();
-
-private:
-  static GamepadServiceTest* sSingleton;
-  virtual ~GamepadServiceTest() {};
-};
-
 }  // namespace dom
 }  // namespace mozilla
-
-#define NS_GAMEPAD_TEST_CID \
-{ 0xfb1fcb57, 0xebab, 0x4cf4, \
-{ 0x96, 0x3b, 0x1e, 0x4d, 0xb8, 0x52, 0x16, 0x96 } }
-#define NS_GAMEPAD_TEST_CONTRACTID "@mozilla.org/gamepad-test;1"
-
 #endif // mozilla_dom_GamepadService_h_
new file mode 100644
--- /dev/null
+++ b/dom/gamepad/GamepadServiceTest.cpp
@@ -0,0 +1,75 @@
+/* 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"
+#include "GamepadFunctions.h"
+
+using namespace mozilla::dom;
+
+/*
+ * Implementation of the test service. This is just to provide a simple binding
+ * of the GamepadService to JavaScript via XPCOM so that we can write Mochitests
+ * that add and remove fake gamepads, avoiding the platform-specific backends.
+ */
+NS_IMPL_ISUPPORTS(GamepadServiceTest, nsIGamepadServiceTest)
+
+GamepadServiceTest* GamepadServiceTest::sSingleton = nullptr;
+
+// static
+already_AddRefed<GamepadServiceTest>
+GamepadServiceTest::CreateService()
+{
+  if (sSingleton == nullptr) {
+    sSingleton = new GamepadServiceTest();
+  }
+  nsRefPtr<GamepadServiceTest> service = sSingleton;
+  return service.forget();
+}
+
+GamepadServiceTest::GamepadServiceTest()
+{
+}
+
+/* uint32_t addGamepad(in unsigned long index, in string id, in unsigned long mapping, in unsigned long numButtons, in unsigned long numAxes); */
+NS_IMETHODIMP
+GamepadServiceTest::AddGamepad(const char* aID,
+                               uint32_t aMapping,
+                               uint32_t aNumButtons,
+                               uint32_t aNumAxes,
+                               uint32_t* aGamepadIndex)
+{
+  *aGamepadIndex = GamepadFunctions::AddGamepad(aID,
+                                                static_cast<GamepadMappingType>(aMapping),
+                                                aNumButtons,
+                                                aNumAxes);
+  return NS_OK;
+}
+
+/* void removeGamepad (in uint32_t index); */
+NS_IMETHODIMP GamepadServiceTest::RemoveGamepad(uint32_t aIndex)
+{
+  GamepadFunctions::RemoveGamepad(aIndex);
+  return NS_OK;
+}
+
+/* void newButtonEvent (in uint32_t index, in uint32_t button,
+   in boolean pressed); */
+NS_IMETHODIMP GamepadServiceTest::NewButtonEvent(uint32_t aIndex,
+                                                 uint32_t aButton,
+                                                 bool aPressed)
+{
+  GamepadFunctions::NewButtonEvent(aIndex, aButton, aPressed);
+  return NS_OK;
+}
+
+/* void newAxisMoveEvent (in uint32_t index, in uint32_t axis,
+   in double value); */
+NS_IMETHODIMP GamepadServiceTest::NewAxisMoveEvent(uint32_t aIndex,
+                                                   uint32_t aAxis,
+                                                   double aValue)
+{
+  GamepadFunctions::NewAxisMoveEvent(aIndex, aAxis, aValue);
+  return NS_OK;
+}
+
new file mode 100644
--- /dev/null
+++ b/dom/gamepad/GamepadServiceTest.h
@@ -0,0 +1,36 @@
+/* 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 "nsIGamepadServiceTest.h"
+
+namespace mozilla {
+namespace dom {
+// Service for testing purposes
+class GamepadServiceTest : public nsIGamepadServiceTest
+{
+public:
+  NS_DECL_ISUPPORTS
+  NS_DECL_NSIGAMEPADSERVICETEST
+
+  GamepadServiceTest();
+
+  static already_AddRefed<GamepadServiceTest> CreateService();
+
+private:
+  static GamepadServiceTest* sSingleton;
+  virtual ~GamepadServiceTest() {};
+};
+
+}  // namespace dom
+}  // namespace mozilla
+
+#define NS_GAMEPAD_TEST_CID \
+{ 0xfb1fcb57, 0xebab, 0x4cf4, \
+{ 0x96, 0x3b, 0x1e, 0x4d, 0xb8, 0x52, 0x16, 0x96 } }
+#define NS_GAMEPAD_TEST_CONTRACTID "@mozilla.org/gamepad-test;1"
+
+#endif
--- a/dom/gamepad/moz.build
+++ b/dom/gamepad/moz.build
@@ -5,24 +5,26 @@
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 EXPORTS.mozilla.dom += [
     'Gamepad.h',
     'GamepadButton.h',
     'GamepadFunctions.h',
     'GamepadMonitoring.h',
     'GamepadService.h',
+    'GamepadServiceTest.h'
     ]
 
 UNIFIED_SOURCES = [
     'Gamepad.cpp',
     'GamepadButton.cpp',
     'GamepadFunctions.cpp',
     'GamepadMonitoring.cpp',
     'GamepadService.cpp',
+    'GamepadServiceTest.cpp'
     ]
 
 if CONFIG['MOZ_GAMEPAD_BACKEND'] == 'stub':
     UNIFIED_SOURCES += [
         'fallback/FallbackGamepad.cpp'
     ]
 elif CONFIG['MOZ_GAMEPAD_BACKEND'] == 'cocoa':
     UNIFIED_SOURCES += [
--- a/dom/interfaces/gamepad/nsIGamepadServiceTest.idl
+++ b/dom/interfaces/gamepad/nsIGamepadServiceTest.idl
@@ -4,23 +4,24 @@
 
 #include "nsISupports.idl"
 
 interface nsIVariant;
 
 /*
  * This interface is intended only for use in tests.
  */
-[scriptable, uuid(b6ed093c-6ea0-4141-a8eb-f99645162651)]
+[scriptable, uuid(c03ec4ed-8a7e-40e7-99da-c609f1760d0c)]
 interface nsIGamepadServiceTest : nsISupports
 {
   const unsigned long NO_MAPPING = 0;
   const unsigned long STANDARD_MAPPING = 1;
 
-  unsigned long addGamepad(in string id, in unsigned long mapping,
+  unsigned long addGamepad(in string id,
+                           in unsigned long mapping,
                            in unsigned long numButtons,
 			   in unsigned long numAxes);
   void removeGamepad(in unsigned long index);
   void newButtonEvent(in unsigned long index, in unsigned long button,
 		      in boolean pressed);
   void newAxisMoveEvent(in unsigned long index, in unsigned long axis,
 			in double value);
 };
--- a/dom/tests/mochitest/gamepad/test_check_timestamp.html
+++ b/dom/tests/mochitest/gamepad/test_check_timestamp.html
@@ -6,24 +6,27 @@
   <title>Test Gamepad.timestamp</title>
   <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
 </head>
 <body>
 <script type="text/javascript" src="mock_gamepad.js"></script>
 <script class="testbody" type="text/javascript">
 SimpleTest.waitForExplicitFinish();
+
+// We need to start the child-side gamepad service if it hasn't been started
+// already, so add event listener first.
+var timea=0;
+window.addEventListener("gamepadbuttondown", buttonpresshandler);
+
 var index = GamepadService.addGamepad("test gamepad", // id
                                       SpecialPowers.Ci.nsIGamepadServiceTest.NO_MAPPING,
                                       4, // buttons
                                       2);// axes
 
-var timea=0;
-window.addEventListener("gamepadbuttondown", buttonpresshandler);
-
 var firstPress = true;
 GamepadService.newButtonEvent(index, 0, true);
 GamepadService.newButtonEvent(index, 0, true);
 
 function cleanup(){
   SpecialPowers.executeSoon(function() {
     GamepadService.removeGamepad(index);
     SimpleTest.finish();
--- a/dom/tests/mochitest/gamepad/test_gamepad.html
+++ b/dom/tests/mochitest/gamepad/test_gamepad.html
@@ -16,16 +16,17 @@ window.addEventListener("gamepadconnecte
 var index = GamepadService.addGamepad("test gamepad", // id
                                       SpecialPowers.Ci.nsIGamepadServiceTest.STANDARD_MAPPING,
                                       4, // buttons
                                       2);// axes
 // Press a button
 GamepadService.newButtonEvent(index, 0, true);
 function connecthandler(e) {
   ok(e.gamepad.timestamp <= performance.now());
+  is(e.gamepad.index, 0, "correct gamepad index");
   is(e.gamepad.id, "test gamepad", "correct gamepad name");
   is(e.gamepad.mapping, "standard", "standard mapping");
   is(e.gamepad.buttons.length, 4, "correct number of buttons");
   is(e.gamepad.axes.length, 2, "correct number of axes");
   SimpleTest.executeSoon(function() {
     GamepadService.removeGamepad(index);
     SimpleTest.finish();
   });
--- a/dom/tests/mochitest/gamepad/test_navigator_gamepads.html
+++ b/dom/tests/mochitest/gamepad/test_navigator_gamepads.html
@@ -56,16 +56,17 @@ function check_first_gamepad(e) {
   index2 = GamepadService.addGamepad("test gamepad 2", // id
                                      SpecialPowers.Ci.nsIGamepadServiceTest.NO_MAPPING,
                                      4, // buttons
                                      2);// axes
 }
 
 function check_second_gamepad(e) {
   // Second gamepad gets added.
+  is(e.gamepad.index, 1, "gamepad index should be 1")
   is(e.gamepad.id, "test gamepad 2", "correct gamepad name");
   var gamepads = navigator.getGamepads();
   is(gamepads.length, 2, "should have two gamepads exposed");
   is(gamepads[e.gamepad.index], e.gamepad, "right gamepad exposed at index");
   // Now remove the first one.
   GamepadService.removeGamepad(index1);
 }
 
--- a/layout/build/nsLayoutModule.cpp
+++ b/layout/build/nsLayoutModule.cpp
@@ -203,17 +203,17 @@ nsresult
 NS_NewXULTreeBuilder(nsISupports* aOuter, REFNSIID aIID, void** aResult);
 #endif
 
 static void Shutdown();
 
 #include "nsGeolocation.h"
 #include "nsDeviceSensors.h"
 #ifdef MOZ_GAMEPAD
-#include "mozilla/dom/GamepadService.h"
+#include "mozilla/dom/GamepadServiceTest.h"
 #endif
 #include "mozilla/dom/nsCSPService.h"
 #include "mozilla/dom/nsCSPContext.h"
 #include "nsICellBroadcastService.h"
 #include "nsISmsService.h"
 #include "nsIMmsService.h"
 #include "nsIMobileConnectionService.h"
 #include "nsIMobileMessageService.h"