Fix existing tests to deal with getComputedStyle lying about :link vs. :visited. (Bug 147777) r=sdwilsh

This commit is contained in:
L. David Baron 2010-04-02 18:58:25 -07:00
parent 5d680f5d9b
commit 7a857e9b5a
9 changed files with 129 additions and 51 deletions

View file

@ -77,14 +77,19 @@ function link123HrefIs(href, testNum) {
var gGen;
function visitedDependentComputedStyle(win, elem, property) {
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
var utils = win.QueryInterface(Components.interfaces.nsIInterfaceRequestor).
getInterface(Components.interfaces.nsIDOMWindowUtils);
return utils.getVisitedDependentComputedStyle(elem, "", property);
}
function getColor(elem) {
return document.defaultView.getComputedStyle(elem, "").color;
return visitedDependentComputedStyle(document.defaultView, elem, "color");
}
function getFill(elem) {
return document.defaultView
.getComputedStyle(elem, "")
.getPropertyValue("fill");
return visitedDependentComputedStyle(document.defaultView, elem, "fill");
}
function setXlinkHref(elem, href) {

View file

@ -51,12 +51,18 @@ function continueTest() {
function checkLinkColor(aElmId, aExpectedColor, aMessage) {
// Because link coloring is asynchronous, we wait until we get the right
// result, or we will time out (resulting in a failure).
var getCS = document.defaultView.getComputedStyle;
while (getCS($(aElmId), "").color != aExpectedColor) {
function getColor() {
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
var utils = document.defaultView.
QueryInterface(Components.interfaces.nsIInterfaceRequestor).
getInterface(Components.interfaces.nsIDOMWindowUtils);
return utils.getVisitedDependentComputedStyle($(aElmId), "", "color");
}
while (getColor() != aExpectedColor) {
setTimeout(continueTest, 10);
return false;
}
is(getCS($(aElmId), "").color, aExpectedColor, aMessage);
is(getColor(), aExpectedColor, aMessage);
return true;
}

View file

@ -2,8 +2,8 @@
<head>
<title>Bug 293235 page1</title>
<style type="text/css">
a:visited { color: rgb(128, 0, 128); }
a:link { color: rgb(0, 0, 128); }
a:visited, a.forcevisited.forcevisited { color: rgb(128, 0, 128); }
a:link, a.forcelink.forcelink { color: rgb(0, 0, 128); }
a:focus { color: rgb(128, 0, 0); }
</style>
</head>

View file

@ -12,6 +12,7 @@
src=
"chrome://mochikit/content/chrome/docshell/test/chrome/docshell_helpers.js">
</script>
<script type="text/javascript" src="chrome://mochikit/content/tests/SimpleTest/WindowSnapshot.js"></script>
<script type="application/javascript"><![CDATA[
const Ci = Components.interfaces;
@ -74,9 +75,22 @@
yield;
// Now that we've been notified, we can check our link color.
is(TestWindow.getWindow().getComputedStyle($("link1"), "").color,
"rgb(0, 0, 128)",
"link not initially blue");
// Since we can't use getComputedStyle() for this because
// getComputedStyle lies about styles that result from :visited,
// we have to take snapshots.
// First, take two reference snapshots.
var link1 = $("link1");
link1.className = "forcelink";
var refLink = snapshotWindow(TestWindow.getWindow());
link1.className = "forcevisited";
var refVisited = snapshotWindow(TestWindow.getWindow());
link1.className = "";
function snapshotsEqual(snap1, snap2) {
return compareSnapshots(snap1, snap2, true)[0];
}
ok(!snapshotsEqual(refLink, refVisited), "references should not match");
ok(snapshotsEqual(refLink, snapshotWindow(TestWindow.getWindow())),
"link should initially be blue");
// Load the page that the link on the previous page points to.
doPageNavigation({
@ -109,9 +123,8 @@
yield;
// Now we can test the link color.
is(TestWindow.getWindow().getComputedStyle($("link1"), "").color,
"rgb(128, 0, 128)",
":visited link wrong color");
ok(snapshotsEqual(refVisited, snapshotWindow(TestWindow.getWindow())),
"visited link should be purple");
// Tell the framework the test is finished. Include the final 'yield'
// statement to prevent a StopIteration exception from being thrown.

View file

@ -107,7 +107,11 @@ function popstateExpected(msg) {
}
function getColor(elem) {
return document.defaultView.getComputedStyle(elem, "").color;
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
var utils = document.defaultView.
QueryInterface(Components.interfaces.nsIInterfaceRequestor).
getInterface(Components.interfaces.nsIDOMWindowUtils);
return utils.getVisitedDependentComputedStyle(elem, "", "color");
}
function getSHistory(theWindow)

View file

@ -200,6 +200,7 @@ _TEST_FILES = test_acid3_test46.html \
ccd-quirks.html \
ccd-standards.html \
ccd.sjs \
visited-pref-iframe.html \
$(NULL)
_BROWSER_FILES = \

View file

@ -7,6 +7,7 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=147777
<title>Test for visited link coloring pref Bug 147777</title>
<script type="application/javascript" src="/MochiKit/packed.js"></script>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="application/javascript" src="/tests/SimpleTest/WindowSnapshot.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
<style type="text/css">
@ -18,14 +19,12 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=147777
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=147777">Mozilla Bug 147777</a>
<p id="display"><a id="mylink" href="test_visited_pref.html">link to this page</a></p>
<iframe id="iframe" src="visited-pref-iframe.html" style="width: 10em; height: 5em"></iframe>
<pre id="test">
<script type="application/javascript">
/** Test for Bug 147777 **/
// NOTE: this test will fail when ran by itself because the URL is different!
function reinsert_node(e) {
var sib = e.nextSibling;
var par = e.parentNode;
@ -50,41 +49,80 @@ function set_pref(val)
dispBranch.setBoolPref("visited_links_enabled", val);
}
is(get_pref(), true, "pref defaults to true");
function snapshotsEqual(snap1, snap2)
{
return compareSnapshots(snap1, snap2, true)[0];
}
// Link coloring is asynchronous (and non-deterministic), so we wait until it
// changes.
var thread = Components.classes["@mozilla.org/thread-manager;1"].
getService(Components.interfaces.nsIThreadManager).
mainThread;
var link = document.getElementById("mylink");
var cs;
var start = Date.now();
do {
while (thread.hasPendingEvents())
thread.processNextEvent(false);
cs = getComputedStyle(link, "");
} while(cs.cssFloat != "right");
var end = Date.now();
is(cs.cssFloat, "right", ":visited selector applies given default preferences");
SimpleTest.waitForExplicitFinish();
window.addEventListener("load", step1, false);
set_pref(false);
var iframe, subdoc, subwin;
var link;
var start;
var timeout;
// we don't handle dynamic changes of this pref; it only takes effect
// when a new page loads
reinsert_node(link);
var unvisref; // reference image for unvisited style
// Wait a while to make sure we don't update the style on our reinserted node.
setTimeout(function() {
is(cs.cssFloat, "left", ":visited selector does not apply given false preference");
function step1()
{
is(get_pref(), true, "pref defaults to true");
iframe = document.getElementById("iframe");
subdoc = iframe.contentDocument;
subwin = iframe.contentWindow;
link = subdoc.getElementById("link");
unvisref = snapshotWindow(subwin, false);
// Now set the href of the link to a location that's actually visited.
link.href = window.location;
start = Date.now();
// And wait for the link to get restyled when the history lets us
// know it is (asynchronously).
setTimeout(poll_for_visited_style, 100);
}
function poll_for_visited_style()
{
var snapshot = snapshotWindow(subwin, false);
if (snapshotsEqual(unvisref, snapshot)) {
// hasn't been styled yet
setTimeout(poll_for_visited_style, 100);
// If it never gets styled correctly, this test will fail because
// this loop will never complete.
} else {
var end = Date.now();
timeout = 3 * Math.max(end - start, 300);
step2();
}
}
function step2()
{
set_pref(false);
// we don't handle dynamic changes of this pref; it only takes effect
// when a new page loads
reinsert_node(link);
setTimeout(step3, timeout);
}
function step3()
{
var snapshot = snapshotWindow(subwin, false);
ok(snapshotsEqual(unvisref, snapshot),
":visited selector does not apply given false preference");
// Set the pref back for the rest of the tests.
set_pref(true);
SimpleTest.finish();
}, 10 * Math.max(end - start, 100));
SimpleTest.waitForExplicitFinish();
}
</script>
</pre>

View file

@ -0,0 +1,7 @@
<!DOCTYPE HTML>
<title>iframe for test_visited_pref.html</title>
<style>
:link { color: blue }
:visited { color: purple }
</style>
<a href="http://www.example.com/url-that-has-not-been-visited" id="link">link</a>

View file

@ -112,6 +112,13 @@ function loadNextTest() {
iframe.src = prefix + subtests[testNum-1];
}
function getColor(doc, win, id) {
var elem = doc.getElementById(id);
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
var utils = win.QueryInterface(Components.interfaces.nsIInterfaceRequestor).
getInterface(Components.interfaces.nsIDOMWindowUtils);
return utils.getVisitedDependentComputedStyle(elem, "", "color");
}
function checkTest() {
switch (testNum) {
@ -123,24 +130,21 @@ function checkTest() {
// run outside of private mode, link should appear as visited
var doc = iframe.contentDocument;
var win = doc.defaultView;
var style = win.getComputedStyle(doc.getElementById("link"), "");
is(style.getPropertyValue("color"), kRed, "Visited link coloring should work outside of private mode");
is(getColor(doc, win, "link"), kRed, "Visited link coloring should work outside of private mode");
break;
case 3:
// run inside of private mode, link should appear as not visited
var doc = iframe.contentDocument;
var win = doc.defaultView;
var style = win.getComputedStyle(doc.getElementById("link"), "");
is(style.getPropertyValue("color"), kBlue, "Visited link coloring should not work inside of private mode");
is(getColor(doc, win, "link"), kBlue, "Visited link coloring should not work inside of private mode");
break;
case 4:
// run outside of private mode, link should appear as visited
var doc = iframe.contentDocument;
var win = doc.defaultView;
var style = win.getComputedStyle(doc.getElementById("link"), "");
is(style.getPropertyValue("color"), kRed, "Visited link coloring should work outside of private mode");
is(getColor(doc, win, "link"), kRed, "Visited link coloring should work outside of private mode");
break;
default: