dom/tests/mochitest/bugs/test_resize_move_windows.html

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial