1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/viewsource/test/browser/browser_viewsourceprefs_nonhtml.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,135 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + * http://creativecommons.org/publicdomain/zero/1.0/ 1.6 + */ 1.7 + 1.8 +let source = "data:text/plain,hello+world"; 1.9 +let mWindow, wrapMenuItem, syntaxMenuItem; 1.10 + 1.11 +// Check the default values are set. 1.12 +function test() { 1.13 + waitForExplicitFinish(); 1.14 + 1.15 + registerCleanupFunction(function() { 1.16 + SpecialPowers.clearUserPref("view_source.tab_size"); 1.17 + SpecialPowers.clearUserPref("view_source.wrap_long_lines"); 1.18 + SpecialPowers.clearUserPref("view_source.syntax_highlight"); 1.19 + }); 1.20 + 1.21 + openViewSourceWindow(source, function(aWindow) { 1.22 + mWindow = aWindow; 1.23 + wrapMenuItem = aWindow.document.getElementById('menu_wrapLongLines'); 1.24 + syntaxMenuItem = aWindow.document.getElementById('menu_highlightSyntax'); 1.25 + 1.26 + // Strip checked="false" attributes, since we're not interested in them. 1.27 + if (wrapMenuItem.getAttribute("checked") == "false") 1.28 + wrapMenuItem.removeAttribute("checked"); 1.29 + if (syntaxMenuItem.getAttribute("checked") == "false") 1.30 + syntaxMenuItem.removeAttribute("checked"); 1.31 + 1.32 + is(wrapMenuItem.hasAttribute("checked"), false, "Wrap menu item not checked by default"); 1.33 + is(syntaxMenuItem.hasAttribute("checked"), true, "Syntax menu item checked by default"); 1.34 + checkStyle(aWindow, "-moz-tab-size", 4); 1.35 + checkStyle(aWindow, "white-space", "pre"); 1.36 + 1.37 + test1(); 1.38 + }); 1.39 +} 1.40 + 1.41 +// Check that the Wrap Long Lines menu item works. 1.42 +function test1() { 1.43 + simulateClick(wrapMenuItem); 1.44 + 1.45 + is(wrapMenuItem.hasAttribute("checked"), true, "Wrap menu item checked"); 1.46 + is(SpecialPowers.getBoolPref("view_source.wrap_long_lines"), true, "Wrap pref set"); 1.47 + checkStyle(mWindow, "white-space", "pre-wrap"); 1.48 + test2(); 1.49 +} 1.50 + 1.51 +function test2() { 1.52 + simulateClick(wrapMenuItem); 1.53 + 1.54 + is(wrapMenuItem.hasAttribute("checked"), false, "Wrap menu item unchecked"); 1.55 + is(SpecialPowers.getBoolPref("view_source.wrap_long_lines"), false, "Wrap pref set"); 1.56 + checkStyle(mWindow, "white-space", "pre"); 1.57 + test3(); 1.58 +} 1.59 + 1.60 +// Check that the Syntax Highlighting menu item works. 1.61 +function test3() { 1.62 + mWindow.gBrowser.addEventListener("pageshow", function test3Handler() { 1.63 + mWindow.gBrowser.removeEventListener("pageshow", test3Handler, false); 1.64 + is(syntaxMenuItem.hasAttribute("checked"), false, "Syntax menu item unchecked"); 1.65 + is(SpecialPowers.getBoolPref("view_source.syntax_highlight"), false, "Syntax highlighting pref set"); 1.66 + 1.67 + checkHighlight(mWindow, false); 1.68 + test4(); 1.69 + }, false); 1.70 + 1.71 + simulateClick(syntaxMenuItem); 1.72 +} 1.73 + 1.74 +function test4() { 1.75 + mWindow.gBrowser.addEventListener("pageshow", function test4Handler() { 1.76 + mWindow.gBrowser.removeEventListener("pageshow", test4Handler, false); 1.77 + is(syntaxMenuItem.hasAttribute("checked"), true, "Syntax menu item checked"); 1.78 + is(SpecialPowers.getBoolPref("view_source.syntax_highlight"), true, "Syntax highlighting pref set"); 1.79 + 1.80 + checkHighlight(mWindow, false); 1.81 + closeViewSourceWindow(mWindow, test5); 1.82 + }, false); 1.83 + 1.84 + simulateClick(syntaxMenuItem); 1.85 +} 1.86 + 1.87 +// Open a new view-source window to check prefs are obeyed. 1.88 +function test5() { 1.89 + SpecialPowers.setIntPref("view_source.tab_size", 2); 1.90 + SpecialPowers.setBoolPref("view_source.wrap_long_lines", true); 1.91 + SpecialPowers.setBoolPref("view_source.syntax_highlight", false); 1.92 + 1.93 + executeSoon(function() { 1.94 + openViewSourceWindow(source, function(aWindow) { 1.95 + wrapMenuItem = aWindow.document.getElementById('menu_wrapLongLines'); 1.96 + syntaxMenuItem = aWindow.document.getElementById('menu_highlightSyntax'); 1.97 + 1.98 + // Strip checked="false" attributes, since we're not interested in them. 1.99 + if (wrapMenuItem.getAttribute("checked") == "false") 1.100 + wrapMenuItem.removeAttribute("checked"); 1.101 + if (syntaxMenuItem.getAttribute("checked") == "false") 1.102 + syntaxMenuItem.removeAttribute("checked"); 1.103 + 1.104 + is(wrapMenuItem.hasAttribute("checked"), true, "Wrap menu item checked"); 1.105 + is(syntaxMenuItem.hasAttribute("checked"), false, "Syntax menu item unchecked"); 1.106 + checkStyle(aWindow, "-moz-tab-size", 2); 1.107 + checkStyle(aWindow, "white-space", "pre-wrap"); 1.108 + checkHighlight(aWindow, false); 1.109 + closeViewSourceWindow(aWindow, finish); 1.110 + }); 1.111 + }); 1.112 +} 1.113 + 1.114 +// Simulate a menu item click, including toggling the checked state. 1.115 +// This saves us from opening the menu and trying to click on the item, 1.116 +// which doesn't work on Mac OS X. 1.117 +function simulateClick(aMenuItem) { 1.118 + if (aMenuItem.hasAttribute("checked")) 1.119 + aMenuItem.removeAttribute("checked"); 1.120 + else 1.121 + aMenuItem.setAttribute("checked", "true"); 1.122 + 1.123 + aMenuItem.click(); 1.124 +} 1.125 + 1.126 +function checkStyle(aWindow, aStyleProperty, aExpectedValue) { 1.127 + let gBrowser = aWindow.gBrowser; 1.128 + let computedStyle = gBrowser.contentWindow.getComputedStyle(gBrowser.contentDocument.body, null); 1.129 + 1.130 + is(computedStyle.getPropertyValue(aStyleProperty), aExpectedValue, "Correct value of " + aStyleProperty); 1.131 +} 1.132 + 1.133 +function checkHighlight(aWindow, aExpected) { 1.134 + let spans = aWindow.gBrowser.contentDocument.getElementsByTagName("span"); 1.135 + is(Array.some(spans, function(aSpan) { 1.136 + return aSpan.className != ""; 1.137 + }), aExpected, "Syntax highlighting " + (aExpected ? "on" : "off")); 1.138 +}