tor-browser

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

test_resize_move_windows.xhtml (8021B)


      1 <?xml version="1.0"?>
      2 <?xml-stylesheet type="text/css" href="chrome://global/skin"?>
      3 <?xml-stylesheet type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?>
      4 <!--
      5 https://bugzilla.mozilla.org/show_bug.cgi?id=565541
      6 -->
      7 <window title="Mozilla Bug 565541"
      8        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
      9  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
     10 
     11  <!-- test results are displayed in the html:body -->
     12  <body xmlns="http://www.w3.org/1999/xhtml">
     13  <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=565541"
     14     target="_blank">Mozilla Bug 565541</a>
     15  </body>
     16 
     17  <!-- test code goes here -->
     18  <script type="application/javascript">
     19  <![CDATA[
     20 
     21  /** Test for Bug 565541 */
     22  var previousX, previousY, previousWidth, previousHeight;
     23 
     24  function backValues()
     25  {
     26    previousX = window.top.screenX;
     27    previousY = window.top.screenY;
     28    previousWidth = window.top.outerWidth;
     29    previousHeight = window.top.outerHeight;
     30  }
     31 
     32  function restoreValues()
     33  {
     34    window.top.moveTo(previousX, previousY);
     35    window.top.resizeTo(previousWidth, previousHeight);
     36  }
     37 
     38  function getNewWidth(aWindow)
     39  {
     40    return (aWindow.innerWidth > (screen.width / 2)) ? 100 : screen.width;
     41  }
     42 
     43  function getNewHeight(aWindow)
     44  {
     45    return (aWindow.innerHeight > (screen.height / 2)) ? 100 : screen.height;
     46  }
     47 
     48  function getNewX(aWindow)
     49  {
     50    return (aWindow.screenX > ((screen.width - aWindow.outerWidth) / 2))
     51      ? 0 : screen.width - aWindow.outerWidth;
     52  }
     53 
     54  function getNewY(aWindow)
     55  {
     56    return (aWindow.screenY > ((screen.height - aWindow.outerHeight) / 2))
     57      ? 0 : screen.height - aWindow.outerHeight;
     58  }
     59 
     60  /**
     61   * hitEventLoop is called when we want to check something but we can't rely on
     62   * an event or a specific number of event loop hiting.
     63   * This method can be called by specifying a condition, a test (using SimpleTest
     64   * API), how many times the event loop has to be hitten and what to call next.
     65   * If times < 0, the event loop will be hitten as long as the condition isn't
     66   * true or the test doesn't time out.
     67   */
     68  function hitEventLoop(condition, test, times, next) {
     69    if (condition() || times == 0) {
     70      test();
     71      next();
     72      return;
     73    }
     74 
     75    setTimeout(hitEventLoop, 0, condition, test, times - 1, next);
     76  }
     77 
     78  function checkChangeIsEnabled(aWindow, aNext)
     79  {
     80    // Something should happen. We are not going to go to the next test until
     81    // it does.
     82    var hits = -1;
     83 
     84    var prevWidth;
     85    var prevHeight;
     86 
     87    var prevX;
     88    var prevY;
     89 
     90    var oWidth;
     91    var oHeight;
     92 
     93    function sizeChangeCondition() {
     94      return aWindow.innerWidth != prevWidth && aWindow.innerHeight != prevHeight;
     95    }
     96 
     97    function sizeChangeTest() {
     98      isnot(aWindow.innerWidth, prevWidth, "Window width should have changed");
     99      isnot(aWindow.innerHeight, prevHeight, "Window height should have changed");
    100 
    101      prevWidth = aWindow.innerWidth;
    102      prevHeight = aWindow.innerHeight;
    103    }
    104 
    105    function posChangeCondition() {
    106      // With GTK, sometimes, only one dimension changes.
    107      if (navigator.platform.includes('Linux')) {
    108        return aWindow.screenX != prevX || aWindow.screenY != prevY;
    109      }
    110      return aWindow.screenX != prevX && aWindow.screenY != prevY;
    111    }
    112 
    113    function posChangeConditionIgnoreLinux() {
    114      if (posChangeCondition()) {
    115        return true;
    116      }
    117 
    118      if (navigator.platform.includes('Linux')) {
    119        return true;
    120      }
    121      return false;
    122    }
    123 
    124    function posChangeTest() {
    125      // With GTK, sometimes, only one dimension changes.
    126      if (navigator.platform.includes('Linux')) {
    127        // With GTK, sometimes, aWindow.screenX changes during two calls.
    128        // So we call it once and save the returned value.
    129        var x = aWindow.screenX;
    130        var y = aWindow.screenY;
    131        if (x != prevX) {
    132          isnot(x, prevX, "Window x position should have changed");
    133        }
    134        if (y != prevY) {
    135          isnot(y, prevY, "Window y position should have changed");
    136        }
    137      } else {
    138        isnot(aWindow.screenX, prevX, "Window x position should have changed");
    139        isnot(aWindow.screenY, prevY, "Window y position should have changed");
    140      }
    141 
    142      prevX = aWindow.screenX;
    143      prevY = aWindow.screenY;
    144    }
    145 
    146    function outerChangeCondition() {
    147      return aWindow.outerWidth != oWidth && aWindow.outerHeight != oHeight;
    148    }
    149 
    150    function outerChangeTest() {
    151      isnot(aWindow.outerWidth, oWidth, "Window outerWidth should have changed");
    152      isnot(aWindow.outerHeight, oHeight, "Window outerHeight should have changed");
    153 
    154      aWindow.resizeTo(oWidth, oHeight);
    155    }
    156 
    157    /**
    158     * Size checks.
    159     */
    160    prevWidth = aWindow.innerWidth;
    161    prevHeight = aWindow.innerHeight;
    162 
    163    aWindow.resizeTo(getNewWidth(aWindow), getNewHeight(aWindow));
    164    hitEventLoop(sizeChangeCondition, sizeChangeTest, hits, function () {
    165      aWindow.resizeBy(getNewWidth(aWindow) - aWindow.innerWidth,
    166                       getNewHeight(aWindow) - aWindow.innerHeight);
    167 
    168    hitEventLoop(sizeChangeCondition, sizeChangeTest, hits, function () {
    169      prevWidth = aWindow.innerWidth;
    170      prevHeight = aWindow.innerHeight;
    171      aWindow.sizeToContent();
    172 
    173    hitEventLoop(sizeChangeCondition, sizeChangeTest, hits, function () {
    174    /**
    175     * Position checks.
    176     */
    177      prevX = aWindow.screenX;
    178      prevY = aWindow.screenY;
    179 
    180      aWindow.moveBy(getNewX(aWindow) - aWindow.screenX,
    181                     getNewY(aWindow) - aWindow.screenY);
    182 
    183    hitEventLoop(posChangeConditionIgnoreLinux, posChangeTest, hits, function () {
    184    /**
    185     * Outer width/height checks.
    186     */
    187      oWidth = aWindow.outerWidth;
    188      oHeight = aWindow.outerHeight;
    189 
    190      aWindow.resizeTo(oWidth * 2, oHeight* 2);
    191 
    192    hitEventLoop(outerChangeCondition, outerChangeTest, hits, aNext);
    193    });
    194    });
    195    });
    196    });
    197  }
    198 
    199  SimpleTest.waitForExplicitFinish();
    200 
    201  function test() {
    202    SimpleTest.waitForFocus(function() {
    203      if (screen.width <= 200 || screen.height <= 200) {
    204        todo(false, "The screen is too small to run this test.");
    205        SimpleTest.finish();
    206        return;
    207      }
    208 
    209      backValues();
    210 
    211      // We are in a chrome context, we can change the size and position.
    212      checkChangeIsEnabled(window.top, async function() {
    213        // We create a window and check that the size and position can be set with
    214        // window.open parameters and can be changed by the created window.
    215        var w = window.open("file_resize_move_windows_1.html", '',
    216          'width=170,height=170,screenX=25,screenY=25');
    217 
    218        await SimpleTest.promiseWaitForCondition(() =>
    219          w.document.readyState == "complete" && w.location.href != "about:blank"
    220        );
    221        SimpleTest.waitForFocus(function() {
    222          w.wrappedJSObject.ok = SimpleTest.ok;
    223          w.wrappedJSObject.check();
    224          // The current window can change the size and position of the created one.
    225          checkChangeIsEnabled(w, async function() {
    226            w.close();
    227            // If we call window.open with an empty string as a third parameter,
    228            // by default, it will create a new tab instead of a new window.
    229            // In a chrome context, the size and position can change.
    230            w = window.open("file_resize_move_windows_2.html", '', '');
    231            await SimpleTest.promiseWaitForCondition(() => w.document.readyState == "complete");
    232            SimpleTest.waitForFocus(function() {
    233              // The current window can change the size and position of the new tab.
    234              // Because we are in a chrome context.
    235              checkChangeIsEnabled(w, function() {
    236                w.close();
    237                restoreValues();
    238                SimpleTest.finish();
    239              });
    240            }, w, false);
    241          });
    242        }, w, false);
    243      });
    244    });
    245  }
    246 
    247  addLoadEvent(function onLoad() {
    248    test();
    249  });
    250  ]]>
    251  </script>
    252 </window>