tor-browser

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

test_resize_move_windows.html (10186B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <!--
      4 https://bugzilla.mozilla.org/show_bug.cgi?id=565541
      5 -->
      6 <head>
      7  <title>Test for Bug 565541</title>
      8  <script src="/tests/SimpleTest/SimpleTest.js"></script>
      9  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
     10 </head>
     11 <body>
     12 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=565541">Mozilla Bug 565541</a>
     13 <p id="display"></p>
     14 <div id="content" style="display: none">
     15  
     16 </div>
     17 <pre id="test">
     18 <script>
     19 
     20 /** Test for Bug 565541 */
     21 
     22 SimpleTest.waitForExplicitFinish();
     23 
     24 var previousX, previousY, previousWidth, previousHeight;
     25 
     26 ["innerWidth", "innerHeight", "screenX", "screenY", "outerWidth",
     27 "outerHeight"].map(function(name) {
     28  window[name+"Getter"] = Object.getOwnPropertyDescriptor(window, name).get;
     29 });
     30 
     31 function backValues()
     32 {
     33  previousX = window.screenX;
     34  previousY = window.screenY;
     35  previousWidth = window.outerWidth;
     36  previousHeight = window.outerHeight;
     37 }
     38 
     39 function restoreValues()
     40 {
     41  window.moveTo(previousX, previousY);
     42  window.resizeTo(previousWidth, previousHeight);
     43 }
     44 
     45 function getNewWidth(aWindow)
     46 {
     47  return (aWindow.innerWidth > (screen.width / 2)) ? 100 : screen.width;
     48 }
     49 
     50 function getNewHeight(aWindow)
     51 {
     52  return (aWindow.innerHeight > (screen.height / 2)) ? 100 : screen.height;
     53 }
     54 
     55 function getNewX(aWindow)
     56 {
     57  return (aWindow.screenX > ((screen.width - aWindow.outerWidth) / 2))
     58    ? 0 : screen.width - aWindow.outerWidth;
     59 }
     60 
     61 function getNewY(aWindow)
     62 {
     63  return (aWindow.screenY > ((screen.height - aWindow.outerHeight) / 2))
     64    ? 0 : screen.height - aWindow.outerHeight;
     65 }
     66 
     67 /**
     68 * hitEventLoop is called when we want to check something but we can't rely on
     69 * an event or a specific number of event loop hiting.
     70 * This method can be called by specifying a condition, a test (using SimpleTest
     71 * API), how many times the event loop has to be hit and what to call next.
     72 * If times < 0, the event loop will be hit as long as the condition isn't
     73 * true or the test doesn't time out.
     74 */
     75 function hitEventLoop(condition, test, times) {
     76  return new Promise(function(resolve, reject) {
     77    function doMagic() {
     78      if (condition() || times == 0) {
     79        test();
     80        resolve();
     81        return;
     82      }
     83 
     84      times--;
     85      setTimeout(doMagic, 0);
     86    }
     87 
     88    doMagic();
     89  });
     90 }
     91 
     92 function checkChangeIsDisabled(aWindow) {
     93  // We want to check that nothing has changed. Having a high value would take
     94  // too much time. Worse thing that could happen is random green.
     95  var hits = 5;
     96 
     97  function getProp(propName) {
     98    return window[propName + "Getter"].call(aWindow);
     99  }
    100 
    101  var originalWidth = getProp("innerWidth");
    102  var originalHeight = getProp("innerHeight");
    103 
    104  var originalX = getProp("screenX");
    105  var originalY = getProp("screenY");
    106 
    107  var oWidth = getProp("outerWidth");
    108  var oHeight = getProp("outerHeight");
    109 
    110  function changeCondition() {
    111    return aWindow.innerWidth != originalWidth ||
    112           aWindow.innerHeight != originalHeight ||
    113           aWindow.screenX != originalX || aWindow.screenY != originalY ||
    114           aWindow.outerWidth != oWidth || aWindow.outerHeight != oHeight;
    115  }
    116 
    117  function changeTest() {
    118    is(getProp("innerWidth"), originalWidth,
    119       "Window width shouldn't have changed");
    120    is(getProp("innerHeight"), originalHeight,
    121       "Window height shouldn't have changed");
    122    is(getProp("screenX"), originalX,
    123       "Window x position shouldn't have changed");
    124    is(getProp("screenY"), originalY,
    125       "Window y position shouldn't have changed");
    126    is(getProp("outerWidth"), oWidth,
    127       "Window outerWidth shouldn't have changed");
    128    is(getProp("outerHeight"), oHeight,
    129       "Window outerHeight shouldn't have changed");
    130  }
    131 
    132  /**
    133   * Size changes.
    134   */
    135  var newWidth = getNewWidth(aWindow);
    136  var newHeight = getNewHeight(aWindow);
    137 
    138  aWindow.resizeTo(newWidth, newHeight);
    139 
    140  aWindow.resizeBy(newWidth - aWindow.innerWidth,
    141                   newHeight - aWindow.innerHeight);
    142 
    143  /**
    144   * Position checks.
    145   */
    146  var newX = getNewX(aWindow);
    147  var newY = getNewY(aWindow);
    148 
    149  aWindow.moveTo(newX, newY);
    150 
    151  aWindow.moveBy(newX - aWindow.screenX,
    152                 newY - aWindow.screenY);
    153 
    154  /**
    155   * Outer width/height checks.
    156   */
    157  aWindow.resizeTo(aWindow.outerWidth * 2, aWindow.outerHeight * 2);
    158 
    159  // We did a lot of changes. Now, we are going to wait and see if something
    160  // happens.
    161  // NOTE: if this happens to fail, you will have to check manually which
    162  // operation has been accepted.
    163  return hitEventLoop(changeCondition, changeTest, hits);
    164 }
    165 
    166 function checkChangeIsEnabled(aWindow, aNext)
    167 {
    168  info(`checkChangeIsEnabled start`);
    169  // Something should happen. We are not going to go to the next test until
    170  // it does.
    171  var hits = -1;
    172 
    173  var prevWidth;
    174  var prevHeight;
    175 
    176  var prevX;
    177  var prevY;
    178 
    179  var oWidth;
    180  var oHeight;
    181 
    182  function sizeChangeCondition() {
    183    return aWindow.outerWidth != prevWidth && aWindow.outerHeight != prevHeight;
    184  }
    185 
    186  function sizeChangeTest() {
    187    isnot(aWindow.outerWidth, prevWidth, "Window width should have changed");
    188    isnot(aWindow.outerHeight, prevHeight, "Window height should have changed");
    189 
    190    prevWidth = aWindow.outerWidth;
    191    prevHeight = aWindow.outerHeight;
    192  }
    193 
    194  function posChangeCondition() {
    195    // With GTK, sometimes, only one dimension changes.
    196    if (navigator.platform.includes('Linux')) {
    197      return aWindow.screenX != prevX || aWindow.screenY != prevY;
    198    }
    199    return aWindow.screenX != prevX && aWindow.screenY != prevY;
    200  }
    201 
    202  function posChangeTest() {
    203    // With GTK, sometimes, only one dimension changes.
    204    if (navigator.platform.includes('Linux')) {
    205      // With GTK, sometimes, aWindow.screenX changes during two calls.
    206      // So we call it once and save the returned value.
    207      var x = aWindow.screenX;
    208      var y = aWindow.screenY;
    209      if (x != prevX) {
    210        isnot(x, prevX, "Window x position should have changed");
    211      }
    212      if (y != prevY) {
    213        isnot(y, prevY, "Window y position should have changed");
    214      }
    215    } else {
    216      isnot(aWindow.screenX, prevX, "Window x position should have changed");
    217      isnot(aWindow.screenY, prevY, "Window y position should have changed");
    218    }
    219 
    220    prevX = aWindow.screenX;
    221    prevY = aWindow.screenY;
    222  }
    223 
    224  function outerChangeCondition() {
    225    return aWindow.outerWidth != oWidth && aWindow.outerHeight != oHeight;
    226  }
    227 
    228  function outerChangeTest() {
    229    isnot(aWindow.outerWidth, oWidth, "Window outerWidth should have changed");
    230    isnot(aWindow.outerHeight, oHeight, "Window outerHeight should have changed");
    231  }
    232 
    233  /**
    234   * Size checks.
    235   */
    236  prevWidth = aWindow.outerWidth;
    237  prevHeight = aWindow.outerHeight;
    238 
    239  aWindow.resizeTo(getNewWidth(aWindow), getNewHeight(aWindow));
    240  hitEventLoop(sizeChangeCondition, sizeChangeTest, hits)
    241  .then(function() {
    242    aWindow.resizeTo(getNewWidth(aWindow), getNewHeight(aWindow));
    243    return hitEventLoop(sizeChangeCondition, sizeChangeTest, hits);
    244  })
    245  .then(function () {
    246    aWindow.resizeBy(getNewWidth(aWindow) - aWindow.innerWidth,
    247                     getNewHeight(aWindow) - aWindow.innerHeight);
    248    return hitEventLoop(sizeChangeCondition, sizeChangeTest, hits);
    249  })
    250  .then(function() {
    251    aWindow.resizeTo(500, 500);
    252    return hitEventLoop(sizeChangeCondition, sizeChangeTest, hits);
    253  })
    254  .then(function() {
    255    /**
    256     * Position checks.
    257     */
    258    prevX = aWindow.screenX;
    259    prevY = aWindow.screenY;
    260 
    261    aWindow.moveTo(getNewX(aWindow), getNewY(aWindow));
    262    return hitEventLoop(posChangeCondition, posChangeTest, hits);
    263  })
    264  .then(function() {
    265    prevX = aWindow.screenX;
    266    prevY = aWindow.screenY;
    267 
    268    aWindow.moveBy(getNewX(aWindow) - aWindow.screenX,
    269                   getNewY(aWindow) - aWindow.screenY);
    270    return hitEventLoop(posChangeCondition, posChangeTest, hits);
    271  })
    272  .then(function() {
    273    /**
    274     * Outer width/height checks.
    275     */
    276    oWidth = aWindow.outerWidth;
    277    oHeight = aWindow.outerHeight;
    278 
    279    aWindow.resizeTo(oWidth * 2, oHeight * 2);
    280    return hitEventLoop(outerChangeCondition, outerChangeTest, hits);
    281  })
    282  .then(function() {
    283    let origWidth = oWidth;
    284    let origHeight = oHeight;
    285 
    286    oWidth = aWindow.outerWidth;
    287    oHeight = aWindow.outerHeight;
    288 
    289    aWindow.resizeTo(origWidth, origHeight);
    290    return hitEventLoop(outerChangeCondition, outerChangeTest, hits);
    291  })
    292  .then(aNext);
    293 }
    294 
    295 SpecialPowers.pushPrefEnv({
    296  "set": [["dom.disable_window_move_resize", false]]},
    297                          function() {
    298 SimpleTest.waitForFocus(function() {
    299  if (screen.width <= 200 || screen.height <= 200) {
    300    todo(false, "The screen needs to be bigger than 200px*200px to run this test.");
    301    SimpleTest.finish();
    302    return;
    303  }
    304 
    305  backValues();
    306 
    307  // The current window can't change it's own size and position.
    308  checkChangeIsDisabled(window).then(function() {
    309    info(`Change was disabled`);
    310    // We create a window and check that it can change its own size and position.
    311    // However, passing size/position parameters to window.open should work.
    312    var w = window.open("file_resize_move_windows_1.html", '',
    313      'width=170,height=170,screenX=65,screenY=65');
    314 
    315    SimpleTest.waitForFocus(function() {
    316      w.check(function() {
    317        // The current window can change the size and position of the created one.
    318        checkChangeIsEnabled(w, function() {
    319          w.close();
    320 
    321          // If we call window.open with an empty string as a third parameter,
    322          // by default, it will create a new tab instead of a new window.
    323          // In that case, we shouldn't allow the caller to change the size/position.
    324          w = window.open("file_resize_move_windows_2.html", '', '');
    325          SimpleTest.waitForFocus(function() {
    326            w.check(function() {
    327 
    328              // The current window can't change the size and position of the new tab.
    329              checkChangeIsDisabled(w).then(function() {
    330                w.close();
    331 
    332                restoreValues();
    333                SimpleTest.finish();
    334              });
    335            });
    336          }, w, false);
    337        });
    338      })
    339    }, w, false);
    340  });
    341 });
    342 }); // SpecialPowers.pushPrefEnv()
    343 
    344 </script>
    345 </pre>
    346 </body>
    347 </html>