summaryrefslogtreecommitdiffstats
path: root/browser/components/sessionstore/test/browser_394759_purge.js
blob: 75144aba1156dd97b8985f845daebac27b3a844c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* 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/. */

Components.utils.import("resource://gre/modules/ForgetAboutSite.jsm");

function waitForClearHistory(aCallback) {
  let observer = {
    observe: function(aSubject, aTopic, aData) {
      Services.obs.removeObserver(this, "browser:purge-domain-data");
      setTimeout(aCallback, 0);
    }
  };
  Services.obs.addObserver(observer, "browser:purge-domain-data", false);
}

function test() {
  waitForExplicitFinish();
  // 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/" }] }], selected: 1 } ],
    _closedWindows : [
      // _closedWindows[0]
      {
        tabs: [
          { entries: [{ url: "http://example.com/", title: REMEMBER }] },
          { entries: [{ url: "http://mozilla.org/", title: FORGET }] }
        ],
        selected: 2,
        title: "mozilla.org",
        _closedTabs: []
      },
      // _closedWindows[1]
      {
        tabs: [
         { entries: [{ url: "http://mozilla.org/", title: FORGET }] },
         { entries: [{ url: "http://example.com/", title: REMEMBER }] },
         { entries: [{ url: "http://example.com/", title: REMEMBER }] },
         { entries: [{ url: "http://mozilla.org/", title: FORGET }] },
         { entries: [{ url: "http://example.com/", title: REMEMBER }] }
        ],
        selected: 5,
        _closedTabs: []
      },
      // _closedWindows[2]
      {
        tabs: [
          { entries: [{ url: "http://example.com/", title: REMEMBER }] }
        ],
        selected: 1,
        _closedTabs: [
          {
            state: {
              entries: [
                { url: "http://mozilla.org/", title: FORGET },
                { url: "http://mozilla.org/again", title: "doesn't matter" }
              ]
            },
            pos: 1,
            title: FORGET
          },
          {
            state: {
              entries: [
                { url: "http://example.com", title: REMEMBER }
              ]
            },
            title: REMEMBER
          }
        ]
      }
    ]
  };

  // set browser to test state
  ss.setBrowserState(JSON.stringify(testState));

  // purge domain & check that we purged correctly for closed windows
  ForgetAboutSite.removeDataFromDomain("mozilla.org");
  waitForClearHistory(function() {
    let closedWindowData = JSON.parse(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);
    finish();
  });
}