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

/**
 * This test is for the sessionstore-closed-objects-changed notifications.
 */

const MAX_TABS_UNDO_PREF = "browser.sessionstore.max_tabs_undo";
const TOPIC = "sessionstore-closed-objects-changed";

let notificationsCount = 0;

async function openWindow(url) {
  let win = await promiseNewWindowLoaded();
  let flags = Ci.nsIWebNavigation.LOAD_FLAGS_REPLACE_HISTORY;
  BrowserTestUtils.loadURIString(win.gBrowser.selectedBrowser, url, { flags });
  await promiseBrowserLoaded(win.gBrowser.selectedBrowser, true, url);
  return win;
}

async function closeWindow(win) {
  await awaitNotification(() => BrowserTestUtils.closeWindow(win));
}

async function openAndCloseWindow(url) {
  let win = await openWindow(url);
  await closeWindow(win);
}

async function openTab(window, url) {
  let tab = await BrowserTestUtils.openNewForegroundTab(window.gBrowser, url);
  await TabStateFlusher.flush(tab.linkedBrowser);
  return tab;
}

async function openAndCloseTab(window, url) {
  let tab = await openTab(window, url);
  await promiseRemoveTabAndSessionState(tab);
}

function countingObserver() {
  notificationsCount++;
}

function assertNotificationCount(count) {
  is(
    notificationsCount,
    count,
    "The expected number of notifications was received."
  );
}

async function awaitNotification(callback) {
  let notification = TestUtils.topicObserved(TOPIC);
  executeSoon(callback);
  await notification;
}

add_task(async function test_closedObjectsChangedNotifications() {
  // Create a closed window so that when we do the purge we can expect a notification.
  await openAndCloseWindow("about:robots");

  // Forget any previous closed windows or tabs from other tests that may have
  // run in the same session.
  await awaitNotification(() =>
    Services.obs.notifyObservers(null, "browser:purge-session-history")
  );

  // Add an observer to count the number of notifications.
  Services.obs.addObserver(countingObserver, TOPIC);

  // Open a new window.
  let win = await openWindow("about:robots");

  info("Opening and closing a tab.");
  await openAndCloseTab(win, "about:mozilla");
  assertNotificationCount(1);

  info("Opening and closing a second tab.");
  await openAndCloseTab(win, "about:mozilla");
  assertNotificationCount(2);

  info(`Changing the ${MAX_TABS_UNDO_PREF} pref.`);
  registerCleanupFunction(function () {
    Services.prefs.clearUserPref(MAX_TABS_UNDO_PREF);
  });
  await awaitNotification(() =>
    Services.prefs.setIntPref(MAX_TABS_UNDO_PREF, 1)
  );
  assertNotificationCount(3);

  info("Undoing close of remaining closed tab.");
  let tab = SessionStore.undoCloseTab(win, 0);
  await promiseTabRestored(tab);
  assertNotificationCount(4);

  info("Closing tab again.");
  await promiseRemoveTabAndSessionState(tab);
  assertNotificationCount(5);

  info("Purging session history.");
  await awaitNotification(() =>
    Services.obs.notifyObservers(null, "browser:purge-session-history")
  );
  assertNotificationCount(6);

  info("Opening and closing another tab.");
  await openAndCloseTab(win, "http://example.com/");
  assertNotificationCount(7);

  info("Purging domain data with no matches.");
  Services.obs.notifyObservers(
    null,
    "browser:purge-session-history-for-domain",
    "mozilla.com"
  );
  assertNotificationCount(7);

  info("Purging domain data with matches.");
  await awaitNotification(() =>
    Services.obs.notifyObservers(
      null,
      "browser:purge-session-history-for-domain",
      "example.com"
    )
  );
  assertNotificationCount(8);

  info("Opening and closing another tab.");
  await openAndCloseTab(win, "http://example.com/");
  assertNotificationCount(9);

  await closeWindow(win);
  assertNotificationCount(10);

  Services.obs.removeObserver(countingObserver, TOPIC);
});