michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /** michael@0: Ensure that frameset history works properly when restoring a tab, michael@0: provided that the frameset is static. michael@0: */ michael@0: michael@0: // Loading a toplevel frameset michael@0: add_task(function() { michael@0: let testURL = getRootDirectory(gTestPath) + "browser_frame_history_index.html"; michael@0: let tab = gBrowser.addTab(testURL); michael@0: gBrowser.selectedTab = tab; michael@0: michael@0: info("Opening a page with three frames, 4 loads should take place"); michael@0: yield waitForLoadsInBrowser(tab.linkedBrowser, 4); michael@0: michael@0: let browser_b = tab.linkedBrowser.contentDocument.getElementsByTagName("frame")[1]; michael@0: let document_b = browser_b.contentDocument; michael@0: let links = document_b.getElementsByTagName("a"); michael@0: michael@0: // We're going to click on the first link, so listen for another load event michael@0: info("Clicking on link 1, 1 load should take place"); michael@0: let promise = waitForLoadsInBrowser(tab.linkedBrowser, 1); michael@0: EventUtils.sendMouseEvent({type:"click"}, links[0], browser_b.contentWindow); michael@0: yield promise; michael@0: michael@0: info("Clicking on link 2, 1 load should take place"); michael@0: promise = waitForLoadsInBrowser(tab.linkedBrowser, 1); michael@0: EventUtils.sendMouseEvent({type:"click"}, links[1], browser_b.contentWindow); michael@0: yield promise; michael@0: michael@0: info("Close then un-close page, 4 loads should take place"); michael@0: gBrowser.removeTab(tab); michael@0: let newTab = ss.undoCloseTab(window, 0); michael@0: yield waitForLoadsInBrowser(newTab.linkedBrowser, 4); michael@0: michael@0: info("Go back in time, 1 load should take place"); michael@0: gBrowser.goBack(); michael@0: yield waitForLoadsInBrowser(newTab.linkedBrowser, 1); michael@0: michael@0: let expectedURLEnds = ["a.html", "b.html", "c1.html"]; michael@0: let frames = newTab.linkedBrowser.contentDocument.getElementsByTagName("frame"); michael@0: for (let i = 0; i < frames.length; i++) { michael@0: is(frames[i].contentDocument.location, michael@0: getRootDirectory(gTestPath) + "browser_frame_history_" + expectedURLEnds[i], michael@0: "frame " + i + " has the right url"); michael@0: } michael@0: gBrowser.removeTab(newTab); michael@0: }); michael@0: michael@0: // Loading the frameset inside an iframe michael@0: add_task(function() { michael@0: let testURL = getRootDirectory(gTestPath) + "browser_frame_history_index2.html"; michael@0: let tab = gBrowser.addTab(testURL); michael@0: gBrowser.selectedTab = tab; michael@0: michael@0: info("iframe: Opening a page with an iframe containing three frames, 5 loads should take place"); michael@0: yield waitForLoadsInBrowser(tab.linkedBrowser, 5); michael@0: michael@0: let browser_b = tab.linkedBrowser.contentDocument. michael@0: getElementById("iframe").contentDocument. michael@0: getElementsByTagName("frame")[1]; michael@0: let document_b = browser_b.contentDocument; michael@0: let links = document_b.getElementsByTagName("a"); michael@0: michael@0: // We're going to click on the first link, so listen for another load event michael@0: info("iframe: Clicking on link 1, 1 load should take place"); michael@0: let promise = waitForLoadsInBrowser(tab.linkedBrowser, 1); michael@0: EventUtils.sendMouseEvent({type:"click"}, links[0], browser_b.contentWindow); michael@0: yield promise; michael@0: michael@0: info("iframe: Clicking on link 2, 1 load should take place"); michael@0: promise = waitForLoadsInBrowser(tab.linkedBrowser, 1); michael@0: EventUtils.sendMouseEvent({type:"click"}, links[1], browser_b.contentWindow); michael@0: yield promise; michael@0: michael@0: info("iframe: Close then un-close page, 5 loads should take place"); michael@0: gBrowser.removeTab(tab); michael@0: let newTab = ss.undoCloseTab(window, 0); michael@0: yield waitForLoadsInBrowser(newTab.linkedBrowser, 5); michael@0: michael@0: info("iframe: Go back in time, 1 load should take place"); michael@0: gBrowser.goBack(); michael@0: yield waitForLoadsInBrowser(newTab.linkedBrowser, 1); michael@0: michael@0: let expectedURLEnds = ["a.html", "b.html", "c1.html"]; michael@0: let frames = newTab.linkedBrowser.contentDocument. michael@0: getElementById("iframe").contentDocument. michael@0: getElementsByTagName("frame"); michael@0: for (let i = 0; i < frames.length; i++) { michael@0: is(frames[i].contentDocument.location, michael@0: getRootDirectory(gTestPath) + "browser_frame_history_" + expectedURLEnds[i], michael@0: "frame " + i + " has the right url"); michael@0: } michael@0: gBrowser.removeTab(newTab); michael@0: }); michael@0: michael@0: // Now, test that we don't record history if the iframe is added dynamically michael@0: add_task(function() { michael@0: // Start with an empty history michael@0: let blankState = JSON.stringify({ michael@0: windows: [{ michael@0: tabs: [{ entries: [{ url: "about:blank" }] }], michael@0: _closedTabs: [] michael@0: }], michael@0: _closedWindows: [] michael@0: }); michael@0: ss.setBrowserState(blankState); michael@0: michael@0: let testURL = getRootDirectory(gTestPath) + "browser_frame_history_index_blank.html"; michael@0: let tab = gBrowser.addTab(testURL); michael@0: gBrowser.selectedTab = tab; michael@0: yield waitForLoadsInBrowser(tab.linkedBrowser, 1); michael@0: michael@0: info("dynamic: Opening a page with an iframe containing three frames, 4 dynamic loads should take place"); michael@0: let doc = tab.linkedBrowser.contentDocument; michael@0: let iframe = doc.createElement("iframe"); michael@0: iframe.id = "iframe"; michael@0: iframe.src="browser_frame_history_index.html"; michael@0: doc.body.appendChild(iframe); michael@0: yield waitForLoadsInBrowser(tab.linkedBrowser, 4); michael@0: michael@0: let browser_b = tab.linkedBrowser.contentDocument. michael@0: getElementById("iframe").contentDocument. michael@0: getElementsByTagName("frame")[1]; michael@0: let document_b = browser_b.contentDocument; michael@0: let links = document_b.getElementsByTagName("a"); michael@0: michael@0: // We're going to click on the first link, so listen for another load event michael@0: info("dynamic: Clicking on link 1, 1 load should take place"); michael@0: let promise = waitForLoadsInBrowser(tab.linkedBrowser, 1); michael@0: EventUtils.sendMouseEvent({type:"click"}, links[0], browser_b.contentWindow); michael@0: yield promise; michael@0: michael@0: info("dynamic: Clicking on link 2, 1 load should take place"); michael@0: promise = waitForLoadsInBrowser(tab.linkedBrowser, 1); michael@0: EventUtils.sendMouseEvent({type:"click"}, links[1], browser_b.contentWindow); michael@0: yield promise; michael@0: michael@0: info("Check in the state that we have not stored this history"); michael@0: let state = ss.getBrowserState(); michael@0: info(JSON.stringify(JSON.parse(state), null, "\t")); michael@0: is(state.indexOf("c1.html"), -1, "History entry was not stored in the session state");; michael@0: gBrowser.removeTab(tab); michael@0: }); michael@0: michael@0: // helper functions michael@0: function waitForLoadsInBrowser(aBrowser, aLoadCount) { michael@0: let deferred = Promise.defer(); michael@0: let loadCount = 0; michael@0: aBrowser.addEventListener("load", function(aEvent) { michael@0: if (++loadCount < aLoadCount) { michael@0: info("Got " + loadCount + " loads, waiting until we have " + aLoadCount); michael@0: return; michael@0: } michael@0: michael@0: aBrowser.removeEventListener("load", arguments.callee, true); michael@0: deferred.resolve(); michael@0: }, true); michael@0: return deferred.promise; michael@0: } michael@0: michael@0: function timeout(delay, task) { michael@0: let deferred = Promise.defer(); michael@0: setTimeout(() => deferred.resolve(true), delay); michael@0: task.then(() => deferred.resolve(false), deferred.reject); michael@0: return deferred.promise; michael@0: }