From 2cecfe5968fe227cb287e90ddd135a1bf5648687 Mon Sep 17 00:00:00 2001 From: Geoff Lankow Date: Mon, 21 Nov 2011 00:34:33 +1300 Subject: [PATCH] Bug 702448 - Create some view source UI mochitests; r=jwein --- .../components/viewsource/test/Makefile.in | 3 + .../viewsource/test/browser/Makefile.in | 54 ++++++++ .../test/browser/browser_bug699356.js | 16 +++ .../test/browser/browser_viewsourceprefs.js | 129 ++++++++++++++++++ .../viewsource/test/browser/head.js | 32 +++++ 5 files changed, 234 insertions(+) create mode 100644 toolkit/components/viewsource/test/browser/Makefile.in create mode 100644 toolkit/components/viewsource/test/browser/browser_bug699356.js create mode 100644 toolkit/components/viewsource/test/browser/browser_viewsourceprefs.js create mode 100644 toolkit/components/viewsource/test/browser/head.js diff --git a/toolkit/components/viewsource/test/Makefile.in b/toolkit/components/viewsource/test/Makefile.in index 2cf06728ac5..be1be80914b 100644 --- a/toolkit/components/viewsource/test/Makefile.in +++ b/toolkit/components/viewsource/test/Makefile.in @@ -43,6 +43,9 @@ VPATH = @srcdir@ relativesrcdir = toolkit/components/viewsource/test include $(DEPTH)/config/autoconf.mk + +DIRS = browser + include $(topsrcdir)/config/rules.mk _CHROME_FILES = \ diff --git a/toolkit/components/viewsource/test/browser/Makefile.in b/toolkit/components/viewsource/test/browser/Makefile.in new file mode 100644 index 00000000000..09b8fd12760 --- /dev/null +++ b/toolkit/components/viewsource/test/browser/Makefile.in @@ -0,0 +1,54 @@ +# ***** BEGIN LICENSE BLOCK ***** +# Version: MPL 1.1/GPL 2.0/LGPL 2.1 +# +# The contents of this file are subject to the Mozilla Public License Version +# 1.1 (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# http://www.mozilla.org/MPL/ +# +# Software distributed under the License is distributed on an "AS IS" basis, +# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License +# for the specific language governing rights and limitations under the +# License. +# +# The Original Code is View Source tests. +# +# The Initial Developer of the Original Code is +# Geoff Lankow . +# +# Portions created by the Initial Developer are Copyright (C) 2011 +# the Initial Developer. All Rights Reserved. +# +# Contributor(s): +# +# Alternatively, the contents of this file may be used under the terms of +# either the GNU General Public License Version 2 or later (the "GPL"), or +# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), +# in which case the provisions of the GPL or the LGPL are applicable instead +# of those above. If you wish to allow use of your version of this file only +# under the terms of either the GPL or the LGPL, and not to allow others to +# use your version of this file under the terms of the MPL, indicate your +# decision by deleting the provisions above and replace them with the notice +# and other provisions required by the GPL or the LGPL. If you do not delete +# the provisions above, a recipient may use your version of this file under +# the terms of any one of the MPL, the GPL or the LGPL. +# +# ***** END LICENSE BLOCK ***** + +DEPTH = ../../../../.. +topsrcdir = @top_srcdir@ +srcdir = @srcdir@ +VPATH = @srcdir@ +relativesrcdir = toolkit/components/viewsource/test/browser + +include $(DEPTH)/config/autoconf.mk +include $(topsrcdir)/config/rules.mk + +_BROWSER_CHROME_FILES = \ + browser_bug699356.js \ + browser_viewsourceprefs.js \ + head.js \ + $(NULL) + +libs:: $(_BROWSER_CHROME_FILES) + $(INSTALL) $(foreach f,$^,"$f") $(DEPTH)/_tests/testing/mochitest/browser/$(relativesrcdir) diff --git a/toolkit/components/viewsource/test/browser/browser_bug699356.js b/toolkit/components/viewsource/test/browser/browser_bug699356.js new file mode 100644 index 00000000000..37d57cd08f8 --- /dev/null +++ b/toolkit/components/viewsource/test/browser/browser_bug699356.js @@ -0,0 +1,16 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ + */ + +function test() { + let source = "about:blank"; + + waitForExplicitFinish(); + openViewSourceWindow(source, function(aWindow) { + let gBrowser = aWindow.gBrowser; + + todo(gBrowser.contentDocument.title == source, "Correct document title"); + todo(aWindow.document.documentElement.getAttribute("title") == "Source of: " + source, "Correct window title"); + closeViewSourceWindow(aWindow, finish); + }); +} diff --git a/toolkit/components/viewsource/test/browser/browser_viewsourceprefs.js b/toolkit/components/viewsource/test/browser/browser_viewsourceprefs.js new file mode 100644 index 00000000000..2aba702a7b6 --- /dev/null +++ b/toolkit/components/viewsource/test/browser/browser_viewsourceprefs.js @@ -0,0 +1,129 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ + */ + +let source = "about:robots"; +let mWindow, wrapMenuItem, syntaxMenuItem; + +// Check the default values are set. +function test() { + waitForExplicitFinish(); + + openViewSourceWindow(source, function(aWindow) { + mWindow = aWindow; + wrapMenuItem = aWindow.document.getElementById('menu_wrapLongLines'); + syntaxMenuItem = aWindow.document.getElementById('menu_highlightSyntax'); + + // Strip checked="false" attributes, since we're not interested in them. + if (wrapMenuItem.getAttribute("checked") == "false") + wrapMenuItem.removeAttribute("checked"); + if (syntaxMenuItem.getAttribute("checked") == "false") + syntaxMenuItem.removeAttribute("checked"); + + is(wrapMenuItem.hasAttribute("checked"), false, "Wrap menu item not checked by default"); + is(syntaxMenuItem.hasAttribute("checked"), true, "Syntax menu item checked by default"); + checkStyle(aWindow, "-moz-tab-size", 4); + checkStyle(aWindow, "white-space", "pre"); + + test1(); + }); +} + +// Check that the Wrap Long Lines menu item works. +function test1() { + simulateClick(wrapMenuItem); + + is(wrapMenuItem.hasAttribute("checked"), true, "Wrap menu item checked"); + is(SpecialPowers.getBoolPref("view_source.wrap_long_lines"), true, "Wrap pref set"); + checkStyle(mWindow, "white-space", "pre-wrap"); + test2(); +} + +function test2() { + simulateClick(wrapMenuItem); + + is(wrapMenuItem.hasAttribute("checked"), false, "Wrap menu item unchecked"); + is(SpecialPowers.getBoolPref("view_source.wrap_long_lines"), false, "Wrap pref set"); + checkStyle(mWindow, "white-space", "pre"); + test3(); +} + +// Check that the Syntax Highlighting menu item works. +function test3() { + mWindow.gBrowser.addEventListener("pageshow", function test3Handler() { + mWindow.gBrowser.removeEventListener("pageshow", test3Handler, false); + is(syntaxMenuItem.hasAttribute("checked"), false, "Syntax menu item unchecked"); + is(SpecialPowers.getBoolPref("view_source.syntax_highlight"), false, "Syntax highlighting pref set"); + + checkHighlight(mWindow, false); + test4(); + }, false); + + simulateClick(syntaxMenuItem); +} + +function test4() { + mWindow.gBrowser.addEventListener("pageshow", function test4Handler() { + mWindow.gBrowser.removeEventListener("pageshow", test4Handler, false); + is(syntaxMenuItem.hasAttribute("checked"), true, "Syntax menu item checked"); + is(SpecialPowers.getBoolPref("view_source.syntax_highlight"), true, "Syntax highlighting pref set"); + + checkHighlight(mWindow, true); + closeViewSourceWindow(mWindow, test5); + }, false); + + simulateClick(syntaxMenuItem); +} + +// Open a new view-source window to check prefs are obeyed. +function test5() { + SpecialPowers.pushPrefEnv({'set': [ + ["view_source.tab_size", 2], + ["view_source.wrap_long_lines", true], + ["view_source.syntax_highlight", false] + ]}, function() { + openViewSourceWindow(source, function(aWindow) { + wrapMenuItem = aWindow.document.getElementById('menu_wrapLongLines'); + syntaxMenuItem = aWindow.document.getElementById('menu_highlightSyntax'); + + // Strip checked="false" attributes, since we're not interested in them. + if (wrapMenuItem.getAttribute("checked") == "false") + wrapMenuItem.removeAttribute("checked"); + if (syntaxMenuItem.getAttribute("checked") == "false") + syntaxMenuItem.removeAttribute("checked"); + + is(wrapMenuItem.hasAttribute("checked"), true, "Wrap menu item checked"); + is(syntaxMenuItem.hasAttribute("checked"), false, "Syntax menu item unchecked"); + checkStyle(aWindow, "-moz-tab-size", 2); + checkStyle(aWindow, "white-space", "pre-wrap"); + checkHighlight(aWindow, false); + closeViewSourceWindow(aWindow, finish); + }); + }); +} + +// Simulate a menu item click, including toggling the checked state. +// This saves us from opening the menu and trying to click on the item, +// which doesn't work on Mac OS X. +function simulateClick(aMenuItem) { + if (aMenuItem.hasAttribute("checked")) + aMenuItem.removeAttribute("checked"); + else + aMenuItem.setAttribute("checked", "true"); + + aMenuItem.click(); +} + +function checkStyle(aWindow, aStyleProperty, aExpectedValue) { + let gBrowser = aWindow.gBrowser; + let computedStyle = gBrowser.contentWindow.getComputedStyle(gBrowser.contentDocument.body, null); + + is(computedStyle.getPropertyValue(aStyleProperty), aExpectedValue, "Correct value of " + aStyleProperty); +} + +function checkHighlight(aWindow, aExpected) { + let spans = aWindow.gBrowser.contentDocument.getElementsByTagName("span"); + is(Array.some(spans, function(aSpan) { + return aSpan.className != ""; + }), aExpected, "Syntax highlighting " + (aExpected ? "on" : "off")); +} diff --git a/toolkit/components/viewsource/test/browser/head.js b/toolkit/components/viewsource/test/browser/head.js new file mode 100644 index 00000000000..8de630352b6 --- /dev/null +++ b/toolkit/components/viewsource/test/browser/head.js @@ -0,0 +1,32 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ + */ + +function openViewSourceWindow(aURI, aCallback) { + let viewSourceWindow = openDialog("chrome://global/content/viewSource.xul", null, null, aURI); + viewSourceWindow.addEventListener("pageshow", function pageShowHandler(event) { + if (event.target.location == "view-source:" + aURI) { + info("View source window opened: " + event.target.location); + viewSourceWindow.removeEventListener("pageshow", pageShowHandler, false); + aCallback(viewSourceWindow); + } + }, false); +} + +function closeViewSourceWindow(aWindow, aCallback) { + Services.wm.addListener({ + onCloseWindow: function() { + Services.wm.removeListener(this); + aCallback(); + } + }); + aWindow.close(); +} + +registerCleanupFunction(function() { + var windows = Services.wm.getEnumerator("navigator:view-source"); + if (windows.hasMoreElements()) + ok(false, "Found unexpected view source window still open"); + while (windows.hasMoreElements()) + windows.getNext().QueryInterface(Components.interfaces.nsIDOMWindow).close(); +});