dom/tests/mochitest/bugs/test_resize_move_windows.html

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     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 type="application/javascript" 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">
    16 </div>
    17 <pre id="test">
    18 <script type="application/javascript">
    20 /** Test for Bug 565541 **/
    22 SimpleTest.waitForExplicitFinish();
    24 var previousX, previousY, previousWidth, previousHeight;
    27 function backValues()
    28 {
    29   previousX = window.screenX;
    30   previousY = window.screenY;
    31   previousWidth = window.innerWidth;
    32   previousHeight = window.innerHeight;
    33 }
    35 function restoreValues()
    36 {
    37   window.screenX = previousX;
    38   window.screenY = previousY;
    39   window.innerWidth = previousWidth;
    40   window.innerHeight = previousHeight;
    41 }
    43 function getNewWidth(aWindow)
    44 {
    45   return (aWindow.innerWidth > (screen.width / 2)) ? 100 : screen.width;
    46 }
    48 function getNewHeight(aWindow)
    49 {
    50   return (aWindow.innerHeight > (screen.height / 2)) ? 100 : screen.height;
    51 }
    53 function getNewX(aWindow)
    54 {
    55   return (aWindow.screenX > ((screen.width - aWindow.outerWidth) / 2))
    56     ? 0 : screen.width - aWindow.outerWidth;
    57 }
    59 function getNewY(aWindow)
    60 {
    61   return (aWindow.screenY > ((screen.height - aWindow.outerHeight) / 2))
    62     ? 0 : screen.height - aWindow.outerHeight;
    63 }
    65 /**
    66  * hitEventLoop is called when we want to check something but we can't rely on
    67  * an event or a specific number of event loop hiting.
    68  * This method can be called by specifying a condition, a test (using SimpleTest
    69  * API), how many times the event loop has to be hitten and what to call next.
    70  * If times < 0, the event loop will be hitten as long as the condition isn't
    71  * true or the test doesn't time out.
    72  */
    73 function hitEventLoop(condition, test, times, next) {
    74   if (condition() || times == 0) {
    75     test();
    76     next();
    77     return;
    78   }
    80   setTimeout(hitEventLoop, 0, condition, test, times - 1, next);
    81 }
    83 function checkChangeIsDisabled(aWindow, aNext)
    84 {
    85   // We want to check that nothing has changed. Having a high value would take
    86   // too much time. Worse thing that could happen is random green.
    87   var hits = 5;
    89   var originalWidth = aWindow.innerWidth;
    90   var originalHeight = aWindow.innerHeight;
    92   var originalX = aWindow.screenX;
    93   var originalY = aWindow.screenY;
    95   var oWidth = aWindow.outerWidth;
    96   var oHeight = aWindow.outerHeight;
    98   function changeCondition() {
    99     return aWindow.innerWidth != originalWidth ||
   100            aWindow.innerHeight != originalHeight ||
   101            aWindow.screenX != originalX || aWindow.screenY != originalY ||
   102            aWindow.outerWidth != oWidth || aWindow.outerHeight != oHeight;
   103   }
   105   function changeTest() {
   106     is(aWindow.innerWidth, originalWidth, "Window width shouldn't have changed");
   107     is(aWindow.innerHeight, originalHeight, "Window height shouldn't have changed");
   108     is(aWindow.screenX, originalX, "Window x position shouldn't have changed");
   109     is(aWindow.screenY, originalY, "Window y position shouldn't have changed");
   110     is(aWindow.outerWidth, oWidth, "Window outerWidth shouldn't have changed");
   111     is(aWindow.outerHeight, oHeight, "Window outerHeight shouldn't have changed");
   112   }
   114   /**
   115    * Size changes.
   116    */
   117   var newWidth = getNewWidth(aWindow);
   118   var newHeight = getNewHeight(aWindow);
   120   aWindow.innerWidth = newWidth;
   121   aWindow.innerHeight = newHeight;
   123   aWindow.resizeTo(newWidth, newHeight);
   125   aWindow.resizeBy(newWidth - aWindow.innerWidth,
   126                    newHeight - aWindow.innerHeight);
   128   aWindow.sizeToContent();
   130   /**
   131    * Position checks.
   132    */
   133   var newX = getNewX(aWindow);
   134   var newY = getNewY(aWindow);
   136   aWindow.screenX = newX;
   137   aWindow.screenY = newY;
   139   aWindow.moveTo(newX, newY);
   141   aWindow.moveBy(newX - aWindow.screenX,
   142                  newY - aWindow.screenY);
   144   /**
   145    * Outer width/height checks.
   146    */
   147   aWindow.outerWidth *= 2;
   148   aWindow.outerHeight *= 2;
   150   // We did a lot of changes. Now, we are going to wait and see if something
   151   // happens.
   152   // NOTE: if this happens to fail, you will have to check manually which
   153   // operation has been accepted.
   154   hitEventLoop(changeCondition, changeTest, hits, aNext);
   155 }
   157 function checkChangeIsEnabled(aWindow, aNext)
   158 {
   159   // Something should happen. We are not going to go to the next test until
   160   // it does.
   161   var hits = -1;
   163   var prevWidth;
   164   var prevHeight;
   166   var prevX;
   167   var prevY;
   169   var oWidth;
   170   var oHeight;
   172   function sizeChangeCondition() {
   173     return aWindow.innerWidth != prevWidth && aWindow.innerHeight != prevHeight;
   174   }
   176   function sizeChangeTest() {
   177     isnot(aWindow.innerWidth, prevWidth, "Window width should have changed");
   178     isnot(aWindow.innerHeight, prevHeight, "Window height should have changed");
   180     prevWidth = aWindow.innerWidth;
   181     prevHeight = aWindow.innerHeight;
   182   }
   184   function posChangeCondition() {
   185     // With GTK, sometimes, only one dimension changes.
   186     if (navigator.platform.indexOf('Linux') != -1) {
   187       return aWindow.screenX != prevX || aWindow.screenY != prevY;
   188     }
   189     return aWindow.screenX != prevX && aWindow.screenY != prevY;
   190   }
   192   function posChangeTest() {
   193     // With GTK, sometimes, only one dimension changes.
   194     if (navigator.platform.indexOf('Linux') != -1) {
   195       // With GTK, sometimes, aWindow.screenX changes during two calls.
   196       // So we call it once and save the returned value.
   197       var x = aWindow.screenX;
   198       var y = aWindow.screenY;
   199       if (x != prevX) {
   200         isnot(x, prevX, "Window x position should have changed");
   201       }
   202       if (y != prevY) {
   203         isnot(y, prevY, "Window y position should have changed");
   204       }
   205     } else {
   206       isnot(aWindow.screenX, prevX, "Window x position should have changed");
   207       isnot(aWindow.screenY, prevY, "Window y position should have changed");
   208     }
   210     prevX = aWindow.screenX;
   211     prevY = aWindow.screenY;
   212   }
   214   function outerChangeCondition() {
   215     return aWindow.outerWidth != oWidth && aWindow.outerHeight != oHeight;
   216   }
   218   function outerChangeTest() {
   219     isnot(aWindow.outerWidth, oWidth, "Window outerWidth should have changed");
   220     isnot(aWindow.outerHeight, oHeight, "Window outerHeight should have changed");
   222     aWindow.outerWidth = oWidth;
   223     aWindow.outerHeight = oHeight;
   224   }
   226   /**
   227    * Size checks.
   228    */
   229   prevWidth = aWindow.innerWidth;
   230   prevHeight = aWindow.innerHeight;
   232   aWindow.innerWidth = getNewWidth(aWindow);
   233   aWindow.innerHeight = getNewHeight(aWindow);
   235   hitEventLoop(sizeChangeCondition, sizeChangeTest, hits, function () {
   236     aWindow.resizeTo(getNewWidth(aWindow), getNewHeight(aWindow));
   238   hitEventLoop(sizeChangeCondition, sizeChangeTest, hits, function () {
   239     aWindow.resizeBy(getNewWidth(aWindow) - aWindow.innerWidth,
   240                      getNewHeight(aWindow) - aWindow.innerHeight);
   242   hitEventLoop(sizeChangeCondition, sizeChangeTest, hits, function () {
   243     prevWidth = aWindow.innerWidth = getNewWidth(aWindow);
   244     prevHeight = aWindow.innerHeight = getNewHeight(aWindow);
   245     aWindow.sizeToContent();
   247   hitEventLoop(sizeChangeCondition, sizeChangeTest, hits, function () {
   248   /**
   249    * Position checks.
   250    */
   251     prevX = aWindow.screenX;
   252     prevY = aWindow.screenY;
   254     aWindow.screenX = getNewX(aWindow);
   255     aWindow.screenY = getNewY(aWindow);
   257   hitEventLoop(posChangeCondition, posChangeTest, hits, function () {
   258     prevX = aWindow.screenX;
   259     prevY = aWindow.screenY;
   261     aWindow.moveTo(getNewX(aWindow), getNewY(aWindow));
   263   hitEventLoop(posChangeCondition, posChangeTest, hits, function () {
   264     prevX = aWindow.screenX;
   265     prevY = aWindow.screenY;
   267     aWindow.moveBy(getNewX(aWindow) - aWindow.screenX,
   268                    getNewY(aWindow) - aWindow.screenY);
   270   hitEventLoop(posChangeCondition, posChangeTest, hits, function () {
   271   /**
   272    * Outer width/height checks.
   273    */
   274     oWidth = aWindow.outerWidth;
   275     oHeight = aWindow.outerHeight;
   277     aWindow.outerWidth = oWidth * 2;
   278     aWindow.outerHeight = oHeight * 2;
   280   hitEventLoop(outerChangeCondition, outerChangeTest, hits, aNext);
   281   });
   282   });
   283   });
   284   });
   285   });
   286   });
   287   });
   288 }
   290 SpecialPowers.pushPrefEnv({"set": [["dom.disable_window_move_resize", false]]}, function() {
   291 SimpleTest.waitForFocus(function() {
   292   if (screen.width <= 200 || screen.height <= 200) {
   293     todo(false, "The screen needs to be bigger than 200px*200px to run this test.");
   294     SimpleTest.finish();
   295     return;
   296   }
   298   backValues();
   300   // The current window can't change it's own size and position.
   301   checkChangeIsDisabled(window, function() {
   302     // We create a window and check that it can change its own size and position.
   303     // However, passing size/position parameters to window.open should work.
   304     var w = window.open("data:text/html,<script>" +
   305       "function check(next) {" +
   306       "  var is_range = function(aTest, aValue, aRange, aMsg) {" +
   307       "    window.opener.ok(aTest < aValue + aRange && aTest > aValue - aRange, aMsg);" +
   308       "  };" +
   309       "  is_range(window.innerWidth, 170, 5, 'parameter width should be taken into account');" +
   310       "  is_range(window.innerHeight, 170, 5, 'parameter height should be taken into account');" +
   311       "  is_range(window.screenX, 65, 5, 'parameter screenX should be taken into account');" +
   312       "  is_range(window.screenY, 65, 5, 'parameter screenY should be taken into account');" +
   313       "  window.opener.checkChangeIsEnabled(window, next);" +
   314       "} <\/script>", '',
   315       'width=170,height=170,screenX=65,screenY=65');
   317     SimpleTest.waitForFocus(function() {
   318       w.check(function() {
   319         // The current window can change the size and position of the created one.
   320         checkChangeIsEnabled(w, function() {
   321           w.close();
   323           // If we call window.open with an empty string as a third parameter,
   324           // by default, it will create a new tab instead of a new window.
   325           // In that case, we shouldn't allow the caller to change the size/position.
   326           w = window.open("data:text/html,<script>" +
   327             "function check(next) {" +
   328             "  window.opener.checkChangeIsDisabled(window, next);" +
   329             "} <\/script>", '', '');
   331           SimpleTest.waitForFocus(function() {
   332             w.check(function() {
   334               // The current window can't change the size and position of the new tab.
   335               checkChangeIsDisabled(w, function() {
   336                 w.close();
   338                 restoreValues();
   339                 SimpleTest.finish();
   340               });
   341             });
   342           }, w, false);
   343         });
   344       })
   345     }, w, false);
   346   });
   347 });
   348 }); // SpecialPowers.pushPrefEnv()
   350 </script>
   351 </pre>
   352 </body>
   353 </html>

mercurial