summaryrefslogtreecommitdiffstats
path: root/browser/components/sessionstore/test/browser_pinned_tabs.js
blob: 7a51da7ccc84b44d2a81c7afe6204376cc680f3b (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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const REMOTE_URL = "https://www.example.com/";
const ABOUT_ROBOTS_URL = "about:robots";
const NO_TITLE_URL = "data:text/plain,foo";

const BACKUP_STATE = SessionStore.getBrowserState();
registerCleanupFunction(() => promiseBrowserState(BACKUP_STATE));

add_setup(async function () {
  await SpecialPowers.pushPrefEnv({
    set: [
      ["browser.sessionstore.restore_on_demand", true],
      ["browser.sessionstore.restore_tabs_lazily", true],
    ],
  });
});

/**
 * When implementing batch insertion of tabs as part of session restore,
 * we started reversing the insertion order of pinned tabs (bug 1607441).
 * This test checks we don't regress that again.
 */
add_task(async function test_pinned_tabs_order() {
  // we expect 3 pinned tabs plus the selected tab get content restored.
  let allTabsRestored = promiseSessionStoreLoads(4);
  await promiseBrowserState({
    windows: [
      {
        selected: 4, // SessionStore uses 1-based indexing.
        tabs: [
          {
            pinned: true,
            entries: [{ url: REMOTE_URL, triggeringPrincipal_base64 }],
          },
          {
            pinned: true,
            entries: [{ url: ABOUT_ROBOTS_URL, triggeringPrincipal_base64 }],
          },
          {
            pinned: true,
            entries: [{ url: NO_TITLE_URL, triggeringPrincipal_base64 }],
          },
          { entries: [{ url: REMOTE_URL, triggeringPrincipal_base64 }] },
          { entries: [{ url: "about:blank", triggeringPrincipal_base64 }] },
        ],
      },
    ],
  });
  await allTabsRestored;
  let [tab1, tab2, tab3, tab4, tab5] = gBrowser.tabs;
  ok(tab1.pinned, "First tab is pinned");
  ok(tab2.pinned, "Second tab is pinned");
  ok(tab3.pinned, "Third tab is pinned");
  ok(!tab4.pinned, "Fourth tab is not pinned");
  ok(!tab5.pinned, "Fifth tab is not pinned");

  ok(tab4.selected, "Fourth tab is selected");
  is(
    tab1.linkedBrowser.currentURI.spec,
    REMOTE_URL,
    "First tab has matching URL"
  );
  is(
    tab2.linkedBrowser.currentURI.spec,
    ABOUT_ROBOTS_URL,
    "Second tab has matching URL"
  );
  is(
    tab3.linkedBrowser.currentURI.spec,
    NO_TITLE_URL,
    "Third tab has matching URL"
  );
  // Clean up for the next task.
  await promiseBrowserState(BACKUP_STATE);
});

/**
 * When fixing the previous regression, pinned tabs started disappearing out
 * of sessions with selected pinned tabs. This test checks that case.
 */
add_task(async function test_selected_pinned_tab_dataloss() {
  // we expect 3 pinned tabs (one of which is selected) get content restored.
  let allTabsRestored = promiseSessionStoreLoads(3);
  await promiseBrowserState({
    windows: [
      {
        selected: 1, // SessionStore uses 1-based indexing.
        tabs: [
          {
            pinned: true,
            entries: [{ url: REMOTE_URL, triggeringPrincipal_base64 }],
          },
          {
            pinned: true,
            entries: [{ url: ABOUT_ROBOTS_URL, triggeringPrincipal_base64 }],
          },
          {
            pinned: true,
            entries: [{ url: NO_TITLE_URL, triggeringPrincipal_base64 }],
          },
          { entries: [{ url: REMOTE_URL, triggeringPrincipal_base64 }] },
          { entries: [{ url: "about:blank", triggeringPrincipal_base64 }] },
        ],
      },
    ],
  });
  await allTabsRestored;
  let [tab1, tab2, tab3, tab4, tab5] = gBrowser.tabs;
  ok(tab5, "Should have 5 tabs");
  ok(tab1.pinned, "First tab is pinned");
  ok(tab2.pinned, "Second tab is pinned");
  ok(tab3.pinned, "Third tab is pinned");
  ok(tab4 && !tab4.pinned, "Fourth tab is not pinned");
  ok(tab5 && !tab5.pinned, "Fifth tab is not pinned");

  ok(tab1 && tab1.selected, "First (pinned) tab is selected");
  is(
    tab1.linkedBrowser.currentURI.spec,
    REMOTE_URL,
    "First tab has matching URL"
  );
  is(
    tab2.linkedBrowser.currentURI.spec,
    ABOUT_ROBOTS_URL,
    "Second tab has matching URL"
  );
  is(
    tab3.linkedBrowser.currentURI.spec,
    NO_TITLE_URL,
    "Third tab has matching URL"
  );
  // Clean up for the next task.
  await promiseBrowserState(BACKUP_STATE);
});

/**
 * While we're here, it seems useful to have a test for mixed pinned and
 * unpinned tabs in session store state, as well as userContextId.
 */
add_task(async function test_mixed_pinned_unpinned() {
  // we expect 3 pinned tabs plus the selected tab get content restored.
  let allTabsRestored = promiseSessionStoreLoads(4);
  await promiseBrowserState({
    windows: [
      {
        selected: 4, // SessionStore uses 1-based indexing.
        tabs: [
          {
            pinned: true,
            entries: [{ url: REMOTE_URL, triggeringPrincipal_base64 }],
          },
          { entries: [{ url: REMOTE_URL, triggeringPrincipal_base64 }] },
          {
            pinned: true,
            entries: [{ url: ABOUT_ROBOTS_URL, triggeringPrincipal_base64 }],
          },
          { entries: [{ url: "about:blank", triggeringPrincipal_base64 }] },
          {
            pinned: true,
            entries: [{ url: NO_TITLE_URL, triggeringPrincipal_base64 }],
          },
        ],
      },
    ],
  });
  await allTabsRestored;
  let [tab1, tab2, tab3, tab4, tab5] = gBrowser.tabs;
  ok(tab1.pinned, "First tab is pinned");
  ok(tab2.pinned, "Second tab is pinned");
  ok(tab3.pinned, "Third tab is pinned");
  ok(!tab4.pinned, "Fourth tab is not pinned");
  ok(!tab5.pinned, "Fifth tab is not pinned");

  // This is confusing to read - the 4th entry in the session data is
  // selected. But the 5th entry is pinned, so it moves to the start of the
  // tabstrip, so when we fetch `gBrowser.tabs`, the 4th entry in the list
  // is actually the 5th tab.
  ok(tab5.selected, "Fifth tab is selected");
  is(
    tab1.linkedBrowser.currentURI.spec,
    REMOTE_URL,
    "First tab has matching URL"
  );
  is(
    tab2.linkedBrowser.currentURI.spec,
    ABOUT_ROBOTS_URL,
    "Second tab has matching URL"
  );
  is(
    tab3.linkedBrowser.currentURI.spec,
    NO_TITLE_URL,
    "Third tab has matching URL"
  );
  // Clean up for the next task.
  await promiseBrowserState(BACKUP_STATE);
});

/**
 * After session restore, if we crash an unpinned tab, we noticed pinned tabs
 * created in the same process would lose all data (Bug 1624511). This test
 * checks that case.
 */
add_task(async function test_pinned_tab_dataloss() {
  // We do not run if there are no crash reporters to avoid
  // problems with the intentional crash.
  if (!AppConstants.MOZ_CRASHREPORTER) {
    return;
  }
  // If we end up increasing the process count limit in future,
  // we want to ensure that we don't stop testing this case
  // of pinned tab data loss.
  if (SpecialPowers.getIntPref("dom.ipc.processCount") > 8) {
    ok(
      false,
      "Process count is greater than 8, update the number of pinned tabs in test."
    );
  }

  // We expect 17 pinned tabs plus the selected tab get content restored.
  // Given that the default process count is currently 8, we need this
  // number of pinned tabs to reproduce the data loss. If this changes,
  // please add more pinned tabs.
  let allTabsRestored = promiseSessionStoreLoads(18);
  await promiseBrowserState({
    windows: [
      {
        selected: 18, // SessionStore uses 1-based indexing.
        tabs: [
          {
            pinned: true,
            entries: [{ url: REMOTE_URL, triggeringPrincipal_base64 }],
          },
          {
            pinned: true,
            entries: [{ url: REMOTE_URL, triggeringPrincipal_base64 }],
          },
          {
            pinned: true,
            entries: [{ url: REMOTE_URL, triggeringPrincipal_base64 }],
          },
          {
            pinned: true,
            entries: [{ url: REMOTE_URL, triggeringPrincipal_base64 }],
          },
          {
            pinned: true,
            entries: [{ url: REMOTE_URL, triggeringPrincipal_base64 }],
          },
          {
            pinned: true,
            entries: [{ url: REMOTE_URL, triggeringPrincipal_base64 }],
          },
          {
            pinned: true,
            entries: [{ url: REMOTE_URL, triggeringPrincipal_base64 }],
          },
          {
            pinned: true,
            entries: [{ url: REMOTE_URL, triggeringPrincipal_base64 }],
          },
          {
            pinned: true,
            entries: [{ url: REMOTE_URL, triggeringPrincipal_base64 }],
          },
          {
            pinned: true,
            entries: [{ url: REMOTE_URL, triggeringPrincipal_base64 }],
          },
          {
            pinned: true,
            entries: [{ url: REMOTE_URL, triggeringPrincipal_base64 }],
          },
          {
            pinned: true,
            entries: [{ url: REMOTE_URL, triggeringPrincipal_base64 }],
          },
          {
            pinned: true,
            entries: [{ url: REMOTE_URL, triggeringPrincipal_base64 }],
          },
          {
            pinned: true,
            entries: [{ url: REMOTE_URL, triggeringPrincipal_base64 }],
          },
          {
            pinned: true,
            entries: [{ url: REMOTE_URL, triggeringPrincipal_base64 }],
          },
          {
            pinned: true,
            entries: [{ url: REMOTE_URL, triggeringPrincipal_base64 }],
          },
          {
            pinned: true,
            entries: [{ url: REMOTE_URL, triggeringPrincipal_base64 }],
          },
          { entries: [{ url: REMOTE_URL, triggeringPrincipal_base64 }] },
        ],
      },
    ],
  });
  await allTabsRestored;

  let tabs = gBrowser.tabs;
  await BrowserTestUtils.crashFrame(tabs[17].linkedBrowser);

  await TestUtils.topicObserved("sessionstore-state-write-complete");

  for (let i = 0; i < tabs.length; i++) {
    let tab = tabs[i];
    is(
      tab.linkedBrowser.currentURI.spec,
      REMOTE_URL,
      `Tab ${i + 1} should have matching URL`
    );
  }

  // Clean up for the next task.
  await promiseBrowserState(BACKUP_STATE);
});