addon-sdk/source/test/addons/private-browsing-supported/test-private-browsing.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.

     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';
     6 const { Ci } = require('chrome');
     7 const { isPrivateBrowsingSupported } = require('sdk/self');
     8 const tabs = require('sdk/tabs');
     9 const { browserWindows: windows } = require('sdk/windows');
    10 const { isPrivate } = require('sdk/private-browsing');
    11 const { getOwnerWindow } = require('sdk/private-browsing/window/utils');
    12 const { is } = require('sdk/system/xul-app');
    13 const { isWindowPBSupported, isTabPBSupported } = require('sdk/private-browsing/utils');
    15 const TAB_URL = 'data:text/html;charset=utf-8,TEST-TAB';
    17 exports.testIsPrivateBrowsingTrue = function(assert) {
    18   assert.ok(isPrivateBrowsingSupported,
    19             'isPrivateBrowsingSupported property is true');
    20 };
    22 // test tab.open with isPrivate: true
    23 // test isPrivate on a tab
    24 // test getOwnerWindow on windows and tabs
    25 exports.testGetOwnerWindow = function(assert, done) {
    26   let window = windows.activeWindow;
    27   let chromeWindow = getOwnerWindow(window);
    28   assert.ok(chromeWindow instanceof Ci.nsIDOMWindow, 'associated window is found');
    30   tabs.open({
    31     url: 'about:blank',
    32     isPrivate: true,
    33     onOpen: function(tab) {
    34       // test that getOwnerWindow works as expected
    35       if (is('Fennec')) {
    36         assert.notStrictEqual(chromeWindow, getOwnerWindow(tab));
    37         assert.ok(getOwnerWindow(tab) instanceof Ci.nsIDOMWindow);
    38       }
    39       else {
    40         if (isWindowPBSupported) {
    41           assert.notStrictEqual(chromeWindow,
    42                                 getOwnerWindow(tab),
    43                                 'associated window is not the same for window and window\'s tab');
    44         }
    45         else {
    46           assert.strictEqual(chromeWindow,
    47                             getOwnerWindow(tab),
    48                             'associated window is the same for window and window\'s tab');
    49         }
    50       }
    52       let pbSupported = isTabPBSupported || isWindowPBSupported;
    54       // test that the tab is private if it should be
    55       assert.equal(isPrivate(tab), pbSupported);
    56       assert.equal(isPrivate(getOwnerWindow(tab)), pbSupported);
    58       tab.close(function() done());
    59     }
    60   });
    61 };
    63 // test that it is possible to open a private tab
    64 exports.testTabOpenPrivate = function(assert, done) {
    65   tabs.open({
    66     url: TAB_URL,
    67     isPrivate: true,
    68     onReady: function(tab) {
    69       assert.equal(tab.url, TAB_URL, 'opened correct tab');
    70       assert.equal(isPrivate(tab), (isWindowPBSupported || isTabPBSupported));
    72       tab.close(function() {
    73         done();
    74       });
    75     }
    76   });
    77 }
    80 // test that it is possible to open a non private tab
    81 exports.testTabOpenPrivateDefault = function(assert, done) {
    82   tabs.open({
    83     url: TAB_URL,
    84     onReady: function(tab) {
    85       assert.equal(tab.url, TAB_URL, 'opened correct tab');
    86       assert.equal(isPrivate(tab), false);
    88       tab.close(function() {
    89         done();
    90       });
    91     }
    92   });
    93 }
    95 // test that it is possible to open a non private tab in explicit case
    96 exports.testTabOpenPrivateOffExplicit = function(assert, done) {
    97   tabs.open({
    98     url: TAB_URL,
    99     isPrivate: false,
   100     onReady: function(tab) {
   101       assert.equal(tab.url, TAB_URL, 'opened correct tab');
   102       assert.equal(isPrivate(tab), false);
   104       tab.close(function() {
   105         done();
   106       });
   107     }
   108   });
   109 }
   111 // test windows.open with isPrivate: true
   112 // test isPrivate on a window
   113 if (!is('Fennec')) {
   114   // test that it is possible to open a private window
   115   exports.testWindowOpenPrivate = function(assert, done) {
   116     windows.open({
   117       url: TAB_URL,
   118       isPrivate: true,
   119       onOpen: function(window) {
   120         let tab = window.tabs[0];
   121         tab.once('ready', function() {
   122           assert.equal(tab.url, TAB_URL, 'opened correct tab');
   123           assert.equal(isPrivate(tab), isWindowPBSupported, 'tab is private');
   125           window.close(function() {
   126             done();
   127           });
   128         });
   129       }
   130     });
   131   };
   133   exports.testIsPrivateOnWindowOn = function(assert, done) {
   134     windows.open({
   135       isPrivate: true,
   136       onOpen: function(window) {
   137         assert.equal(isPrivate(window), isWindowPBSupported, 'isPrivate for a window is true when it should be');
   138         assert.equal(isPrivate(window.tabs[0]), isWindowPBSupported, 'isPrivate for a tab is false when it should be');
   139         window.close(done);
   140       }
   141     });
   142   };
   144   exports.testIsPrivateOnWindowOffImplicit = function(assert, done) {
   145     windows.open({
   146       onOpen: function(window) {
   147         assert.equal(isPrivate(window), false, 'isPrivate for a window is false when it should be');
   148         assert.equal(isPrivate(window.tabs[0]), false, 'isPrivate for a tab is false when it should be');
   149         window.close(done);
   150       }
   151     })
   152   }
   154   exports.testIsPrivateOnWindowOffExplicit = function(assert, done) {
   155     windows.open({
   156       isPrivate: false,
   157       onOpen: function(window) {
   158         assert.equal(isPrivate(window), false, 'isPrivate for a window is false when it should be');
   159         assert.equal(isPrivate(window.tabs[0]), false, 'isPrivate for a tab is false when it should be');
   160         window.close(done);
   161       }
   162     })
   163   }
   164 }

mercurial