michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: function test() { michael@0: TestRunner.run(); michael@0: } michael@0: michael@0: /** michael@0: * This test ensures that after closing a window we keep its state data around michael@0: * as long as something keeps a reference to it. It should only be possible to michael@0: * read data after closing - writing should fail. michael@0: */ michael@0: michael@0: function runTests() { michael@0: // Open a new window. michael@0: let win = OpenBrowserWindow(); michael@0: yield whenDelayedStartupFinished(win, next); michael@0: michael@0: // Load some URL in the current tab. michael@0: let flags = Ci.nsIWebNavigation.LOAD_FLAGS_REPLACE_HISTORY; michael@0: win.gBrowser.selectedBrowser.loadURIWithFlags("about:robots", flags); michael@0: yield whenBrowserLoaded(win.gBrowser.selectedBrowser); michael@0: michael@0: // Open a second tab and close the first one. michael@0: let tab = win.gBrowser.addTab("about:mozilla"); michael@0: yield whenBrowserLoaded(tab.linkedBrowser); michael@0: SyncHandlers.get(tab.linkedBrowser).flush(); michael@0: win.gBrowser.removeTab(win.gBrowser.tabs[0]); michael@0: michael@0: // Make sure our window is still tracked by sessionstore michael@0: // and the window state is as expected. michael@0: ok("__SSi" in win, "window is being tracked by sessionstore"); michael@0: ss.setWindowValue(win, "foo", "bar"); michael@0: checkWindowState(win); michael@0: michael@0: let state = ss.getWindowState(win); michael@0: let closedTabData = ss.getClosedTabData(win); michael@0: michael@0: // Close our window and wait a tick. michael@0: whenWindowClosed(win); michael@0: yield win.close(); michael@0: michael@0: // SessionStore should no longer track our window michael@0: // but it should still report the same state. michael@0: ok(!("__SSi" in win), "sessionstore does no longer track our window"); michael@0: checkWindowState(win); michael@0: michael@0: // Make sure we're not allowed to modify state data. michael@0: ok(shouldThrow(() => ss.setWindowState(win, {})), michael@0: "we're not allowed to modify state data anymore"); michael@0: ok(shouldThrow(() => ss.setWindowValue(win, "foo", "baz")), michael@0: "we're not allowed to modify state data anymore"); michael@0: } michael@0: michael@0: function checkWindowState(window) { michael@0: let {windows: [{tabs}]} = JSON.parse(ss.getWindowState(window)); michael@0: is(tabs.length, 1, "the window has a single tab"); michael@0: is(tabs[0].entries[0].url, "about:mozilla", "the tab is about:mozilla"); michael@0: michael@0: is(ss.getClosedTabCount(window), 1, "the window has one closed tab"); michael@0: let [{state: {entries: [{url}]}}] = JSON.parse(ss.getClosedTabData(window)); michael@0: is(url, "about:robots", "the closed tab is about:robots"); michael@0: michael@0: is(ss.getWindowValue(window, "foo"), "bar", "correct extData value"); michael@0: } michael@0: michael@0: function shouldThrow(f) { michael@0: try { michael@0: f(); michael@0: } catch (e) { michael@0: return true; michael@0: } michael@0: } michael@0: michael@0: function whenWindowClosed(window) { michael@0: window.addEventListener("SSWindowClosing", function onClosing() { michael@0: window.removeEventListener("SSWindowClosing", onClosing); michael@0: executeSoon(next); michael@0: }); michael@0: }