test_navigator_gamepads_iframe.html (4017B)
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 isnot = window.parent.isnot; 15 let SimpleTest = window.parent.SimpleTest; 16 let SpecialPowers = window.parent.SpecialPowers; 17 18 var content_index1 = 0; 19 var internal_index2; 20 var content_index2 = 1; 21 22 var testNum = 0; 23 var tests = [ 24 check_first_gamepad, 25 check_second_gamepad, 26 check_gamepad_hole, 27 check_no_gamepads, 28 ]; 29 30 function run_next_test(event) { 31 SpecialPowers.executeSoon(async function() { 32 await tests[testNum++](event); 33 }); 34 } 35 36 function buttonhandler(e) { 37 run_next_test(e); 38 } 39 40 function disconnecthandler(e) { 41 run_next_test(e); 42 } 43 window.addEventListener("gamepadbuttondown", buttonhandler); 44 window.addEventListener("gamepaddisconnected", disconnecthandler); 45 46 runGamepadTest(startTest) 47 48 async function startTest() { 49 // gamepads should be empty first 50 is(navigator.getGamepads().length, 0, "should be zero gamepads exposed"); 51 // Add a gamepad 52 internal_index1 = await GamepadService.addGamepad("test gamepad 1", // id 53 GamepadService.standardMapping, 54 GamepadService.noHand, 55 4, // buttons 56 2, 57 0, 58 0, 59 0); 60 61 // Press a button to make the gamepad visible to the page. 62 await GamepadService.newButtonEvent(internal_index1, 0, true, true); 63 } 64 65 async function check_first_gamepad(e) { 66 ok(true, "Checking first gamepad"); 67 // First gamepad gets added. 68 is(e.gamepad.id, "test gamepad 1", "correct gamepad name"); 69 var gamepads = navigator.getGamepads(); 70 is(gamepads.length, 1, "should have one gamepad exposed"); 71 is(gamepads[e.gamepad.index], e.gamepad, "right gamepad exposed at index"); 72 is(gamepads[content_index1], e.gamepad, "gamepad counter working correctly"); 73 // Add a second gamepad, should automatically show up. 74 internal_index2 = await GamepadService.addGamepad("test gamepad 2", // id 75 GamepadService.standardMapping, 76 GamepadService.noHand, 77 4, // buttons 78 2, 79 0, 80 0, 81 0); 82 83 await GamepadService.newButtonEvent(internal_index2, 0, true, true); 84 85 ok(true, "Done checking first gamepad"); 86 } 87 88 async function check_second_gamepad(e) { 89 ok(true, "Checking second gamepad"); 90 // Second gamepad gets added. 91 is(e.gamepad.index, 1, "gamepad index should be 1") 92 is(e.gamepad.id, "test gamepad 2", "correct gamepad name"); 93 var gamepads = navigator.getGamepads(); 94 is(gamepads.length, 2, "should have two gamepads exposed"); 95 is(gamepads[e.gamepad.index], e.gamepad, "right gamepad exposed at index"); 96 is(gamepads[content_index2], e.gamepad, "gamepad counter working correctly"); 97 // Now remove the first one. 98 await GamepadService.removeGamepad(internal_index1); 99 ok(true, "Done checking second gamepad"); 100 } 101 102 async function check_gamepad_hole(e) { 103 ok(true, "Checking gamepad hole"); 104 // First gamepad gets removed. 105 var gamepads = navigator.getGamepads(); 106 is(gamepads.length, 2, "gamepads should have two entries"); 107 is(gamepads[content_index1], null, "should be a hole in the gamepad list"); 108 isnot(gamepads[content_index2], null, "second gamepad should exist"); 109 // Now remove the second one. 110 await GamepadService.removeGamepad(internal_index2); 111 ok(true, "Done checking gamepad hole"); 112 } 113 114 function check_no_gamepads(e) { 115 ok(true, "Checking no gamepads"); 116 // Second gamepad gets removed. 117 var gamepads = navigator.getGamepads(); 118 is(gamepads.length, 0, "gamepads should be empty"); 119 SimpleTest.finish(); 120 } 121 </script> 122 </body> 123 </html>