Bug 852944 - Update mochitests to reflect new Gamepad API
authorKyle Machulis <kyle@nonpolynomial.com>
Tue, 24 Feb 2015 18:24:51 -0800
changeset 398156 61a558fbec42fa5f7c88f2eac5e5b1d9ec2b9019
parent 398155 de55cde3c4ec3bea31c2e83d834a41ff88085ab7
child 398157 88845229726b070bfdd74da3eb3a59be92a2661a
push id55657
push userkmachulis@mozilla.com
push dateWed, 25 Feb 2015 04:10:52 +0000
treeherdertry@88845229726b [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_gamepad.html
dom/tests/mochitest/gamepad/test_navigator_gamepads.html
layout/build/nsLayoutModule.cpp
--- a/dom/gamepad/GamepadService.cpp
+++ b/dom/gamepad/GamepadService.cpp
@@ -565,79 +565,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
@@ -21,17 +21,18 @@ class nsIDOMDocument;
 
 namespace mozilla {
 namespace dom {
 
 class EventTarget;
 
 class GamepadService : public nsIObserver
 {
- public:
+  friend class GamepadServiceTest;
+public:
   NS_DECL_ISUPPORTS
   NS_DECL_NSIOBSERVER
 
   // Returns true if we actually have a service up and running
   static bool IsServiceRunning();
   // Get the singleton service
   static already_AddRefed<GamepadService> GetService();
   // Return true if the API is preffed on.
@@ -129,33 +130,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,79 @@
+/* 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 "mozilla/Hal.h"
+
+#include "GamepadServiceTest.h"
+#include "GamepadService.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()
+{
+  /* member initializers and constructor code */
+  nsRefPtr<GamepadService> service = GamepadService::GetService();
+}
+
+/* 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,38 @@
+/* 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 <stdint.h>
+#include "nsAutoPtr.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);
+                           in unsigned long numAxes);
   void removeGamepad(in unsigned long index);
   void newButtonEvent(in unsigned long index, in unsigned long button,
-		      in boolean pressed);
+                      in boolean pressed);
   void newAxisMoveEvent(in unsigned long index, in unsigned long axis,
-			in double value);
+                        in double value);
 };
--- 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
@@ -36,17 +36,17 @@ function disconnecthandler(e) {
 }
 window.addEventListener("gamepadconnected", connecthandler);
 window.addEventListener("gamepaddisconnected", disconnecthandler);
 // Add a gamepad
 var index1 = GamepadService.addGamepad("test gamepad 1", // id
                                        SpecialPowers.Ci.nsIGamepadServiceTest.NO_MAPPING,
                                        4, // buttons
                                        2);// axes
-var index2;
+var index2 = 1;
 
 // Press a button to make the gamepad visible to the page.
 GamepadService.newButtonEvent(index1, 0, true);
 
 function check_first_gamepad(e) {
   // First gamepad gets added.
   is(e.gamepad.id, "test gamepad 1", "correct gamepad name");
   var gamepads = navigator.getGamepads();
@@ -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
@@ -204,16 +204,17 @@ NS_NewXULTreeBuilder(nsISupports* aOuter
 #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"