1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/tests/mochitest/bugs/test_resize_move_windows.html Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,353 @@ 1.4 +<!DOCTYPE HTML> 1.5 +<html> 1.6 +<!-- 1.7 +https://bugzilla.mozilla.org/show_bug.cgi?id=565541 1.8 +--> 1.9 +<head> 1.10 + <title>Test for Bug 565541</title> 1.11 + <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> 1.12 + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> 1.13 +</head> 1.14 +<body> 1.15 +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=565541">Mozilla Bug 565541</a> 1.16 +<p id="display"></p> 1.17 +<div id="content" style="display: none"> 1.18 + 1.19 +</div> 1.20 +<pre id="test"> 1.21 +<script type="application/javascript"> 1.22 + 1.23 +/** Test for Bug 565541 **/ 1.24 + 1.25 +SimpleTest.waitForExplicitFinish(); 1.26 + 1.27 +var previousX, previousY, previousWidth, previousHeight; 1.28 + 1.29 + 1.30 +function backValues() 1.31 +{ 1.32 + previousX = window.screenX; 1.33 + previousY = window.screenY; 1.34 + previousWidth = window.innerWidth; 1.35 + previousHeight = window.innerHeight; 1.36 +} 1.37 + 1.38 +function restoreValues() 1.39 +{ 1.40 + window.screenX = previousX; 1.41 + window.screenY = previousY; 1.42 + window.innerWidth = previousWidth; 1.43 + window.innerHeight = previousHeight; 1.44 +} 1.45 + 1.46 +function getNewWidth(aWindow) 1.47 +{ 1.48 + return (aWindow.innerWidth > (screen.width / 2)) ? 100 : screen.width; 1.49 +} 1.50 + 1.51 +function getNewHeight(aWindow) 1.52 +{ 1.53 + return (aWindow.innerHeight > (screen.height / 2)) ? 100 : screen.height; 1.54 +} 1.55 + 1.56 +function getNewX(aWindow) 1.57 +{ 1.58 + return (aWindow.screenX > ((screen.width - aWindow.outerWidth) / 2)) 1.59 + ? 0 : screen.width - aWindow.outerWidth; 1.60 +} 1.61 + 1.62 +function getNewY(aWindow) 1.63 +{ 1.64 + return (aWindow.screenY > ((screen.height - aWindow.outerHeight) / 2)) 1.65 + ? 0 : screen.height - aWindow.outerHeight; 1.66 +} 1.67 + 1.68 +/** 1.69 + * hitEventLoop is called when we want to check something but we can't rely on 1.70 + * an event or a specific number of event loop hiting. 1.71 + * This method can be called by specifying a condition, a test (using SimpleTest 1.72 + * API), how many times the event loop has to be hitten and what to call next. 1.73 + * If times < 0, the event loop will be hitten as long as the condition isn't 1.74 + * true or the test doesn't time out. 1.75 + */ 1.76 +function hitEventLoop(condition, test, times, next) { 1.77 + if (condition() || times == 0) { 1.78 + test(); 1.79 + next(); 1.80 + return; 1.81 + } 1.82 + 1.83 + setTimeout(hitEventLoop, 0, condition, test, times - 1, next); 1.84 +} 1.85 + 1.86 +function checkChangeIsDisabled(aWindow, aNext) 1.87 +{ 1.88 + // We want to check that nothing has changed. Having a high value would take 1.89 + // too much time. Worse thing that could happen is random green. 1.90 + var hits = 5; 1.91 + 1.92 + var originalWidth = aWindow.innerWidth; 1.93 + var originalHeight = aWindow.innerHeight; 1.94 + 1.95 + var originalX = aWindow.screenX; 1.96 + var originalY = aWindow.screenY; 1.97 + 1.98 + var oWidth = aWindow.outerWidth; 1.99 + var oHeight = aWindow.outerHeight; 1.100 + 1.101 + function changeCondition() { 1.102 + return aWindow.innerWidth != originalWidth || 1.103 + aWindow.innerHeight != originalHeight || 1.104 + aWindow.screenX != originalX || aWindow.screenY != originalY || 1.105 + aWindow.outerWidth != oWidth || aWindow.outerHeight != oHeight; 1.106 + } 1.107 + 1.108 + function changeTest() { 1.109 + is(aWindow.innerWidth, originalWidth, "Window width shouldn't have changed"); 1.110 + is(aWindow.innerHeight, originalHeight, "Window height shouldn't have changed"); 1.111 + is(aWindow.screenX, originalX, "Window x position shouldn't have changed"); 1.112 + is(aWindow.screenY, originalY, "Window y position shouldn't have changed"); 1.113 + is(aWindow.outerWidth, oWidth, "Window outerWidth shouldn't have changed"); 1.114 + is(aWindow.outerHeight, oHeight, "Window outerHeight shouldn't have changed"); 1.115 + } 1.116 + 1.117 + /** 1.118 + * Size changes. 1.119 + */ 1.120 + var newWidth = getNewWidth(aWindow); 1.121 + var newHeight = getNewHeight(aWindow); 1.122 + 1.123 + aWindow.innerWidth = newWidth; 1.124 + aWindow.innerHeight = newHeight; 1.125 + 1.126 + aWindow.resizeTo(newWidth, newHeight); 1.127 + 1.128 + aWindow.resizeBy(newWidth - aWindow.innerWidth, 1.129 + newHeight - aWindow.innerHeight); 1.130 + 1.131 + aWindow.sizeToContent(); 1.132 + 1.133 + /** 1.134 + * Position checks. 1.135 + */ 1.136 + var newX = getNewX(aWindow); 1.137 + var newY = getNewY(aWindow); 1.138 + 1.139 + aWindow.screenX = newX; 1.140 + aWindow.screenY = newY; 1.141 + 1.142 + aWindow.moveTo(newX, newY); 1.143 + 1.144 + aWindow.moveBy(newX - aWindow.screenX, 1.145 + newY - aWindow.screenY); 1.146 + 1.147 + /** 1.148 + * Outer width/height checks. 1.149 + */ 1.150 + aWindow.outerWidth *= 2; 1.151 + aWindow.outerHeight *= 2; 1.152 + 1.153 + // We did a lot of changes. Now, we are going to wait and see if something 1.154 + // happens. 1.155 + // NOTE: if this happens to fail, you will have to check manually which 1.156 + // operation has been accepted. 1.157 + hitEventLoop(changeCondition, changeTest, hits, aNext); 1.158 +} 1.159 + 1.160 +function checkChangeIsEnabled(aWindow, aNext) 1.161 +{ 1.162 + // Something should happen. We are not going to go to the next test until 1.163 + // it does. 1.164 + var hits = -1; 1.165 + 1.166 + var prevWidth; 1.167 + var prevHeight; 1.168 + 1.169 + var prevX; 1.170 + var prevY; 1.171 + 1.172 + var oWidth; 1.173 + var oHeight; 1.174 + 1.175 + function sizeChangeCondition() { 1.176 + return aWindow.innerWidth != prevWidth && aWindow.innerHeight != prevHeight; 1.177 + } 1.178 + 1.179 + function sizeChangeTest() { 1.180 + isnot(aWindow.innerWidth, prevWidth, "Window width should have changed"); 1.181 + isnot(aWindow.innerHeight, prevHeight, "Window height should have changed"); 1.182 + 1.183 + prevWidth = aWindow.innerWidth; 1.184 + prevHeight = aWindow.innerHeight; 1.185 + } 1.186 + 1.187 + function posChangeCondition() { 1.188 + // With GTK, sometimes, only one dimension changes. 1.189 + if (navigator.platform.indexOf('Linux') != -1) { 1.190 + return aWindow.screenX != prevX || aWindow.screenY != prevY; 1.191 + } 1.192 + return aWindow.screenX != prevX && aWindow.screenY != prevY; 1.193 + } 1.194 + 1.195 + function posChangeTest() { 1.196 + // With GTK, sometimes, only one dimension changes. 1.197 + if (navigator.platform.indexOf('Linux') != -1) { 1.198 + // With GTK, sometimes, aWindow.screenX changes during two calls. 1.199 + // So we call it once and save the returned value. 1.200 + var x = aWindow.screenX; 1.201 + var y = aWindow.screenY; 1.202 + if (x != prevX) { 1.203 + isnot(x, prevX, "Window x position should have changed"); 1.204 + } 1.205 + if (y != prevY) { 1.206 + isnot(y, prevY, "Window y position should have changed"); 1.207 + } 1.208 + } else { 1.209 + isnot(aWindow.screenX, prevX, "Window x position should have changed"); 1.210 + isnot(aWindow.screenY, prevY, "Window y position should have changed"); 1.211 + } 1.212 + 1.213 + prevX = aWindow.screenX; 1.214 + prevY = aWindow.screenY; 1.215 + } 1.216 + 1.217 + function outerChangeCondition() { 1.218 + return aWindow.outerWidth != oWidth && aWindow.outerHeight != oHeight; 1.219 + } 1.220 + 1.221 + function outerChangeTest() { 1.222 + isnot(aWindow.outerWidth, oWidth, "Window outerWidth should have changed"); 1.223 + isnot(aWindow.outerHeight, oHeight, "Window outerHeight should have changed"); 1.224 + 1.225 + aWindow.outerWidth = oWidth; 1.226 + aWindow.outerHeight = oHeight; 1.227 + } 1.228 + 1.229 + /** 1.230 + * Size checks. 1.231 + */ 1.232 + prevWidth = aWindow.innerWidth; 1.233 + prevHeight = aWindow.innerHeight; 1.234 + 1.235 + aWindow.innerWidth = getNewWidth(aWindow); 1.236 + aWindow.innerHeight = getNewHeight(aWindow); 1.237 + 1.238 + hitEventLoop(sizeChangeCondition, sizeChangeTest, hits, function () { 1.239 + aWindow.resizeTo(getNewWidth(aWindow), getNewHeight(aWindow)); 1.240 + 1.241 + hitEventLoop(sizeChangeCondition, sizeChangeTest, hits, function () { 1.242 + aWindow.resizeBy(getNewWidth(aWindow) - aWindow.innerWidth, 1.243 + getNewHeight(aWindow) - aWindow.innerHeight); 1.244 + 1.245 + hitEventLoop(sizeChangeCondition, sizeChangeTest, hits, function () { 1.246 + prevWidth = aWindow.innerWidth = getNewWidth(aWindow); 1.247 + prevHeight = aWindow.innerHeight = getNewHeight(aWindow); 1.248 + aWindow.sizeToContent(); 1.249 + 1.250 + hitEventLoop(sizeChangeCondition, sizeChangeTest, hits, function () { 1.251 + /** 1.252 + * Position checks. 1.253 + */ 1.254 + prevX = aWindow.screenX; 1.255 + prevY = aWindow.screenY; 1.256 + 1.257 + aWindow.screenX = getNewX(aWindow); 1.258 + aWindow.screenY = getNewY(aWindow); 1.259 + 1.260 + hitEventLoop(posChangeCondition, posChangeTest, hits, function () { 1.261 + prevX = aWindow.screenX; 1.262 + prevY = aWindow.screenY; 1.263 + 1.264 + aWindow.moveTo(getNewX(aWindow), getNewY(aWindow)); 1.265 + 1.266 + hitEventLoop(posChangeCondition, posChangeTest, hits, function () { 1.267 + prevX = aWindow.screenX; 1.268 + prevY = aWindow.screenY; 1.269 + 1.270 + aWindow.moveBy(getNewX(aWindow) - aWindow.screenX, 1.271 + getNewY(aWindow) - aWindow.screenY); 1.272 + 1.273 + hitEventLoop(posChangeCondition, posChangeTest, hits, function () { 1.274 + /** 1.275 + * Outer width/height checks. 1.276 + */ 1.277 + oWidth = aWindow.outerWidth; 1.278 + oHeight = aWindow.outerHeight; 1.279 + 1.280 + aWindow.outerWidth = oWidth * 2; 1.281 + aWindow.outerHeight = oHeight * 2; 1.282 + 1.283 + hitEventLoop(outerChangeCondition, outerChangeTest, hits, aNext); 1.284 + }); 1.285 + }); 1.286 + }); 1.287 + }); 1.288 + }); 1.289 + }); 1.290 + }); 1.291 +} 1.292 + 1.293 +SpecialPowers.pushPrefEnv({"set": [["dom.disable_window_move_resize", false]]}, function() { 1.294 +SimpleTest.waitForFocus(function() { 1.295 + if (screen.width <= 200 || screen.height <= 200) { 1.296 + todo(false, "The screen needs to be bigger than 200px*200px to run this test."); 1.297 + SimpleTest.finish(); 1.298 + return; 1.299 + } 1.300 + 1.301 + backValues(); 1.302 + 1.303 + // The current window can't change it's own size and position. 1.304 + checkChangeIsDisabled(window, function() { 1.305 + // We create a window and check that it can change its own size and position. 1.306 + // However, passing size/position parameters to window.open should work. 1.307 + var w = window.open("data:text/html,<script>" + 1.308 + "function check(next) {" + 1.309 + " var is_range = function(aTest, aValue, aRange, aMsg) {" + 1.310 + " window.opener.ok(aTest < aValue + aRange && aTest > aValue - aRange, aMsg);" + 1.311 + " };" + 1.312 + " is_range(window.innerWidth, 170, 5, 'parameter width should be taken into account');" + 1.313 + " is_range(window.innerHeight, 170, 5, 'parameter height should be taken into account');" + 1.314 + " is_range(window.screenX, 65, 5, 'parameter screenX should be taken into account');" + 1.315 + " is_range(window.screenY, 65, 5, 'parameter screenY should be taken into account');" + 1.316 + " window.opener.checkChangeIsEnabled(window, next);" + 1.317 + "} <\/script>", '', 1.318 + 'width=170,height=170,screenX=65,screenY=65'); 1.319 + 1.320 + SimpleTest.waitForFocus(function() { 1.321 + w.check(function() { 1.322 + // The current window can change the size and position of the created one. 1.323 + checkChangeIsEnabled(w, function() { 1.324 + w.close(); 1.325 + 1.326 + // If we call window.open with an empty string as a third parameter, 1.327 + // by default, it will create a new tab instead of a new window. 1.328 + // In that case, we shouldn't allow the caller to change the size/position. 1.329 + w = window.open("data:text/html,<script>" + 1.330 + "function check(next) {" + 1.331 + " window.opener.checkChangeIsDisabled(window, next);" + 1.332 + "} <\/script>", '', ''); 1.333 + 1.334 + SimpleTest.waitForFocus(function() { 1.335 + w.check(function() { 1.336 + 1.337 + // The current window can't change the size and position of the new tab. 1.338 + checkChangeIsDisabled(w, function() { 1.339 + w.close(); 1.340 + 1.341 + restoreValues(); 1.342 + SimpleTest.finish(); 1.343 + }); 1.344 + }); 1.345 + }, w, false); 1.346 + }); 1.347 + }) 1.348 + }, w, false); 1.349 + }); 1.350 +}); 1.351 +}); // SpecialPowers.pushPrefEnv() 1.352 + 1.353 +</script> 1.354 +</pre> 1.355 +</body> 1.356 +</html>