Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 3 | |
michael@0 | 4 | function test() { |
michael@0 | 5 | let instance, widthBeforeClose, heightBeforeClose; |
michael@0 | 6 | let mgr = ResponsiveUI.ResponsiveUIManager; |
michael@0 | 7 | |
michael@0 | 8 | waitForExplicitFinish(); |
michael@0 | 9 | |
michael@0 | 10 | gBrowser.selectedTab = gBrowser.addTab(); |
michael@0 | 11 | gBrowser.selectedBrowser.addEventListener("load", function onload() { |
michael@0 | 12 | gBrowser.selectedBrowser.removeEventListener("load", onload, true); |
michael@0 | 13 | waitForFocus(startTest, content); |
michael@0 | 14 | }, true); |
michael@0 | 15 | |
michael@0 | 16 | content.location = "data:text/html,mop"; |
michael@0 | 17 | |
michael@0 | 18 | function startTest() { |
michael@0 | 19 | document.getElementById("Tools:ResponsiveUI").removeAttribute("disabled"); |
michael@0 | 20 | mgr.once("on", function() {executeSoon(onUIOpen)}); |
michael@0 | 21 | synthesizeKeyFromKeyTag("key_responsiveUI"); |
michael@0 | 22 | } |
michael@0 | 23 | |
michael@0 | 24 | function onUIOpen() { |
michael@0 | 25 | // Is it open? |
michael@0 | 26 | let container = gBrowser.getBrowserContainer(); |
michael@0 | 27 | is(container.getAttribute("responsivemode"), "true", "In responsive mode."); |
michael@0 | 28 | |
michael@0 | 29 | // Menus are correctly updated? |
michael@0 | 30 | is(document.getElementById("Tools:ResponsiveUI").getAttribute("checked"), "true", "menus checked"); |
michael@0 | 31 | |
michael@0 | 32 | instance = gBrowser.selectedTab.__responsiveUI; |
michael@0 | 33 | ok(instance, "instance of the module is attached to the tab."); |
michael@0 | 34 | |
michael@0 | 35 | if (instance._floatingScrollbars) { |
michael@0 | 36 | ensureScrollbarsAreFloating(); |
michael@0 | 37 | } |
michael@0 | 38 | |
michael@0 | 39 | instance.transitionsEnabled = false; |
michael@0 | 40 | |
michael@0 | 41 | testPresets(); |
michael@0 | 42 | } |
michael@0 | 43 | |
michael@0 | 44 | function ensureScrollbarsAreFloating() { |
michael@0 | 45 | let body = gBrowser.contentDocument.body; |
michael@0 | 46 | let html = gBrowser.contentDocument.documentElement; |
michael@0 | 47 | |
michael@0 | 48 | let originalWidth = body.getBoundingClientRect().width; |
michael@0 | 49 | |
michael@0 | 50 | html.style.overflowY = "scroll"; // Force scrollbars |
michael@0 | 51 | // Flush. Should not be needed as getBoundingClientRect() should flush, |
michael@0 | 52 | // but just in case. |
michael@0 | 53 | gBrowser.contentWindow.getComputedStyle(html).overflowY; |
michael@0 | 54 | let newWidth = body.getBoundingClientRect().width; |
michael@0 | 55 | is(originalWidth, newWidth, "Floating scrollbars are presents"); |
michael@0 | 56 | } |
michael@0 | 57 | |
michael@0 | 58 | function testPresets() { |
michael@0 | 59 | function testOnePreset(c) { |
michael@0 | 60 | if (c == 0) { |
michael@0 | 61 | executeSoon(testCustom); |
michael@0 | 62 | return; |
michael@0 | 63 | } |
michael@0 | 64 | instance.menulist.selectedIndex = c; |
michael@0 | 65 | let item = instance.menulist.firstChild.childNodes[c]; |
michael@0 | 66 | let [width, height] = extractSizeFromString(item.getAttribute("label")); |
michael@0 | 67 | is(content.innerWidth, width, "preset " + c + ": dimension valid (width)"); |
michael@0 | 68 | is(content.innerHeight, height, "preset " + c + ": dimension valid (height)"); |
michael@0 | 69 | |
michael@0 | 70 | testOnePreset(c - 1); |
michael@0 | 71 | } |
michael@0 | 72 | // Starting from length - 4 because last 3 items are not presets : separator, addbutton and removebutton |
michael@0 | 73 | testOnePreset(instance.menulist.firstChild.childNodes.length - 4); |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | function extractSizeFromString(str) { |
michael@0 | 77 | let numbers = str.match(/(\d+)[^\d]*(\d+)/); |
michael@0 | 78 | if (numbers) { |
michael@0 | 79 | return [numbers[1], numbers[2]]; |
michael@0 | 80 | } else { |
michael@0 | 81 | return [null, null]; |
michael@0 | 82 | } |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | function testCustom() { |
michael@0 | 86 | let initialWidth = content.innerWidth; |
michael@0 | 87 | let initialHeight = content.innerHeight; |
michael@0 | 88 | |
michael@0 | 89 | let x = 2, y = 2; |
michael@0 | 90 | EventUtils.synthesizeMouse(instance.resizer, x, y, {type: "mousedown"}, window); |
michael@0 | 91 | x += 20; y += 10; |
michael@0 | 92 | EventUtils.synthesizeMouse(instance.resizer, x, y, {type: "mousemove"}, window); |
michael@0 | 93 | EventUtils.synthesizeMouse(instance.resizer, x, y, {type: "mouseup"}, window); |
michael@0 | 94 | |
michael@0 | 95 | let expectedWidth = initialWidth + 20; |
michael@0 | 96 | let expectedHeight = initialHeight + 10; |
michael@0 | 97 | info("initial width: " + initialWidth); |
michael@0 | 98 | info("initial height: " + initialHeight); |
michael@0 | 99 | is(content.innerWidth, expectedWidth, "Size correcty updated (width)."); |
michael@0 | 100 | is(content.innerHeight, expectedHeight, "Size correcty updated (height)."); |
michael@0 | 101 | is(instance.menulist.selectedIndex, 0, "Custom menuitem selected"); |
michael@0 | 102 | let [width, height] = extractSizeFromString(instance.menulist.firstChild.firstChild.getAttribute("label")); |
michael@0 | 103 | is(width, expectedWidth, "Label updated (width)."); |
michael@0 | 104 | is(height, expectedHeight, "Label updated (height)."); |
michael@0 | 105 | testCustom2(); |
michael@0 | 106 | } |
michael@0 | 107 | |
michael@0 | 108 | function testCustom2() { |
michael@0 | 109 | let initialWidth = content.innerWidth; |
michael@0 | 110 | let initialHeight = content.innerHeight; |
michael@0 | 111 | |
michael@0 | 112 | let x = 2, y = 2; |
michael@0 | 113 | EventUtils.synthesizeMouse(instance.resizer, x, y, {type: "mousedown"}, window); |
michael@0 | 114 | x += 23; y += 13; |
michael@0 | 115 | EventUtils.synthesizeMouse(instance.resizer, x, y, {type: "mousemove", shiftKey: true}, window); |
michael@0 | 116 | EventUtils.synthesizeMouse(instance.resizer, x, y, {type: "mouseup"}, window); |
michael@0 | 117 | |
michael@0 | 118 | let expectedWidth = initialWidth + 20; |
michael@0 | 119 | let expectedHeight = initialHeight + 10; |
michael@0 | 120 | is(content.innerWidth, expectedWidth, "with shift: Size correcty updated (width)."); |
michael@0 | 121 | is(content.innerHeight, expectedHeight, "with shift: Size correcty updated (height)."); |
michael@0 | 122 | is(instance.menulist.selectedIndex, 0, "with shift: Custom menuitem selected"); |
michael@0 | 123 | let [width, height] = extractSizeFromString(instance.menulist.firstChild.firstChild.getAttribute("label")); |
michael@0 | 124 | is(width, expectedWidth, "Label updated (width)."); |
michael@0 | 125 | is(height, expectedHeight, "Label updated (height)."); |
michael@0 | 126 | testCustom3(); |
michael@0 | 127 | } |
michael@0 | 128 | |
michael@0 | 129 | function testCustom3() { |
michael@0 | 130 | let initialWidth = content.innerWidth; |
michael@0 | 131 | let initialHeight = content.innerHeight; |
michael@0 | 132 | |
michael@0 | 133 | let x = 2, y = 2; |
michael@0 | 134 | EventUtils.synthesizeMouse(instance.resizer, x, y, {type: "mousedown"}, window); |
michael@0 | 135 | x += 60; y += 30; |
michael@0 | 136 | EventUtils.synthesizeMouse(instance.resizer, x, y, {type: "mousemove", ctrlKey: true}, window); |
michael@0 | 137 | EventUtils.synthesizeMouse(instance.resizer, x, y, {type: "mouseup"}, window); |
michael@0 | 138 | |
michael@0 | 139 | let expectedWidth = initialWidth + 10; |
michael@0 | 140 | let expectedHeight = initialHeight + 5; |
michael@0 | 141 | is(content.innerWidth, expectedWidth, "with ctrl: Size correcty updated (width)."); |
michael@0 | 142 | is(content.innerHeight, expectedHeight, "with ctrl: Size correcty updated (height)."); |
michael@0 | 143 | is(instance.menulist.selectedIndex, 0, "with ctrl: Custom menuitem selected"); |
michael@0 | 144 | let [width, height] = extractSizeFromString(instance.menulist.firstChild.firstChild.getAttribute("label")); |
michael@0 | 145 | is(width, expectedWidth, "Label updated (width)."); |
michael@0 | 146 | is(height, expectedHeight, "Label updated (height)."); |
michael@0 | 147 | |
michael@0 | 148 | rotate(); |
michael@0 | 149 | } |
michael@0 | 150 | |
michael@0 | 151 | |
michael@0 | 152 | function rotate() { |
michael@0 | 153 | let initialWidth = content.innerWidth; |
michael@0 | 154 | let initialHeight = content.innerHeight; |
michael@0 | 155 | |
michael@0 | 156 | info("rotate"); |
michael@0 | 157 | instance.rotate(); |
michael@0 | 158 | |
michael@0 | 159 | is(content.innerWidth, initialHeight, "The width is now the height."); |
michael@0 | 160 | is(content.innerHeight, initialWidth, "The height is now the width."); |
michael@0 | 161 | let [width, height] = extractSizeFromString(instance.menulist.firstChild.firstChild.getAttribute("label")); |
michael@0 | 162 | is(width, initialHeight, "Label updated (width)."); |
michael@0 | 163 | is(height, initialWidth, "Label updated (height)."); |
michael@0 | 164 | |
michael@0 | 165 | widthBeforeClose = content.innerWidth; |
michael@0 | 166 | heightBeforeClose = content.innerHeight; |
michael@0 | 167 | |
michael@0 | 168 | info("XXX BUG 851296: instance.closing: " + !!instance.closing); |
michael@0 | 169 | |
michael@0 | 170 | mgr.once("off", function() { |
michael@0 | 171 | info("XXX BUG 851296: 'off' received."); |
michael@0 | 172 | executeSoon(restart); |
michael@0 | 173 | }); |
michael@0 | 174 | EventUtils.synthesizeKey("VK_ESCAPE", {}); |
michael@0 | 175 | } |
michael@0 | 176 | |
michael@0 | 177 | function restart() { |
michael@0 | 178 | info("XXX BUG 851296: restarting."); |
michael@0 | 179 | info("XXX BUG 851296: __responsiveUI: " + gBrowser.selectedTab.__responsiveUI); |
michael@0 | 180 | mgr.once("on", function() { |
michael@0 | 181 | info("XXX BUG 851296: 'on' received."); |
michael@0 | 182 | executeSoon(onUIOpen2); |
michael@0 | 183 | }); |
michael@0 | 184 | //XXX BUG 851296: synthesizeKeyFromKeyTag("key_responsiveUI"); |
michael@0 | 185 | mgr.toggle(window, gBrowser.selectedTab); |
michael@0 | 186 | info("XXX BUG 851296: restart() finished."); |
michael@0 | 187 | } |
michael@0 | 188 | |
michael@0 | 189 | function onUIOpen2() { |
michael@0 | 190 | info("XXX BUG 851296: onUIOpen2."); |
michael@0 | 191 | let container = gBrowser.getBrowserContainer(); |
michael@0 | 192 | is(container.getAttribute("responsivemode"), "true", "In responsive mode."); |
michael@0 | 193 | |
michael@0 | 194 | // Menus are correctly updated? |
michael@0 | 195 | is(document.getElementById("Tools:ResponsiveUI").getAttribute("checked"), "true", "menus checked"); |
michael@0 | 196 | |
michael@0 | 197 | is(content.innerWidth, widthBeforeClose, "width restored."); |
michael@0 | 198 | is(content.innerHeight, heightBeforeClose, "height restored."); |
michael@0 | 199 | |
michael@0 | 200 | mgr.once("off", function() {executeSoon(testScreenshot)}); |
michael@0 | 201 | EventUtils.synthesizeKey("VK_ESCAPE", {}); |
michael@0 | 202 | } |
michael@0 | 203 | |
michael@0 | 204 | function testScreenshot() { |
michael@0 | 205 | let isWinXP = navigator.userAgent.indexOf("Windows NT 5.1") != -1; |
michael@0 | 206 | if (isWinXP) { |
michael@0 | 207 | // We have issues testing this on Windows XP. |
michael@0 | 208 | // See https://bugzilla.mozilla.org/show_bug.cgi?id=848760#c17 |
michael@0 | 209 | return finishUp(); |
michael@0 | 210 | } |
michael@0 | 211 | |
michael@0 | 212 | info("screenshot"); |
michael@0 | 213 | instance.screenshot("responsiveui"); |
michael@0 | 214 | let FileUtils = (Cu.import("resource://gre/modules/FileUtils.jsm", {})).FileUtils; |
michael@0 | 215 | |
michael@0 | 216 | // while(1) until we find the file. |
michael@0 | 217 | // no need for a timeout, the test will get killed anyway. |
michael@0 | 218 | info("checking if file exists in 200ms"); |
michael@0 | 219 | function checkIfFileExist() { |
michael@0 | 220 | let file = FileUtils.getFile("DfltDwnld", [ "responsiveui.png" ]); |
michael@0 | 221 | if (file.exists()) { |
michael@0 | 222 | ok(true, "Screenshot file exists"); |
michael@0 | 223 | file.remove(false); |
michael@0 | 224 | finishUp(); |
michael@0 | 225 | } else { |
michael@0 | 226 | setTimeout(checkIfFileExist, 200); |
michael@0 | 227 | } |
michael@0 | 228 | } |
michael@0 | 229 | checkIfFileExist(); |
michael@0 | 230 | } |
michael@0 | 231 | |
michael@0 | 232 | function finishUp() { |
michael@0 | 233 | |
michael@0 | 234 | // Menus are correctly updated? |
michael@0 | 235 | is(document.getElementById("Tools:ResponsiveUI").getAttribute("checked"), "false", "menu unchecked"); |
michael@0 | 236 | |
michael@0 | 237 | delete instance; |
michael@0 | 238 | gBrowser.removeCurrentTab(); |
michael@0 | 239 | finish(); |
michael@0 | 240 | } |
michael@0 | 241 | |
michael@0 | 242 | function synthesizeKeyFromKeyTag(aKeyId) { |
michael@0 | 243 | let key = document.getElementById(aKeyId); |
michael@0 | 244 | isnot(key, null, "Successfully retrieved the <key> node"); |
michael@0 | 245 | |
michael@0 | 246 | let modifiersAttr = key.getAttribute("modifiers"); |
michael@0 | 247 | |
michael@0 | 248 | let name = null; |
michael@0 | 249 | |
michael@0 | 250 | if (key.getAttribute("keycode")) |
michael@0 | 251 | name = key.getAttribute("keycode"); |
michael@0 | 252 | else if (key.getAttribute("key")) |
michael@0 | 253 | name = key.getAttribute("key"); |
michael@0 | 254 | |
michael@0 | 255 | isnot(name, null, "Successfully retrieved keycode/key"); |
michael@0 | 256 | |
michael@0 | 257 | let modifiers = { |
michael@0 | 258 | shiftKey: modifiersAttr.match("shift"), |
michael@0 | 259 | ctrlKey: modifiersAttr.match("ctrl"), |
michael@0 | 260 | altKey: modifiersAttr.match("alt"), |
michael@0 | 261 | metaKey: modifiersAttr.match("meta"), |
michael@0 | 262 | accelKey: modifiersAttr.match("accel") |
michael@0 | 263 | } |
michael@0 | 264 | |
michael@0 | 265 | info("XXX BUG 851296: key name: " + name); |
michael@0 | 266 | info("XXX BUG 851296: key modifiers: " + JSON.stringify(modifiers)); |
michael@0 | 267 | EventUtils.synthesizeKey(name, modifiers); |
michael@0 | 268 | } |
michael@0 | 269 | } |