1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/devtools/responsivedesign/test/browser_responsiveui.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,269 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +function test() { 1.8 + let instance, widthBeforeClose, heightBeforeClose; 1.9 + let mgr = ResponsiveUI.ResponsiveUIManager; 1.10 + 1.11 + waitForExplicitFinish(); 1.12 + 1.13 + gBrowser.selectedTab = gBrowser.addTab(); 1.14 + gBrowser.selectedBrowser.addEventListener("load", function onload() { 1.15 + gBrowser.selectedBrowser.removeEventListener("load", onload, true); 1.16 + waitForFocus(startTest, content); 1.17 + }, true); 1.18 + 1.19 + content.location = "data:text/html,mop"; 1.20 + 1.21 + function startTest() { 1.22 + document.getElementById("Tools:ResponsiveUI").removeAttribute("disabled"); 1.23 + mgr.once("on", function() {executeSoon(onUIOpen)}); 1.24 + synthesizeKeyFromKeyTag("key_responsiveUI"); 1.25 + } 1.26 + 1.27 + function onUIOpen() { 1.28 + // Is it open? 1.29 + let container = gBrowser.getBrowserContainer(); 1.30 + is(container.getAttribute("responsivemode"), "true", "In responsive mode."); 1.31 + 1.32 + // Menus are correctly updated? 1.33 + is(document.getElementById("Tools:ResponsiveUI").getAttribute("checked"), "true", "menus checked"); 1.34 + 1.35 + instance = gBrowser.selectedTab.__responsiveUI; 1.36 + ok(instance, "instance of the module is attached to the tab."); 1.37 + 1.38 + if (instance._floatingScrollbars) { 1.39 + ensureScrollbarsAreFloating(); 1.40 + } 1.41 + 1.42 + instance.transitionsEnabled = false; 1.43 + 1.44 + testPresets(); 1.45 + } 1.46 + 1.47 + function ensureScrollbarsAreFloating() { 1.48 + let body = gBrowser.contentDocument.body; 1.49 + let html = gBrowser.contentDocument.documentElement; 1.50 + 1.51 + let originalWidth = body.getBoundingClientRect().width; 1.52 + 1.53 + html.style.overflowY = "scroll"; // Force scrollbars 1.54 + // Flush. Should not be needed as getBoundingClientRect() should flush, 1.55 + // but just in case. 1.56 + gBrowser.contentWindow.getComputedStyle(html).overflowY; 1.57 + let newWidth = body.getBoundingClientRect().width; 1.58 + is(originalWidth, newWidth, "Floating scrollbars are presents"); 1.59 + } 1.60 + 1.61 + function testPresets() { 1.62 + function testOnePreset(c) { 1.63 + if (c == 0) { 1.64 + executeSoon(testCustom); 1.65 + return; 1.66 + } 1.67 + instance.menulist.selectedIndex = c; 1.68 + let item = instance.menulist.firstChild.childNodes[c]; 1.69 + let [width, height] = extractSizeFromString(item.getAttribute("label")); 1.70 + is(content.innerWidth, width, "preset " + c + ": dimension valid (width)"); 1.71 + is(content.innerHeight, height, "preset " + c + ": dimension valid (height)"); 1.72 + 1.73 + testOnePreset(c - 1); 1.74 + } 1.75 + // Starting from length - 4 because last 3 items are not presets : separator, addbutton and removebutton 1.76 + testOnePreset(instance.menulist.firstChild.childNodes.length - 4); 1.77 + } 1.78 + 1.79 + function extractSizeFromString(str) { 1.80 + let numbers = str.match(/(\d+)[^\d]*(\d+)/); 1.81 + if (numbers) { 1.82 + return [numbers[1], numbers[2]]; 1.83 + } else { 1.84 + return [null, null]; 1.85 + } 1.86 + } 1.87 + 1.88 + function testCustom() { 1.89 + let initialWidth = content.innerWidth; 1.90 + let initialHeight = content.innerHeight; 1.91 + 1.92 + let x = 2, y = 2; 1.93 + EventUtils.synthesizeMouse(instance.resizer, x, y, {type: "mousedown"}, window); 1.94 + x += 20; y += 10; 1.95 + EventUtils.synthesizeMouse(instance.resizer, x, y, {type: "mousemove"}, window); 1.96 + EventUtils.synthesizeMouse(instance.resizer, x, y, {type: "mouseup"}, window); 1.97 + 1.98 + let expectedWidth = initialWidth + 20; 1.99 + let expectedHeight = initialHeight + 10; 1.100 + info("initial width: " + initialWidth); 1.101 + info("initial height: " + initialHeight); 1.102 + is(content.innerWidth, expectedWidth, "Size correcty updated (width)."); 1.103 + is(content.innerHeight, expectedHeight, "Size correcty updated (height)."); 1.104 + is(instance.menulist.selectedIndex, 0, "Custom menuitem selected"); 1.105 + let [width, height] = extractSizeFromString(instance.menulist.firstChild.firstChild.getAttribute("label")); 1.106 + is(width, expectedWidth, "Label updated (width)."); 1.107 + is(height, expectedHeight, "Label updated (height)."); 1.108 + testCustom2(); 1.109 + } 1.110 + 1.111 + function testCustom2() { 1.112 + let initialWidth = content.innerWidth; 1.113 + let initialHeight = content.innerHeight; 1.114 + 1.115 + let x = 2, y = 2; 1.116 + EventUtils.synthesizeMouse(instance.resizer, x, y, {type: "mousedown"}, window); 1.117 + x += 23; y += 13; 1.118 + EventUtils.synthesizeMouse(instance.resizer, x, y, {type: "mousemove", shiftKey: true}, window); 1.119 + EventUtils.synthesizeMouse(instance.resizer, x, y, {type: "mouseup"}, window); 1.120 + 1.121 + let expectedWidth = initialWidth + 20; 1.122 + let expectedHeight = initialHeight + 10; 1.123 + is(content.innerWidth, expectedWidth, "with shift: Size correcty updated (width)."); 1.124 + is(content.innerHeight, expectedHeight, "with shift: Size correcty updated (height)."); 1.125 + is(instance.menulist.selectedIndex, 0, "with shift: Custom menuitem selected"); 1.126 + let [width, height] = extractSizeFromString(instance.menulist.firstChild.firstChild.getAttribute("label")); 1.127 + is(width, expectedWidth, "Label updated (width)."); 1.128 + is(height, expectedHeight, "Label updated (height)."); 1.129 + testCustom3(); 1.130 + } 1.131 + 1.132 + function testCustom3() { 1.133 + let initialWidth = content.innerWidth; 1.134 + let initialHeight = content.innerHeight; 1.135 + 1.136 + let x = 2, y = 2; 1.137 + EventUtils.synthesizeMouse(instance.resizer, x, y, {type: "mousedown"}, window); 1.138 + x += 60; y += 30; 1.139 + EventUtils.synthesizeMouse(instance.resizer, x, y, {type: "mousemove", ctrlKey: true}, window); 1.140 + EventUtils.synthesizeMouse(instance.resizer, x, y, {type: "mouseup"}, window); 1.141 + 1.142 + let expectedWidth = initialWidth + 10; 1.143 + let expectedHeight = initialHeight + 5; 1.144 + is(content.innerWidth, expectedWidth, "with ctrl: Size correcty updated (width)."); 1.145 + is(content.innerHeight, expectedHeight, "with ctrl: Size correcty updated (height)."); 1.146 + is(instance.menulist.selectedIndex, 0, "with ctrl: Custom menuitem selected"); 1.147 + let [width, height] = extractSizeFromString(instance.menulist.firstChild.firstChild.getAttribute("label")); 1.148 + is(width, expectedWidth, "Label updated (width)."); 1.149 + is(height, expectedHeight, "Label updated (height)."); 1.150 + 1.151 + rotate(); 1.152 + } 1.153 + 1.154 + 1.155 + function rotate() { 1.156 + let initialWidth = content.innerWidth; 1.157 + let initialHeight = content.innerHeight; 1.158 + 1.159 + info("rotate"); 1.160 + instance.rotate(); 1.161 + 1.162 + is(content.innerWidth, initialHeight, "The width is now the height."); 1.163 + is(content.innerHeight, initialWidth, "The height is now the width."); 1.164 + let [width, height] = extractSizeFromString(instance.menulist.firstChild.firstChild.getAttribute("label")); 1.165 + is(width, initialHeight, "Label updated (width)."); 1.166 + is(height, initialWidth, "Label updated (height)."); 1.167 + 1.168 + widthBeforeClose = content.innerWidth; 1.169 + heightBeforeClose = content.innerHeight; 1.170 + 1.171 + info("XXX BUG 851296: instance.closing: " + !!instance.closing); 1.172 + 1.173 + mgr.once("off", function() { 1.174 + info("XXX BUG 851296: 'off' received."); 1.175 + executeSoon(restart); 1.176 + }); 1.177 + EventUtils.synthesizeKey("VK_ESCAPE", {}); 1.178 + } 1.179 + 1.180 + function restart() { 1.181 + info("XXX BUG 851296: restarting."); 1.182 + info("XXX BUG 851296: __responsiveUI: " + gBrowser.selectedTab.__responsiveUI); 1.183 + mgr.once("on", function() { 1.184 + info("XXX BUG 851296: 'on' received."); 1.185 + executeSoon(onUIOpen2); 1.186 + }); 1.187 + //XXX BUG 851296: synthesizeKeyFromKeyTag("key_responsiveUI"); 1.188 + mgr.toggle(window, gBrowser.selectedTab); 1.189 + info("XXX BUG 851296: restart() finished."); 1.190 + } 1.191 + 1.192 + function onUIOpen2() { 1.193 + info("XXX BUG 851296: onUIOpen2."); 1.194 + let container = gBrowser.getBrowserContainer(); 1.195 + is(container.getAttribute("responsivemode"), "true", "In responsive mode."); 1.196 + 1.197 + // Menus are correctly updated? 1.198 + is(document.getElementById("Tools:ResponsiveUI").getAttribute("checked"), "true", "menus checked"); 1.199 + 1.200 + is(content.innerWidth, widthBeforeClose, "width restored."); 1.201 + is(content.innerHeight, heightBeforeClose, "height restored."); 1.202 + 1.203 + mgr.once("off", function() {executeSoon(testScreenshot)}); 1.204 + EventUtils.synthesizeKey("VK_ESCAPE", {}); 1.205 + } 1.206 + 1.207 + function testScreenshot() { 1.208 + let isWinXP = navigator.userAgent.indexOf("Windows NT 5.1") != -1; 1.209 + if (isWinXP) { 1.210 + // We have issues testing this on Windows XP. 1.211 + // See https://bugzilla.mozilla.org/show_bug.cgi?id=848760#c17 1.212 + return finishUp(); 1.213 + } 1.214 + 1.215 + info("screenshot"); 1.216 + instance.screenshot("responsiveui"); 1.217 + let FileUtils = (Cu.import("resource://gre/modules/FileUtils.jsm", {})).FileUtils; 1.218 + 1.219 + // while(1) until we find the file. 1.220 + // no need for a timeout, the test will get killed anyway. 1.221 + info("checking if file exists in 200ms"); 1.222 + function checkIfFileExist() { 1.223 + let file = FileUtils.getFile("DfltDwnld", [ "responsiveui.png" ]); 1.224 + if (file.exists()) { 1.225 + ok(true, "Screenshot file exists"); 1.226 + file.remove(false); 1.227 + finishUp(); 1.228 + } else { 1.229 + setTimeout(checkIfFileExist, 200); 1.230 + } 1.231 + } 1.232 + checkIfFileExist(); 1.233 + } 1.234 + 1.235 + function finishUp() { 1.236 + 1.237 + // Menus are correctly updated? 1.238 + is(document.getElementById("Tools:ResponsiveUI").getAttribute("checked"), "false", "menu unchecked"); 1.239 + 1.240 + delete instance; 1.241 + gBrowser.removeCurrentTab(); 1.242 + finish(); 1.243 + } 1.244 + 1.245 + function synthesizeKeyFromKeyTag(aKeyId) { 1.246 + let key = document.getElementById(aKeyId); 1.247 + isnot(key, null, "Successfully retrieved the <key> node"); 1.248 + 1.249 + let modifiersAttr = key.getAttribute("modifiers"); 1.250 + 1.251 + let name = null; 1.252 + 1.253 + if (key.getAttribute("keycode")) 1.254 + name = key.getAttribute("keycode"); 1.255 + else if (key.getAttribute("key")) 1.256 + name = key.getAttribute("key"); 1.257 + 1.258 + isnot(name, null, "Successfully retrieved keycode/key"); 1.259 + 1.260 + let modifiers = { 1.261 + shiftKey: modifiersAttr.match("shift"), 1.262 + ctrlKey: modifiersAttr.match("ctrl"), 1.263 + altKey: modifiersAttr.match("alt"), 1.264 + metaKey: modifiersAttr.match("meta"), 1.265 + accelKey: modifiersAttr.match("accel") 1.266 + } 1.267 + 1.268 + info("XXX BUG 851296: key name: " + name); 1.269 + info("XXX BUG 851296: key modifiers: " + JSON.stringify(modifiers)); 1.270 + EventUtils.synthesizeKey(name, modifiers); 1.271 + } 1.272 +}