|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 * http://creativecommons.org/publicdomain/zero/1.0/ |
|
3 */ |
|
4 |
|
5 let source = "data:text/plain,hello+world"; |
|
6 let mWindow, wrapMenuItem, syntaxMenuItem; |
|
7 |
|
8 // Check the default values are set. |
|
9 function test() { |
|
10 waitForExplicitFinish(); |
|
11 |
|
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 }); |
|
17 |
|
18 openViewSourceWindow(source, function(aWindow) { |
|
19 mWindow = aWindow; |
|
20 wrapMenuItem = aWindow.document.getElementById('menu_wrapLongLines'); |
|
21 syntaxMenuItem = aWindow.document.getElementById('menu_highlightSyntax'); |
|
22 |
|
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"); |
|
28 |
|
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"); |
|
33 |
|
34 test1(); |
|
35 }); |
|
36 } |
|
37 |
|
38 // Check that the Wrap Long Lines menu item works. |
|
39 function test1() { |
|
40 simulateClick(wrapMenuItem); |
|
41 |
|
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 } |
|
47 |
|
48 function test2() { |
|
49 simulateClick(wrapMenuItem); |
|
50 |
|
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 } |
|
56 |
|
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"); |
|
63 |
|
64 checkHighlight(mWindow, false); |
|
65 test4(); |
|
66 }, false); |
|
67 |
|
68 simulateClick(syntaxMenuItem); |
|
69 } |
|
70 |
|
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"); |
|
76 |
|
77 checkHighlight(mWindow, false); |
|
78 closeViewSourceWindow(mWindow, test5); |
|
79 }, false); |
|
80 |
|
81 simulateClick(syntaxMenuItem); |
|
82 } |
|
83 |
|
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); |
|
89 |
|
90 executeSoon(function() { |
|
91 openViewSourceWindow(source, function(aWindow) { |
|
92 wrapMenuItem = aWindow.document.getElementById('menu_wrapLongLines'); |
|
93 syntaxMenuItem = aWindow.document.getElementById('menu_highlightSyntax'); |
|
94 |
|
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"); |
|
100 |
|
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 } |
|
110 |
|
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"); |
|
119 |
|
120 aMenuItem.click(); |
|
121 } |
|
122 |
|
123 function checkStyle(aWindow, aStyleProperty, aExpectedValue) { |
|
124 let gBrowser = aWindow.gBrowser; |
|
125 let computedStyle = gBrowser.contentWindow.getComputedStyle(gBrowser.contentDocument.body, null); |
|
126 |
|
127 is(computedStyle.getPropertyValue(aStyleProperty), aExpectedValue, "Correct value of " + aStyleProperty); |
|
128 } |
|
129 |
|
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 } |