1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/tests/mochitest/chrome/test_resize_move_windows.xul Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,279 @@ 1.4 +<?xml version="1.0"?> 1.5 +<?xml-stylesheet type="text/css" href="chrome://global/skin"?> 1.6 +<?xml-stylesheet type="text/css" href="/tests/SimpleTest/test.css"?> 1.7 +<!-- 1.8 +https://bugzilla.mozilla.org/show_bug.cgi?id=565541 1.9 +--> 1.10 +<window title="Mozilla Bug 565541" 1.11 + xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"> 1.12 + <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> 1.13 + 1.14 + <!-- test results are displayed in the html:body --> 1.15 + <body xmlns="http://www.w3.org/1999/xhtml"> 1.16 + <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=565541" 1.17 + target="_blank">Mozilla Bug 565541</a> 1.18 + </body> 1.19 + 1.20 + <!-- test code goes here --> 1.21 + <script type="application/javascript"> 1.22 + <![CDATA[ 1.23 + 1.24 + /** Test for Bug 565541 **/ 1.25 + var previousX, previousY, previousWidth, previousHeight; 1.26 + 1.27 + function backValues() 1.28 + { 1.29 + previousX = window.top.screenX; 1.30 + previousY = window.top.screenY; 1.31 + previousWidth = window.top.innerWidth; 1.32 + previousHeight = window.top.innerHeight; 1.33 + } 1.34 + 1.35 + function restoreValues() 1.36 + { 1.37 + window.top.screenX = previousX; 1.38 + window.top.screenY = previousY; 1.39 + window.top.innerWidth = previousWidth; 1.40 + window.top.innerHeight = previousHeight; 1.41 + } 1.42 + 1.43 + function getNewWidth(aWindow) 1.44 + { 1.45 + return (aWindow.innerWidth > (screen.width / 2)) ? 100 : screen.width; 1.46 + } 1.47 + 1.48 + function getNewHeight(aWindow) 1.49 + { 1.50 + return (aWindow.innerHeight > (screen.height / 2)) ? 100 : screen.height; 1.51 + } 1.52 + 1.53 + function getNewX(aWindow) 1.54 + { 1.55 + return (aWindow.screenX > ((screen.width - aWindow.outerWidth) / 2)) 1.56 + ? 0 : screen.width - aWindow.outerWidth; 1.57 + } 1.58 + 1.59 + function getNewY(aWindow) 1.60 + { 1.61 + return (aWindow.screenY > ((screen.height - aWindow.outerHeight) / 2)) 1.62 + ? 0 : screen.height - aWindow.outerHeight; 1.63 + } 1.64 + 1.65 + /** 1.66 + * hitEventLoop is called when we want to check something but we can't rely on 1.67 + * an event or a specific number of event loop hiting. 1.68 + * This method can be called by specifying a condition, a test (using SimpleTest 1.69 + * API), how many times the event loop has to be hitten and what to call next. 1.70 + * If times < 0, the event loop will be hitten as long as the condition isn't 1.71 + * true or the test doesn't time out. 1.72 + */ 1.73 + function hitEventLoop(condition, test, times, next) { 1.74 + if (condition() || times == 0) { 1.75 + test(); 1.76 + next(); 1.77 + return; 1.78 + } 1.79 + 1.80 + setTimeout(hitEventLoop, 0, condition, test, times - 1, next); 1.81 + } 1.82 + 1.83 + function checkChangeIsEnabled(aWindow, aNext) 1.84 + { 1.85 + // Something should happen. We are not going to go to the next test until 1.86 + // it does. 1.87 + var hits = -1; 1.88 + 1.89 + var prevWidth; 1.90 + var prevHeight; 1.91 + 1.92 + var prevX; 1.93 + var prevY; 1.94 + 1.95 + var oWidth; 1.96 + var oHeight; 1.97 + 1.98 + function sizeChangeCondition() { 1.99 + return aWindow.innerWidth != prevWidth && aWindow.innerHeight != prevHeight; 1.100 + } 1.101 + 1.102 + function sizeChangeTest() { 1.103 + isnot(aWindow.innerWidth, prevWidth, "Window width should have changed"); 1.104 + isnot(aWindow.innerHeight, prevHeight, "Window height should have changed"); 1.105 + 1.106 + prevWidth = aWindow.innerWidth; 1.107 + prevHeight = aWindow.innerHeight; 1.108 + } 1.109 + 1.110 + function posChangeCondition() { 1.111 + // With GTK, sometimes, only one dimension changes. 1.112 + if (navigator.platform.indexOf('Linux') != -1) { 1.113 + return aWindow.screenX != prevX || aWindow.screenY != prevY; 1.114 + } 1.115 + return aWindow.screenX != prevX && aWindow.screenY != prevY; 1.116 + } 1.117 + 1.118 + function posChangeConditionIgnoreLinux() { 1.119 + if (posChangeCondition()) { 1.120 + return true; 1.121 + } 1.122 + 1.123 + if (navigator.platform.indexOf('Linux') != -1) { 1.124 + return true; 1.125 + } 1.126 + } 1.127 + 1.128 + function posChangeTest() { 1.129 + // With GTK, sometimes, only one dimension changes. 1.130 + if (navigator.platform.indexOf('Linux') != -1) { 1.131 + // With GTK, sometimes, aWindow.screenX changes during two calls. 1.132 + // So we call it once and save the returned value. 1.133 + var x = aWindow.screenX; 1.134 + var y = aWindow.screenY; 1.135 + if (x != prevX) { 1.136 + isnot(x, prevX, "Window x position should have changed"); 1.137 + } 1.138 + if (y != prevY) { 1.139 + isnot(y, prevY, "Window y position should have changed"); 1.140 + } 1.141 + } else { 1.142 + isnot(aWindow.screenX, prevX, "Window x position should have changed"); 1.143 + isnot(aWindow.screenY, prevY, "Window y position should have changed"); 1.144 + } 1.145 + 1.146 + prevX = aWindow.screenX; 1.147 + prevY = aWindow.screenY; 1.148 + } 1.149 + 1.150 + function outerChangeCondition() { 1.151 + return aWindow.outerWidth != oWidth && aWindow.outerHeight != oHeight; 1.152 + } 1.153 + 1.154 + function outerChangeTest() { 1.155 + isnot(aWindow.outerWidth, oWidth, "Window outerWidth should have changed"); 1.156 + isnot(aWindow.outerHeight, oHeight, "Window outerHeight should have changed"); 1.157 + 1.158 + aWindow.outerWidth = oWidth; 1.159 + aWindow.outerHeight = oHeight; 1.160 + } 1.161 + 1.162 + /** 1.163 + * Size checks. 1.164 + */ 1.165 + prevWidth = aWindow.innerWidth; 1.166 + prevHeight = aWindow.innerHeight; 1.167 + 1.168 + aWindow.innerWidth = getNewWidth(aWindow); 1.169 + aWindow.innerHeight = getNewHeight(aWindow); 1.170 + 1.171 + hitEventLoop(sizeChangeCondition, sizeChangeTest, hits, function () { 1.172 + aWindow.resizeTo(getNewWidth(aWindow), getNewHeight(aWindow)); 1.173 + 1.174 + hitEventLoop(sizeChangeCondition, sizeChangeTest, hits, function () { 1.175 + aWindow.resizeBy(getNewWidth(aWindow) - aWindow.innerWidth, 1.176 + getNewHeight(aWindow) - aWindow.innerHeight); 1.177 + 1.178 + hitEventLoop(sizeChangeCondition, sizeChangeTest, hits, function () { 1.179 + prevWidth = aWindow.innerWidth = getNewWidth(aWindow); 1.180 + prevHeight = aWindow.innerHeight = getNewHeight(aWindow); 1.181 + aWindow.sizeToContent(); 1.182 + 1.183 + hitEventLoop(sizeChangeCondition, sizeChangeTest, hits, function () { 1.184 + /** 1.185 + * Position checks. 1.186 + */ 1.187 + prevX = aWindow.screenX; 1.188 + prevY = aWindow.screenY; 1.189 + 1.190 + aWindow.screenX = getNewX(aWindow); 1.191 + aWindow.screenY = getNewY(aWindow); 1.192 + 1.193 + hitEventLoop(posChangeCondition, posChangeTest, hits, function () { 1.194 + aWindow.moveTo(getNewX(aWindow), getNewY(aWindow)); 1.195 + 1.196 + hitEventLoop(posChangeCondition, posChangeTest, hits, function () { 1.197 + aWindow.moveBy(getNewX(aWindow) - aWindow.screenX, 1.198 + getNewY(aWindow) - aWindow.screenY); 1.199 + 1.200 + hitEventLoop(posChangeConditionIgnoreLinux, posChangeTest, hits, function () { 1.201 + /** 1.202 + * Outer width/height checks. 1.203 + */ 1.204 + oWidth = aWindow.outerWidth; 1.205 + oHeight = aWindow.outerHeight; 1.206 + 1.207 + aWindow.outerWidth = oWidth * 2; 1.208 + aWindow.outerHeight = oHeight * 2; 1.209 + 1.210 + hitEventLoop(outerChangeCondition, outerChangeTest, hits, aNext); 1.211 + }); 1.212 + }); 1.213 + }); 1.214 + }); 1.215 + }); 1.216 + }); 1.217 + }); 1.218 + } 1.219 + 1.220 + SimpleTest.waitForExplicitFinish(); 1.221 + SimpleTest.waitForFocus(function() { 1.222 + if (screen.width <= 200 || screen.height <= 200) { 1.223 + todo(false, "The screen is too small to run this test."); 1.224 + SimpleTest.finish(); 1.225 + } 1.226 + 1.227 + backValues(); 1.228 + 1.229 + // We are in a chrome context, we can change the size and position. 1.230 + checkChangeIsEnabled(window.top, function() { 1.231 + // We create a window and check that the size and position can be set with 1.232 + // window.open parameters and can be changed by the created window. 1.233 + var w = window.open("data:text/html,<script>" + 1.234 + "function check(next) {" + 1.235 + " var is_range = function(aTest, aValue, aRange, aMsg) {" + 1.236 + " ok(aTest < aValue + aRange && aTest > aValue - aRange, aMsg);" + 1.237 + " };" + 1.238 + " is_range(window.innerWidth, 170, 5, 'parameter width should be taken into account');" + 1.239 + " is_range(window.innerHeight, 170, 5, 'parameter height should be taken into account');" + 1.240 + " is_range(window.screenX, 25, 5, 'parameter screenX should be taken into account');" + 1.241 + " is_range(window.screenY, 25, 5, 'parameter screenY should be taken into account');" + 1.242 + " checkChangeIsEnabled(window, next);" + 1.243 + "} <\/script>", '', 1.244 + 'width=170,height=170,screenX=25,screenY=25'); 1.245 + 1.246 + SimpleTest.waitForFocus(function() { 1.247 + w.wrappedJSObject.ok = SimpleTest.ok; 1.248 + w.wrappedJSObject.checkChangeIsEnabled = window.checkChangeIsEnabled; 1.249 + w.wrappedJSObject.check(function() { 1.250 + // The current window can change the size and position of the created one. 1.251 + checkChangeIsEnabled(w, function() { 1.252 + w.close(); 1.253 + 1.254 + // If we call window.open with an empty string as a third parameter, 1.255 + // by default, it will create a new tab instead of a new window. 1.256 + // In a chrome context, the size and position can change. 1.257 + w = window.open("data:text/html,<script>" + 1.258 + "function check(next) {" + 1.259 + " checkChangeIsEnabled(window, next);" + 1.260 + "} <\/script>", '', ''); 1.261 + 1.262 + SimpleTest.waitForFocus(function() { 1.263 + w.wrappedJSObject.checkChangeIsEnabled = window.checkChangeIsEnabled; 1.264 + w.wrappedJSObject.check(function() { 1.265 + // The current window can change the size and position of the new tab. 1.266 + // Because we are in a chrome context. 1.267 + checkChangeIsEnabled(w, function() { 1.268 + w.close(); 1.269 + 1.270 + restoreValues(); 1.271 + SimpleTest.finish(); 1.272 + }); 1.273 + }); 1.274 + }, w, false); 1.275 + }); 1.276 + }); 1.277 + }, w, false); 1.278 + }); 1.279 + }); 1.280 + ]]> 1.281 + </script> 1.282 +</window>