tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_gamepad_multitouch_crossorigin_iframe.html (6773B)


      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 let tests = [
     18  touchAdd,
     19  touchcheck1,
     20  touchAdd,
     21  touchcheck2
     22 ];
     23 
     24 let gamepad_index;
     25 let testNum = 0;
     26 let touchData1 = [{touchId: 0, surfaceId: 0, pos: new Float32Array([-0.5, 0.5]), surf: new Float32Array([100, 100])},
     27                  {touchId: 1, surfaceId: 0, pos: new Float32Array([-0.1, 1.0]), surf: new Float32Array([100, 100])}];
     28 let touchData2 = [{touchId: 2, surfaceId: 0, pos: new Float32Array([-0.2, 0.3]), surf: new Float32Array([120, 200])},
     29                  {touchId: 3, surfaceId: 0, pos: new Float32Array([-0.4, 0.7]), surf: new Float32Array([120, 200])}];
     30 
     31 let data = [
     32  touchData1,
     33  touchData2
     34 ];
     35 let dataNum = 0;
     36 
     37 window.addEventListener("gamepadconnected", connecthandler);
     38 window.addEventListener("gamepadbuttondown", function() {
     39  // Wait to ensure that all frames received the button press as well.
     40  ok(true, "gamepadbuttondown");
     41  SpecialPowers.executeSoon(tests[testNum++]);
     42 });
     43 
     44 async function pressButton() {
     45  await GamepadService.newButtonEvent(gamepad_index, 0, true, true);
     46  await GamepadService.newButtonEvent(gamepad_index, 0, false, false);
     47 }
     48 
     49 let frames_loaded = 0;
     50 async function startTest() {
     51  frames_loaded++;
     52  let promise = SpecialPowers.pushPrefEnv({ "set": [
     53                              ["dom.gamepad.extensions.enabled", true],
     54                              ["dom.gamepad.extensions.lightindicator", true],
     55                              ["dom.gamepad.extensions.multitouch", true]] });
     56  if (frames_loaded == 2) {
     57    await promise;
     58    // Add a gamepad
     59    gamepad_index = await GamepadService.addGamepad("test gamepad", // id
     60                        GamepadService.standardMapping,
     61                        GamepadService.leftHand,
     62                        4,
     63                        2,
     64                        1,
     65                        1,
     66                        2)
     67    await gamepad_loaded();
     68  }
     69 }
     70 
     71 let f1, f2;
     72 async function gamepad_loaded() {
     73  f1 = document.getElementById('f1');
     74  f2 = document.getElementById('f2');
     75  await GamepadService.newButtonEvent(gamepad_index, 0, true, true);
     76 }
     77 
     78 function connecthandler(e) {
     79  ok(e.gamepad.timestamp <= performance.now(),
     80    "gamepad.timestamp should less than or equal to performance.now()");
     81  is(e.gamepad.index, 0, "correct gamepad index");
     82  is(e.gamepad.id, "test gamepad", "correct gamepad name");
     83  is(e.gamepad.mapping, "standard", "standard mapping");
     84  is(e.gamepad.hand, "left", "left hand");
     85  is(e.gamepad.buttons.length, 4, "correct number of buttons");
     86  is(e.gamepad.axes.length, 2, "correct number of axes");
     87  is(e.gamepad.hapticActuators.length, 1, "correct number of haptics");
     88  is(e.gamepad.lightIndicators.length, 1, "correct number of light indicators");
     89  is(e.gamepad.touchEvents.length, 2, "correct number of touches");
     90 }
     91 
     92 function checkValueInFloat32Array(array1, array2) {
     93  if (array1.length != array2.length) {
     94    return false;
     95  }
     96  let index = 0;
     97  while (index < array2.length) {
     98    if (array1[index] != array2[index]) {
     99      return false;
    100    }
    101    ++index;
    102  }
    103  return true;
    104 }
    105 
    106 async function touchAdd() {
    107  for(let count = 0; count < data[dataNum].length; count++) {
    108    const touch = data[dataNum][count];
    109    await GamepadService.newTouch(gamepad_index, count, touch.touchId,
    110                            touch.surfaceId, touch.pos,
    111                            touch.surf);
    112  }
    113  ++dataNum;
    114  await pressButton();
    115 }
    116 
    117 async function touchcheck1() {
    118  let touches = f1.contentWindow.gamepad.touchEvents;
    119  is(touches.length, data[0].length, "f1 number of touches");
    120 
    121  let count = 0;
    122  touches.forEach(function(touch) {
    123    is(touch.touchId, data[0][count].touchId,
    124    "correct GamepadTouch touchId");
    125    is(touch.surfaceId, data[0][count].surfaceId,
    126    "correct GamepadTouch surfaceId");
    127    is(checkValueInFloat32Array(touch.position, data[0][count].pos), true,
    128      "correct touch position");
    129    is(checkValueInFloat32Array(touch.surfaceDimensions, data[0][count].surf), true,
    130      "correct touch surfaceDimensions");
    131 
    132    ++count;
    133  });
    134 
    135  touches = f2.contentWindow.gamepad.touchEvents;
    136  is(touches.length, data[0].length,"f2 number of touches");
    137 
    138  count = 0;
    139  touches.forEach(function(touch) {
    140    is(touch.touchId, data[0][count].touchId,
    141    "correct GamepadTouch touchId");
    142    is(touch.surfaceId, data[0][count].surfaceId,
    143    "correct GamepadTouch surfaceId");
    144    is(checkValueInFloat32Array(touch.position, data[0][count].pos), true,
    145      "correct touch position");
    146    is(checkValueInFloat32Array(touch.surfaceDimensions, data[0][count].surf), true,
    147      "correct touch surfaceDimensions");
    148 
    149    ++count;
    150  });
    151  
    152  pressButton();
    153 }
    154 
    155 async function touchcheck2() {
    156  let touches = f1.contentWindow.gamepad.touchEvents;
    157  is(touches.length, data[1].length, "f1 number of touches");
    158 
    159  let count = 0;
    160  touches.forEach(function(touch) {
    161    is(touch.touchId, data[1][count].touchId,
    162      "correct GamepadTouch touchId");
    163    is(touch.surfaceId, data[1][count].surfaceId,
    164      "correct GamepadTouch surfaceId");
    165    is(checkValueInFloat32Array(touch.position, data[1][count].pos), true,
    166      "correct touch position");
    167    is(checkValueInFloat32Array(touch.surfaceDimensions, data[1][count].surf), true,
    168      "correct touch surfaceDimensions");
    169 
    170    ++count;
    171  });
    172 
    173  touches = f2.contentWindow.gamepad.touchEvents;
    174  is(touches.length, data[1].length,"f2 number of touches");
    175 
    176  count = 0;
    177  touches.forEach(function(touch) {
    178    is(touch.touchId, data[1][count].touchId,
    179      "correct GamepadTouch touchId");
    180    is(touch.surfaceId, data[1][count].surfaceId,
    181      "correct GamepadTouch surfaceId");
    182    is(checkValueInFloat32Array(touch.position, data[1][count].pos), true,
    183      "correct touch position");
    184    is(checkValueInFloat32Array(touch.surfaceDimensions, data[1][count].surf), true,
    185      "correct touch surfaceDimensions");
    186 
    187    ++count;
    188  });
    189    
    190  SpecialPowers.executeSoon(cleanup);
    191 }
    192 
    193 function cleanup(){
    194  SpecialPowers.executeSoon(async function() {
    195    await GamepadService.removeGamepad(gamepad_index);
    196    SimpleTest.finish();
    197  });
    198 }
    199 
    200 </script>
    201 <iframe id="f1" src="gamepad_frame_state.html" onload="runGamepadTest(startTest)"></iframe>
    202 <iframe id="f2" src="gamepad_frame_state.html" onload="runGamepadTest(startTest)"></iframe>
    203 </body>
    204 </html>