tor-browser

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

file_changeLockElement.html (3870B)


      1 <!DOCTYPE html>
      2 <html lang="en">
      3 <head>
      4  <meta charset="UTF-8">
      5  <title>Bug 1284788</title>
      6  <script src="/tests/SimpleTest/EventUtils.js"></script>
      7  <script src="/tests/SimpleTest/SimpleTest.js"></script>
      8  <script src="pointerlock_utils.js"></script>
      9  <link rel="stylesheet" href="/tests/SimpleTest/test.css"/>
     10  <style>
     11    #block1, #block2, #block3 {
     12      background: blue;
     13      width: 50px; height: 50px;
     14      margin: 10px;
     15    }
     16  </style>
     17 </head>
     18 <body>
     19  <div id="block1"></div>
     20  <div id="block2"></div>
     21  <div id="block3"></div>
     22  <div id="test">
     23    <script>
     24      SimpleTest.waitForExplicitFinish();
     25      SimpleTest.requestFlakyTimeout("For changing pointer lock element not in a valid user event handler");
     26 
     27      var block1 = document.getElementById("block1");
     28      var block2 = document.getElementById("block2");
     29      var block3 = document.getElementById("block3");
     30 
     31      class ClickTester {
     32        constructor(target) {
     33          this._target = target;
     34          this._callback = null;
     35          document.addEventListener("click", this);
     36        }
     37 
     38        synthesize(callback) {
     39          ok(!this._callback, "No callback should have been hooked");
     40          this._callback = callback;
     41          synthesizeMouseAtCenter(this._target, {}, window);
     42        }
     43 
     44        handleEvent(e) {
     45          ok(!!this._callback, "Should have hooked a callback");
     46          var callback = this._callback;
     47          this._callback = null;
     48          callback(e);
     49        }
     50      };
     51 
     52      var tester = new ClickTester(block3);
     53      // It would be called in handler of load event in pointerlock_utils.js
     54      function start() {
     55        tester.synthesize(firstClick);
     56      }
     57 
     58      function firstClick(e) {
     59        is(e.target, block3, "Click is triggered inside block3");
     60        document.addEventListener("pointerlockchange", lockedPointerOnBlock1);
     61        block1.requestPointerLock();
     62      }
     63 
     64      function lockedPointerOnBlock1() {
     65        document.removeEventListener("pointerlockchange", lockedPointerOnBlock1);
     66        is(document.pointerLockElement, block1, "Pointer should be locked on #block1");
     67        SimpleTest.executeSoon(() => {
     68          tester.synthesize(secondClick);
     69        });
     70      }
     71 
     72      function secondClick(e) {
     73        is(e.target, block1, "Event should be redirected to block1");
     74        // Use 2s to ensure that we never consider this as an extension of user input.
     75        setTimeout(() => {
     76          document.addEventListener("pointerlockchange", lockedPointerOnBlock2);
     77          block2.requestPointerLock();
     78        }, 2000);
     79      }
     80 
     81      function lockedPointerOnBlock2() {
     82        document.removeEventListener("pointerlockchange", lockedPointerOnBlock2);
     83        is(document.pointerLockElement, block2, "Pointer should be locked on #block2");
     84        SimpleTest.executeSoon(() => {
     85          tester.synthesize(thirdClick);
     86        });
     87      }
     88 
     89      function thirdClick(e) {
     90        is(e.target, block2, "Event should be redirected to block2");
     91        // Use 2s to ensure that we never consider this as an extension of user input.
     92        setTimeout(() => {
     93          document.addEventListener("pointerlockchange", lockedPointerOnBlock1Again);
     94          block1.requestPointerLock();
     95        }, 2000);
     96      }
     97 
     98      function lockedPointerOnBlock1Again() {
     99        document.removeEventListener("pointerlockchange", lockedPointerOnBlock1Again);
    100        is(document.pointerLockElement, block1, "Pointer should be locked on #block1 again");
    101        SimpleTest.executeSoon(() => {
    102          tester.synthesize(fourthClick);
    103        });
    104      }
    105 
    106      function fourthClick(e) {
    107        is(e.target, block1, "Event should be redirected to block1 again");
    108        document.addEventListener("pointerlockchange", () => SimpleTest.finish());
    109        document.exitPointerLock();
    110      }
    111 
    112    </script>
    113  </div>
    114 </body>
    115 </html>