From ebb0fdfb5bfb4995e5c1913a12a41690605e0269 Mon Sep 17 00:00:00 2001 From: Kyle Machulis Date: Thu, 3 Mar 2016 14:30:08 -0800 Subject: [PATCH] Bug 1156957 - Make Gamepad Mochitests work on e10s; r=ted --- dom/gamepad/GamepadFunctions.cpp | 1 - .../gamepad/gamepad_frame_state.html | 2 +- .../gamepad_service_test_chrome_script.js | 36 ++++++++++ dom/tests/mochitest/gamepad/mochitest.ini | 3 +- dom/tests/mochitest/gamepad/mock_gamepad.js | 72 ++++++++++++++++++- .../gamepad/test_check_timestamp.html | 31 ++++---- dom/tests/mochitest/gamepad/test_gamepad.html | 36 ++++++---- .../gamepad/test_gamepad_connect_events.html | 27 ++++--- .../test_gamepad_frame_state_sync.html | 41 +++++++---- .../gamepad/test_gamepad_hidden_frame.html | 29 ++++---- .../gamepad/test_navigator_gamepads.html | 34 +++++---- 11 files changed, 228 insertions(+), 84 deletions(-) create mode 100644 dom/tests/mochitest/gamepad/gamepad_service_test_chrome_script.js diff --git a/dom/gamepad/GamepadFunctions.cpp b/dom/gamepad/GamepadFunctions.cpp index 06e31c177f6a..eb7728845ae4 100644 --- a/dom/gamepad/GamepadFunctions.cpp +++ b/dom/gamepad/GamepadFunctions.cpp @@ -50,7 +50,6 @@ AddGamepad(const char* aID, gGamepadIndex++; GamepadAdded a(NS_ConvertUTF8toUTF16(nsDependentCString(aID)), index, (uint32_t)aMapping, aNumButtons, aNumAxes); - gGamepadIndex++; NotifyGamepadChange(a); return index; } diff --git a/dom/tests/mochitest/gamepad/gamepad_frame_state.html b/dom/tests/mochitest/gamepad/gamepad_frame_state.html index 740b6ca33bda..a53b91b2cbe8 100644 --- a/dom/tests/mochitest/gamepad/gamepad_frame_state.html +++ b/dom/tests/mochitest/gamepad/gamepad_frame_state.html @@ -8,7 +8,7 @@ var gamepad; window.addEventListener("gamepadconnected", function(e) { gamepad = e.gamepad; -}); + }); diff --git a/dom/tests/mochitest/gamepad/gamepad_service_test_chrome_script.js b/dom/tests/mochitest/gamepad/gamepad_service_test_chrome_script.js new file mode 100644 index 000000000000..34307872d48e --- /dev/null +++ b/dom/tests/mochitest/gamepad/gamepad_service_test_chrome_script.js @@ -0,0 +1,36 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +var { classes: Cc, interfaces: Ci, results: Cr, utils: Cu } = Components; +const { Services } = Cu.import('resource://gre/modules/Services.jsm'); + +var GamepadService = (function() { + return Cc["@mozilla.org/gamepad-test;1"].getService(Ci.nsIGamepadServiceTest); +})(); + + +addMessageListener('add-gamepad', function(message) { + var index = GamepadService.addGamepad(message.name, + message.mapping, + message.buttons, + message.axes); + sendAsyncMessage('new-gamepad-index', index); +}); + +addMessageListener('new-button-event', function(message) { + GamepadService.newButtonEvent(message.index, + message.button, + message.status); +}); + +addMessageListener('new-button-value-event', function(message) { + GamepadService.newButtonValueEvent(message.index, + message.button, + message.status, + message.value); +}); + +addMessageListener('remove-gamepad', function(message) { + GamepadService.removeGamepad(message.index); +}); + diff --git a/dom/tests/mochitest/gamepad/mochitest.ini b/dom/tests/mochitest/gamepad/mochitest.ini index 523f69f5f21f..cf586c0d241f 100644 --- a/dom/tests/mochitest/gamepad/mochitest.ini +++ b/dom/tests/mochitest/gamepad/mochitest.ini @@ -1,9 +1,10 @@ [DEFAULT] -skip-if=e10s || (buildapp == 'b2g' && toolkit != 'gonk') #Bug 931116, b2g desktop specific, initial triage +skip-if=(buildapp == 'b2g' && toolkit != 'gonk') #Bug 931116, b2g desktop specific, initial triage support-files = gamepad_frame.html gamepad_frame_state.html mock_gamepad.js + gamepad_service_test_chrome_script.js [test_check_timestamp.html] [test_gamepad.html] diff --git a/dom/tests/mochitest/gamepad/mock_gamepad.js b/dom/tests/mochitest/gamepad/mock_gamepad.js index 5c1c24256034..1a14ec149469 100644 --- a/dom/tests/mochitest/gamepad/mock_gamepad.js +++ b/dom/tests/mochitest/gamepad/mock_gamepad.js @@ -1,5 +1,71 @@ /* Any copyright is dedicated to the Public Domain. * http://creativecommons.org/publicdomain/zero/1.0/ */ -var GamepadService = (function() { - return SpecialPowers.Cc["@mozilla.org/gamepad-test;1"].getService(SpecialPowers.Ci.nsIGamepadServiceTest); -})(); + +// Determine whether this process is a parent or child process. +let appInfo = SpecialPowers.Cc["@mozilla.org/xre/app-info;1"]; +let isParentProcess = + !appInfo || appInfo.getService(SpecialPowers.Ci.nsIXULRuntime) + .processType == SpecialPowers.Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT; + +var GamepadService; +var GamepadScript; + +// sendAsyncMessage fails when running in process, so if we're not e10s, just +// use GamepadServiceTest directly. +if (isParentProcess) { + GamepadService = SpecialPowers.Cc["@mozilla.org/gamepad-test;1"].getService(SpecialPowers.Ci.nsIGamepadServiceTest); +} else { + // If we're e10s, use the proxy script to access GamepadServiceTest in the + // parent process. + let url = SimpleTest.getTestFileURL("gamepad_service_test_chrome_script.js"); + GamepadScript = SpecialPowers.loadChromeScript(url); + + GamepadService = { + addGamepad: function (name, mapping, buttons, axes) { + GamepadScript.sendAsyncMessage("add-gamepad", { + name: name, + mapping: mapping, + buttons: buttons, + axes: axes + }); + }, + newButtonEvent: function (index, button, status) { + GamepadScript.sendAsyncMessage("new-button-event", { + index: index, + button: button, + status: status + }); + }, + newButtonValueEvent: function (index, button, status, value) { + GamepadScript.sendAsyncMessage("new-button-value-event", { + index: index, + button: button, + status: status, + value: value + }); + }, + removeGamepad: function (index) { + GamepadScript.sendAsyncMessage("remove-gamepad", { + index: index + }); + }, + finish: function () { + GamepadScript.destroy(); + } + }; +} + +var addGamepad = function(name, mapping, buttons, axes) { + return new Promise((resolve, reject) => { + if (isParentProcess) { + resolve(GamepadService.addGamepad(name, mapping, buttons, axes)); + } else { + var listener = (index) => { + GamepadScript.removeMessageListener('new-gamepad-index', listener); + resolve(index); + }; + GamepadScript.addMessageListener('new-gamepad-index', listener); + GamepadService.addGamepad(name, mapping, buttons, axes); + } + }); +} diff --git a/dom/tests/mochitest/gamepad/test_check_timestamp.html b/dom/tests/mochitest/gamepad/test_check_timestamp.html index 338feafcc950..5f01ae5c125b 100644 --- a/dom/tests/mochitest/gamepad/test_check_timestamp.html +++ b/dom/tests/mochitest/gamepad/test_check_timestamp.html @@ -12,17 +12,22 @@ diff --git a/dom/tests/mochitest/gamepad/test_gamepad_connect_events.html b/dom/tests/mochitest/gamepad/test_gamepad_connect_events.html index 16bc6533dd65..08988a2ba59c 100644 --- a/dom/tests/mochitest/gamepad/test_gamepad_connect_events.html +++ b/dom/tests/mochitest/gamepad/test_gamepad_connect_events.html @@ -14,18 +14,27 @@ - + diff --git a/dom/tests/mochitest/gamepad/test_gamepad_frame_state_sync.html b/dom/tests/mochitest/gamepad/test_gamepad_frame_state_sync.html index 8d37a7e11a77..5f8b4e5fe4de 100644 --- a/dom/tests/mochitest/gamepad/test_gamepad_frame_state_sync.html +++ b/dom/tests/mochitest/gamepad/test_gamepad_frame_state_sync.html @@ -11,10 +11,8 @@ - - + + diff --git a/dom/tests/mochitest/gamepad/test_gamepad_hidden_frame.html b/dom/tests/mochitest/gamepad/test_gamepad_hidden_frame.html index f1e6d57443c4..3b9ae2d52e95 100644 --- a/dom/tests/mochitest/gamepad/test_gamepad_hidden_frame.html +++ b/dom/tests/mochitest/gamepad/test_gamepad_hidden_frame.html @@ -11,10 +11,6 @@ - - + + diff --git a/dom/tests/mochitest/gamepad/test_navigator_gamepads.html b/dom/tests/mochitest/gamepad/test_navigator_gamepads.html index 8a64999e7c11..a509401795e2 100644 --- a/dom/tests/mochitest/gamepad/test_navigator_gamepads.html +++ b/dom/tests/mochitest/gamepad/test_navigator_gamepads.html @@ -27,27 +27,29 @@ function run_next_test(event) { // gamepads should be empty first is(navigator.getGamepads().length, 0, "should be zero gamepads exposed"); -function connecthandler(e) { +function buttonhandler(e) { run_next_test(e); } function disconnecthandler(e) { run_next_test(e); } -window.addEventListener("gamepadconnected", connecthandler); +window.addEventListener("gamepadbuttondown", buttonhandler); window.addEventListener("gamepaddisconnected", disconnecthandler); // Add a gamepad -var internal_index1 = GamepadService.addGamepad("test gamepad 1", // id - SpecialPowers.Ci.nsIGamepadServiceTest.NO_MAPPING, - 4, // buttons - 2);// axes +addGamepad("test gamepad 1", // id + SpecialPowers.Ci.nsIGamepadServiceTest.NO_MAPPING, + 4, // buttons + 2).then(function(index) { + internal_index1 = index; + // Press a button to make the gamepad visible to the page. + GamepadService.newButtonEvent(internal_index1, 0, true); + }); + var content_index1 = 0; var internal_index2; var content_index2 = 1; -// Press a button to make the gamepad visible to the page. -GamepadService.newButtonEvent(internal_index1, 0, true); - function check_first_gamepad(e) { ok(true, "Checking first gamepad"); // First gamepad gets added. @@ -57,15 +59,18 @@ function check_first_gamepad(e) { is(gamepads[e.gamepad.index], e.gamepad, "right gamepad exposed at index"); is(gamepads[content_index1], e.gamepad, "gamepad counter working correctly"); // Add a second gamepad, should automatically show up. - internal_index2 = GamepadService.addGamepad("test gamepad 2", // id - SpecialPowers.Ci.nsIGamepadServiceTest.NO_MAPPING, - 4, // buttons - 2);// axes + addGamepad("test gamepad 2", // id + SpecialPowers.Ci.nsIGamepadServiceTest.NO_MAPPING, + 4, // buttons + 2).then(function(index) { + internal_index2 = index; + GamepadService.newButtonEvent(internal_index2, 0, true); + }); ok(true, "Done checking first gamepad"); } function check_second_gamepad(e) { - ok(true, "Checking seceond gamepad"); + ok(true, "Checking second gamepad"); // Second gamepad gets added. is(e.gamepad.index, 1, "gamepad index should be 1") is(e.gamepad.id, "test gamepad 2", "correct gamepad name"); @@ -100,4 +105,3 @@ function check_no_gamepads(e) { -