addon-sdk/source/test/test-private-browsing.js

branch
TOR_BUG_9701
changeset 10
ac0c01689b40
equal deleted inserted replaced
-1:000000000000 0:7c226751a452
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 'use strict';
5
6 const { Ci, Cu } = require('chrome');
7 const { safeMerge } = require('sdk/util/object');
8 const windows = require('sdk/windows').browserWindows;
9 const tabs = require('sdk/tabs');
10 const winUtils = require('sdk/window/utils');
11 const { isWindowPrivate } = winUtils;
12 const { isPrivateBrowsingSupported } = require('sdk/self');
13 const { is } = require('sdk/system/xul-app');
14 const { isPrivate } = require('sdk/private-browsing');
15 const { getOwnerWindow } = require('sdk/private-browsing/window/utils');
16 const { LoaderWithHookedConsole } = require("sdk/test/loader");
17 const { getMode, isGlobalPBSupported,
18 isWindowPBSupported, isTabPBSupported } = require('sdk/private-browsing/utils');
19 const { pb } = require('./private-browsing/helper');
20 const prefs = require('sdk/preferences/service');
21 const { set: setPref } = require("sdk/preferences/service");
22 const DEPRECATE_PREF = "devtools.errorconsole.deprecation_warnings";
23
24 const { Services } = Cu.import("resource://gre/modules/Services.jsm", {});
25
26 const kAutoStartPref = "browser.privatebrowsing.autostart";
27
28 // is global pb is enabled?
29 if (isGlobalPBSupported) {
30 safeMerge(module.exports, require('./private-browsing/global'));
31
32 exports.testGlobalOnlyOnFirefox = function(assert) {
33 assert.ok(is("Firefox"), "isGlobalPBSupported is only true on Firefox");
34 }
35 }
36 else if (isWindowPBSupported) {
37 safeMerge(module.exports, require('./private-browsing/windows'));
38
39 exports.testPWOnlyOnFirefox = function(assert) {
40 assert.ok(is("Firefox"), "isWindowPBSupported is only true on Firefox");
41 }
42 }
43 // only on Fennec
44 else if (isTabPBSupported) {
45 safeMerge(module.exports, require('./private-browsing/tabs'));
46
47 exports.testPTOnlyOnFennec = function(assert) {
48 assert.ok(is("Fennec"), "isTabPBSupported is only true on Fennec");
49 }
50 }
51
52 exports.testIsPrivateDefaults = function(assert) {
53 assert.equal(isPrivate(), false, 'undefined is not private');
54 assert.equal(isPrivate('test'), false, 'strings are not private');
55 assert.equal(isPrivate({}), false, 'random objects are not private');
56 assert.equal(isPrivate(4), false, 'numbers are not private');
57 assert.equal(isPrivate(/abc/), false, 'regex are not private');
58 assert.equal(isPrivate(function() {}), false, 'functions are not private');
59 };
60
61 exports.testWindowDefaults = function(assert) {
62 setPref(DEPRECATE_PREF, true);
63 // Ensure that browserWindow still works while being deprecated
64 let { loader, messages } = LoaderWithHookedConsole(module);
65 let windows = loader.require("sdk/windows").browserWindows;
66 assert.equal(windows.activeWindow.isPrivateBrowsing, false,
67 'window is not private browsing by default');
68 assert.ok(/DEPRECATED.+isPrivateBrowsing/.test(messages[0].msg),
69 'isPrivateBrowsing is deprecated');
70
71 let chromeWin = winUtils.getMostRecentBrowserWindow();
72 assert.equal(getMode(chromeWin), false);
73 assert.equal(isWindowPrivate(chromeWin), false);
74 };
75
76 // tests for the case where private browsing doesn't exist
77 exports.testIsActiveDefault = function(assert) {
78 assert.equal(pb.isActive, false,
79 'pb.isActive returns false when private browsing isn\'t supported');
80 };
81
82 exports.testIsPrivateBrowsingFalseDefault = function(assert) {
83 assert.equal(isPrivateBrowsingSupported, false,
84 'isPrivateBrowsingSupported property is false by default');
85 };
86
87 exports.testGetOwnerWindow = function(assert, done) {
88 let window = windows.activeWindow;
89 let chromeWindow = getOwnerWindow(window);
90 assert.ok(chromeWindow instanceof Ci.nsIDOMWindow, 'associated window is found');
91
92 tabs.open({
93 url: 'about:blank',
94 isPrivate: true,
95 onOpen: function(tab) {
96 // test that getOwnerWindow works as expected
97 if (is('Fennec')) {
98 assert.notStrictEqual(chromeWindow, getOwnerWindow(tab));
99 assert.ok(getOwnerWindow(tab) instanceof Ci.nsIDOMWindow);
100 }
101 else {
102 assert.strictEqual(chromeWindow, getOwnerWindow(tab), 'associated window is the same for window and window\'s tab');
103 }
104
105 // test that the tab is not private
106 // private flag should be ignored by default
107 assert.ok(!isPrivate(tab));
108 assert.ok(!isPrivate(getOwnerWindow(tab)));
109
110 tab.close(done);
111 }
112 });
113 };
114
115 exports.testNSIPrivateBrowsingChannel = function(assert) {
116 let channel = Services.io.newChannel("about:blank", null, null);
117 channel.QueryInterface(Ci.nsIPrivateBrowsingChannel);
118 assert.equal(isPrivate(channel), false, 'isPrivate detects non-private channels');
119 channel.setPrivate(true);
120 assert.ok(isPrivate(channel), 'isPrivate detects private channels');
121 }
122
123 exports.testNewGlobalPBService = function(assert) {
124 assert.equal(isPrivate(), false, 'isPrivate() is false by default');
125 prefs.set(kAutoStartPref, true);
126 assert.equal(prefs.get(kAutoStartPref, false), true, kAutoStartPref + ' is true now');
127 assert.equal(isPrivate(), true, 'isPrivate() is true now');
128 prefs.set(kAutoStartPref, false);
129 assert.equal(isPrivate(), false, 'isPrivate() is false again');
130 };
131
132 require('sdk/test').run(exports);

mercurial