browser/components/sessionstore/test/browser_frame_history.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 /**
michael@0 6 Ensure that frameset history works properly when restoring a tab,
michael@0 7 provided that the frameset is static.
michael@0 8 */
michael@0 9
michael@0 10 // Loading a toplevel frameset
michael@0 11 add_task(function() {
michael@0 12 let testURL = getRootDirectory(gTestPath) + "browser_frame_history_index.html";
michael@0 13 let tab = gBrowser.addTab(testURL);
michael@0 14 gBrowser.selectedTab = tab;
michael@0 15
michael@0 16 info("Opening a page with three frames, 4 loads should take place");
michael@0 17 yield waitForLoadsInBrowser(tab.linkedBrowser, 4);
michael@0 18
michael@0 19 let browser_b = tab.linkedBrowser.contentDocument.getElementsByTagName("frame")[1];
michael@0 20 let document_b = browser_b.contentDocument;
michael@0 21 let links = document_b.getElementsByTagName("a");
michael@0 22
michael@0 23 // We're going to click on the first link, so listen for another load event
michael@0 24 info("Clicking on link 1, 1 load should take place");
michael@0 25 let promise = waitForLoadsInBrowser(tab.linkedBrowser, 1);
michael@0 26 EventUtils.sendMouseEvent({type:"click"}, links[0], browser_b.contentWindow);
michael@0 27 yield promise;
michael@0 28
michael@0 29 info("Clicking on link 2, 1 load should take place");
michael@0 30 promise = waitForLoadsInBrowser(tab.linkedBrowser, 1);
michael@0 31 EventUtils.sendMouseEvent({type:"click"}, links[1], browser_b.contentWindow);
michael@0 32 yield promise;
michael@0 33
michael@0 34 info("Close then un-close page, 4 loads should take place");
michael@0 35 gBrowser.removeTab(tab);
michael@0 36 let newTab = ss.undoCloseTab(window, 0);
michael@0 37 yield waitForLoadsInBrowser(newTab.linkedBrowser, 4);
michael@0 38
michael@0 39 info("Go back in time, 1 load should take place");
michael@0 40 gBrowser.goBack();
michael@0 41 yield waitForLoadsInBrowser(newTab.linkedBrowser, 1);
michael@0 42
michael@0 43 let expectedURLEnds = ["a.html", "b.html", "c1.html"];
michael@0 44 let frames = newTab.linkedBrowser.contentDocument.getElementsByTagName("frame");
michael@0 45 for (let i = 0; i < frames.length; i++) {
michael@0 46 is(frames[i].contentDocument.location,
michael@0 47 getRootDirectory(gTestPath) + "browser_frame_history_" + expectedURLEnds[i],
michael@0 48 "frame " + i + " has the right url");
michael@0 49 }
michael@0 50 gBrowser.removeTab(newTab);
michael@0 51 });
michael@0 52
michael@0 53 // Loading the frameset inside an iframe
michael@0 54 add_task(function() {
michael@0 55 let testURL = getRootDirectory(gTestPath) + "browser_frame_history_index2.html";
michael@0 56 let tab = gBrowser.addTab(testURL);
michael@0 57 gBrowser.selectedTab = tab;
michael@0 58
michael@0 59 info("iframe: Opening a page with an iframe containing three frames, 5 loads should take place");
michael@0 60 yield waitForLoadsInBrowser(tab.linkedBrowser, 5);
michael@0 61
michael@0 62 let browser_b = tab.linkedBrowser.contentDocument.
michael@0 63 getElementById("iframe").contentDocument.
michael@0 64 getElementsByTagName("frame")[1];
michael@0 65 let document_b = browser_b.contentDocument;
michael@0 66 let links = document_b.getElementsByTagName("a");
michael@0 67
michael@0 68 // We're going to click on the first link, so listen for another load event
michael@0 69 info("iframe: Clicking on link 1, 1 load should take place");
michael@0 70 let promise = waitForLoadsInBrowser(tab.linkedBrowser, 1);
michael@0 71 EventUtils.sendMouseEvent({type:"click"}, links[0], browser_b.contentWindow);
michael@0 72 yield promise;
michael@0 73
michael@0 74 info("iframe: Clicking on link 2, 1 load should take place");
michael@0 75 promise = waitForLoadsInBrowser(tab.linkedBrowser, 1);
michael@0 76 EventUtils.sendMouseEvent({type:"click"}, links[1], browser_b.contentWindow);
michael@0 77 yield promise;
michael@0 78
michael@0 79 info("iframe: Close then un-close page, 5 loads should take place");
michael@0 80 gBrowser.removeTab(tab);
michael@0 81 let newTab = ss.undoCloseTab(window, 0);
michael@0 82 yield waitForLoadsInBrowser(newTab.linkedBrowser, 5);
michael@0 83
michael@0 84 info("iframe: Go back in time, 1 load should take place");
michael@0 85 gBrowser.goBack();
michael@0 86 yield waitForLoadsInBrowser(newTab.linkedBrowser, 1);
michael@0 87
michael@0 88 let expectedURLEnds = ["a.html", "b.html", "c1.html"];
michael@0 89 let frames = newTab.linkedBrowser.contentDocument.
michael@0 90 getElementById("iframe").contentDocument.
michael@0 91 getElementsByTagName("frame");
michael@0 92 for (let i = 0; i < frames.length; i++) {
michael@0 93 is(frames[i].contentDocument.location,
michael@0 94 getRootDirectory(gTestPath) + "browser_frame_history_" + expectedURLEnds[i],
michael@0 95 "frame " + i + " has the right url");
michael@0 96 }
michael@0 97 gBrowser.removeTab(newTab);
michael@0 98 });
michael@0 99
michael@0 100 // Now, test that we don't record history if the iframe is added dynamically
michael@0 101 add_task(function() {
michael@0 102 // Start with an empty history
michael@0 103 let blankState = JSON.stringify({
michael@0 104 windows: [{
michael@0 105 tabs: [{ entries: [{ url: "about:blank" }] }],
michael@0 106 _closedTabs: []
michael@0 107 }],
michael@0 108 _closedWindows: []
michael@0 109 });
michael@0 110 ss.setBrowserState(blankState);
michael@0 111
michael@0 112 let testURL = getRootDirectory(gTestPath) + "browser_frame_history_index_blank.html";
michael@0 113 let tab = gBrowser.addTab(testURL);
michael@0 114 gBrowser.selectedTab = tab;
michael@0 115 yield waitForLoadsInBrowser(tab.linkedBrowser, 1);
michael@0 116
michael@0 117 info("dynamic: Opening a page with an iframe containing three frames, 4 dynamic loads should take place");
michael@0 118 let doc = tab.linkedBrowser.contentDocument;
michael@0 119 let iframe = doc.createElement("iframe");
michael@0 120 iframe.id = "iframe";
michael@0 121 iframe.src="browser_frame_history_index.html";
michael@0 122 doc.body.appendChild(iframe);
michael@0 123 yield waitForLoadsInBrowser(tab.linkedBrowser, 4);
michael@0 124
michael@0 125 let browser_b = tab.linkedBrowser.contentDocument.
michael@0 126 getElementById("iframe").contentDocument.
michael@0 127 getElementsByTagName("frame")[1];
michael@0 128 let document_b = browser_b.contentDocument;
michael@0 129 let links = document_b.getElementsByTagName("a");
michael@0 130
michael@0 131 // We're going to click on the first link, so listen for another load event
michael@0 132 info("dynamic: Clicking on link 1, 1 load should take place");
michael@0 133 let promise = waitForLoadsInBrowser(tab.linkedBrowser, 1);
michael@0 134 EventUtils.sendMouseEvent({type:"click"}, links[0], browser_b.contentWindow);
michael@0 135 yield promise;
michael@0 136
michael@0 137 info("dynamic: Clicking on link 2, 1 load should take place");
michael@0 138 promise = waitForLoadsInBrowser(tab.linkedBrowser, 1);
michael@0 139 EventUtils.sendMouseEvent({type:"click"}, links[1], browser_b.contentWindow);
michael@0 140 yield promise;
michael@0 141
michael@0 142 info("Check in the state that we have not stored this history");
michael@0 143 let state = ss.getBrowserState();
michael@0 144 info(JSON.stringify(JSON.parse(state), null, "\t"));
michael@0 145 is(state.indexOf("c1.html"), -1, "History entry was not stored in the session state");;
michael@0 146 gBrowser.removeTab(tab);
michael@0 147 });
michael@0 148
michael@0 149 // helper functions
michael@0 150 function waitForLoadsInBrowser(aBrowser, aLoadCount) {
michael@0 151 let deferred = Promise.defer();
michael@0 152 let loadCount = 0;
michael@0 153 aBrowser.addEventListener("load", function(aEvent) {
michael@0 154 if (++loadCount < aLoadCount) {
michael@0 155 info("Got " + loadCount + " loads, waiting until we have " + aLoadCount);
michael@0 156 return;
michael@0 157 }
michael@0 158
michael@0 159 aBrowser.removeEventListener("load", arguments.callee, true);
michael@0 160 deferred.resolve();
michael@0 161 }, true);
michael@0 162 return deferred.promise;
michael@0 163 }
michael@0 164
michael@0 165 function timeout(delay, task) {
michael@0 166 let deferred = Promise.defer();
michael@0 167 setTimeout(() => deferred.resolve(true), delay);
michael@0 168 task.then(() => deferred.resolve(false), deferred.reject);
michael@0 169 return deferred.promise;
michael@0 170 }

mercurial