summaryrefslogtreecommitdiffstats
path: root/toolkit/components/places/tests/browser/browser_visituri_privatebrowsing_perwindowpb.js
blob: 2d170115f646b8f1139539f10698c7f056a457cc (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/. */

const initialURL =
  "http://example.com/tests/toolkit/components/places/tests/browser/begin.html";
const finalURL =
  "http://test1.example.com/tests/toolkit/components/places/tests/browser/final.html";

var observer;
var visitSavedPromise;

add_setup(async function () {
  visitSavedPromise = new Promise(resolve => {
    observer = {
      observe(subject, topic) {
        // The uri-visit-saved topic should only work when on normal mode.
        if (topic == "uri-visit-saved") {
          Services.obs.removeObserver(observer, "uri-visit-saved");

          // The expected visit should be the finalURL because private mode
          // should not register a visit with the initialURL.
          let uri = subject.QueryInterface(Ci.nsIURI);
          resolve(uri.spec);
        }
      },
    };
  });

  Services.obs.addObserver(observer, "uri-visit-saved");

  registerCleanupFunction(async function () {
    await PlacesUtils.history.clear();
  });
});

// Note: The private window test must be the first one to run, since we'll listen
// to the first uri-visit-saved notification, and we expect this test to not
// fire any, so we'll just find the non-private window test notification.
add_task(async function test_private_browsing_window() {
  await testLoadInWindow({ private: true }, initialURL);
});

add_task(async function test_normal_window() {
  await testLoadInWindow({ private: false }, finalURL);

  let url = await visitSavedPromise;
  Assert.equal(url, finalURL, "Check received expected visit");
});

async function testLoadInWindow(options, url) {
  let win = await BrowserTestUtils.openNewBrowserWindow(options);

  registerCleanupFunction(async function () {
    await BrowserTestUtils.closeWindow(win);
  });

  let loadedPromise = BrowserTestUtils.browserLoaded(
    win.gBrowser.selectedBrowser
  );
  BrowserTestUtils.startLoadingURIString(win.gBrowser.selectedBrowser, url);
  await loadedPromise;
}