summaryrefslogtreecommitdiffstats
path: root/browser/components/sessionstore/test/browser_merge_closed_tabs.js
blob: 2c6a946cdf2134a57d7c992ce3a7d5d0903e4a71 (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
/* 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(async function () {
  const initialState = {
    windows: [
      {
        tabs: [
          { entries: [{ url: "about:blank", triggeringPrincipal_base64 }] },
        ],
        _closedTabs: [
          {
            state: {
              entries: [
                { ID: 1000, url: "about:blank", triggeringPrincipal_base64 },
              ],
            },
          },
          {
            state: {
              entries: [
                { ID: 1001, url: "about:blank", triggeringPrincipal_base64 },
              ],
            },
          },
        ],
      },
    ],
  };

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

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

  // Open a new window and restore it to an initial state.
  let win = await promiseNewWindowLoaded({ private: false });
  await setWindowState(win, initialState, true);
  is(
    SessionStore.getClosedTabCountForWindow(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).
  await setWindowState(win, restoreState);

  // Verify the windows closed tab data is correct.
  let iClosed = initialState.windows[0]._closedTabs;
  let rClosed = restoreState.windows[0]._closedTabs;
  let cData = SessionStore.getClosedTabDataForWindow(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.
  Services.prefs.clearUserPref("browser.sessionstore.max_tabs_undo");
  await BrowserTestUtils.closeWindow(win);
});