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

Thu, 15 Jan 2015 15:59:08 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:59:08 +0100
branch
TOR_BUG_9701
changeset 10
ac0c01689b40
permissions
-rw-r--r--

Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4 'use strict';
michael@0 5
michael@0 6 const { pb, pbUtils } = require('./helper');
michael@0 7 const { onFocus, openDialog, open } = require('sdk/window/utils');
michael@0 8 const { open: openPromise, close, focus, promise } = require('sdk/window/helpers');
michael@0 9 const { isPrivate } = require('sdk/private-browsing');
michael@0 10 const { browserWindows: windows } = require('sdk/windows');
michael@0 11 const { defer } = require('sdk/core/promise');
michael@0 12 const tabs = require('sdk/tabs');
michael@0 13
michael@0 14 // test openDialog() from window/utils with private option
michael@0 15 // test isActive state in pwpb case
michael@0 16 // test isPrivate on ChromeWindow
michael@0 17 exports.testPerWindowPrivateBrowsingGetter = function(assert, done) {
michael@0 18 let win = openDialog({
michael@0 19 private: true
michael@0 20 });
michael@0 21
michael@0 22 promise(win, 'DOMContentLoaded').then(function onload() {
michael@0 23 assert.equal(pbUtils.getMode(win),
michael@0 24 true, 'Newly opened window is in PB mode');
michael@0 25 assert.ok(isPrivate(win), 'isPrivate(window) is true');
michael@0 26 assert.equal(pb.isActive, false, 'PB mode is not active');
michael@0 27
michael@0 28 close(win).then(function() {
michael@0 29 assert.equal(pb.isActive, false, 'PB mode is not active');
michael@0 30 done();
michael@0 31 });
michael@0 32 });
michael@0 33 }
michael@0 34
michael@0 35 // test open() from window/utils with private feature
michael@0 36 // test isActive state in pwpb case
michael@0 37 // test isPrivate on ChromeWindow
michael@0 38 exports.testPerWindowPrivateBrowsingGetter = function(assert, done) {
michael@0 39 let win = open('chrome://browser/content/browser.xul', {
michael@0 40 features: {
michael@0 41 private: true
michael@0 42 }
michael@0 43 });
michael@0 44
michael@0 45 promise(win, 'DOMContentLoaded').then(function onload() {
michael@0 46 assert.equal(pbUtils.getMode(win),
michael@0 47 true, 'Newly opened window is in PB mode');
michael@0 48 assert.ok(isPrivate(win), 'isPrivate(window) is true');
michael@0 49 assert.equal(pb.isActive, false, 'PB mode is not active');
michael@0 50
michael@0 51 close(win).then(function() {
michael@0 52 assert.equal(pb.isActive, false, 'PB mode is not active');
michael@0 53 done();
michael@0 54 });
michael@0 55 });
michael@0 56 }
michael@0 57
michael@0 58 exports.testIsPrivateOnWindowOpen = function(assert, done) {
michael@0 59 windows.open({
michael@0 60 isPrivate: true,
michael@0 61 onOpen: function(window) {
michael@0 62 assert.equal(isPrivate(window), false, 'isPrivate for a window is true when it should be');
michael@0 63 assert.equal(isPrivate(window.tabs[0]), false, 'isPrivate for a tab is false when it should be');
michael@0 64 window.close(done);
michael@0 65 }
michael@0 66 });
michael@0 67 }
michael@0 68
michael@0 69 exports.testIsPrivateOnWindowOpenFromPrivate = function(assert, done) {
michael@0 70 // open a private window
michael@0 71 openPromise(null, {
michael@0 72 features: {
michael@0 73 private: true,
michael@0 74 chrome: true,
michael@0 75 titlebar: true,
michael@0 76 toolbar: true
michael@0 77 }
michael@0 78 }).then(focus).then(function(window) {
michael@0 79 let { promise, resolve } = defer();
michael@0 80
michael@0 81 assert.equal(isPrivate(window), true, 'the only open window is private');
michael@0 82
michael@0 83 windows.open({
michael@0 84 url: 'about:blank',
michael@0 85 onOpen: function(w) {
michael@0 86 assert.equal(isPrivate(w), false, 'new test window is not private');
michael@0 87 w.close(function() resolve(window));
michael@0 88 }
michael@0 89 });
michael@0 90
michael@0 91 return promise;
michael@0 92 }).then(close).
michael@0 93 then(done, assert.fail);
michael@0 94 };
michael@0 95
michael@0 96 exports.testOpenTabWithPrivateWindow = function(assert, done) {
michael@0 97 function start() {
michael@0 98 openPromise(null, {
michael@0 99 features: {
michael@0 100 private: true,
michael@0 101 toolbar: true
michael@0 102 }
michael@0 103 }).then(focus).then(function(window) {
michael@0 104 let { promise, resolve } = defer();
michael@0 105 assert.equal(isPrivate(window), true, 'the focused window is private');
michael@0 106
michael@0 107 tabs.open({
michael@0 108 url: 'about:blank',
michael@0 109 onOpen: function(tab) {
michael@0 110 assert.equal(isPrivate(tab), false, 'the opened tab is not private');
michael@0 111 // not closing this tab on purpose.. for now...
michael@0 112 // we keep this tab open because we closed all windows
michael@0 113 // and must keep a non-private window open at end of this test for next ones.
michael@0 114 resolve(window);
michael@0 115 }
michael@0 116 });
michael@0 117
michael@0 118 return promise;
michael@0 119 }).then(close).then(done, assert.fail);
michael@0 120 }
michael@0 121
michael@0 122 (function closeWindows() {
michael@0 123 if (windows.length > 0) {
michael@0 124 return windows.activeWindow.close(closeWindows);
michael@0 125 }
michael@0 126 assert.pass('all pre test windows have been closed');
michael@0 127 return start();
michael@0 128 })()
michael@0 129 };
michael@0 130
michael@0 131 exports.testIsPrivateOnWindowOff = function(assert, done) {
michael@0 132 windows.open({
michael@0 133 onOpen: function(window) {
michael@0 134 assert.equal(isPrivate(window), false, 'isPrivate for a window is false when it should be');
michael@0 135 assert.equal(isPrivate(window.tabs[0]), false, 'isPrivate for a tab is false when it should be');
michael@0 136 window.close(done);
michael@0 137 }
michael@0 138 })
michael@0 139 }

mercurial