toolkit/components/places/tests/test_bug_461710_perwindowpb.html
author Dave Townsend <dtownsend@oxymoronical.com>
Wed, 03 Feb 2016 13:12:18 -0800
changeset 319500 345ff6c8b0a24a357c9f0b4d8d4f9c6e5c71eb16
parent 298138 7cce3d53cf661033119f929729cb11984f51d1b5
child 319503 cb217f7271c24838782ff4f772af29a7ba9f53e8
permissions -rw-r--r--
Bug 1245649: Merge browser and toolkit eslint rule settings. r=felipe

<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=461710
-->
<head>
  <title>Test for Bug 461710</title>
  <script type="text/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css" />
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=461710">Mozilla Bug 461710</a>
<p id="display"></p>
<pre id="test">
<script class="testbody" type="text/javascript">

/** Test for Bug 461710 **/

SimpleTest.waitForExplicitFinish();

const Ci = SpecialPowers.Ci;
const Cc = SpecialPowers.Cc;
const Cr = SpecialPowers.Cr;

SpecialPowers.Cu.import("resource://gre/modules/NetUtil.jsm", window);
var Services = SpecialPowers.Services;

var gIframe;

/**
 * Helper function which waits until another function returns true, and
 * then notifies a callback.
 *
 * Original function stolen from docshell/test/chrome/docshell_helpers.js.
 *
 * Parameters:
 *
 *    fn: a function which is evaluated repeatedly, and when it turns true,
 *        the onWaitComplete callback is notified.
 *
 *    onWaitComplete:  a callback which will be notified when fn() returns
 *        true.
 */
function waitForTrue(fn, onWaitComplete) {
  var start = new Date().valueOf();

  // Loop until the test function returns true, or until a timeout occurs,
  // if a timeout is defined.
  var intervalid =
    setInterval(
      function() {
        if (fn.call()) {
          // Stop calling the test function and notify the callback.
          clearInterval(intervalid);
          onWaitComplete.call();
        }
      }, 20);
}

const kRed = "rgb(255, 0, 0)";
const kBlue = "rgb(0, 0, 255)";

var testpath = "/tests/toolkit/components/places/tests/mochitest/bug_461710/";
var prefix = "http://mochi.test:8888" + testpath;
var subtests = [
                   "visited_page.html",   // 1
                   "link_page.html",      // 2
                   "link_page-2.html",    // 3
                   "link_page-3.html"     // 4
               ];

var testNum = 0;
function loadNextTest() {
  // run the initialization code for each test
  switch (++testNum) {
    case 1:
      gIframe = normalWindowIframe;
      break;

    case 2:
      break;

    case 3:
      gIframe = privateWindowIframe;
      break;

    case 4:
      gIframe = normalWindowIframe;
      break;

    default:
      ok(false, "Unexpected call to loadNextTest for test #" + testNum);
  }

  if (testNum == 1)
    observer.expectURL(prefix + subtests[0], "uri-visit-saved");
  else
    observer.expectURL(prefix + subtests[0]);

  waitForTrue(() => observer.resolved, function() {
    // And the nodes get notified after the "link-visited" topic, so
    // we need to execute soon...
    SimpleTest.executeSoon(handleLoad);
  });

  gIframe.src = prefix + subtests[testNum-1];
}

function getColor(doc, win, id) {
  var elem = doc.getElementById(id);
  var utils = SpecialPowers.getDOMWindowUtils(win);
  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 = gIframe.contentDocument;
      var win = doc.defaultView;
      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
      doc = gIframe.contentDocument;
      win = doc.defaultView;
      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
      doc = gIframe.contentDocument;
      win = doc.defaultView;
      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);
  }
}

function handleLoad() {
  checkTest();

  if (testNum < subtests.length) {
    loadNextTest();
  } else {
    normalWindow.close();
    privateWindow.close();

    SimpleTest.finish();
  }
}

var contentPage = "http://mochi.test:8888/tests/toolkit/components/places/tests/mochitest/bug_461710/iframe.html";

function whenDelayedStartupFinished(aWindow, aCallback) {
  Services.obs.addObserver(function observer(aSubject, aTopic) {
    if (aWindow == aSubject) {
      Services.obs.removeObserver(observer, aTopic);
    }

    if (aWindow.content == null || aWindow.content.location.href != contentPage) {
      aWindow.addEventListener("DOMContentLoaded", function onInnerLoad() {
        aWindow.removeEventListener("DOMContentLoaded", onInnerLoad, true);
        SimpleTest.executeSoon(function() { aCallback(aWindow); });
      }, true);

      aWindow.gBrowser.loadURI(contentPage);
    }
  }, "browser-delayed-startup-finished", false);
}

function testOnWindow(aIsPrivate, callback) {
  var mainWindow = window.QueryInterface(Ci.nsIInterfaceRequestor)
                         .getInterface(Ci.nsIWebNavigation)
                         .QueryInterface(Ci.nsIDocShellTreeItem)
                         .rootTreeItem
                         .QueryInterface(Ci.nsIInterfaceRequestor)
                         .getInterface(Ci.nsIDOMWindow);
  var win = mainWindow.OpenBrowserWindow({private: aIsPrivate});
  whenDelayedStartupFinished(win, function() { callback(win); });
};

const URI_VISITED_RESOLUTION_TOPIC = "visited-status-resolution";
var observer = {
  uri: null,
  resolved: true,
  observe: function (aSubject, aTopic, aData) {

    if (this.uri.equals(SpecialPowers.wrap(aSubject).QueryInterface(Ci.nsIURI))) {
      this.resolved = true;

      Services.obs.removeObserver(this, aTopic);
    }
  },
  expectURL: function (url, aOverrideTopic) {
    ok(this.resolved, "Can't set the expected URL when another is yet to be resolved");
    this.resolved = false;

    this.uri = SpecialPowers.wrap(NetUtil).newURI(url);
    var topic = aOverrideTopic || URI_VISITED_RESOLUTION_TOPIC;
    Services.obs.addObserver(this, topic, false);
  }
};

var normalWindow;
var privateWindow;

var normalWindowIframe;
var privateWindowIframe;

testOnWindow(false, function(aWin) {
  var selectedBrowser = aWin.gBrowser.selectedBrowser;

   normalWindow = aWin;
   normalWindowIframe = selectedBrowser.contentDocument.getElementById("iframe");

  testOnWindow(true, function(aPrivateWin) {
    selectedBrowser = aPrivateWin.gBrowser.selectedBrowser;

    privateWindow = aPrivateWin;
    privateWindowIframe = selectedBrowser.contentDocument.getElementById("iframe");

    loadNextTest();
  });
});

</script>
</pre>
</body>
</html>