summaryrefslogtreecommitdiffstats
path: root/browser/components/sessionstore/test/browser_reopen_all_windows.js
blob: 532e689f50151b22d23f1d99e472702b2d59a461 (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
"use strict";

const PATH = "browser/browser/components/sessionstore/test/empty.html";
var URLS_WIN1 = [
  "https://example.com/" + PATH,
  "https://example.org/" + PATH,
  "http://test1.mochi.test:8888/" + PATH,
  "http://test1.example.com/" + PATH,
];
var EXPECTED_URLS_WIN1 = ["about:blank", ...URLS_WIN1];

var URLS_WIN2 = [
  "http://sub1.test1.mochi.test:8888/" + PATH,
  "http://sub2.xn--lt-uia.mochi.test:8888/" + PATH,
  "http://test2.mochi.test:8888/" + PATH,
  "http://sub1.test2.example.org/" + PATH,
  "http://sub2.test1.example.org/" + PATH,
  "http://test2.example.com/" + PATH,
];
var EXPECTED_URLS_WIN2 = ["about:blank", ...URLS_WIN2];

requestLongerTimeout(4);

function allTabsRestored(win, expectedUrls) {
  return new Promise(resolve => {
    let tabsRestored = 0;
    function handler(event) {
      let spec = event.target.linkedBrowser.currentURI.spec;
      if (expectedUrls.includes(spec)) {
        tabsRestored++;
      }
      info(`Got SSTabRestored for ${spec}, tabsRestored=${tabsRestored}`);
      if (tabsRestored === expectedUrls.length) {
        win.gBrowser.tabContainer.removeEventListener(
          "SSTabRestored",
          handler,
          true
        );
        resolve();
      }
    }
    win.gBrowser.tabContainer.addEventListener("SSTabRestored", handler, true);
  });
}

async function windowAndTabsRestored(win, expectedUrls) {
  await TestUtils.topicObserved(
    "browser-window-before-show",
    subject => subject === win
  );
  return allTabsRestored(win, expectedUrls);
}

add_task(async () => {
  await SpecialPowers.pushPrefEnv({
    set: [
      // Set the pref to true so we know exactly how many tabs should be restoring at
      // any given time. This guarantees that a finishing load won't start another.
      ["browser.sessionstore.restore_on_demand", false],
    ],
  });

  forgetClosedWindows();
  is(SessionStore.getClosedWindowCount(), 0, "starting with no closed windows");

  // Open window 1, with different tabs
  let win1 = await BrowserTestUtils.openNewBrowserWindow();
  for (let url of URLS_WIN1) {
    await BrowserTestUtils.openNewForegroundTab(win1.gBrowser, url);
  }

  // Open window 2, with different tabs
  let win2 = await BrowserTestUtils.openNewBrowserWindow();
  for (let url of URLS_WIN2) {
    await BrowserTestUtils.openNewForegroundTab(win2.gBrowser, url);
  }

  await TabStateFlusher.flushWindow(win1);
  await TabStateFlusher.flushWindow(win2);

  // Close both windows
  await BrowserTestUtils.closeWindow(win1);
  await BrowserTestUtils.closeWindow(win2);
  await forceSaveState();

  // Verify both windows were accounted for by session store
  is(
    ss.getClosedWindowCount(),
    2,
    "The closed windows was added to Recently Closed Windows"
  );

  // We previously used to manually navigate the Library menu to click the
  // "Reopen all Windows" button, but that reopens all windows at once without
  // returning a reference to each window. Since we need to attach listeners to
  // these windows *before* they start restoring tabs, we now manually call
  // undoCloseWindow() here, which has the same effect, but also gives us the
  // window references.
  info("Reopening windows");
  let restoredWindows = [];
  while (SessionStore.getClosedWindowCount() > 0) {
    restoredWindows.unshift(undoCloseWindow());
  }
  is(restoredWindows.length, 2, "Reopened correct number of windows");

  let win1Restored = windowAndTabsRestored(
    restoredWindows[0],
    EXPECTED_URLS_WIN1
  );
  let win2Restored = windowAndTabsRestored(
    restoredWindows[1],
    EXPECTED_URLS_WIN2
  );

  info("About to wait for tabs to be restored");
  await Promise.all([win1Restored, win2Restored]);

  is(
    restoredWindows[0].gBrowser.tabs.length,
    EXPECTED_URLS_WIN1.length,
    "All tabs restored"
  );
  is(
    restoredWindows[1].gBrowser.tabs.length,
    EXPECTED_URLS_WIN2.length,
    "All tabs restored"
  );

  // Verify that tabs opened as expected
  Assert.deepEqual(
    restoredWindows[0].gBrowser.tabs.map(
      tab => tab.linkedBrowser.currentURI.spec
    ),
    EXPECTED_URLS_WIN1
  );
  Assert.deepEqual(
    restoredWindows[1].gBrowser.tabs.map(
      tab => tab.linkedBrowser.currentURI.spec
    ),
    EXPECTED_URLS_WIN2
  );

  info("About to close windows");
  await BrowserTestUtils.closeWindow(restoredWindows[0]);
  await BrowserTestUtils.closeWindow(restoredWindows[1]);
});