summaryrefslogtreecommitdiffstats
path: root/netwerk/test/unit/test_bug826063.js
blob: 233e13a9e350f45142b86f42f51dbcf05d0ebd8a (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

/**
 * Test that nsIPrivateBrowsingChannel.isChannelPrivate yields the correct
 * result for various combinations of .setPrivate() and nsILoadContexts
 */

Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/NetUtil.jsm");


var URIs = [
  "http://example.org",
  "https://example.org",
  "ftp://example.org"
  ];

function LoadContext(usePrivateBrowsing) {
  this.usePrivateBrowsing = usePrivateBrowsing;
}
LoadContext.prototype = {
  originAttributes: {},
  QueryInterface: XPCOMUtils.generateQI([Ci.nsILoadContext, Ci.nsIInterfaceRequestor]),
  getInterface: XPCOMUtils.generateQI([Ci.nsILoadContext])
};

function getChannels() {
  for (let u of URIs) {
    yield NetUtil.newChannel({
      uri: u,
      loadUsingSystemPrincipal: true
    });
  }
}

function checkPrivate(channel, shouldBePrivate) {
  do_check_eq(channel.QueryInterface(Ci.nsIPrivateBrowsingChannel).isChannelPrivate,
              shouldBePrivate);
}

/**
 * Default configuration
 * Default is non-private
 */
add_test(function test_plain() {
  for (let c of getChannels()) {
    checkPrivate(c, false);
  }
  run_next_test();
});

/**
 * Explicitly setPrivate(true), no load context
 */
add_test(function test_setPrivate_private() {
  for (let c of getChannels()) {
    c.QueryInterface(Ci.nsIPrivateBrowsingChannel).setPrivate(true);
    checkPrivate(c, true);
  }
  run_next_test();
});

/**
 * Explicitly setPrivate(false), no load context
 */
add_test(function test_setPrivate_regular() {
  for (let c of getChannels()) {
    c.QueryInterface(Ci.nsIPrivateBrowsingChannel).setPrivate(false);
    checkPrivate(c, false);
  }
  run_next_test();
});

/**
 * Load context mandates private mode
 */
add_test(function test_LoadContextPrivate() {
  let ctx = new LoadContext(true);
  for (let c of getChannels()) {
    c.notificationCallbacks = ctx;
    checkPrivate(c, true);
  }
  run_next_test();
});

/**
 * Load context mandates regular mode
 */
add_test(function test_LoadContextRegular() {
  let ctx = new LoadContext(false);
  for (let c of getChannels()) {
    c.notificationCallbacks = ctx;
    checkPrivate(c, false);
  }
  run_next_test();
});


// Do not test simultanous uses of .setPrivate and load context.
// There is little merit in doing so, and combining both will assert in
// Debug builds anyway.


function run_test() {
    run_next_test();
}