browser/components/sessionstore/test/browser_frame_history.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/components/sessionstore/test/browser_frame_history.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,170 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +/**
     1.9 + Ensure that frameset history works properly when restoring a tab,
    1.10 + provided that the frameset is static.
    1.11 + */
    1.12 +
    1.13 +// Loading a toplevel frameset
    1.14 +add_task(function() {
    1.15 +  let testURL = getRootDirectory(gTestPath) + "browser_frame_history_index.html";
    1.16 +  let tab = gBrowser.addTab(testURL);
    1.17 +  gBrowser.selectedTab = tab;
    1.18 +
    1.19 +  info("Opening a page with three frames, 4 loads should take place");
    1.20 +  yield waitForLoadsInBrowser(tab.linkedBrowser, 4);
    1.21 +
    1.22 +  let browser_b = tab.linkedBrowser.contentDocument.getElementsByTagName("frame")[1];
    1.23 +  let document_b = browser_b.contentDocument;
    1.24 +  let links = document_b.getElementsByTagName("a");
    1.25 +
    1.26 +  // We're going to click on the first link, so listen for another load event
    1.27 +  info("Clicking on link 1, 1 load should take place");
    1.28 +  let promise = waitForLoadsInBrowser(tab.linkedBrowser, 1);
    1.29 +  EventUtils.sendMouseEvent({type:"click"}, links[0], browser_b.contentWindow);
    1.30 +  yield promise;
    1.31 +
    1.32 +  info("Clicking on link 2, 1 load should take place");
    1.33 +  promise = waitForLoadsInBrowser(tab.linkedBrowser, 1);
    1.34 +  EventUtils.sendMouseEvent({type:"click"}, links[1], browser_b.contentWindow);
    1.35 +  yield promise;
    1.36 +
    1.37 +  info("Close then un-close page, 4 loads should take place");
    1.38 +  gBrowser.removeTab(tab);
    1.39 +  let newTab = ss.undoCloseTab(window, 0);
    1.40 +  yield waitForLoadsInBrowser(newTab.linkedBrowser, 4);
    1.41 +
    1.42 +  info("Go back in time, 1 load should take place");
    1.43 +  gBrowser.goBack();
    1.44 +  yield waitForLoadsInBrowser(newTab.linkedBrowser, 1);
    1.45 +
    1.46 +  let expectedURLEnds = ["a.html", "b.html", "c1.html"];
    1.47 +  let frames = newTab.linkedBrowser.contentDocument.getElementsByTagName("frame");
    1.48 +  for (let i = 0; i < frames.length; i++) {
    1.49 +    is(frames[i].contentDocument.location,
    1.50 +       getRootDirectory(gTestPath) + "browser_frame_history_" + expectedURLEnds[i],
    1.51 +       "frame " + i + " has the right url");
    1.52 +  }
    1.53 +  gBrowser.removeTab(newTab);
    1.54 +});
    1.55 +
    1.56 +// Loading the frameset inside an iframe
    1.57 +add_task(function() {
    1.58 +  let testURL = getRootDirectory(gTestPath) + "browser_frame_history_index2.html";
    1.59 +  let tab = gBrowser.addTab(testURL);
    1.60 +  gBrowser.selectedTab = tab;
    1.61 +
    1.62 +  info("iframe: Opening a page with an iframe containing three frames, 5 loads should take place");
    1.63 +  yield waitForLoadsInBrowser(tab.linkedBrowser, 5);
    1.64 +
    1.65 +  let browser_b = tab.linkedBrowser.contentDocument.
    1.66 +    getElementById("iframe").contentDocument.
    1.67 +    getElementsByTagName("frame")[1];
    1.68 +  let document_b = browser_b.contentDocument;
    1.69 +  let links = document_b.getElementsByTagName("a");
    1.70 +
    1.71 +  // We're going to click on the first link, so listen for another load event
    1.72 +  info("iframe: Clicking on link 1, 1 load should take place");
    1.73 +  let promise = waitForLoadsInBrowser(tab.linkedBrowser, 1);
    1.74 +  EventUtils.sendMouseEvent({type:"click"}, links[0], browser_b.contentWindow);
    1.75 +  yield promise;
    1.76 +
    1.77 +  info("iframe: Clicking on link 2, 1 load should take place");
    1.78 +  promise = waitForLoadsInBrowser(tab.linkedBrowser, 1);
    1.79 +  EventUtils.sendMouseEvent({type:"click"}, links[1], browser_b.contentWindow);
    1.80 +  yield promise;
    1.81 +
    1.82 +  info("iframe: Close then un-close page, 5 loads should take place");
    1.83 +  gBrowser.removeTab(tab);
    1.84 +  let newTab = ss.undoCloseTab(window, 0);
    1.85 +  yield waitForLoadsInBrowser(newTab.linkedBrowser, 5);
    1.86 +
    1.87 +  info("iframe: Go back in time, 1 load should take place");
    1.88 +  gBrowser.goBack();
    1.89 +  yield waitForLoadsInBrowser(newTab.linkedBrowser, 1);
    1.90 +
    1.91 +  let expectedURLEnds = ["a.html", "b.html", "c1.html"];
    1.92 +  let frames = newTab.linkedBrowser.contentDocument.
    1.93 +    getElementById("iframe").contentDocument.
    1.94 +        getElementsByTagName("frame");
    1.95 +  for (let i = 0; i < frames.length; i++) {
    1.96 +    is(frames[i].contentDocument.location,
    1.97 +       getRootDirectory(gTestPath) + "browser_frame_history_" + expectedURLEnds[i],
    1.98 +       "frame " + i + " has the right url");
    1.99 +  }
   1.100 +  gBrowser.removeTab(newTab);
   1.101 +});
   1.102 +
   1.103 +// Now, test that we don't record history if the iframe is added dynamically
   1.104 +add_task(function() {
   1.105 +  // Start with an empty history
   1.106 +    let blankState = JSON.stringify({
   1.107 +      windows: [{
   1.108 +        tabs: [{ entries: [{ url: "about:blank" }] }],
   1.109 +        _closedTabs: []
   1.110 +      }],
   1.111 +      _closedWindows: []
   1.112 +    });
   1.113 +    ss.setBrowserState(blankState);
   1.114 +
   1.115 +  let testURL = getRootDirectory(gTestPath) + "browser_frame_history_index_blank.html";
   1.116 +  let tab = gBrowser.addTab(testURL);
   1.117 +  gBrowser.selectedTab = tab;
   1.118 +  yield waitForLoadsInBrowser(tab.linkedBrowser, 1);
   1.119 +
   1.120 +  info("dynamic: Opening a page with an iframe containing three frames, 4 dynamic loads should take place");
   1.121 +  let doc = tab.linkedBrowser.contentDocument;
   1.122 +  let iframe = doc.createElement("iframe");
   1.123 +  iframe.id = "iframe";
   1.124 +  iframe.src="browser_frame_history_index.html";
   1.125 +  doc.body.appendChild(iframe);
   1.126 +  yield waitForLoadsInBrowser(tab.linkedBrowser, 4);
   1.127 +
   1.128 +  let browser_b = tab.linkedBrowser.contentDocument.
   1.129 +    getElementById("iframe").contentDocument.
   1.130 +    getElementsByTagName("frame")[1];
   1.131 +  let document_b = browser_b.contentDocument;
   1.132 +  let links = document_b.getElementsByTagName("a");
   1.133 +
   1.134 +  // We're going to click on the first link, so listen for another load event
   1.135 +  info("dynamic: Clicking on link 1, 1 load should take place");
   1.136 +  let promise = waitForLoadsInBrowser(tab.linkedBrowser, 1);
   1.137 +  EventUtils.sendMouseEvent({type:"click"}, links[0], browser_b.contentWindow);
   1.138 +  yield promise;
   1.139 +
   1.140 +  info("dynamic: Clicking on link 2, 1 load should take place");
   1.141 +  promise = waitForLoadsInBrowser(tab.linkedBrowser, 1);
   1.142 +  EventUtils.sendMouseEvent({type:"click"}, links[1], browser_b.contentWindow);
   1.143 +  yield promise;
   1.144 +
   1.145 +  info("Check in the state that we have not stored this history");
   1.146 +  let state = ss.getBrowserState();
   1.147 +  info(JSON.stringify(JSON.parse(state), null, "\t"));
   1.148 +  is(state.indexOf("c1.html"), -1, "History entry was not stored in the session state");;
   1.149 +  gBrowser.removeTab(tab);
   1.150 +});
   1.151 +
   1.152 +// helper functions
   1.153 +function waitForLoadsInBrowser(aBrowser, aLoadCount) {
   1.154 +  let deferred = Promise.defer();
   1.155 +  let loadCount = 0;
   1.156 +  aBrowser.addEventListener("load", function(aEvent) {
   1.157 +    if (++loadCount < aLoadCount) {
   1.158 +      info("Got " + loadCount + " loads, waiting until we have " + aLoadCount);
   1.159 +      return;
   1.160 +    }
   1.161 +
   1.162 +    aBrowser.removeEventListener("load", arguments.callee, true);
   1.163 +    deferred.resolve();
   1.164 +  }, true);
   1.165 +  return deferred.promise;
   1.166 +}
   1.167 +
   1.168 +function timeout(delay, task) {
   1.169 +  let deferred = Promise.defer();
   1.170 +  setTimeout(() => deferred.resolve(true), delay);
   1.171 +  task.then(() => deferred.resolve(false), deferred.reject);
   1.172 +  return deferred.promise;
   1.173 +}

mercurial