summaryrefslogtreecommitdiffstats
path: root/browser/components/sessionstore/test/browser_394759_purge.js
diff options
context:
space:
mode:
Diffstat (limited to 'browser/components/sessionstore/test/browser_394759_purge.js')
-rw-r--r--browser/components/sessionstore/test/browser_394759_purge.js247
1 files changed, 247 insertions, 0 deletions
diff --git a/browser/components/sessionstore/test/browser_394759_purge.js b/browser/components/sessionstore/test/browser_394759_purge.js
new file mode 100644
index 0000000000..e5218c9936
--- /dev/null
+++ b/browser/components/sessionstore/test/browser_394759_purge.js
@@ -0,0 +1,247 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+let { ForgetAboutSite } = ChromeUtils.importESModule(
+ "resource://gre/modules/ForgetAboutSite.sys.mjs"
+);
+
+function promiseClearHistory() {
+ return new Promise(resolve => {
+ let observer = {
+ observe(aSubject, aTopic, aData) {
+ Services.obs.removeObserver(
+ this,
+ "browser:purge-session-history-for-domain"
+ );
+ resolve();
+ },
+ };
+ Services.obs.addObserver(
+ observer,
+ "browser:purge-session-history-for-domain"
+ );
+ });
+}
+
+add_task(async function () {
+ // utility functions
+ function countClosedTabsByTitle(aClosedTabList, aTitle) {
+ return aClosedTabList.filter(aData => aData.title == aTitle).length;
+ }
+
+ function countOpenTabsByTitle(aOpenTabList, aTitle) {
+ return aOpenTabList.filter(aData =>
+ aData.entries.some(aEntry => aEntry.title == aTitle)
+ ).length;
+ }
+
+ // backup old state
+ let oldState = ss.getBrowserState();
+ let oldState_wins = JSON.parse(oldState).windows.length;
+ if (oldState_wins != 1) {
+ ok(
+ false,
+ "oldState in test_purge has " + oldState_wins + " windows instead of 1"
+ );
+ }
+
+ // create a new state for testing
+ const REMEMBER = Date.now(),
+ FORGET = Math.random();
+ let testState = {
+ windows: [
+ {
+ tabs: [
+ {
+ entries: [
+ { url: "http://example.com/", triggeringPrincipal_base64 },
+ ],
+ },
+ ],
+ selected: 1,
+ },
+ ],
+ _closedWindows: [
+ // _closedWindows[0]
+ {
+ tabs: [
+ {
+ entries: [
+ {
+ url: "http://example.com/",
+ triggeringPrincipal_base64,
+ title: REMEMBER,
+ },
+ ],
+ },
+ {
+ entries: [
+ {
+ url: "http://mozilla.org/",
+ triggeringPrincipal_base64,
+ title: FORGET,
+ },
+ ],
+ },
+ ],
+ selected: 2,
+ title: "mozilla.org",
+ _closedTabs: [],
+ },
+ // _closedWindows[1]
+ {
+ tabs: [
+ {
+ entries: [
+ {
+ url: "http://mozilla.org/",
+ triggeringPrincipal_base64,
+ title: FORGET,
+ },
+ ],
+ },
+ {
+ entries: [
+ {
+ url: "http://example.com/",
+ triggeringPrincipal_base64,
+ title: REMEMBER,
+ },
+ ],
+ },
+ {
+ entries: [
+ {
+ url: "http://example.com/",
+ triggeringPrincipal_base64,
+ title: REMEMBER,
+ },
+ ],
+ },
+ {
+ entries: [
+ {
+ url: "http://mozilla.org/",
+ triggeringPrincipal_base64,
+ title: FORGET,
+ },
+ ],
+ },
+ {
+ entries: [
+ {
+ url: "http://example.com/",
+ triggeringPrincipal_base64,
+ title: REMEMBER,
+ },
+ ],
+ },
+ ],
+ selected: 5,
+ _closedTabs: [],
+ },
+ // _closedWindows[2]
+ {
+ tabs: [
+ {
+ entries: [
+ {
+ url: "http://example.com/",
+ triggeringPrincipal_base64,
+ title: REMEMBER,
+ },
+ ],
+ },
+ ],
+ selected: 1,
+ _closedTabs: [
+ {
+ state: {
+ entries: [
+ {
+ url: "http://mozilla.org/",
+ triggeringPrincipal_base64,
+ title: FORGET,
+ },
+ {
+ url: "http://mozilla.org/again",
+ triggeringPrincipal_base64,
+ title: "doesn't matter",
+ },
+ ],
+ },
+ pos: 1,
+ title: FORGET,
+ },
+ {
+ state: {
+ entries: [
+ {
+ url: "http://example.com",
+ triggeringPrincipal_base64,
+ title: REMEMBER,
+ },
+ ],
+ },
+ title: REMEMBER,
+ },
+ ],
+ },
+ ],
+ };
+
+ // set browser to test state
+ ss.setBrowserState(JSON.stringify(testState));
+
+ // purge domain & check that we purged correctly for closed windows
+ let clearHistoryPromise = promiseClearHistory();
+ await ForgetAboutSite.removeDataFromDomain("mozilla.org");
+ await clearHistoryPromise;
+
+ let closedWindowData = ss.getClosedWindowData();
+
+ // First set of tests for _closedWindows[0] - tests basics
+ let win = closedWindowData[0];
+ is(win.tabs.length, 1, "1 tab was removed");
+ is(countOpenTabsByTitle(win.tabs, FORGET), 0, "The correct tab was removed");
+ is(
+ countOpenTabsByTitle(win.tabs, REMEMBER),
+ 1,
+ "The correct tab was remembered"
+ );
+ is(win.selected, 1, "Selected tab has changed");
+ is(win.title, REMEMBER, "The window title was correctly updated");
+
+ // Test more complicated case
+ win = closedWindowData[1];
+ is(win.tabs.length, 3, "2 tabs were removed");
+ is(
+ countOpenTabsByTitle(win.tabs, FORGET),
+ 0,
+ "The correct tabs were removed"
+ );
+ is(
+ countOpenTabsByTitle(win.tabs, REMEMBER),
+ 3,
+ "The correct tabs were remembered"
+ );
+ is(win.selected, 3, "Selected tab has changed");
+ is(win.title, REMEMBER, "The window title was correctly updated");
+
+ // Tests handling of _closedTabs
+ win = closedWindowData[2];
+ is(
+ countClosedTabsByTitle(win._closedTabs, REMEMBER),
+ 1,
+ "The correct number of tabs were removed, and the correct ones"
+ );
+ is(
+ countClosedTabsByTitle(win._closedTabs, FORGET),
+ 0,
+ "All tabs to be forgotten were indeed removed"
+ );
+
+ // restore pre-test state
+ ss.setBrowserState(oldState);
+});