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/
3 */
5 let source = "data:text/plain,hello+world";
6 let mWindow, wrapMenuItem, syntaxMenuItem;
8 // Check the default values are set.
9 function test() {
10 waitForExplicitFinish();
12 registerCleanupFunction(function() {
13 SpecialPowers.clearUserPref("view_source.tab_size");
14 SpecialPowers.clearUserPref("view_source.wrap_long_lines");
15 SpecialPowers.clearUserPref("view_source.syntax_highlight");
16 });
18 openViewSourceWindow(source, function(aWindow) {
19 mWindow = aWindow;
20 wrapMenuItem = aWindow.document.getElementById('menu_wrapLongLines');
21 syntaxMenuItem = aWindow.document.getElementById('menu_highlightSyntax');
23 // Strip checked="false" attributes, since we're not interested in them.
24 if (wrapMenuItem.getAttribute("checked") == "false")
25 wrapMenuItem.removeAttribute("checked");
26 if (syntaxMenuItem.getAttribute("checked") == "false")
27 syntaxMenuItem.removeAttribute("checked");
29 is(wrapMenuItem.hasAttribute("checked"), false, "Wrap menu item not checked by default");
30 is(syntaxMenuItem.hasAttribute("checked"), true, "Syntax menu item checked by default");
31 checkStyle(aWindow, "-moz-tab-size", 4);
32 checkStyle(aWindow, "white-space", "pre");
34 test1();
35 });
36 }
38 // Check that the Wrap Long Lines menu item works.
39 function test1() {
40 simulateClick(wrapMenuItem);
42 is(wrapMenuItem.hasAttribute("checked"), true, "Wrap menu item checked");
43 is(SpecialPowers.getBoolPref("view_source.wrap_long_lines"), true, "Wrap pref set");
44 checkStyle(mWindow, "white-space", "pre-wrap");
45 test2();
46 }
48 function test2() {
49 simulateClick(wrapMenuItem);
51 is(wrapMenuItem.hasAttribute("checked"), false, "Wrap menu item unchecked");
52 is(SpecialPowers.getBoolPref("view_source.wrap_long_lines"), false, "Wrap pref set");
53 checkStyle(mWindow, "white-space", "pre");
54 test3();
55 }
57 // Check that the Syntax Highlighting menu item works.
58 function test3() {
59 mWindow.gBrowser.addEventListener("pageshow", function test3Handler() {
60 mWindow.gBrowser.removeEventListener("pageshow", test3Handler, false);
61 is(syntaxMenuItem.hasAttribute("checked"), false, "Syntax menu item unchecked");
62 is(SpecialPowers.getBoolPref("view_source.syntax_highlight"), false, "Syntax highlighting pref set");
64 checkHighlight(mWindow, false);
65 test4();
66 }, false);
68 simulateClick(syntaxMenuItem);
69 }
71 function test4() {
72 mWindow.gBrowser.addEventListener("pageshow", function test4Handler() {
73 mWindow.gBrowser.removeEventListener("pageshow", test4Handler, false);
74 is(syntaxMenuItem.hasAttribute("checked"), true, "Syntax menu item checked");
75 is(SpecialPowers.getBoolPref("view_source.syntax_highlight"), true, "Syntax highlighting pref set");
77 checkHighlight(mWindow, false);
78 closeViewSourceWindow(mWindow, test5);
79 }, false);
81 simulateClick(syntaxMenuItem);
82 }
84 // Open a new view-source window to check prefs are obeyed.
85 function test5() {
86 SpecialPowers.setIntPref("view_source.tab_size", 2);
87 SpecialPowers.setBoolPref("view_source.wrap_long_lines", true);
88 SpecialPowers.setBoolPref("view_source.syntax_highlight", false);
90 executeSoon(function() {
91 openViewSourceWindow(source, function(aWindow) {
92 wrapMenuItem = aWindow.document.getElementById('menu_wrapLongLines');
93 syntaxMenuItem = aWindow.document.getElementById('menu_highlightSyntax');
95 // Strip checked="false" attributes, since we're not interested in them.
96 if (wrapMenuItem.getAttribute("checked") == "false")
97 wrapMenuItem.removeAttribute("checked");
98 if (syntaxMenuItem.getAttribute("checked") == "false")
99 syntaxMenuItem.removeAttribute("checked");
101 is(wrapMenuItem.hasAttribute("checked"), true, "Wrap menu item checked");
102 is(syntaxMenuItem.hasAttribute("checked"), false, "Syntax menu item unchecked");
103 checkStyle(aWindow, "-moz-tab-size", 2);
104 checkStyle(aWindow, "white-space", "pre-wrap");
105 checkHighlight(aWindow, false);
106 closeViewSourceWindow(aWindow, finish);
107 });
108 });
109 }
111 // Simulate a menu item click, including toggling the checked state.
112 // This saves us from opening the menu and trying to click on the item,
113 // which doesn't work on Mac OS X.
114 function simulateClick(aMenuItem) {
115 if (aMenuItem.hasAttribute("checked"))
116 aMenuItem.removeAttribute("checked");
117 else
118 aMenuItem.setAttribute("checked", "true");
120 aMenuItem.click();
121 }
123 function checkStyle(aWindow, aStyleProperty, aExpectedValue) {
124 let gBrowser = aWindow.gBrowser;
125 let computedStyle = gBrowser.contentWindow.getComputedStyle(gBrowser.contentDocument.body, null);
127 is(computedStyle.getPropertyValue(aStyleProperty), aExpectedValue, "Correct value of " + aStyleProperty);
128 }
130 function checkHighlight(aWindow, aExpected) {
131 let spans = aWindow.gBrowser.contentDocument.getElementsByTagName("span");
132 is(Array.some(spans, function(aSpan) {
133 return aSpan.className != "";
134 }), aExpected, "Syntax highlighting " + (aExpected ? "on" : "off"));
135 }