summaryrefslogtreecommitdiffstats
path: root/browser/components/sessionstore/test/browser_merge_closed_tabs.js
blob: b26e86f22b3118290075d9a6edb3c5bc900c6f06 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

/**
 * This test ensures that closed tabs are merged when restoring
 * a window state without overwriting tabs.
 */
add_task(function () {
  const initialState = {
    windows: [{
      tabs: [
        { entries: [{ url: "about:blank" }] }
      ],
      _closedTabs: [
        { state: { entries: [{ ID: 1000, url: "about:blank" }]} },
        { state: { entries: [{ ID: 1001, url: "about:blank" }]} }
      ]
    }]
  }

  const restoreState = {
    windows: [{
      tabs: [
        { entries: [{ url: "about:robots" }] }
      ],
      _closedTabs: [
        { state: { entries: [{ ID: 1002, url: "about:robots" }]} },
        { state: { entries: [{ ID: 1003, url: "about:robots" }]} },
        { state: { entries: [{ ID: 1004, url: "about:robots" }]} }
      ]
    }]
  }

  const maxTabsUndo = 4;
  gPrefService.setIntPref("browser.sessionstore.max_tabs_undo", maxTabsUndo);

  // Open a new window and restore it to an initial state.
  let win = yield promiseNewWindowLoaded({private: false});
  SessionStore.setWindowState(win, JSON.stringify(initialState), true);
  is(SessionStore.getClosedTabCount(win), 2, "2 closed tabs after restoring initial state");

  // Restore the new state but do not overwrite existing tabs (this should
  // cause the closed tabs to be merged).
  SessionStore.setWindowState(win, JSON.stringify(restoreState), false);

  // Verify the windows closed tab data is correct.
  let iClosed = initialState.windows[0]._closedTabs;
  let rClosed = restoreState.windows[0]._closedTabs;
  let cData = JSON.parse(SessionStore.getClosedTabData(win));

  is(cData.length, Math.min(iClosed.length + rClosed.length, maxTabsUndo),
     "Number of closed tabs is correct");

  // When the closed tabs are merged the restored tabs are considered to be
  // closed more recently.
  for (let i = 0; i < cData.length; i++) {
    if (i < rClosed.length) {
      is(cData[i].state.entries[0].ID, rClosed[i].state.entries[0].ID,
         "Closed tab entry matches");
    } else {
      is(cData[i].state.entries[0].ID, iClosed[i - rClosed.length].state.entries[0].ID,
         "Closed tab entry matches");
    }
  }

  // Clean up.
  gPrefService.clearUserPref("browser.sessionstore.max_tabs_undo");
  yield BrowserTestUtils.closeWindow(win);
});