summaryrefslogtreecommitdiffstats
path: root/browser/components/extensions/test/browser/browser_ext_sessions_restore.js
blob: 0f10f6651763040d1c4fd6203eb56852a2063efb (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
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";

SimpleTest.requestCompleteLog();

ChromeUtils.defineESModuleGetters(this, {
  SessionStore: "resource:///modules/sessionstore/SessionStore.sys.mjs",
  TabStateFlusher: "resource:///modules/sessionstore/TabStateFlusher.sys.mjs",
});

add_task(async function test_sessions_restore() {
  function background() {
    let notificationCount = 0;
    browser.sessions.onChanged.addListener(() => {
      notificationCount++;
      browser.test.sendMessage("notificationCount", notificationCount);
    });
    browser.test.onMessage.addListener((msg, data) => {
      if (msg == "check-sessions") {
        browser.sessions.getRecentlyClosed().then(recentlyClosed => {
          browser.test.sendMessage("recentlyClosed", recentlyClosed);
        });
      } else if (msg == "restore") {
        browser.sessions.restore(data).then(sessions => {
          browser.test.sendMessage("restored", sessions);
        });
      } else if (msg == "restore-reject") {
        browser.sessions.restore("not-a-valid-session-id").then(
          sessions => {
            browser.test.fail("restore rejected with an invalid sessionId");
          },
          error => {
            browser.test.assertTrue(
              error.message.includes(
                "Could not restore object using sessionId not-a-valid-session-id."
              )
            );
            browser.test.sendMessage("restore-rejected");
          }
        );
      }
    });
    browser.test.sendMessage("ready");
  }

  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      permissions: ["sessions", "tabs"],
    },
    background,
  });

  async function assertNotificationCount(expected) {
    let notificationCount = await extension.awaitMessage("notificationCount");
    is(
      notificationCount,
      expected,
      "the expected number of notifications was fired"
    );
  }

  await extension.startup();

  const {
    Management: {
      global: { windowTracker, tabTracker },
    },
  } = ChromeUtils.importESModule("resource://gre/modules/Extension.sys.mjs");

  function checkLocalTab(tab, expectedUrl) {
    let realTab = tabTracker.getTab(tab.id);
    let tabState = JSON.parse(SessionStore.getTabState(realTab));
    is(
      tabState.entries[0].url,
      expectedUrl,
      "restored tab has the expected url"
    );
  }

  await extension.awaitMessage("ready");

  let win = await BrowserTestUtils.openNewBrowserWindow();
  BrowserTestUtils.loadURIString(win.gBrowser.selectedBrowser, "about:config");
  await BrowserTestUtils.browserLoaded(win.gBrowser.selectedBrowser);
  for (let url of ["about:robots", "about:mozilla"]) {
    await BrowserTestUtils.openNewForegroundTab(win.gBrowser, url);
  }
  await BrowserTestUtils.closeWindow(win);
  await assertNotificationCount(1);

  extension.sendMessage("check-sessions");
  let recentlyClosed = await extension.awaitMessage("recentlyClosed");

  // Check that our expected window is the most recently closed.
  is(
    recentlyClosed[0].window.tabs.length,
    3,
    "most recently closed window has the expected number of tabs"
  );

  // Restore the window.
  extension.sendMessage("restore");
  await assertNotificationCount(2);
  let restored = await extension.awaitMessage("restored");

  is(
    restored.window.tabs.length,
    3,
    "restore returned a window with the expected number of tabs"
  );
  checkLocalTab(restored.window.tabs[0], "about:config");
  checkLocalTab(restored.window.tabs[1], "about:robots");
  checkLocalTab(restored.window.tabs[2], "about:mozilla");

  // Close the window again.
  let window = windowTracker.getWindow(restored.window.id);
  await BrowserTestUtils.closeWindow(window);
  await assertNotificationCount(3);

  // Restore the window using the sessionId.
  extension.sendMessage("check-sessions");
  recentlyClosed = await extension.awaitMessage("recentlyClosed");
  extension.sendMessage("restore", recentlyClosed[0].window.sessionId);
  await assertNotificationCount(4);
  restored = await extension.awaitMessage("restored");

  is(
    restored.window.tabs.length,
    3,
    "restore returned a window with the expected number of tabs"
  );
  checkLocalTab(restored.window.tabs[0], "about:config");
  checkLocalTab(restored.window.tabs[1], "about:robots");
  checkLocalTab(restored.window.tabs[2], "about:mozilla");

  // Close the window again.
  window = windowTracker.getWindow(restored.window.id);
  await BrowserTestUtils.closeWindow(window);
  // notificationCount = yield extension.awaitMessage("notificationCount");
  await assertNotificationCount(5);

  // Open and close a tab.
  let tab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    "about:robots"
  );
  await TabStateFlusher.flush(tab.linkedBrowser);
  BrowserTestUtils.removeTab(tab);
  await assertNotificationCount(6);

  // Restore the most recently closed item.
  extension.sendMessage("restore");
  await assertNotificationCount(7);
  restored = await extension.awaitMessage("restored");

  tab = restored.tab;
  ok(tab, "restore returned a tab");
  checkLocalTab(tab, "about:robots");

  // Close the tab again.
  let realTab = tabTracker.getTab(tab.id);
  BrowserTestUtils.removeTab(realTab);
  await assertNotificationCount(8);

  // Restore the tab using the sessionId.
  extension.sendMessage("check-sessions");
  recentlyClosed = await extension.awaitMessage("recentlyClosed");
  extension.sendMessage("restore", recentlyClosed[0].tab.sessionId);
  await assertNotificationCount(9);
  restored = await extension.awaitMessage("restored");

  tab = restored.tab;
  ok(tab, "restore returned a tab");
  checkLocalTab(tab, "about:robots");

  // Close the tab again.
  realTab = tabTracker.getTab(tab.id);
  BrowserTestUtils.removeTab(realTab);
  await assertNotificationCount(10);

  // Try to restore something with an invalid sessionId.
  extension.sendMessage("restore-reject");
  restored = await extension.awaitMessage("restore-rejected");

  await extension.unload();
});

add_task(async function test_sessions_event_page() {
  await SpecialPowers.pushPrefEnv({
    set: [["extensions.eventPages.enabled", true]],
  });

  let extension = ExtensionTestUtils.loadExtension({
    useAddonManager: "permanent",
    manifest: {
      browser_specific_settings: { gecko: { id: "eventpage@sessions" } },
      permissions: ["sessions", "tabs"],
      background: { persistent: false },
    },
    background() {
      browser.sessions.onChanged.addListener(() => {
        browser.test.sendMessage("changed");
      });
      browser.test.sendMessage("ready");
    },
  });

  await extension.startup();
  await extension.awaitMessage("ready");

  // test events waken background
  await extension.terminateBackground();
  let win = await BrowserTestUtils.openNewBrowserWindow();
  BrowserTestUtils.loadURIString(win.gBrowser.selectedBrowser, "about:config");
  await BrowserTestUtils.browserLoaded(win.gBrowser.selectedBrowser);
  for (let url of ["about:robots", "about:mozilla"]) {
    await BrowserTestUtils.openNewForegroundTab(win.gBrowser, url);
  }
  await BrowserTestUtils.closeWindow(win);

  await extension.awaitMessage("ready");
  await extension.awaitMessage("changed");
  ok(true, "persistent event woke background");

  await extension.unload();
  await SpecialPowers.popPrefEnv();
});