summaryrefslogtreecommitdiffstats
path: root/browser/components/sessionstore/test/browser_491577.js
blob: 87d77b71c44d2724293dff1ac433f67be13e2d61 (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/* 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/. */

add_task(async function test_deleteClosedWindow() {
  /** Test for Bug 491577 **/

  const REMEMBER = Date.now(),
    FORGET = Math.random();
  let test_state = {
    windows: [
      {
        tabs: [
          {
            entries: [
              { url: "http://example.com/", triggeringPrincipal_base64 },
            ],
          },
        ],
        selected: 1,
      },
    ],
    _closedWindows: [
      // _closedWindows[0]
      {
        tabs: [
          {
            entries: [
              {
                url: "http://example.com/",
                triggeringPrincipal_base64,
                title: "title",
              },
            ],
          },
          {
            entries: [
              {
                url: "http://mozilla.org/",
                triggeringPrincipal_base64,
                title: "title",
              },
            ],
          },
        ],
        selected: 2,
        title: FORGET,
        _closedTabs: [],
      },
      // _closedWindows[1]
      {
        tabs: [
          {
            entries: [
              {
                url: "http://mozilla.org/",
                triggeringPrincipal_base64,
                title: "title",
              },
            ],
          },
          {
            entries: [
              {
                url: "http://example.com/",
                triggeringPrincipal_base64,
                title: "title",
              },
            ],
          },
          {
            entries: [
              {
                url: "http://mozilla.org/",
                triggeringPrincipal_base64,
                title: "title",
              },
            ],
          },
        ],
        selected: 3,
        title: REMEMBER,
        _closedTabs: [],
      },
      // _closedWindows[2]
      {
        tabs: [
          {
            entries: [
              {
                url: "http://example.com/",
                triggeringPrincipal_base64,
                title: "title",
              },
            ],
          },
        ],
        selected: 1,
        title: FORGET,
        _closedTabs: [
          {
            state: {
              entries: [
                {
                  url: "http://mozilla.org/",
                  triggeringPrincipal_base64,
                  title: "title",
                },
                {
                  url: "http://mozilla.org/again",
                  triggeringPrincipal_base64,
                  title: "title",
                },
              ],
            },
            pos: 1,
            title: "title",
          },
          {
            state: {
              entries: [
                {
                  url: "http://example.com",
                  triggeringPrincipal_base64,
                  title: "title",
                },
              ],
            },
            title: "title",
          },
        ],
      },
    ],
  };
  let remember_count = 1;

  function countByTitle(aClosedWindowList, aTitle) {
    return aClosedWindowList.filter(aData => aData.title == aTitle).length;
  }

  function testForError(aFunction) {
    try {
      aFunction();
      return false;
    } catch (ex) {
      return ex.name == "NS_ERROR_ILLEGAL_VALUE";
    }
  }

  // open a window and add the above closed window list
  let newWin = openDialog(location, "_blank", "chrome,all,dialog=no");
  await promiseWindowLoaded(newWin);
  Services.prefs.setIntPref(
    "browser.sessionstore.max_windows_undo",
    test_state._closedWindows.length
  );
  await setWindowState(newWin, test_state, true);

  let closedWindows = ss.getClosedWindowData();
  is(
    closedWindows.length,
    test_state._closedWindows.length,
    "Closed window list has the expected length"
  );
  is(
    countByTitle(closedWindows, FORGET),
    test_state._closedWindows.length - remember_count,
    "The correct amount of windows are to be forgotten"
  );
  is(
    countByTitle(closedWindows, REMEMBER),
    remember_count,
    "Everything is set up."
  );

  // all of the following calls with illegal arguments should throw NS_ERROR_ILLEGAL_VALUE
  ok(
    testForError(() => ss.forgetClosedWindow(-1)),
    "Invalid window for forgetClosedWindow throws"
  );
  ok(
    testForError(() =>
      ss.forgetClosedWindow(test_state._closedWindows.length + 1)
    ),
    "Invalid window for forgetClosedWindow throws"
  );

  // Remove third window, then first window
  ss.forgetClosedWindow(2);
  ss.forgetClosedWindow(null);

  closedWindows = ss.getClosedWindowData();
  is(
    closedWindows.length,
    remember_count,
    "The correct amount of windows were removed"
  );
  is(
    countByTitle(closedWindows, FORGET),
    0,
    "All windows specifically forgotten were indeed removed"
  );
  is(
    countByTitle(closedWindows, REMEMBER),
    remember_count,
    "... and windows not specifically forgetten weren't."
  );

  // clean up
  Services.prefs.clearUserPref("browser.sessionstore.max_windows_undo");
  await BrowserTestUtils.closeWindow(newWin);
});