dom/tests/mochitest/gamepad/test_navigator_gamepads.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/tests/mochitest/gamepad/test_navigator_gamepads.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,91 @@
     1.4 +<!-- Any copyright is dedicated to the Public Domain.
     1.5 +   - http://creativecommons.org/publicdomain/zero/1.0/ -->
     1.6 +<!DOCTYPE HTML>
     1.7 +<html>
     1.8 +<head>
     1.9 +  <title>Test gamepad</title>
    1.10 +  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
    1.11 +  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
    1.12 +</head>
    1.13 +<body>
    1.14 +<script type="text/javascript" src="mock_gamepad.js"></script>
    1.15 +<script class="testbody" type="text/javascript">
    1.16 +SimpleTest.waitForExplicitFinish();
    1.17 +
    1.18 +var testNum = 0;
    1.19 +var tests = [
    1.20 +  check_first_gamepad,
    1.21 +  check_second_gamepad,
    1.22 +  check_gamepad_hole,
    1.23 +  check_no_gamepads,
    1.24 +];
    1.25 +
    1.26 +function run_next_test(event) {
    1.27 +  SpecialPowers.executeSoon(function() { tests[testNum++](event); });
    1.28 +}
    1.29 +
    1.30 +// gamepads should be empty first
    1.31 +is(navigator.getGamepads().length, 0, "should be zero gamepads exposed");
    1.32 +
    1.33 +function connecthandler(e) {
    1.34 + run_next_test(e);
    1.35 +}
    1.36 +
    1.37 +function disconnecthandler(e) {
    1.38 +  run_next_test(e);
    1.39 +}
    1.40 +window.addEventListener("gamepadconnected", connecthandler);
    1.41 +window.addEventListener("gamepaddisconnected", disconnecthandler);
    1.42 +// Add a gamepad
    1.43 +var index1 = GamepadService.addGamepad("test gamepad 1", // id
    1.44 +                                       SpecialPowers.Ci.nsIGamepadServiceTest.NO_MAPPING,
    1.45 +                                       4, // buttons
    1.46 +                                       2);// axes
    1.47 +var index2;
    1.48 +
    1.49 +// Press a button to make the gamepad visible to the page.
    1.50 +GamepadService.newButtonEvent(index1, 0, true);
    1.51 +
    1.52 +function check_first_gamepad(e) {
    1.53 +  // First gamepad gets added.
    1.54 +  is(e.gamepad.id, "test gamepad 1", "correct gamepad name");
    1.55 +  var gamepads = navigator.getGamepads();
    1.56 +  is(gamepads.length, 1, "should have one gamepad exposed");
    1.57 +  is(gamepads[e.gamepad.index], e.gamepad, "right gamepad exposed at index");
    1.58 +  // Add a second gamepad, should automatically show up.
    1.59 +  index2 = GamepadService.addGamepad("test gamepad 2", // id
    1.60 +                                     SpecialPowers.Ci.nsIGamepadServiceTest.NO_MAPPING,
    1.61 +                                     4, // buttons
    1.62 +                                     2);// axes
    1.63 +}
    1.64 +
    1.65 +function check_second_gamepad(e) {
    1.66 +  // Second gamepad gets added.
    1.67 +  is(e.gamepad.id, "test gamepad 2", "correct gamepad name");
    1.68 +  var gamepads = navigator.getGamepads();
    1.69 +  is(gamepads.length, 2, "should have two gamepads exposed");
    1.70 +  is(gamepads[e.gamepad.index], e.gamepad, "right gamepad exposed at index");
    1.71 +  // Now remove the first one.
    1.72 +  GamepadService.removeGamepad(index1);
    1.73 +}
    1.74 +
    1.75 +function check_gamepad_hole(e) {
    1.76 +  // First gamepad gets removed.
    1.77 +  var gamepads = navigator.getGamepads();
    1.78 +  is(gamepads.length, 2, "gamepads should have two entries");
    1.79 +  is(gamepads[index1], null, "should be a hole in the gamepad list");
    1.80 +  isnot(gamepads[index2], null, "second gamepad should exist");
    1.81 +  // Now remove the second one.
    1.82 +  GamepadService.removeGamepad(index2);
    1.83 +}
    1.84 +
    1.85 +function check_no_gamepads(e) {
    1.86 +  // Second gamepad gets removed.
    1.87 +  var gamepads = navigator.getGamepads();
    1.88 +  is(gamepads.length, 0, "gamepads should be empty");
    1.89 +  SimpleTest.finish();
    1.90 +}
    1.91 +</script>
    1.92 +</body>
    1.93 +</html>
    1.94 +

mercurial