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

const PAGE = "http://example.com/";

/**
 * Creates a tab in the current window worth storing in the
 * closedTabs array, and then closes it. Runs a synchronous
 * forgetFn passed in that should cause us to forget the tab,
 * and then ensures that after the tab has sent its final
 * update message that we didn't accidentally store it in
 * the closedTabs array.
 *
 * @param forgetFn (function)
 *        A synchronous function that should cause the tab
 *        to be forgotten.
 * @returns Promise
 */
let forgetTabHelper = async function (forgetFn) {
  // We want to suppress all non-final updates from the browser tabs
  // so as to eliminate any racy-ness with this test.
  await pushPrefs(["browser.sessionstore.debug.no_auto_updates", true]);

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

  is(
    ss.getClosedTabCountForWindow(window),
    0,
    "We should have 0 closed tabs being stored."
  );

  // Create a tab worth remembering.
  let tab = BrowserTestUtils.addTab(gBrowser, PAGE);
  let browser = tab.linkedBrowser;
  await BrowserTestUtils.browserLoaded(browser, false, PAGE);
  await TabStateFlusher.flush(browser);

  // Now close the tab, and immediately choose to forget it.
  let promise = promiseRemoveTabAndSessionState(tab);

  // At this point, the tab will have closed, but the final update
  // to SessionStore hasn't come up yet. Now do the operation that
  // should cause us to forget the tab.
  forgetFn();

  is(
    ss.getClosedTabCountForWindow(window),
    0,
    "Should have forgotten the closed tab"
  );

  // Now wait for the final update to come up.
  await promise;

  is(
    ss.getClosedTabCountForWindow(window),
    0,
    "Should not have stored the forgotten closed tab"
  );
};

/**
 * Creates a new window worth storing in the closeWIndows array,
 * and then closes it. Runs a synchronous forgetFn passed in that
 * should cause us to forget the window, and then ensures that after
 * the window has sent its final update message that we didn't
 * accidentally store it in the closedWindows array.
 *
 * @param forgetFn (function)
 *        A synchronous function that should cause the window
 *        to be forgotten.
 * @returns Promise
 */
let forgetWinHelper = async function (forgetFn) {
  // We want to suppress all non-final updates from the browser tabs
  // so as to eliminate any racy-ness with this test.
  await pushPrefs(["browser.sessionstore.debug.no_auto_updates", true]);

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

  is(
    ss.getClosedWindowCount(),
    0,
    "We should have 0 closed windows being stored."
  );

  let newWin = await BrowserTestUtils.openNewBrowserWindow();

  // Create a tab worth remembering.
  let tab = newWin.gBrowser.selectedTab;
  let browser = tab.linkedBrowser;
  BrowserTestUtils.loadURIString(browser, PAGE);
  await BrowserTestUtils.browserLoaded(browser, false, PAGE);
  await TabStateFlusher.flush(browser);

  // Now close the window and immediately choose to forget it.
  let windowClosed = BrowserTestUtils.windowClosed(newWin);

  let handled = false;
  whenDomWindowClosedHandled(() => {
    // At this point, the window will have closed and the onClose handler
    // has run, but the final update  to SessionStore hasn't come up yet.
    // Now do the oepration that should cause us to forget the window.
    forgetFn();

    is(ss.getClosedWindowCount(), 0, "Should have forgotten the closed window");

    handled = true;
  });

  newWin.close();

  // Now wait for the final update to come up.
  await windowClosed;

  ok(handled, "domwindowclosed should already be handled here");

  is(ss.getClosedWindowCount(), 0, "Should not have stored the closed window");
};

/**
 * Tests that if we choose to forget a tab while waiting for its
 * final flush to complete, we don't accidentally store it.
 */
add_task(async function test_forget_closed_tab() {
  await forgetTabHelper(() => {
    ss.forgetClosedTab(window, 0);
  });
});

/**
 * Tests that if we choose to forget a tab while waiting for its
 * final flush to complete, we don't accidentally store it.
 */
add_task(async function test_forget_closed_window() {
  await forgetWinHelper(() => {
    ss.forgetClosedWindow(0);
  });
});

/**
 * Tests that if we choose to purge history while waiting for a
 * final flush of a tab to complete, we don't accidentally store it.
 */
add_task(async function test_forget_purged_tab() {
  await forgetTabHelper(() => {
    Services.obs.notifyObservers(null, "browser:purge-session-history");
  });
});

/**
 * Tests that if we choose to purge history while waiting for a
 * final flush of a window to complete, we don't accidentally
 * store it.
 */
add_task(async function test_forget_purged_window() {
  await forgetWinHelper(() => {
    Services.obs.notifyObservers(null, "browser:purge-session-history");
  });
});