Bug 461199 (Part 26) - Update content tests to work with the new async isVisited API
☠☠ backed out by ef33df18ff0e ☠ ☠
authorShawn Wilsher <sdwilsh@shawnwilsher.com>
Wed, 17 Feb 2010 14:04:33 -0800
changeset 38191 2bd2f67e8f5e2c0f01f0d5537673c95a348afcad
parent 38190 1c37899b532ee7cf699ac3dd26921485f1abc515
child 38192 b0c9f8e500573ce8cae005c08216a42ead29ced9
child 38225 ef33df18ff0e3e18f44f893ddea9729cdb495e2f
push id1
push userroot
push dateTue, 26 Apr 2011 22:38:44 +0000
treeherdermozilla-beta@bfdb6e623a36 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
bugs461199
milestone1.9.3a2pre
Bug 461199 (Part 26) - Update content tests to work with the new async isVisited API
content/html/content/test/test_bug209275.xhtml
content/html/content/test/test_bug481335.xhtml
--- a/content/html/content/test/test_bug209275.xhtml
+++ b/content/html/content/test/test_bug209275.xhtml
@@ -51,17 +51,17 @@ https://bugzilla.mozilla.org/show_bug.cg
   <a href="#" id="link4">link4</a>
   <a href=""  id="colorlink">colorlink</a>
   <a href="#" id="link5">link5</a>
   <iframe id="iframe"></iframe>
 
   <svg width="5cm" height="3cm" viewBox="0 0 5 3" version="1.1"
      xmlns="http://www.w3.org/2000/svg"
      xmlns:xlink="http://www.w3.org/1999/xlink">
-  <a xlink:href="http://www.w3.org" id="ellipselink">
+  <a xlink:href="" id="ellipselink">
     <ellipse cx="2.5" cy="1.5" rx="2" ry="1" id="ellipse" />
   </a>
   </svg>
 
 </div>
 <pre id="test">
 <script type="text/javascript;version=1.7">
 <![CDATA[
@@ -86,61 +86,49 @@ function getFill(elem) {
           .getComputedStyle(elem, "")
           .getPropertyValue("fill");
 }
 
 function setXlinkHref(elem, href) {
   elem.setAttributeNS("http://www.w3.org/1999/xlink", "href", href);
 }
 
+function continueTest() {
+  gGen.next();
+}
+
 var iframe = document.getElementById("iframe");
 var iframeCw = iframe.contentWindow;
 
 function run() {
   var iframe = document.getElementById("iframe");
   var iframeCw = iframe.contentWindow;
 
-  // First, get the visited/unvisited link/ellipse colors.
-  var unvisitedColor;
-  var visitedColor;
-  var unvisitedFill;
-  var visitedFill;
+  // First, set the visited/unvisited link/ellipse colors.
+  const unvisitedColor = "rgb(0, 0, 238)";
+  const visitedColor = "rgb(85, 26, 139)";
+  const unvisitedFill = "rgb(0, 0, 255)";
+  const visitedFill = "rgb(128, 0, 128)";
 
-  var rand = Date.now() + "-" + Math.random();
-  // Set colorlink and ellipselink's hrefs
-  $("colorlink").href = rand;
-  setXlinkHref($("ellipselink"), rand);
-  unvisitedColor = getColor($("colorlink"));
-  unvisitedFill = getFill($("ellipse"));
-
-  // Set the link's href to our current location so we can get the visited link
-  // color.
-  $("colorlink").href = document.location;
-  setXlinkHref($("ellipselink"), document.location);
-  visitedColor = getColor($("colorlink"));
-  visitedFill = getFill($("ellipse"));
-  isnot(visitedColor, unvisitedColor,
-        "visited/unvisited link colors are the same?");
-  isnot(visitedFill, unvisitedFill,
-        "visited/unvisited ellipse fill colors are the same?");
+  const rand = Date.now() + "-" + Math.random();
 
   // Now we can start the tests in earnest.
 
   var loc = location;
   // everything from the location up to and including the final forward slash
   var path = /(.*\/)[^\/]*/.exec(location)[1];
 
   // Set colorlink's href so we can check that it changes colors after we
   // change the base href.
   $('colorlink').href = "http://example.com/" + rand;
-  setXlinkHref($("ellipse"), "http://example.com/" + rand);
+  setXlinkHref($("ellipselink"), "http://example.com/" + rand);
 
   // Load http://example.com/${rand} into an iframe so we can test that changing
   // the document's base changes the visitedness of our links.
-  iframe.onload = function() { gGen.next(); };
+  iframe.onload = continueTest;
   iframeCw.location = "http://example.com/" + rand;
   yield; // wait for onload to fire.
 
   // Make sure things are what as we expect them at the beginning.
   link123HrefIs("http://localhost:8888/", 1);
   is($('link4').href, loc + "#", "link 4 test 1");
   is($('link5').href, loc + "#", "link 5 test 1");
 
@@ -152,20 +140,30 @@ function run() {
   $('base1').href = "http://example.com";
 
   // Were the links' hrefs updated after the base change?
   link123HrefIs("http://example.com/", 2);
   is($('link4').href, "http://example.com/#", "link 4 test 2");
   is(link5.href, "http://example.com/#", "link 5 test 2");
 
   // Were colorlink's color and ellipse's fill updated appropriately?
+  // Because link coloring is asynchronous, we wait until it is updated (or we
+  // timeout and fail anyway).
+  while (getColor($('colorlink')) != visitedColor) {
+    setTimeout(continueTest, 10);
+    yield;
+  }
   is(getColor($('colorlink')), visitedColor,
      "Wrong link color after base change.");
-  is(getFill($('ellipse')), visitedFill,
-     "Wrong ellipse fill after base change.");
+  while (getFill($('ellipselink')) != visitedFill) {
+    setTimeout(continueTest, 10);
+    yield;
+  }
+  is(getFill($('ellipselink')), visitedFill,
+          "Wrong ellipse fill after base change.");
 
   $('base1').href = "foo/";
   // Should be interpreted relative to current URI (not the current base), so
   // base should now be http://localhost:8888/foo/
 
   link123HrefIs("http://localhost:8888/", 3);
   is($('link4').href, path + "foo/#", "link 4 test 3");
 
--- a/content/html/content/test/test_bug481335.xhtml
+++ b/content/html/content/test/test_bug481335.xhtml
@@ -2,87 +2,117 @@
 <!--
 https://bugzilla.mozilla.org/show_bug.cgi?id=481335
 -->
 <head>
   <title>Test for Bug 481335</title>
   <script type="application/javascript" src="/MochiKit/packed.js"></script>
   <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
+  <style type="text/css">
+    a { color:blue; }
+    a:visited { color:red; }
+  </style>
 </head>
 <body>
 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=481335">Mozilla Bug 481335</a>
 <p id="display">
   <a id="t">A link</a>
   <iframe id="i"></iframe>
 </p>
 <p id="newparent" xml:base="http://www.example.com/"></p>
 <div id="content" style="display: none"></div>
 <pre id="test">
-<script type="application/javascript">
+<script type="application/javascript;version=1.7">
 <![CDATA[
 
 /** Test for Bug 481335 **/
 SimpleTest.waitForExplicitFinish();
 var rand = Date.now() + "-" + Math.random();
 
 is($("t").href, "",
    "Unexpected href before set");
 is($("t").href, "",
    "Unexpected cached href before set");
 
 $("t").setAttribute("href", rand);
 is($("t").href,
-   window.location.href.replace(/test_bug481335.xhtml/, rand),
+   window.location.href.replace(/test_bug481335.xhtml([\?].*)?/, rand),
    "Unexpected href after set");
 is($("t").href,
-   window.location.href.replace(/test_bug481335.xhtml/, rand),
+   window.location.href.replace(/test_bug481335.xhtml([\?].*)?/, rand),
    "Unexpected cached href after set");
-var unvisitedColor = document.defaultView.getComputedStyle($("t"), "").color;
-var visitedColor;
+const unvisitedColor = "rgb(0, 0, 255)";
+const visitedColor = "rgb(255, 0, 0)";
 
-function afterThirdLoad() {
-  is(document.defaultView.getComputedStyle($("t"), "").color, visitedColor,
-     "Should be visited now after third load");
-  SimpleTest.finish();
+let tests = testIterator();
+function continueTest() {
+  tests.next();
 }
 
-function afterSecondLoad() {
-  is(document.defaultView.getComputedStyle($("t"), "").color, visitedColor,
-       "Should be visited now");
-  $("t").pathname = rand;
-  is(document.defaultView.getComputedStyle($("t"), "").color, visitedColor,
-       "Should still be visited after setting pathname to its existing value");
-  $("t").pathname += "x";
-  isnot(document.defaultView.getComputedStyle($("t"), "").color, visitedColor,
-       "Should not be visited after changing pathname");
-  $("t").pathname = $("t").pathname;
-  isnot(document.defaultView.getComputedStyle($("t"), "").color, visitedColor,
-       "Should not be visited after setting unvisited pathname to existing value");
-
-  $("i").onload = afterThirdLoad;
-  $("i").src = $("t").href;
+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) {
+    setTimeout(continueTest, 10);
+    return false;
+  }
+  is(getCS($(aElmId), "").color, aExpectedColor, aMessage);
+  return true;
 }
 
-function afterFirstLoad() {
-  visitedColor = document.defaultView.getComputedStyle($("t"), "").color;
-  isnot(visitedColor, unvisitedColor, "Why are the colors the same?");
+function testIterator() {
+  // After first load
   $("newparent").appendChild($("t"));
   is($("t").href, "http://www.example.com/" + rand,
      "Unexpected href after move");
   is($("t").href, "http://www.example.com/" + rand,
      "Unexpected cached href after move");
-  is(document.defaultView.getComputedStyle($("t"), "").color, unvisitedColor,
-     "Should be unvisited now");
+  while (!checkLinkColor("t", unvisitedColor, "Should be unvisited now"))
+    yield;
+
+  $("i").src = $("t").href;
+  yield;
 
-  $("i").onload = afterSecondLoad;
+  // After second load
+  while (!checkLinkColor("t", visitedColor, "Should be visited now"))
+    yield;
+  $("t").pathname = rand;
+  while (!checkLinkColor("t", visitedColor,
+            "Should still be visited after setting pathname to its existing value")) {
+    yield;
+  }
+  /* TODO uncomment this test with the landing of bug 534526.  See
+   * https://bugzilla.mozilla.org/show_bug.cgi?id=461199#c167
+  $("t").pathname += "x";
+  while (!checkLinkColor("t", unvisitedColor,
+            "Should not be visited after changing pathname")) {
+    yield;
+  }
+  $("t").pathname = $("t").pathname;
+  while (!checkLinkColor("t", unvisitedColor,
+            "Should not be visited after setting unvisited pathname to existing value")) {
+    yield;
+  }
+  */
+
   $("i").src = $("t").href;
+  yield;
+
+  // After third load
+  while (!checkLinkColor("t", visitedColor,
+                         "Should be visited now after third load")) {
+    yield;
+  }
+  SimpleTest.finish();
+  yield;
 }
 
 addLoadEvent(function() {
-  $("i").onload = afterFirstLoad;
+  $("i").onload = continueTest;
   $("i").src = $("t").href;
 });
 ]]>
 </script>
 </pre>
 </body>
 </html>