Bug 1156957 - Make gamepad mochitests work on e10s; r=ted

This commit is contained in:
Kyle Machulis 2016-03-04 13:31:12 -08:00
Родитель efed9717c0
Коммит 70f7183ad7
11 изменённых файлов: 228 добавлений и 84 удалений

Просмотреть файл

@ -50,7 +50,6 @@ AddGamepad(const char* aID,
gGamepadIndex++;
GamepadAdded a(NS_ConvertUTF8toUTF16(nsDependentCString(aID)), index,
(uint32_t)aMapping, aNumButtons, aNumAxes);
gGamepadIndex++;
NotifyGamepadChange<GamepadAdded>(a);
return index;
}

Просмотреть файл

@ -8,7 +8,7 @@
var gamepad;
window.addEventListener("gamepadconnected", function(e) {
gamepad = e.gamepad;
});
});
</script>
</head>
<body>

Просмотреть файл

@ -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);
});

Просмотреть файл

@ -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]

Просмотреть файл

@ -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);
}
});
}

Просмотреть файл

@ -12,17 +12,22 @@
<script class="testbody" type="text/javascript">
SimpleTest.waitForExplicitFinish();
var index = GamepadService.addGamepad("test gamepad", // id
SpecialPowers.Ci.nsIGamepadServiceTest.NO_MAPPING,
4, // buttons
2);// axes
var index;
addGamepad("test gamepad 1", // id
SpecialPowers.Ci.nsIGamepadServiceTest.NO_MAPPING,
4, // buttons
2).then(function(i) {
index = i;
// Press a button to make the gamepad visible to the page.
GamepadService.newButtonEvent(index, 0, true);
GamepadService.newButtonEvent(index, 0, true);
});
var timea=0;
window.addEventListener("gamepadbuttondown", buttonpresshandler);
var firstPress = true;
GamepadService.newButtonEvent(index, 0, true);
GamepadService.newButtonEvent(index, 0, true);
var testOver = false;
function cleanup(){
SpecialPowers.executeSoon(function() {
@ -32,17 +37,19 @@ function cleanup(){
}
function buttonpresshandler(e) {
if(timea == 0){
timea = e.gamepad.timestamp;
if (testOver) {
return;
}
else{
ok(timea <= e.gamepad.timestamp);
if (timea == 0){
timea = e.gamepad.timestamp;
} else {
ok(timea <= e.gamepad.timestamp, "Timestamp less than last timestamp");
}
GamepadService.newButtonEvent(index, 0, false);
if (!firstPress) {
testOver = true;
SpecialPowers.executeSoon(cleanup);
}
else {
} else {
firstPress = false;
}
}

Просмотреть файл

@ -11,13 +11,23 @@
<script type="text/javascript" src="mock_gamepad.js"></script>
<script class="testbody" type="text/javascript">
SimpleTest.waitForExplicitFinish();
// Due to gamepad being a polling API instead of event driven, test ordering
// ends up being a little weird in order to deal with e10s. Calls to
// GamepadService are async across processes, so we'll need to make sure
// we account for timing before checking values.
window.addEventListener("gamepadconnected", connecthandler);
var index;
// Add a gamepad
var index = GamepadService.addGamepad("test gamepad", // id
SpecialPowers.Ci.nsIGamepadServiceTest.STANDARD_MAPPING,
4, // buttons
2);// axes
GamepadService.newButtonEvent(index, 0, true);
addGamepad("test gamepad", // id
SpecialPowers.Ci.nsIGamepadServiceTest.STANDARD_MAPPING,
4,
2).then(function(i) {
index = i;
// Simulate button events on the gamepad we added
GamepadService.newButtonEvent(index, 0, true);
GamepadService.newButtonValueEvent(index, 1, true, 0.5);
});
function connecthandler(e) {
ok(e.gamepad.timestamp <= performance.now(),
"gamepad.timestamp should less than or equal to performance.now()");
@ -26,21 +36,17 @@ function connecthandler(e) {
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");
// Press a button
GamepadService.newButtonEvent(index, 0, true);
gamepads = navigator.getGamepads();
is(gamepads[0].buttons[0].pressed, true, "gamepad button should register as pressed")
GamepadService.newButtonValueEvent(index, 1, true, 0.5);
gamepads = navigator.getGamepads();
is(gamepads[0].buttons[1].pressed, true, "gamepad button should register as pressed")
is(gamepads[0].buttons[1].value, 0.5, "gamepad button value should be 0.5")
// Execute button event tests later, since we need to make sure button
// event updates executed on the parent process first.
SimpleTest.executeSoon(function() {
gamepads = navigator.getGamepads();
is(gamepads[0].buttons[0].pressed, true, "gamepad button should register as pressed");
is(gamepads[0].buttons[1].pressed, true, "gamepad button should register as pressed");
is(gamepads[0].buttons[1].value, 0.5, "gamepad button value should be 0.5");
GamepadService.removeGamepad(index);
SimpleTest.finish();
});
}
</script>
</body>
</html>

Просмотреть файл

@ -14,18 +14,27 @@
<script type="text/javascript" src="mock_gamepad.js"></script>
<script class="testbody" type="text/javascript">
SimpleTest.waitForExplicitFinish();
var index = GamepadService.addGamepad("test gamepad", // id
SpecialPowers.Ci.nsIGamepadServiceTest.NO_MAPPING,
4, // buttons
2);// axes
var gamepad_index;
function pressButton() {
GamepadService.newButtonEvent(index, 0, true);
GamepadService.newButtonEvent(index, 0, false);
GamepadService.newButtonEvent(gamepad_index, 0, true);
GamepadService.newButtonEvent(gamepad_index, 0, false);
}
// Add a gamepad
function startTests() {
addGamepad("test gamepad", // id
SpecialPowers.Ci.nsIGamepadServiceTest.STANDARD_MAPPING,
4, // buttons
2).then(function(i) {
gamepad_index = i;
gamepad_connected()
});
}
var f1, f2;
function frame_loaded() {
function gamepad_connected() {
f1 = document.getElementById('f1');
pressButton();
}
@ -57,11 +66,11 @@ function test1() {
function test2() {
is(f1.contentWindow.connectedEvents, 1, "right number of connection events in frame 1");
is(f2.contentWindow.connectedEvents, 1, "right number of connection events in frame 2");
GamepadService.removeGamepad(index);
GamepadService.removeGamepad(gamepad_index);
SimpleTest.finish();
}
</script>
<iframe id="f1" src="gamepad_frame.html" onload="frame_loaded()"></iframe>
<iframe id="f1" src="gamepad_frame.html" onload="startTests()"></iframe>
</body>
</html>

Просмотреть файл

@ -11,10 +11,8 @@
<script type="text/javascript" src="mock_gamepad.js"></script>
<script class="testbody" type="text/javascript">
SimpleTest.waitForExplicitFinish();
var index = GamepadService.addGamepad("test gamepad", // id
SpecialPowers.Ci.nsIGamepadServiceTest.NO_MAPPING,
4, // buttons
2);// axes
// Add a gamepad
var index;
function setFrameVisible(f, visible) {
var Ci = SpecialPowers.Ci;
@ -23,15 +21,24 @@ function setFrameVisible(f, visible) {
}
var frames_loaded = 0;
var f1, f2;
function frame_loaded() {
function startTest() {
frames_loaded++;
if (frames_loaded == 2) {
f1 = document.getElementById('f1');
f2 = document.getElementById('f2');
// Now press the button, but don't release it.
GamepadService.newButtonEvent(index, 0, true);
}
addGamepad("test gamepad", // id
SpecialPowers.Ci.nsIGamepadServiceTest.STANDARD_MAPPING,
4, // buttons
2).then(function(i) {
index = i;
gamepad_loaded();
});
}
}
var f1, f2;
function gamepad_loaded() {
f1 = document.getElementById('f1');
f2 = document.getElementById('f2');
// Now press the button, but don't release it.
GamepadService.newButtonEvent(index, 0, true);
}
window.addEventListener("gamepadbuttondown", function() {
@ -52,10 +59,14 @@ function check_button_pressed() {
// Now release the button, then hide the second frame.
GamepadService.newButtonEvent(index, 0, false);
setFrameVisible(f2, false);
SpecialPowers.executeSoon(function() {
// Now press the button, but don't release it.
GamepadService.newButtonEvent(index, 0, true);
ok(!f1.contentWindow.gamepad.buttons[0].pressed, "frame 1 no button pressed");
ok(!f2.contentWindow.gamepad.buttons[0].pressed, "frame 2 no button pressed");
setFrameVisible(f2, false);
SpecialPowers.executeSoon(function() {
GamepadService.newButtonEvent(index, 0, true);
});
});
}
@ -80,7 +91,7 @@ function check_second_frame_no_button_press () {
}
</script>
<iframe id="f1" src="gamepad_frame_state.html" onload="frame_loaded()"></iframe>
<iframe id="f2" src="gamepad_frame_state.html" onload="frame_loaded()"></iframe>
<iframe id="f1" src="gamepad_frame_state.html" onload="startTest()"></iframe>
<iframe id="f2" src="gamepad_frame_state.html" onload="startTest()"></iframe>
</body>
</html>

Просмотреть файл

@ -11,10 +11,6 @@
<script type="text/javascript" src="mock_gamepad.js"></script>
<script class="testbody" type="text/javascript">
SimpleTest.waitForExplicitFinish();
var index = GamepadService.addGamepad("test gamepad", // id
SpecialPowers.Ci.nsIGamepadServiceTest.NO_MAPPING,
4, // buttons
2);// axes
function pressButton() {
GamepadService.newButtonEvent(index, 0, true);
@ -28,15 +24,24 @@ function setFrameVisible(f, visible) {
}
var frames_loaded = 0;
var f1, f2;
function frame_loaded() {
function startTest() {
frames_loaded++;
if (frames_loaded == 2) {
f1 = document.getElementById('f1');
f2 = document.getElementById('f2');
pressButton();
if (frames_loaded == 2) {
addGamepad("test gamepad", // id
SpecialPowers.Ci.nsIGamepadServiceTest.STANDARD_MAPPING,
4, // buttons
2).then(function(i) {
index = i;
gamepad_loaded();
});
}
}
var f1, f2;
function gamepad_loaded() {
f1 = document.getElementById('f1');
f2 = document.getElementById('f2');
pressButton();
}
window.addEventListener("gamepadbuttondown", function() {
// Wait to ensure that all frames received the button press as well.
@ -66,7 +71,7 @@ function test2() {
}
</script>
<iframe id="f1" src="gamepad_frame.html" onload="frame_loaded()"></iframe>
<iframe id="f2" src="gamepad_frame.html" onload="frame_loaded()"></iframe>
<iframe id="f1" src="gamepad_frame.html" onload="startTest()"></iframe>
<iframe id="f2" src="gamepad_frame.html" onload="startTest()"></iframe>
</body>
</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) {
</script>
</body>
</html>