summaryrefslogtreecommitdiffstats
path: root/browser/components/sessionstore/test/browser_should_restore_tab.js
blob: ab9513083ad7acf8be65a0a42a6a3f05575445c1 (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
const NOTIFY_CLOSED_OBJECTS_CHANGED = "sessionstore-closed-objects-changed";

add_setup(async () => {
  registerCleanupFunction(async () => {
    Services.obs.notifyObservers(null, "browser:purge-session-history");
  });
});

async function check_tab_close_notification(openedTab, expectNotification) {
  let tabUrl = openedTab.linkedBrowser.currentURI.spec;
  let win = openedTab.ownerGlobal;
  let initialTabCount = SessionStore.getClosedTabCountForWindow(win);

  let tabClosed = BrowserTestUtils.waitForTabClosing(openedTab);
  let notified = false;
  function topicObserver(_, topic) {
    notified = true;
  }
  Services.obs.addObserver(topicObserver, NOTIFY_CLOSED_OBJECTS_CHANGED);

  BrowserTestUtils.removeTab(openedTab);
  await tabClosed;
  // SessionStore does a setTimeout(notify, 0) to notifyObservers when it handles TabClose
  // We need to wait long enough to be confident the observer would have been notified
  // if it was going to be.
  let ticks = 0;
  await TestUtils.waitForCondition(() => {
    return ++ticks > 1;
  });

  Services.obs.removeObserver(topicObserver, NOTIFY_CLOSED_OBJECTS_CHANGED);
  if (expectNotification) {
    Assert.ok(
      notified,
      `Expected ${NOTIFY_CLOSED_OBJECTS_CHANGED} when the ${tabUrl} tab closed`
    );
    Assert.equal(
      SessionStore.getClosedTabCountForWindow(win),
      initialTabCount + 1,
      "Expected closed tab count to have incremented"
    );
  } else {
    Assert.ok(
      !notified,
      `Expected no ${NOTIFY_CLOSED_OBJECTS_CHANGED} when the ${tabUrl} tab closed`
    );
    Assert.equal(
      SessionStore.getClosedTabCountForWindow(win),
      initialTabCount,
      "Expected closed tab count to have not changed"
    );
  }
}

add_task(async function test_control_case() {
  let win = window;
  let tabOpenedAndSwitchedTo = BrowserTestUtils.switchTab(
    win.gBrowser,
    () => {}
  );
  let tab = await BrowserTestUtils.openNewForegroundTab(
    win.gBrowser,
    "https://example.com/"
  );
  await tabOpenedAndSwitchedTo;
  await check_tab_close_notification(tab, true);
});

add_task(async function test_about_new_tab() {
  let win = window;
  let tabOpenedAndSwitchedTo = BrowserTestUtils.switchTab(
    win.gBrowser,
    () => {}
  );
  // This opens about:newtab:
  win.BrowserOpenTab();
  let tab = await tabOpenedAndSwitchedTo;
  await check_tab_close_notification(tab, false);
});

add_task(async function test_about_home() {
  let win = window;
  let tabOpenedAndSwitchedTo = BrowserTestUtils.switchTab(
    win.gBrowser,
    () => {}
  );
  let tab = await BrowserTestUtils.openNewForegroundTab(
    win.gBrowser,
    "about:home"
  );
  await tabOpenedAndSwitchedTo;
  await check_tab_close_notification(tab, false);
});

add_task(async function test_navigated_about_home() {
  let win = window;
  let tabOpenedAndSwitchedTo = BrowserTestUtils.switchTab(
    win.gBrowser,
    () => {}
  );
  let tab = await BrowserTestUtils.openNewForegroundTab(
    win.gBrowser,
    "https://example.com/"
  );
  await tabOpenedAndSwitchedTo;
  BrowserTestUtils.startLoadingURIString(tab.linkedBrowser, "about:home");
  await BrowserTestUtils.browserLoaded(tab.linkedBrowser);
  // even if we end up on an ignorable URL,
  // if there's meaningful history, we should save this tab
  await check_tab_close_notification(tab, true);
});

add_task(async function test_about_blank() {
  let win = window;
  let tabOpenedAndSwitchedTo = BrowserTestUtils.switchTab(
    win.gBrowser,
    () => {}
  );
  let tab = await BrowserTestUtils.openNewForegroundTab(
    win.gBrowser,
    "about:blank"
  );
  await tabOpenedAndSwitchedTo;
  await check_tab_close_notification(tab, false);
});

add_task(async function test_about_privatebrowsing() {
  let win = window;
  let tabOpenedAndSwitchedTo = BrowserTestUtils.switchTab(
    win.gBrowser,
    () => {}
  );
  let tab = await BrowserTestUtils.openNewForegroundTab(
    win.gBrowser,
    "about:privatebrowsing"
  );
  await tabOpenedAndSwitchedTo;
  await check_tab_close_notification(tab, false);
});