<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin" type="text/css"?>

<window id="293235Test"
        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
        width="600"
        height="600"
        onload="setTimeout(runTests, 0);"
        title="bug 293235 test">

  <script type="application/javascript" src= "chrome://mochikit/content/chrome-harness.js" />
  <script type="application/javascript" src="docshell_helpers.js" />
  <script src="chrome://mochikit/content/tests/SimpleTest/WindowSnapshot.js"></script>

  <script type="application/javascript"><![CDATA[
    var {NetUtil} = ChromeUtils.import("resource://gre/modules/NetUtil.jsm");

    // Return the Element object for the specified element id
    function $(id) { return TestWindow.getDocument().getElementById(id); }

    ////
    // Generator function for test steps for bug 293235:
    // A visited link should have the :visited style applied
    // to it when displayed on a page which was fetched from
    // the bfcache.
    //
    async function runTests() {
      // Register our observer to know when the link lookup is complete.
      let testURI = NetUtil.newURI(getHttpUrl("bug293235_p2.html"));
      let os = SpecialPowers.Services.obs;
      // Load a test page containing a link that should be initially
      // blue, per the :link style.
      await new Promise(resolve => {
        doPageNavigation({
          uri: getHttpUrl("bug293235.html"),
          onNavComplete: resolve,
        });
      });

      // Now that we've been notified, we can check our link color.
      // 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 = await snapshotWindow(TestWindow.getWindow());
      link1.className = "forcevisited";
      var refVisited = await 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, await snapshotWindow(TestWindow.getWindow())),
         "link should initially be blue");

      let observedVisit = false, observedPageShow = false;
      await new Promise(resolve => {
        function maybeResolve() {
          ok(true, "maybe run next test? visited: " + observedVisit + " pageShow: " + observedPageShow);
          if (observedVisit && observedPageShow)
            resolve();
        }

        // Because adding visits is async, we will not be notified immediately.
        let visitObserver = {
          observe(aSubject, aTopic, aData)
          {
            if (!testURI.equals(aSubject.QueryInterface(Ci.nsIURI))) {
              return;
            }
            os.removeObserver(this, aTopic);
            observedVisit = true;
            maybeResolve();
          },
        };
        os.addObserver(visitObserver, "uri-visit-saved");
        // Load the page that the link on the previous page points to.
        doPageNavigation({
          uri: getHttpUrl("bug293235_p2.html"),
          onNavComplete() {
            observedPageShow = true;
            maybeResolve();
          }
        });
      })

      // And the nodes get notified after the "uri-visit-saved" topic, so
      // we need to execute soon...
      await new Promise(SimpleTest.executeSoon);

      // Go back, verify the original page was loaded from the bfcache,
      // and verify that the link is now purple, per the
      // :visited style.
      await new Promise(resolve => {
        doPageNavigation({
          back: true,
          eventsToListenFor: ["pageshow"],
          expectedEvents: [ { type: "pageshow",
                              persisted: true,
                              title: "Bug 293235 page1" } ],
          onNavComplete: resolve,
        });
      })

      // Now we can test the link color.
      ok(snapshotsEqual(refVisited, await snapshotWindow(TestWindow.getWindow())),
         "visited link should be purple");

      // Tell the framework the test is finished.
      finish();
    }

  ]]></script>

  <browser type="content" primary="true" flex="1" id="content" src="about:blank"/>
</window>