summaryrefslogtreecommitdiffstats
path: root/browser/components/privatebrowsing/test/browser/browser_privatebrowsing_lastpbcontextexited.js
blob: c46417933a58de0aebb2241915ca29c2d9041d9d (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

function test() {
  // We need to open a new window for this so that its docshell would get destroyed
  // when clearing the PB mode flag.
  function runTest(aCloseWindow, aCallback) {
    let newWin = OpenBrowserWindow({ private: true });
    SimpleTest.waitForFocus(function () {
      let expectedExiting = true;
      let expectedExited = false;
      let observerExiting = {
        observe(aSubject, aTopic, aData) {
          is(
            aTopic,
            "last-pb-context-exiting",
            "Correct topic should be dispatched (exiting)"
          );
          is(expectedExiting, true, "notification not expected yet (exiting)");
          expectedExited = true;
          Services.obs.removeObserver(
            observerExiting,
            "last-pb-context-exiting"
          );
        },
      };
      let observerExited = {
        observe(aSubject, aTopic, aData) {
          is(
            aTopic,
            "last-pb-context-exited",
            "Correct topic should be dispatched (exited)"
          );
          is(expectedExited, true, "notification not expected yet (exited)");
          Services.obs.removeObserver(observerExited, "last-pb-context-exited");
          aCallback();
        },
      };
      Services.obs.addObserver(observerExiting, "last-pb-context-exiting");
      Services.obs.addObserver(observerExited, "last-pb-context-exited");
      expectedExiting = true;
      aCloseWindow(newWin);
      newWin = null;
      SpecialPowers.forceGC();
    }, newWin);
  }

  waitForExplicitFinish();

  runTest(
    function (newWin) {
      // Simulate pressing the window close button
      newWin.document.getElementById("cmd_closeWindow").doCommand();
    },
    function () {
      runTest(function (newWin) {
        // Simulate closing the last tab
        newWin.document.getElementById("cmd_close").doCommand();
      }, finish);
    }
  );
}