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