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: "use strict";
michael@0:
michael@0: const TEST_URL = "data:text/html;charset=utf-8," +
michael@0: "";
michael@0:
michael@0: /**
michael@0: * This test ensures that closing a window is a reversible action. We will
michael@0: * close the the window, restore it and check that all data has been restored.
michael@0: * This includes window-specific data as well as form data for tabs.
michael@0: */
michael@0: function test() {
michael@0: waitForExplicitFinish();
michael@0:
michael@0: let uniqueKey = "bug 394759";
michael@0: let uniqueValue = "unik" + Date.now();
michael@0: let uniqueText = "pi != " + Math.random();
michael@0:
michael@0: // Clear the list of closed windows.
michael@0: while (SessionStore.getClosedWindowCount()) {
michael@0: SessionStore.forgetClosedWindow(0);
michael@0: }
michael@0:
michael@0: provideWindow(function onTestURLLoaded(newWin) {
michael@0: newWin.gBrowser.addTab().linkedBrowser.stop();
michael@0:
michael@0: // mark the window with some unique data to be restored later on
michael@0: ss.setWindowValue(newWin, uniqueKey, uniqueValue);
michael@0: let [txt, chk] = newWin.content.document.querySelectorAll("#txt, #chk");
michael@0: txt.value = uniqueText;
michael@0:
michael@0: let browser = newWin.gBrowser.selectedBrowser;
michael@0: setInputChecked(browser, {id: "chk", checked: true}).then(() => {
michael@0: newWin.close();
michael@0:
michael@0: // Now give it time to close
michael@0: executeSoon(function() {
michael@0: is(ss.getClosedWindowCount(), 1,
michael@0: "The closed window was added to Recently Closed Windows");
michael@0: let data = JSON.parse(ss.getClosedWindowData())[0];
michael@0: ok(data.title == TEST_URL && JSON.stringify(data).indexOf(uniqueText) > -1,
michael@0: "The closed window data was stored correctly");
michael@0:
michael@0: // reopen the closed window and ensure its integrity
michael@0: let newWin2 = ss.undoCloseWindow(0);
michael@0:
michael@0: ok(newWin2 instanceof ChromeWindow,
michael@0: "undoCloseWindow actually returned a window");
michael@0: is(ss.getClosedWindowCount(), 0,
michael@0: "The reopened window was removed from Recently Closed Windows");
michael@0:
michael@0: // SSTabRestored will fire more than once, so we need to make sure we count them
michael@0: let restoredTabs = 0;
michael@0: let expectedTabs = data.tabs.length;
michael@0: newWin2.addEventListener("SSTabRestored", function sstabrestoredListener(aEvent) {
michael@0: ++restoredTabs;
michael@0: info("Restored tab " + restoredTabs + "/" + expectedTabs);
michael@0: if (restoredTabs < expectedTabs) {
michael@0: return;
michael@0: }
michael@0:
michael@0: is(restoredTabs, expectedTabs, "correct number of tabs restored");
michael@0: newWin2.removeEventListener("SSTabRestored", sstabrestoredListener, true);
michael@0:
michael@0: is(newWin2.gBrowser.tabs.length, 2,
michael@0: "The window correctly restored 2 tabs");
michael@0: is(newWin2.gBrowser.currentURI.spec, TEST_URL,
michael@0: "The window correctly restored the URL");
michael@0:
michael@0: let [txt, chk] = newWin2.content.document.querySelectorAll("#txt, #chk");
michael@0: ok(txt.value == uniqueText && chk.checked,
michael@0: "The window correctly restored the form");
michael@0: is(ss.getWindowValue(newWin2, uniqueKey), uniqueValue,
michael@0: "The window correctly restored the data associated with it");
michael@0:
michael@0: // clean up
michael@0: newWin2.close();
michael@0: finish();
michael@0: }, true);
michael@0: });
michael@0: });
michael@0: }, TEST_URL);
michael@0: }
michael@0:
michael@0: function setInputChecked(browser, data) {
michael@0: return sendMessage(browser, "ss-test:setInputChecked", data);
michael@0: }