test_gamepad_iframe.html (2949B)
1 <!-- Any copyright is dedicated to the Public Domain. 2 - http://creativecommons.org/publicdomain/zero/1.0/ --> 3 <!DOCTYPE HTML> 4 <html> 5 <head> 6 <title>Test gamepad</title> 7 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 8 </head> 9 <body> 10 <script type="text/javascript" src="mock_gamepad.js"></script> 11 <script class="testbody" type="text/javascript"> 12 let ok = window.parent.ok; 13 let is = window.parent.is; 14 let SimpleTest = window.parent.SimpleTest; 15 let SpecialPowers = window.parent.SpecialPowers; 16 17 // This should be held for the entire time the testing API is being used to 18 // ensure monitoring has been started and that a single GamepadPlatformService 19 // instance is used for the entire test 20 var GamepadsKungFuDeathGrip = navigator.getGamepads(); 21 22 // Due to gamepad being a polling API instead of event driven, test ordering 23 // ends up being a little weird in order to deal with e10s. Calls to 24 // GamepadService are async across processes, so we'll need to make sure 25 // we account for timing before checking values. 26 window.addEventListener("gamepadconnected", connecthandler); 27 var index; 28 var testNum = 0; 29 30 window.addEventListener("gamepadbuttondown", () => { 31 SpecialPowers.executeSoon(buttontest1); 32 }); 33 34 window.addEventListener("gamepadbuttonup", () => { 35 SpecialPowers.executeSoon(buttontest2); 36 }); 37 38 runGamepadTest(startTest); 39 40 async function startTest() { 41 // Add a gamepad 42 index = await GamepadService.addGamepad("test gamepad", // id 43 GamepadService.standardMapping, 44 GamepadService.noHand, 45 4, 46 2, 47 0, 48 0, 49 0); 50 await GamepadService.newButtonEvent(index, 0, true, true); 51 } 52 53 function connecthandler(e) { 54 ok(e.gamepad.timestamp <= performance.now(), 55 "gamepad.timestamp should less than or equal to performance.now()"); 56 is(e.gamepad.index, 0, "correct gamepad index"); 57 is(e.gamepad.id, "test gamepad", "correct gamepad name"); 58 is(e.gamepad.mapping, "standard", "standard mapping"); 59 is(e.gamepad.buttons.length, 4, "correct number of buttons"); 60 is(e.gamepad.axes.length, 2, "correct number of axes"); 61 } 62 63 async function buttontest1() { 64 var gamepads = navigator.getGamepads(); 65 is(gamepads[0].buttons[0].pressed, true, "gamepad button should register as pressed"); 66 is(gamepads[0].buttons[0].touched, true, "gamepad button should register as touched"); 67 await GamepadService.newButtonValueEvent(index, 1, true, true, 0.5); 68 } 69 70 async function buttontest2() { 71 var gamepads = navigator.getGamepads(); 72 is(gamepads[0].buttons[1].pressed, true, "gamepad button should register as pressed"); 73 is(gamepads[0].buttons[1].touched, true, "gamepad button should register as touched"); 74 is(gamepads[0].buttons[1].value, 0.5, "gamepad button value should be 0.5"); 75 await GamepadService.removeGamepad(index); 76 SimpleTest.finish(); 77 } 78 79 </script> 80 </body> 81 </html>