test_gamepad_extensions_iframe.html (6138B)
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 var tests = [ 18 poseadd, 19 posecheck, 20 touchadd, 21 touchcheck, 22 haptictest // Keep haptictest at the last because it will test removing gamepad from the service. 23 ]; 24 var gamepad_index = 0; 25 var testNum = 0; 26 var poseOrient = new Float32Array([-0.203, -0.235, 0.740, -0.596]); 27 var posePos = new Float32Array([-0.0233, -0.707, -0.763]); 28 var poseAngVel = new Float32Array([-0.0008, 0.00147, 0.001]); 29 var poseAngAcc = new Float32Array([-0.494, 0.476, -0.241]); 30 var poseLinVel = new Float32Array([0.003,0.024,-0.068]); 31 var poseLinAcc = new Float32Array([-1.211,21.427,-2.348]); 32 var touchData = [{touchId: 0, surfaceId: 0, pos: new Float32Array([-0.5, 0.5]), surf: new Float32Array([100, 100])}, 33 {touchId: 1, surfaceId: 0, pos: new Float32Array([-0.1, 1.0]), surf: new Float32Array([100, 100])}]; 34 35 window.addEventListener("gamepadconnected", connecthandler); 36 window.addEventListener("gamepadbuttondown", function() { 37 // Wait to ensure that all frames received the button press as well. 38 SpecialPowers.executeSoon(async ()=> { 39 await tests[testNum++]() 40 }); 41 }); 42 43 async function pressButton() { 44 await GamepadService.newButtonEvent(gamepad_index, 0, true, true); 45 await GamepadService.newButtonEvent(gamepad_index, 0, false, false); 46 } 47 48 async function startTest() { 49 await SpecialPowers.pushPrefEnv({ "set": [ 50 ["dom.gamepad.extensions.enabled", true], 51 ["dom.gamepad.extensions.lightindicator", true], 52 ["dom.gamepad.extensions.multitouch", true]] }); 53 // Add a gamepad 54 gamepad_index = await GamepadService.addGamepad("test gamepad", // id 55 GamepadService.standardMapping, 56 GamepadService.leftHand, 57 4, 58 2, 59 1, 60 1, 61 2); 62 63 // Simulate button events on the gamepad we added 64 await pressButton(); 65 66 } 67 68 function connecthandler(e) { 69 ok(e.gamepad.timestamp <= performance.now(), 70 "gamepad.timestamp should less than or equal to performance.now()"); 71 is(e.gamepad.index, 0, "correct gamepad index"); 72 is(e.gamepad.id, "test gamepad", "correct gamepad name"); 73 is(e.gamepad.mapping, "standard", "standard mapping"); 74 is(e.gamepad.hand, "left", "left hand"); 75 is(e.gamepad.buttons.length, 4, "correct number of buttons"); 76 is(e.gamepad.axes.length, 2, "correct number of axes"); 77 is(e.gamepad.hapticActuators.length, 1, "correct number of haptics"); 78 is(e.gamepad.lightIndicators.length, 1, "correct number of light indicators"); 79 is(e.gamepad.touchEvents.length, 2, "correct number of touches"); 80 } 81 82 function checkValueInFloat32Array(array1, array2) { 83 if (array1.length != array2.length) { 84 return false; 85 } 86 var index = 0; 87 while (index < array2.length) { 88 if (array1[index] != array2[index]) { 89 return false; 90 } 91 ++index; 92 } 93 return true; 94 } 95 96 async function poseadd() { 97 await GamepadService.newPoseMove(gamepad_index, poseOrient, 98 posePos, poseAngVel, poseAngAcc, 99 poseLinVel, poseLinAcc); 100 await pressButton(); 101 } 102 103 async function posecheck() { 104 var gamepads = navigator.getGamepads(); 105 var pose = gamepads[0].pose; 106 is(gamepads[0].pose.hasOrientation, true, 107 "correct gamepadPose hasOrientation"); 108 is(gamepads[0].pose.hasPosition, true, 109 "correct gamepadPose hasPosition"); 110 is(checkValueInFloat32Array(pose.orientation, poseOrient), true, 111 "correct gamepadPose orientation"); 112 is(checkValueInFloat32Array(pose.position, posePos), true, 113 "correct gamepadPose position"); 114 is(checkValueInFloat32Array(pose.angularVelocity, poseAngVel), true, 115 "correct gamepadPose angularVelocity"); 116 is(checkValueInFloat32Array(pose.angularAcceleration, poseAngAcc), true, 117 "correct gamepadPose angularAcceleration"); 118 is(checkValueInFloat32Array(pose.linearVelocity, poseLinVel), true, 119 "correct gamepadPose linearVelocity"); 120 is(checkValueInFloat32Array(pose.linearAcceleration, poseLinAcc), true, 121 "correct gamepadPose linearAcceleration"); 122 await pressButton(); 123 } 124 125 function haptictest() { 126 var gamepads = navigator.getGamepads(); 127 var hapticActuators = gamepads[0].hapticActuators[0]; 128 hapticActuators.pulse(1, 100).then(async function(result) { 129 is(result, true, "gamepad hapticActuators test success."); 130 await GamepadService.removeGamepad(gamepad_index); 131 SimpleTest.finish(); 132 }); 133 } 134 135 async function touchadd() { 136 for(var count = 0; count < touchData.length; count++) { 137 const touch = touchData[count]; 138 await GamepadService.newTouch(gamepad_index, count, touch.touchId, 139 touch.surfaceId, touch.pos, 140 touch.surf); 141 } 142 await pressButton(); 143 } 144 145 async function touchcheck() { 146 var gamepads = navigator.getGamepads(); 147 var touches = gamepads[0].touchEvents; 148 149 is(touches.length, 2, " number of touches"); 150 151 var count = 0; 152 touches.forEach(function(touch) { 153 is(touch.touchId, touchData[count].touchId, 154 "correct GamepadTouch touchId"); 155 is(touch.surfaceId, touchData[count].surfaceId, 156 "correct GamepadTouch surfaceId"); 157 is(checkValueInFloat32Array(touch.position, touchData[count].pos), true, 158 "correct touch position"); 159 is(checkValueInFloat32Array(touch.surfaceDimensions, touchData[count].surf), true, 160 "correct touch surfaceDimensions"); 161 162 ++count; 163 }); 164 165 await pressButton(); 166 } 167 168 </script> 169 <iframe id="f1" src="gamepad_frame_state.html" onload="runGamepadTest(startTest)"></iframe> 170 </body> 171 </html>