Fix existing tests to deal with getComputedStyle lying about :link vs. :visited. (Bug 147777) r=sdwilsh
authorL. David Baron <dbaron@dbaron.org>
Fri, 02 Apr 2010 18:58:25 -0700
changeset 40175 bb3c411b50d502c065e839fcf69101067d8f15d2
parent 40174 b158ba7e6d6550b460c7ca638bd68f8c4701c743
child 40176 4c89e5f153310037f11eded58fec5de494ed994c
push id12591
push userdbaron@mozilla.com
push dateSat, 03 Apr 2010 02:02:09 +0000
treeherdermozilla-central@85754ddc898e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssdwilsh
bugs147777
milestone1.9.3a4pre
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
Fix existing tests to deal with getComputedStyle lying about :link vs. :visited. (Bug 147777) r=sdwilsh
content/html/content/test/test_bug209275.xhtml
content/html/content/test/test_bug481335.xhtml
docshell/test/chrome/bug293235.html
docshell/test/chrome/bug293235_window.xul
dom/tests/mochitest/whatwg/test_bug500328.html
layout/style/test/Makefile.in
layout/style/test/test_visited_pref.html
layout/style/test/visited-pref-iframe.html
toolkit/components/places/tests/mochitest/test_bug_461710.html
--- a/content/html/content/test/test_bug209275.xhtml
+++ b/content/html/content/test/test_bug209275.xhtml
@@ -72,24 +72,29 @@ SimpleTest.waitForExplicitFinish();
 function link123HrefIs(href, testNum) {
   is($('link1').href, href, "link1 test " + testNum);
   is($('link2').href, href, "link2 test " + testNum);
   is($('link3').href, href, "link3 test " + 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) {
   elem.setAttributeNS("http://www.w3.org/1999/xlink", "href", href);
 }
 
 function continueTest() {
   gGen.next();
--- a/content/html/content/test/test_bug481335.xhtml
+++ b/content/html/content/test/test_bug481335.xhtml
@@ -46,22 +46,28 @@ const visitedColor = "rgb(255, 0, 0)";
 let tests = testIterator();
 function continueTest() {
   tests.next();
 }
 
 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;
 }
 
 function testIterator() {
   // After first load
   $("newparent").appendChild($("t"));
   is($("t").href, "http://www.example.com/" + rand,
      "Unexpected href after move");
--- a/docshell/test/chrome/bug293235.html
+++ b/docshell/test/chrome/bug293235.html
@@ -1,13 +1,13 @@
 <html>
   <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>
   <body>
     <a id="link1" href="bug293235_p2.html">This is a test link.</a>
   </body>
 </html>
--- a/docshell/test/chrome/bug293235_window.xul
+++ b/docshell/test/chrome/bug293235_window.xul
@@ -7,16 +7,17 @@
         height="600"
         onload="setTimeout(nextTest,0);"
         title="bug 293235 test">
 
   <script type="application/javascript"
     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;
     const Cc = Components.classes;
     Components.utils.import("resource://gre/modules/NetUtil.jsm");
 
     // Define the generator-iterator for the tests.
     var tests = testIterator();
@@ -69,19 +70,32 @@
       });
       yield;
 
       // Before we go any further, make sure our link has been notified.
       waitForTrue(notified, nextTest);
       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({
         uri: getHttpUrl("bug293235_p2.html"),
         onNavComplete: nextTest
       });
       yield;
 
@@ -104,19 +118,18 @@
         expectedEvents: [ { type: "pageshow",
                             persisted: true,
                             title: "Bug 293235 page1" } ],
         onNavComplete: nextTest
       });
       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.
       finish();
       yield;
     }
 
   ]]></script>
--- a/dom/tests/mochitest/whatwg/test_bug500328.html
+++ b/dom/tests/mochitest/whatwg/test_bug500328.html
@@ -102,17 +102,21 @@ function noPopStateExpected(msg) {
 }
 
 function popstateExpected(msg) {
   is(gNumPopStates, 1, msg);
   gNumPopStates = 0;
 }
 
 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)
 {
   netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
 
   const Ci = Components.interfaces;
   var sh = theWindow.QueryInterface(Ci.nsIInterfaceRequestor)
--- a/layout/style/test/Makefile.in
+++ b/layout/style/test/Makefile.in
@@ -195,16 +195,17 @@ GARBAGE += css_properties.js
 		media_queries_dynamic_xbl_iframe.html \
 		media_queries_dynamic_xbl_style.css \
 		bug453896_iframe.html \
 		bug517224.sjs \
 		test_bug525952.html \
 		ccd-quirks.html \
 		ccd-standards.html \
 		ccd.sjs \
+		visited-pref-iframe.html \
 		$(NULL)
 
 _BROWSER_FILES = \
 		browser_bug453896.js \
 		bug453896_iframe.html \
 		media_queries_iframe.html \
 		$(NULL)
 
--- a/layout/style/test/test_visited_pref.html
+++ b/layout/style/test/test_visited_pref.html
@@ -2,35 +2,34 @@
 <html>
 <!--
 https://bugzilla.mozilla.org/show_bug.cgi?id=147777
 -->
 <head>
   <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">
 
   :link { float: left; }
 
   :visited { float: right; }
 
   </style>
 </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;
   par.removeChild(e);
   par.insertBefore(e, sib);
 }
 
 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
@@ -45,48 +44,87 @@ function get_pref()
 }
 
 function set_pref(val)
 {
     netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
     dispBranch.setBoolPref("visited_links_enabled", val);
 }
 
-is(get_pref(), true, "pref defaults to true");
+function snapshotsEqual(snap1, snap2)
+{
+  return compareSnapshots(snap1, snap2, true)[0];
+}
+
+SimpleTest.waitForExplicitFinish();
+window.addEventListener("load", step1, false);
+
+var iframe, subdoc, subwin;
+var link;
+var start;
+var timeout;
+
+var unvisref; // reference image for unvisited style
+
+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();
 
-// 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");
+  // 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);
 
-set_pref(false);
+    // 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);
+  // we don't handle dynamic changes of this pref; it only takes effect
+  // when a new page loads
+  reinsert_node(link);
 
-// 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");
+  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>
 </body>
 </html>
new file mode 100644
--- /dev/null
+++ b/layout/style/test/visited-pref-iframe.html
@@ -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>
--- a/toolkit/components/places/tests/mochitest/test_bug_461710.html
+++ b/toolkit/components/places/tests/mochitest/test_bug_461710.html
@@ -107,45 +107,49 @@ function loadNextTest() {
       // And the nodes get notified after the "link-visited" topic, so
       // we need to execute soon...
       SimpleTest.executeSoon(handleLoad);
     });
   }
   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) {
     case 1:
       // nothing to do here, we just want to mark the page as visited
       break;
 
     case 2:
       // 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:
       ok(false, "Unexpected call to checkTest for test #" + testNum);
   }
 }