Bug 702448 - Create some view source UI mochitests; r=jwein

This commit is contained in:
Geoff Lankow 2011-11-21 00:34:33 +13:00
parent cdf5934b9d
commit 2cecfe5968
5 changed files with 234 additions and 0 deletions

View File

@ -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 = \

View File

@ -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 <geoff@darktrojan.net>.
#
# 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)

View File

@ -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);
});
}

View File

@ -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"));
}

View File

@ -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();
});