Bug 1714703 - Promisify GamepadServiceTest r=tjr,peterv
[gecko.git] / dom / tests / mochitest / gamepad / test_gamepad_connect_events_iframe.html
blob278e6796c0dd739b491dd3c2312ec1a55e658925
1 <!-- Any copyright is dedicated to the Public Domain.
2 - http://creativecommons.org/publicdomain/zero/1.0/ -->
3 <!-- bug 893785 - Test that sending a gamepadconnected event to a new window
4 doesn't result in a gamepadconnected event being sent to existing
5 windows that have already received it. -->
6 <!DOCTYPE HTML>
7 <html>
8 <head>
9 <title>Test hidden frames</title>
10 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
11 </head>
12 <body>
13 <script type="text/javascript" src="mock_gamepad.js"></script>
14 <script class="testbody" type="text/javascript">
15 let ok = window.parent.ok;
16 let is = window.parent.is;
17 let SimpleTest = window.parent.SimpleTest;
18 let SpecialPowers = window.parent.SpecialPowers;
20 var gamepad_index;
22 async function pressButton() {
23 await GamepadService.newButtonEvent(gamepad_index, 0, true, true);
24 await GamepadService.newButtonEvent(gamepad_index, 0, false, false);
27 // Add a gamepad
28 async function startTests() {
29 window.addEventListener("gamepadbuttondown", function() {
30 // Wait to ensure that all frames received the button press as well.
31 SpecialPowers.executeSoon(tests[testNum++]);
32 });
34 gamepad_index = await GamepadService.addGamepad("test gamepad", // id
35 GamepadService.standardMapping,
36 GamepadService.noHand,
37 4, // buttons
43 await gamepad_connected();
46 var f1, f2;
47 async function gamepad_connected() {
48 f1 = document.getElementById('f1');
49 await pressButton();
52 var testNum = 0;
53 var tests = [
54 test1,
55 test2,
58 function test1() {
59 is(f1.contentWindow.connectedEvents, 1, "right number of connection events in frame 1");
61 // Now add another frame.
62 f2 = document.createElement("iframe");
63 f2.addEventListener("load", async () => {
64 // When the frame is loaded, press a button again.
65 await pressButton();
66 });
67 f2.src = "gamepad_frame.html";
68 document.body.appendChild(f2);
71 async function test2() {
72 is(f1.contentWindow.connectedEvents, 1, "right number of connection events in frame 1");
73 is(f2.contentWindow.connectedEvents, 1, "right number of connection events in frame 2");
74 is(f1.contentWindow.idlConnected, 1, "right number of IDL connection events in frame 1");
75 is(f2.contentWindow.idlConnected, 1, "right number of IDL connection events in frame 2");
76 await GamepadService.removeGamepad(gamepad_index);
77 SimpleTest.finish();
80 </script>
81 <iframe id="f1" src="gamepad_frame.html" onload="runGamepadTest(startTests)"></iframe>
82 </body>
83 </html>