summaryrefslogtreecommitdiffstats
path: root/browser/components/extensions/test/browser/browser_ext_sessions_getRecentlyClosed_tabs.js
blob: 6c386aaf9794686ee8ded3713431bbbc618ddb6e (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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";

function expectedTabInfo(tab, window) {
  let browser = tab.linkedBrowser;
  return {
    url: browser.currentURI.spec,
    title: browser.contentTitle,
    favIconUrl: window.gBrowser.getIcon(tab) || undefined,
    // 'selected' is marked as unsupported in schema, so we've removed it.
    // For more details, see bug 1337509
    selected: undefined,
  };
}

function checkTabInfo(expected, actual) {
  for (let prop in expected) {
    is(
      actual[prop],
      expected[prop],
      `Expected value found for ${prop} of tab object.`
    );
  }
}

add_task(async function test_sessions_get_recently_closed_tabs() {
  // Below, the test makes assumptions about the last accessed time of tabs that are
  // not true is we execute fast and reduce the timer precision enough
  await SpecialPowers.pushPrefEnv({
    set: [
      ["privacy.reduceTimerPrecision", false],
      ["browser.navigation.requireUserInteraction", false],
    ],
  });

  async function background() {
    browser.test.onMessage.addListener(async msg => {
      if (msg == "check-sessions") {
        let recentlyClosed = await browser.sessions.getRecentlyClosed();
        browser.test.sendMessage("recentlyClosed", recentlyClosed);
      }
    });
  }

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

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

  // Ensure that getRecentlyClosed returns correct results after the back
  // button has been used.
  let goBackPromise = BrowserTestUtils.waitForLocationChange(
    win.gBrowser,
    "about:mozilla"
  );
  tabBrowser.goBack();
  await goBackPromise;

  let expectedTabs = [];
  let tab = win.gBrowser.selectedTab;
  // Because there is debounce logic in ContentLinkHandler.jsm to reduce the
  // favicon loads, we have to wait some time before checking that icon was
  // stored properly. If that page doesn't have favicon links, let it timeout.
  try {
    await BrowserTestUtils.waitForCondition(
      () => {
        return gBrowser.getIcon(tab) != null;
      },
      "wait for favicon load to finish",
      100,
      5
    );
  } catch (e) {
    // This page doesn't have any favicon link, just continue.
  }
  expectedTabs.push(expectedTabInfo(tab, win));
  let lastAccessedTimes = new Map();
  lastAccessedTimes.set("about:mozilla", tab.lastAccessed);

  for (let url of ["about:robots", "about:buildconfig"]) {
    tab = await BrowserTestUtils.openNewForegroundTab(win.gBrowser, url);
    try {
      await BrowserTestUtils.waitForCondition(
        () => {
          return gBrowser.getIcon(tab) != null;
        },
        "wait for favicon load to finish",
        100,
        5
      );
    } catch (e) {
      // This page doesn't have any favicon link, just continue.
    }
    expectedTabs.push(expectedTabInfo(tab, win));
    lastAccessedTimes.set(url, tab.lastAccessed);
  }

  await extension.startup();

  let sessionUpdatePromise = BrowserTestUtils.waitForSessionStoreUpdate(tab);
  // Test with a closed tab.
  BrowserTestUtils.removeTab(tab);
  await sessionUpdatePromise;

  extension.sendMessage("check-sessions");
  let recentlyClosed = await extension.awaitMessage("recentlyClosed");
  let tabInfo = recentlyClosed[0].tab;
  let expectedTab = expectedTabs.pop();
  checkTabInfo(expectedTab, tabInfo);
  ok(
    tabInfo.lastAccessed > lastAccessedTimes.get(tabInfo.url),
    "lastAccessed has been updated"
  );

  // Test with a closed window containing tabs.
  await BrowserTestUtils.closeWindow(win);

  extension.sendMessage("check-sessions");
  recentlyClosed = await extension.awaitMessage("recentlyClosed");
  let tabInfos = recentlyClosed[0].window.tabs;
  is(tabInfos.length, 2, "Expected number of tabs in closed window.");
  for (let x = 0; x < tabInfos.length; x++) {
    checkTabInfo(expectedTabs[x], tabInfos[x]);
    ok(
      tabInfos[x].lastAccessed > lastAccessedTimes.get(tabInfos[x].url),
      "lastAccessed has been updated"
    );
  }

  await extension.unload();

  // Test without tabs and host permissions.
  extension = ExtensionTestUtils.loadExtension({
    manifest: {
      permissions: ["sessions"],
    },
    background,
  });

  await extension.startup();

  extension.sendMessage("check-sessions");
  recentlyClosed = await extension.awaitMessage("recentlyClosed");
  tabInfos = recentlyClosed[0].window.tabs;
  is(tabInfos.length, 2, "Expected number of tabs in closed window.");
  for (let tabInfo of tabInfos) {
    for (let prop in expectedTabs[0]) {
      is(
        undefined,
        tabInfo[prop],
        `${prop} of tab object is undefined without tabs permission.`
      );
    }
  }

  await extension.unload();

  // Test with host permission.
  win = await BrowserTestUtils.openNewBrowserWindow();
  tabBrowser = win.gBrowser.selectedBrowser;
  BrowserTestUtils.loadURIString(tabBrowser, "http://example.com/testpage");
  await BrowserTestUtils.browserLoaded(
    tabBrowser,
    false,
    "http://example.com/testpage"
  );
  tab = win.gBrowser.getTabForBrowser(tabBrowser);
  try {
    await BrowserTestUtils.waitForCondition(
      () => {
        return gBrowser.getIcon(tab) != null;
      },
      "wait for favicon load to finish",
      100,
      5
    );
  } catch (e) {
    // This page doesn't have any favicon link, just continue.
  }
  expectedTab = expectedTabInfo(tab, win);
  await BrowserTestUtils.closeWindow(win);

  extension = ExtensionTestUtils.loadExtension({
    manifest: {
      permissions: ["sessions", "http://example.com/*"],
    },
    background,
  });
  await extension.startup();

  extension.sendMessage("check-sessions");
  recentlyClosed = await extension.awaitMessage("recentlyClosed");
  tabInfo = recentlyClosed[0].window.tabs[0];
  checkTabInfo(expectedTab, tabInfo);

  await extension.unload();
});

add_task(
  async function test_sessions_get_recently_closed_for_loading_non_web_controlled_blank_page() {
    info("Prepare extension that calls browser.sessions.getRecentlyClosed()");
    let extension = ExtensionTestUtils.loadExtension({
      manifest: {
        permissions: ["sessions", "tabs"],
      },
      background: async () => {
        browser.test.onMessage.addListener(async msg => {
          if (msg == "check-sessions") {
            let recentlyClosed = await browser.sessions.getRecentlyClosed();
            browser.test.sendMessage("recentlyClosed", recentlyClosed);
          }
        });
      },
    });

    info(
      "Open a page having a link for non web controlled page in _blank target"
    );
    const testRoot = getRootDirectory(gTestPath).replace(
      "chrome://mochitests/content",
      "https://example.com"
    );
    let url = `${testRoot}file_has_non_web_controlled_blank_page_link.html`;
    let win = await BrowserTestUtils.openNewBrowserWindow();
    BrowserTestUtils.loadURIString(win.gBrowser.selectedBrowser, url);
    await BrowserTestUtils.browserLoaded(
      win.gBrowser.selectedBrowser,
      false,
      url
    );

    info("Open the non web controlled page in _blank target");
    let onNewTabOpened = new Promise(resolve =>
      win.gBrowser.addTabsProgressListener({
        onStateChange(browser, webProgress, request, stateFlags, status) {
          if (stateFlags & Ci.nsIWebProgressListener.STATE_START) {
            win.gBrowser.removeTabsProgressListener(this);
            resolve(win.gBrowser.getTabForBrowser(browser));
          }
        },
      })
    );
    let targetUrl = await SpecialPowers.spawn(
      win.gBrowser.selectedBrowser,
      [],
      () => {
        const target = content.document.querySelector("a");
        EventUtils.synthesizeMouseAtCenter(target, {}, content);
        return target.href;
      }
    );
    let tab = await onNewTabOpened;

    info("Remove tab while loading to get getRecentlyClosed()");
    await extension.startup();
    let sessionUpdatePromise = BrowserTestUtils.waitForSessionStoreUpdate(tab);
    BrowserTestUtils.removeTab(tab);
    await sessionUpdatePromise;

    info("Check the result of getRecentlyClosed()");
    extension.sendMessage("check-sessions");
    let recentlyClosed = await extension.awaitMessage("recentlyClosed");
    checkTabInfo(
      {
        index: 1,
        url: targetUrl,
        title: targetUrl,
        favIconUrl: undefined,
        selected: undefined,
      },
      recentlyClosed[0].tab
    );

    await extension.unload();
    await BrowserTestUtils.closeWindow(win);
  }
);