browser/components/sessionstore/test/browser_394759_purge.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 Components.utils.import("resource://gre/modules/ForgetAboutSite.jsm");
michael@0 6
michael@0 7 function waitForClearHistory(aCallback) {
michael@0 8 let observer = {
michael@0 9 observe: function(aSubject, aTopic, aData) {
michael@0 10 Services.obs.removeObserver(this, "browser:purge-domain-data");
michael@0 11 setTimeout(aCallback, 0);
michael@0 12 }
michael@0 13 };
michael@0 14 Services.obs.addObserver(observer, "browser:purge-domain-data", false);
michael@0 15 }
michael@0 16
michael@0 17 function test() {
michael@0 18 waitForExplicitFinish();
michael@0 19 // utility functions
michael@0 20 function countClosedTabsByTitle(aClosedTabList, aTitle)
michael@0 21 aClosedTabList.filter(function (aData) aData.title == aTitle).length;
michael@0 22
michael@0 23 function countOpenTabsByTitle(aOpenTabList, aTitle)
michael@0 24 aOpenTabList.filter(function (aData) aData.entries.some(function (aEntry) aEntry.title == aTitle)).length
michael@0 25
michael@0 26 // backup old state
michael@0 27 let oldState = ss.getBrowserState();
michael@0 28 let oldState_wins = JSON.parse(oldState).windows.length;
michael@0 29 if (oldState_wins != 1)
michael@0 30 ok(false, "oldState in test_purge has " + oldState_wins + " windows instead of 1");
michael@0 31
michael@0 32 // create a new state for testing
michael@0 33 const REMEMBER = Date.now(), FORGET = Math.random();
michael@0 34 let testState = {
michael@0 35 windows: [ { tabs: [{ entries: [{ url: "http://example.com/" }] }], selected: 1 } ],
michael@0 36 _closedWindows : [
michael@0 37 // _closedWindows[0]
michael@0 38 {
michael@0 39 tabs: [
michael@0 40 { entries: [{ url: "http://example.com/", title: REMEMBER }] },
michael@0 41 { entries: [{ url: "http://mozilla.org/", title: FORGET }] }
michael@0 42 ],
michael@0 43 selected: 2,
michael@0 44 title: "mozilla.org",
michael@0 45 _closedTabs: []
michael@0 46 },
michael@0 47 // _closedWindows[1]
michael@0 48 {
michael@0 49 tabs: [
michael@0 50 { entries: [{ url: "http://mozilla.org/", title: FORGET }] },
michael@0 51 { entries: [{ url: "http://example.com/", title: REMEMBER }] },
michael@0 52 { entries: [{ url: "http://example.com/", title: REMEMBER }] },
michael@0 53 { entries: [{ url: "http://mozilla.org/", title: FORGET }] },
michael@0 54 { entries: [{ url: "http://example.com/", title: REMEMBER }] }
michael@0 55 ],
michael@0 56 selected: 5,
michael@0 57 _closedTabs: []
michael@0 58 },
michael@0 59 // _closedWindows[2]
michael@0 60 {
michael@0 61 tabs: [
michael@0 62 { entries: [{ url: "http://example.com/", title: REMEMBER }] }
michael@0 63 ],
michael@0 64 selected: 1,
michael@0 65 _closedTabs: [
michael@0 66 {
michael@0 67 state: {
michael@0 68 entries: [
michael@0 69 { url: "http://mozilla.org/", title: FORGET },
michael@0 70 { url: "http://mozilla.org/again", title: "doesn't matter" }
michael@0 71 ]
michael@0 72 },
michael@0 73 pos: 1,
michael@0 74 title: FORGET
michael@0 75 },
michael@0 76 {
michael@0 77 state: {
michael@0 78 entries: [
michael@0 79 { url: "http://example.com", title: REMEMBER }
michael@0 80 ]
michael@0 81 },
michael@0 82 title: REMEMBER
michael@0 83 }
michael@0 84 ]
michael@0 85 }
michael@0 86 ]
michael@0 87 };
michael@0 88
michael@0 89 // set browser to test state
michael@0 90 ss.setBrowserState(JSON.stringify(testState));
michael@0 91
michael@0 92 // purge domain & check that we purged correctly for closed windows
michael@0 93 ForgetAboutSite.removeDataFromDomain("mozilla.org");
michael@0 94 waitForClearHistory(function() {
michael@0 95 let closedWindowData = JSON.parse(ss.getClosedWindowData());
michael@0 96
michael@0 97 // First set of tests for _closedWindows[0] - tests basics
michael@0 98 let win = closedWindowData[0];
michael@0 99 is(win.tabs.length, 1, "1 tab was removed");
michael@0 100 is(countOpenTabsByTitle(win.tabs, FORGET), 0,
michael@0 101 "The correct tab was removed");
michael@0 102 is(countOpenTabsByTitle(win.tabs, REMEMBER), 1,
michael@0 103 "The correct tab was remembered");
michael@0 104 is(win.selected, 1, "Selected tab has changed");
michael@0 105 is(win.title, REMEMBER, "The window title was correctly updated");
michael@0 106
michael@0 107 // Test more complicated case
michael@0 108 win = closedWindowData[1];
michael@0 109 is(win.tabs.length, 3, "2 tabs were removed");
michael@0 110 is(countOpenTabsByTitle(win.tabs, FORGET), 0,
michael@0 111 "The correct tabs were removed");
michael@0 112 is(countOpenTabsByTitle(win.tabs, REMEMBER), 3,
michael@0 113 "The correct tabs were remembered");
michael@0 114 is(win.selected, 3, "Selected tab has changed");
michael@0 115 is(win.title, REMEMBER, "The window title was correctly updated");
michael@0 116
michael@0 117 // Tests handling of _closedTabs
michael@0 118 win = closedWindowData[2];
michael@0 119 is(countClosedTabsByTitle(win._closedTabs, REMEMBER), 1,
michael@0 120 "The correct number of tabs were removed, and the correct ones");
michael@0 121 is(countClosedTabsByTitle(win._closedTabs, FORGET), 0,
michael@0 122 "All tabs to be forgotten were indeed removed");
michael@0 123
michael@0 124 // restore pre-test state
michael@0 125 ss.setBrowserState(oldState);
michael@0 126 finish();
michael@0 127 });
michael@0 128 }

mercurial