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

/**
 * This test checks that pending tabs are treated like fully loaded tabs when
 * purging session history. Just like for fully loaded tabs we want to remove
 * every but the current shistory entry.
 */

const TAB_STATE = {
  entries: [
    { url: "about:mozilla", triggeringPrincipal_base64 },
    { url: "about:robots", triggeringPrincipal_base64 },
  ],
  index: 1,
};

function checkTabContents(browser) {
  return SpecialPowers.spawn(browser, [], async function () {
    let webNavigation = docShell.QueryInterface(Ci.nsIWebNavigation);
    let history = webNavigation.sessionHistory;
    Assert.ok(
      history &&
        history.count == 1 &&
        content.document.documentURI == "about:mozilla",
      "expected tab contents found"
    );
  });
}

add_task(async function () {
  // Create a new tab.
  let tab = BrowserTestUtils.addTab(gBrowser, "about:blank");
  let browser = tab.linkedBrowser;
  await promiseBrowserLoaded(browser);
  await promiseTabState(tab, TAB_STATE);

  // Create another new tab.
  let tab2 = BrowserTestUtils.addTab(gBrowser, "about:blank");
  let browser2 = tab2.linkedBrowser;
  await promiseBrowserLoaded(browser2);

  // The tab shouldn't be restored right away.
  Services.prefs.setBoolPref("browser.sessionstore.restore_on_demand", true);

  // Prepare the tab state.
  let promise = promiseTabRestoring(tab2);
  ss.setTabState(tab2, JSON.stringify(TAB_STATE));
  ok(tab2.hasAttribute("pending"), "tab is pending");
  await promise;

  // Purge session history.
  Services.obs.notifyObservers(null, "browser:purge-session-history");
  await checkTabContents(browser);
  ok(tab2.hasAttribute("pending"), "tab is still pending");

  // Kick off tab restoration.
  gBrowser.selectedTab = tab2;
  await promiseTabRestored(tab2);
  await checkTabContents(browser2);
  ok(!tab2.hasAttribute("pending"), "tab is not pending anymore");

  // Cleanup.
  gBrowser.removeTab(tab2);
  gBrowser.removeTab(tab);
});