dom/tests/mochitest/gamepad/test_gamepad_frame_state_sync.html

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     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 hidden frames</title>
     7   <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
     8   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
     9 </head>
    10 <body>
    11 <script type="text/javascript" src="mock_gamepad.js"></script>
    12 <script class="testbody" type="text/javascript">
    13 SimpleTest.waitForExplicitFinish();
    14 var index = GamepadService.addGamepad("test gamepad", // id
    15                                       SpecialPowers.Ci.nsIGamepadServiceTest.NO_MAPPING,
    16                                       4, // buttons
    17                                       2);// axes
    19 function setFrameVisible(f, visible) {
    20   var Ci = SpecialPowers.Ci;
    21   var docshell = SpecialPowers.wrap(f.contentWindow).QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIWebNavigation).QueryInterface(Ci.nsIDocShell);
    22   docshell.isActive = visible;
    23 }
    25 var frames_loaded = 0;
    26 var f1, f2;
    27 function frame_loaded() {
    28   frames_loaded++;
    29   if (frames_loaded == 2) {
    30     f1 = document.getElementById('f1');
    31     f2 = document.getElementById('f2');
    32     // Now press the button, but don't release it.
    33     GamepadService.newButtonEvent(index, 0, true);
    34  }
    35 }
    37 window.addEventListener("gamepadbuttondown", function() {
    38   // Wait to ensure that all frames received the button press as well.
    39  SpecialPowers.executeSoon(tests[testNum++]);
    40 });
    42 var testNum = 0;
    43 var tests = [
    44   check_button_pressed,
    45   check_second_frame_no_button_press,
    46 ];
    48 function check_button_pressed() {
    49   // At this point the both frames should see the button as pressed.
    50   ok(f1.contentWindow.gamepad.buttons[0].pressed, "frame 1 sees button pressed");
    51   ok(f2.contentWindow.gamepad.buttons[0].pressed, "frame 2 sees button pressed");
    53   // Now release the button, then hide the second frame.
    54   GamepadService.newButtonEvent(index, 0, false);
    55   setFrameVisible(f2, false);
    56   SpecialPowers.executeSoon(function() {
    57     // Now press the button, but don't release it.
    58     GamepadService.newButtonEvent(index, 0, true);
    59   });
    60 }
    62 function check_second_frame_no_button_press () {
    63   /*
    64    * At this point the first frame should see the button as pressed,
    65    * but the second frame should not, since it's hidden.
    66    */
    67   ok(f1.contentWindow.gamepad.buttons[0].pressed, "frame 1 sees button pressed");
    68   ok(!f2.contentWindow.gamepad.buttons[0].pressed, "frame 2 should not see button pressed");
    70   // Now unhide the second frame.
    71   setFrameVisible(f2, true);
    72   SpecialPowers.executeSoon(function() {
    73     // Now that the frame is visible again, it should see the button
    74     // that was pressed.
    75     ok(f2.contentWindow.gamepad.buttons[0].pressed, "frame 2 sees button pressed");
    76     // cleanup
    77     GamepadService.removeGamepad(index);
    78     SimpleTest.finish();
    79   });
    80 }
    82 </script>
    83 <iframe id="f1" src="gamepad_frame_state.html" onload="frame_loaded()"></iframe>
    84 <iframe id="f2" src="gamepad_frame_state.html" onload="frame_loaded()"></iframe>
    85 </body>
    86 </html>

mercurial