summaryrefslogtreecommitdiffstats
path: root/browser/components/sessionstore/test/browser_closed_tabs_windows.js
blob: 8fb99112f9302032414dda49c1376405daf161c6 (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
const ORIG_STATE = ss.getBrowserState();

const multiWindowState = {
  windows: [
    {
      tabs: [
        {
          entries: [
            { url: "https://example.com#0", triggeringPrincipal_base64 },
          ],
        },
      ],
      _closedTabs: [
        {
          state: {
            entries: [
              {
                url: "https://example.com#closed0",
                triggeringPrincipal_base64,
              },
            ],
          },
        },
        {
          state: {
            entries: [
              {
                url: "https://example.com#closed1",
                triggeringPrincipal_base64,
              },
            ],
          },
        },
      ],
      selected: 1,
    },
    {
      tabs: [
        {
          entries: [
            { url: "https://example.org#1", triggeringPrincipal_base64 },
          ],
        },
        {
          entries: [
            { url: "https://example.org#2", triggeringPrincipal_base64 },
          ],
        },
      ],
      _closedTabs: [
        {
          state: {
            entries: [
              {
                url: "https://example.org#closed0",
                triggeringPrincipal_base64,
              },
            ],
          },
        },
      ],
      selected: 1,
    },
  ],
};

async function setupWithBrowserState(browserState) {
  let numTabs = browserState.windows.reduce((count, winData) => {
    return count + winData.tabs.length;
  }, 0);
  let loadCount = 0;
  let windowOpenedCount = 1; // pre-count the first window
  let promiseRestoringTabs = new Promise(resolve => {
    gProgressListener.setCallback(function (
      aBrowser,
      aNeedRestore,
      aRestoring,
      aRestored
    ) {
      if (++loadCount == numTabs) {
        // We don't actually care about load order in this test, just that they all
        // do load.
        is(loadCount, numTabs, "all tabs were restored");
        is(aNeedRestore, 0, "there are no tabs left needing restore");

        gProgressListener.unsetCallback();
        resolve();
      }
    });
  });

  // We also want to catch the 2nd window, so we need to observe domwindowopened
  Services.ww.registerNotification(function observer(aSubject, aTopic, aData) {
    if (aTopic == "domwindowopened") {
      let win = aSubject;
      win.addEventListener(
        "load",
        function () {
          if (++windowOpenedCount == browserState.windows.length) {
            Services.ww.unregisterNotification(observer);
            win.gBrowser.addTabsProgressListener(gProgressListener);
          }
        },
        { once: true }
      );
    }
  });

  const stateRestored = TestUtils.topicObserved(
    "sessionstore-browser-state-restored"
  );
  await ss.setBrowserState(JSON.stringify(browserState));
  await stateRestored;
  await promiseRestoringTabs;
}
add_setup(async function testSetup() {
  await setupWithBrowserState(multiWindowState);
});

add_task(async function test_ClosedTabMethods() {
  let sessionStoreUpdated;
  const browserWindows = BrowserWindowTracker.orderedWindows;
  Assert.equal(
    browserWindows.length,
    multiWindowState.windows.length,
    `We expect ${multiWindowState.windows} open browser windows`
  );

  for (let idx = 0; idx < browserWindows.length; idx++) {
    const win = browserWindows[idx];
    const winData = multiWindowState.windows[idx];
    info(`window ${idx}: ${win.gBrowser.selectedBrowser.currentURI.spec}`);
    Assert.equal(
      winData._closedTabs.length,
      SessionStore.getClosedTabDataForWindow(win).length,
      `getClosedTabDataForWindow() for window ${idx} returned the expected number of objects`
    );
  }

  let closedCount;
  closedCount = SessionStore.getClosedTabCountForWindow(browserWindows[0]);
  Assert.equal(2, closedCount, "2 closed tab for this window");

  closedCount = SessionStore.getClosedTabCountForWindow(browserWindows[1]);
  Assert.equal(1, closedCount, "1 closed tab for this window");

  sessionStoreUpdated = TestUtils.topicObserved(
    "sessionstore-closed-objects-changed"
  );
  SessionStore.undoCloseTab(browserWindows[0], 0);
  await sessionStoreUpdated;

  Assert.equal(
    1,
    SessionStore.getClosedTabCountForWindow(browserWindows[0]),
    "Now theres one closed tab"
  );
  Assert.equal(
    1,
    SessionStore.getClosedTabCountForWindow(browserWindows[1]),
    "Theres still one closed tab in the 2nd window"
  );

  sessionStoreUpdated = TestUtils.topicObserved(
    "sessionstore-closed-objects-changed"
  );
  SessionStore.forgetClosedTab(browserWindows[0], 0);
  await sessionStoreUpdated;

  Assert.equal(
    0,
    SessionStore.getClosedTabCountForWindow(browserWindows[0]),
    "No closed tabs after forgetting the last one"
  );
  Assert.equal(
    1,
    SessionStore.getClosedTabCountForWindow(browserWindows[1]),
    "Theres still one closed tab in the 2nd window"
  );

  await promiseAllButPrimaryWindowClosed();

  Assert.equal(
    0,
    SessionStore.getClosedTabCountForWindow(browserWindows[0]),
    "Closed tab count is unchanged after closing the other browser window"
  );

  // Cleanup.
  await promiseBrowserState(ORIG_STATE);
});