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: Components.utils.import("resource://gre/modules/ForgetAboutSite.jsm"); michael@0: michael@0: function waitForClearHistory(aCallback) { michael@0: let observer = { michael@0: observe: function(aSubject, aTopic, aData) { michael@0: Services.obs.removeObserver(this, "browser:purge-domain-data"); michael@0: setTimeout(aCallback, 0); michael@0: } michael@0: }; michael@0: Services.obs.addObserver(observer, "browser:purge-domain-data", false); michael@0: } michael@0: michael@0: function test() { michael@0: waitForExplicitFinish(); michael@0: // utility functions michael@0: function countClosedTabsByTitle(aClosedTabList, aTitle) michael@0: aClosedTabList.filter(function (aData) aData.title == aTitle).length; michael@0: michael@0: function countOpenTabsByTitle(aOpenTabList, aTitle) michael@0: aOpenTabList.filter(function (aData) aData.entries.some(function (aEntry) aEntry.title == aTitle)).length michael@0: michael@0: // backup old state michael@0: let oldState = ss.getBrowserState(); michael@0: let oldState_wins = JSON.parse(oldState).windows.length; michael@0: if (oldState_wins != 1) michael@0: ok(false, "oldState in test_purge has " + oldState_wins + " windows instead of 1"); michael@0: michael@0: // create a new state for testing michael@0: const REMEMBER = Date.now(), FORGET = Math.random(); michael@0: let testState = { michael@0: windows: [ { tabs: [{ entries: [{ url: "http://example.com/" }] }], selected: 1 } ], michael@0: _closedWindows : [ michael@0: // _closedWindows[0] michael@0: { michael@0: tabs: [ michael@0: { entries: [{ url: "http://example.com/", title: REMEMBER }] }, michael@0: { entries: [{ url: "http://mozilla.org/", title: FORGET }] } michael@0: ], michael@0: selected: 2, michael@0: title: "mozilla.org", michael@0: _closedTabs: [] michael@0: }, michael@0: // _closedWindows[1] michael@0: { michael@0: tabs: [ michael@0: { entries: [{ url: "http://mozilla.org/", title: FORGET }] }, michael@0: { entries: [{ url: "http://example.com/", title: REMEMBER }] }, michael@0: { entries: [{ url: "http://example.com/", title: REMEMBER }] }, michael@0: { entries: [{ url: "http://mozilla.org/", title: FORGET }] }, michael@0: { entries: [{ url: "http://example.com/", title: REMEMBER }] } michael@0: ], michael@0: selected: 5, michael@0: _closedTabs: [] michael@0: }, michael@0: // _closedWindows[2] michael@0: { michael@0: tabs: [ michael@0: { entries: [{ url: "http://example.com/", title: REMEMBER }] } michael@0: ], michael@0: selected: 1, michael@0: _closedTabs: [ michael@0: { michael@0: state: { michael@0: entries: [ michael@0: { url: "http://mozilla.org/", title: FORGET }, michael@0: { url: "http://mozilla.org/again", title: "doesn't matter" } michael@0: ] michael@0: }, michael@0: pos: 1, michael@0: title: FORGET michael@0: }, michael@0: { michael@0: state: { michael@0: entries: [ michael@0: { url: "http://example.com", title: REMEMBER } michael@0: ] michael@0: }, michael@0: title: REMEMBER michael@0: } michael@0: ] michael@0: } michael@0: ] michael@0: }; michael@0: michael@0: // set browser to test state michael@0: ss.setBrowserState(JSON.stringify(testState)); michael@0: michael@0: // purge domain & check that we purged correctly for closed windows michael@0: ForgetAboutSite.removeDataFromDomain("mozilla.org"); michael@0: waitForClearHistory(function() { michael@0: let closedWindowData = JSON.parse(ss.getClosedWindowData()); michael@0: michael@0: // First set of tests for _closedWindows[0] - tests basics michael@0: let win = closedWindowData[0]; michael@0: is(win.tabs.length, 1, "1 tab was removed"); michael@0: is(countOpenTabsByTitle(win.tabs, FORGET), 0, michael@0: "The correct tab was removed"); michael@0: is(countOpenTabsByTitle(win.tabs, REMEMBER), 1, michael@0: "The correct tab was remembered"); michael@0: is(win.selected, 1, "Selected tab has changed"); michael@0: is(win.title, REMEMBER, "The window title was correctly updated"); michael@0: michael@0: // Test more complicated case michael@0: win = closedWindowData[1]; michael@0: is(win.tabs.length, 3, "2 tabs were removed"); michael@0: is(countOpenTabsByTitle(win.tabs, FORGET), 0, michael@0: "The correct tabs were removed"); michael@0: is(countOpenTabsByTitle(win.tabs, REMEMBER), 3, michael@0: "The correct tabs were remembered"); michael@0: is(win.selected, 3, "Selected tab has changed"); michael@0: is(win.title, REMEMBER, "The window title was correctly updated"); michael@0: michael@0: // Tests handling of _closedTabs michael@0: win = closedWindowData[2]; michael@0: is(countClosedTabsByTitle(win._closedTabs, REMEMBER), 1, michael@0: "The correct number of tabs were removed, and the correct ones"); michael@0: is(countClosedTabsByTitle(win._closedTabs, FORGET), 0, michael@0: "All tabs to be forgotten were indeed removed"); michael@0: michael@0: // restore pre-test state michael@0: ss.setBrowserState(oldState); michael@0: finish(); michael@0: }); michael@0: }