toolkit/components/places/tests/test_bug_461710_perwindowpb.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/places/tests/test_bug_461710_perwindowpb.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,232 @@
     1.4 +<!DOCTYPE HTML>
     1.5 +<html>
     1.6 +<!--
     1.7 +https://bugzilla.mozilla.org/show_bug.cgi?id=461710
     1.8 +-->
     1.9 +<head>
    1.10 +  <title>Test for Bug 461710</title>
    1.11 +  <script type="text/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
    1.12 +  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css" />
    1.13 +</head>
    1.14 +<body>
    1.15 +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=461710">Mozilla Bug 461710</a>
    1.16 +<p id="display"></p>
    1.17 +<pre id="test">
    1.18 +<script class="testbody" type="text/javascript">
    1.19 +
    1.20 +/** Test for Bug 461710 **/
    1.21 +
    1.22 +SimpleTest.waitForExplicitFinish();
    1.23 +
    1.24 +const Ci = SpecialPowers.Ci;
    1.25 +const Cc = SpecialPowers.Cc;
    1.26 +const Cr = SpecialPowers.Cr;
    1.27 +
    1.28 +SpecialPowers.Cu.import("resource://gre/modules/NetUtil.jsm", window);
    1.29 +var Services = SpecialPowers.Services;
    1.30 +
    1.31 +var gIframe;
    1.32 +
    1.33 +/**
    1.34 + * Helper function which waits until another function returns true, and
    1.35 + * then notifies a callback.
    1.36 + *
    1.37 + * Original function stolen from docshell/test/chrome/docshell_helpers.js.
    1.38 + *
    1.39 + * Parameters:
    1.40 + *
    1.41 + *    fn: a function which is evaluated repeatedly, and when it turns true,
    1.42 + *        the onWaitComplete callback is notified.
    1.43 + *
    1.44 + *    onWaitComplete:  a callback which will be notified when fn() returns
    1.45 + *        true.
    1.46 + */
    1.47 +function waitForTrue(fn, onWaitComplete) {
    1.48 +  var start = new Date().valueOf();
    1.49 +
    1.50 +  // Loop until the test function returns true, or until a timeout occurs,
    1.51 +  // if a timeout is defined.
    1.52 +  var intervalid =
    1.53 +    setInterval(
    1.54 +      function() {
    1.55 +        if (fn.call()) {
    1.56 +          // Stop calling the test function and notify the callback.
    1.57 +          clearInterval(intervalid);
    1.58 +          onWaitComplete.call();
    1.59 +        }
    1.60 +      }, 20);
    1.61 +}
    1.62 +
    1.63 +const kRed = "rgb(255, 0, 0)";
    1.64 +const kBlue = "rgb(0, 0, 255)";
    1.65 +
    1.66 +var testpath = "/tests/toolkit/components/places/tests/mochitest/bug_461710/";
    1.67 +var prefix = "http://mochi.test:8888" + testpath;
    1.68 +var subtests = [
    1.69 +                   "visited_page.html",   // 1
    1.70 +                   "link_page.html",      // 2
    1.71 +                   "link_page-2.html",    // 3
    1.72 +                   "link_page-3.html"     // 4
    1.73 +               ];
    1.74 +
    1.75 +var testNum = 0;
    1.76 +function loadNextTest() {
    1.77 +  // run the initialization code for each test
    1.78 +  switch (++testNum) {
    1.79 +    case 1:
    1.80 +      gIframe = normalWindowIframe;
    1.81 +      break;
    1.82 +
    1.83 +    case 2:
    1.84 +      break;
    1.85 +
    1.86 +    case 3:
    1.87 +      gIframe = privateWindowIframe;
    1.88 +      break;
    1.89 +
    1.90 +    case 4:
    1.91 +      gIframe = normalWindowIframe;
    1.92 +      break;
    1.93 +
    1.94 +    default:
    1.95 +      ok(false, "Unexpected call to loadNextTest for test #" + testNum);
    1.96 +  }
    1.97 +
    1.98 +  if (testNum == 1)
    1.99 +    observer.expectURL(prefix + subtests[0], "uri-visit-saved");
   1.100 +  else
   1.101 +    observer.expectURL(prefix + subtests[0]);
   1.102 +
   1.103 +  waitForTrue(function() observer.resolved, function() {
   1.104 +    // And the nodes get notified after the "link-visited" topic, so
   1.105 +    // we need to execute soon...
   1.106 +    SimpleTest.executeSoon(handleLoad);
   1.107 +  });
   1.108 +
   1.109 +  gIframe.src = prefix + subtests[testNum-1];
   1.110 +}
   1.111 +
   1.112 +function getColor(doc, win, id) {
   1.113 +  var elem = doc.getElementById(id);
   1.114 +  var utils = SpecialPowers.getDOMWindowUtils(win);
   1.115 +  return utils.getVisitedDependentComputedStyle(elem, "", "color");
   1.116 +}
   1.117 +
   1.118 +function checkTest() {
   1.119 +  switch (testNum) {
   1.120 +    case 1:
   1.121 +      // nothing to do here, we just want to mark the page as visited
   1.122 +      break;
   1.123 +
   1.124 +    case 2:
   1.125 +      // run outside of private mode, link should appear as visited
   1.126 +      var doc = gIframe.contentDocument;
   1.127 +      var win = doc.defaultView;
   1.128 +      is(getColor(doc, win, "link"), kRed, "Visited link coloring should work outside of private mode");
   1.129 +      break;
   1.130 +
   1.131 +    case 3:
   1.132 +      // run inside of private mode, link should appear as not visited
   1.133 +      var doc = gIframe.contentDocument;
   1.134 +      var win = doc.defaultView;
   1.135 +      is(getColor(doc, win, "link"), kBlue, "Visited link coloring should not work inside of private mode");
   1.136 +      break;
   1.137 +
   1.138 +    case 4:
   1.139 +      // run outside of private mode, link should appear as visited
   1.140 +      var doc = gIframe.contentDocument;
   1.141 +      var win = doc.defaultView;
   1.142 +      is(getColor(doc, win, "link"), kRed, "Visited link coloring should work outside of private mode");
   1.143 +      break;
   1.144 +
   1.145 +    default:
   1.146 +      ok(false, "Unexpected call to checkTest for test #" + testNum);
   1.147 +  }
   1.148 +}
   1.149 +
   1.150 +function handleLoad() {
   1.151 +  checkTest();
   1.152 +
   1.153 +  if (testNum < subtests.length) {
   1.154 +    loadNextTest();
   1.155 +  } else {
   1.156 +    normalWindow.close();
   1.157 +    privateWindow.close();
   1.158 +
   1.159 +    SimpleTest.finish();
   1.160 +  }
   1.161 +}
   1.162 +
   1.163 +var mainWindow = window.QueryInterface(Ci.nsIInterfaceRequestor)
   1.164 +                    .getInterface(Ci.nsIWebNavigation)
   1.165 +                    .QueryInterface(Ci.nsIDocShellTreeItem)
   1.166 +                    .rootTreeItem
   1.167 +                    .QueryInterface(Ci.nsIInterfaceRequestor)
   1.168 +                    .getInterface(Ci.nsIDOMWindow);
   1.169 +var contentPage = "http://mochi.test:8888/tests/toolkit/components/places/tests/mochitest/bug_461710/iframe.html";
   1.170 +
   1.171 +function testOnWindow(aIsPrivate, aCallback) {
   1.172 +  var win = mainWindow.OpenBrowserWindow({private: aIsPrivate});
   1.173 +  win.addEventListener("load", function onLoad() {
   1.174 +    win.removeEventListener("load", onLoad, false);
   1.175 +    win.addEventListener("DOMContentLoaded", function onInnerLoad() {
   1.176 +      if (win.content.location.href != contentPage) {
   1.177 +        win.gBrowser.loadURI(contentPage);
   1.178 +        return;
   1.179 +      }
   1.180 +      win.removeEventListener("DOMContentLoaded", onInnerLoad, true);
   1.181 +      win.gBrowser.selectedBrowser.focus();
   1.182 +      SimpleTest.executeSoon(function() { aCallback(win); });
   1.183 +    }, true);
   1.184 +    SimpleTest.executeSoon(function() { win.gBrowser.loadURI(contentPage); });
   1.185 +  }, true);
   1.186 +}
   1.187 +
   1.188 +const URI_VISITED_RESOLUTION_TOPIC = "visited-status-resolution";
   1.189 +var observer = {
   1.190 +  uri: null,
   1.191 +  resolved: true,
   1.192 +  observe: function (aSubject, aTopic, aData) {
   1.193 +
   1.194 +    if (this.uri.equals(SpecialPowers.wrap(aSubject).QueryInterface(Ci.nsIURI))) {
   1.195 +      this.resolved = true;
   1.196 +
   1.197 +      Services.obs.removeObserver(this, aTopic);
   1.198 +    }
   1.199 +  },
   1.200 +  expectURL: function (url, aOverrideTopic) {
   1.201 +    ok(this.resolved, "Can't set the expected URL when another is yet to be resolved");
   1.202 +    this.resolved = false;
   1.203 +
   1.204 +    this.uri = SpecialPowers.wrap(NetUtil).newURI(url);
   1.205 +    var topic = aOverrideTopic || URI_VISITED_RESOLUTION_TOPIC;
   1.206 +    Services.obs.addObserver(this, topic, false);
   1.207 +  }
   1.208 +};
   1.209 +
   1.210 +var normalWindow;
   1.211 +var privateWindow;
   1.212 +
   1.213 +var normalWindowIframe;
   1.214 +var privateWindowIframe;
   1.215 +
   1.216 +testOnWindow(false, function(aWin) {
   1.217 +  var selectedBrowser = aWin.gBrowser.selectedBrowser;
   1.218 +
   1.219 +   normalWindow = aWin;
   1.220 +   normalWindowIframe = selectedBrowser.contentDocument.getElementById("iframe");
   1.221 +
   1.222 +  testOnWindow(true, function(aPrivateWin) {
   1.223 +    selectedBrowser = aPrivateWin.gBrowser.selectedBrowser;
   1.224 +
   1.225 +    privateWindow = aPrivateWin;
   1.226 +    privateWindowIframe = selectedBrowser.contentDocument.getElementById("iframe");
   1.227 +
   1.228 +    loadNextTest();
   1.229 +  });
   1.230 +});
   1.231 +
   1.232 +</script>
   1.233 +</pre>
   1.234 +</body>
   1.235 +</html>

mercurial