toolkit/components/viewsource/test/browser/browser_viewsourceprefs_nonhtml.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

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

mercurial