1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/components/sessionstore/test/browser_dying_cache.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,80 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + * http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +function test() { 1.8 + TestRunner.run(); 1.9 +} 1.10 + 1.11 +/** 1.12 + * This test ensures that after closing a window we keep its state data around 1.13 + * as long as something keeps a reference to it. It should only be possible to 1.14 + * read data after closing - writing should fail. 1.15 + */ 1.16 + 1.17 +function runTests() { 1.18 + // Open a new window. 1.19 + let win = OpenBrowserWindow(); 1.20 + yield whenDelayedStartupFinished(win, next); 1.21 + 1.22 + // Load some URL in the current tab. 1.23 + let flags = Ci.nsIWebNavigation.LOAD_FLAGS_REPLACE_HISTORY; 1.24 + win.gBrowser.selectedBrowser.loadURIWithFlags("about:robots", flags); 1.25 + yield whenBrowserLoaded(win.gBrowser.selectedBrowser); 1.26 + 1.27 + // Open a second tab and close the first one. 1.28 + let tab = win.gBrowser.addTab("about:mozilla"); 1.29 + yield whenBrowserLoaded(tab.linkedBrowser); 1.30 + SyncHandlers.get(tab.linkedBrowser).flush(); 1.31 + win.gBrowser.removeTab(win.gBrowser.tabs[0]); 1.32 + 1.33 + // Make sure our window is still tracked by sessionstore 1.34 + // and the window state is as expected. 1.35 + ok("__SSi" in win, "window is being tracked by sessionstore"); 1.36 + ss.setWindowValue(win, "foo", "bar"); 1.37 + checkWindowState(win); 1.38 + 1.39 + let state = ss.getWindowState(win); 1.40 + let closedTabData = ss.getClosedTabData(win); 1.41 + 1.42 + // Close our window and wait a tick. 1.43 + whenWindowClosed(win); 1.44 + yield win.close(); 1.45 + 1.46 + // SessionStore should no longer track our window 1.47 + // but it should still report the same state. 1.48 + ok(!("__SSi" in win), "sessionstore does no longer track our window"); 1.49 + checkWindowState(win); 1.50 + 1.51 + // Make sure we're not allowed to modify state data. 1.52 + ok(shouldThrow(() => ss.setWindowState(win, {})), 1.53 + "we're not allowed to modify state data anymore"); 1.54 + ok(shouldThrow(() => ss.setWindowValue(win, "foo", "baz")), 1.55 + "we're not allowed to modify state data anymore"); 1.56 +} 1.57 + 1.58 +function checkWindowState(window) { 1.59 + let {windows: [{tabs}]} = JSON.parse(ss.getWindowState(window)); 1.60 + is(tabs.length, 1, "the window has a single tab"); 1.61 + is(tabs[0].entries[0].url, "about:mozilla", "the tab is about:mozilla"); 1.62 + 1.63 + is(ss.getClosedTabCount(window), 1, "the window has one closed tab"); 1.64 + let [{state: {entries: [{url}]}}] = JSON.parse(ss.getClosedTabData(window)); 1.65 + is(url, "about:robots", "the closed tab is about:robots"); 1.66 + 1.67 + is(ss.getWindowValue(window, "foo"), "bar", "correct extData value"); 1.68 +} 1.69 + 1.70 +function shouldThrow(f) { 1.71 + try { 1.72 + f(); 1.73 + } catch (e) { 1.74 + return true; 1.75 + } 1.76 +} 1.77 + 1.78 +function whenWindowClosed(window) { 1.79 + window.addEventListener("SSWindowClosing", function onClosing() { 1.80 + window.removeEventListener("SSWindowClosing", onClosing); 1.81 + executeSoon(next); 1.82 + }); 1.83 +}