summaryrefslogtreecommitdiff
path: root/system/docshell/test/unit/test_setUsePrivateBrowsing.js
blob: 7391868954068f517993c513418a0a783cc3a37b (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
"use strict";

const {utils: Cu} = Components;

Cu.import("resource://gre/modules/AppConstants.jsm");
Cu.import("resource://gre/modules/Services.jsm");

add_task(function*() {
  let webNav = Services.appShell.createWindowlessBrowser(false);

  let loadContext = webNav.QueryInterface(Ci.nsIInterfaceRequestor)
                          .getInterface(Ci.nsILoadContext);

  let docShell = webNav.getInterface(Ci.nsIDocShell);

  equal(loadContext.usePrivateBrowsing, false, "Should start out in non-private mode");

  loadContext.usePrivateBrowsing = true;
  equal(loadContext.usePrivateBrowsing, true,
        "Should be able to change to private mode prior to a document load");

  loadContext.usePrivateBrowsing = false;
  equal(loadContext.usePrivateBrowsing, false,
        "Should be able to change to non-private mode prior to a document load");

  let oa = docShell.getOriginAttributes();

  oa.privateBrowsingId = 1;
  docShell.setOriginAttributes(oa);

  equal(loadContext.usePrivateBrowsing, true,
        "Should be able to change origin attributes prior to a document load");

  oa.privateBrowsingId = 0;
  docShell.setOriginAttributes(oa);

  equal(loadContext.usePrivateBrowsing, false,
        "Should be able to change origin attributes prior to a document load");

  webNav.loadURI("data:text/html,", webNav.LOAD_FLAGS_NONE, null, null, null);

  // Return to the event loop so the load can begin.
  yield new Promise(do_execute_soon);

  // This causes a failed assertion rather than an exception on debug
  // builds.
  if (!AppConstants.DEBUG) {
    Assert.throws(() => { loadContext.usePrivateBrowsing = true; },
                  /NS_ERROR_FAILURE/,
                  "Should not be able to change private browsing state after initial load has started");

    oa.privateBrowsingId = 1;
    Assert.throws(() => { docShell.setOriginAttributes(oa); },
                  /NS_ERROR_FAILURE/,
                  "Should not be able to change origin attributes after initial load has started");

    equal(loadContext.usePrivateBrowsing, false,
          "Should not be able to change private browsing state after initial load has started");

    loadContext.usePrivateBrowsing = false;
    ok(true, "Should be able to set usePrivateBrowsing to its current value even after initial load");
  }

  webNav.close();
});