addon-sdk/source/test/test-ui-sidebar-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 module.metadata = {
     7   'engines': {
     8     'Firefox': '*'
     9   }
    10 };
    12 const { Loader } = require('sdk/test/loader');
    13 const { show, hide } = require('sdk/ui/sidebar/actions');
    14 const { isShowing } = require('sdk/ui/sidebar/utils');
    15 const { getMostRecentBrowserWindow, isWindowPrivate } = require('sdk/window/utils');
    16 const { open, close, focus, promise: windowPromise } = require('sdk/window/helpers');
    17 const { setTimeout } = require('sdk/timers');
    18 const { isPrivate } = require('sdk/private-browsing');
    19 const { data } = require('sdk/self');
    20 const { URL } = require('sdk/url');
    22 const { BUILTIN_SIDEBAR_MENUITEMS, isSidebarShowing,
    23         getSidebarMenuitems, getExtraSidebarMenuitems, makeID, simulateCommand,
    24         simulateClick, isChecked } = require('./sidebar/utils');
    26 exports.testSideBarIsNotInNewPrivateWindows = function(assert, done) {
    27   const { Sidebar } = require('sdk/ui/sidebar');
    28   let testName = 'testSideBarIsNotInNewPrivateWindows';
    29   let sidebar = Sidebar({
    30     id: testName,
    31     title: testName,
    32     url: 'data:text/html;charset=utf-8,'+testName
    33   });
    35   let startWindow = getMostRecentBrowserWindow();
    36   let ele = startWindow.document.getElementById(makeID(testName));
    37   assert.ok(ele, 'sidebar element was added');
    39   open(null, { features: { private: true } }).then(function(window) {
    40       let ele = window.document.getElementById(makeID(testName));
    41       assert.ok(isPrivate(window), 'the new window is private');
    42       assert.equal(ele, null, 'sidebar element was not added');
    44       sidebar.destroy();
    45       assert.ok(!window.document.getElementById(makeID(testName)), 'sidebar id DNE');
    46       assert.ok(!startWindow.document.getElementById(makeID(testName)), 'sidebar id DNE');
    48       close(window).then(done, assert.fail);
    49   })
    50 }
    52 exports.testSidebarIsNotOpenInNewPrivateWindow = function(assert, done) {
    53   const { Sidebar } = require('sdk/ui/sidebar');
    54   let testName = 'testSidebarIsNotOpenInNewPrivateWindow';
    55   let window = getMostRecentBrowserWindow();
    57     let sidebar = Sidebar({
    58       id: testName,
    59       title: testName,
    60       url: 'data:text/html;charset=utf-8,'+testName
    61     });
    63     sidebar.on('show', function() {
    64       assert.equal(isPrivate(window), false, 'the new window is not private');
    65       assert.equal(isSidebarShowing(window), true, 'the sidebar is showing');
    66       assert.equal(isShowing(sidebar), true, 'the sidebar is showing');
    68       let window2 = window.OpenBrowserWindow({private: true});
    69       windowPromise(window2, 'load').then(focus).then(function() {
    70         // TODO: find better alt to setTimeout...
    71         setTimeout(function() {
    72           assert.equal(isPrivate(window2), true, 'the new window is private');
    73           assert.equal(isSidebarShowing(window), true, 'the sidebar is showing in old window still');
    74           assert.equal(isSidebarShowing(window2), false, 'the sidebar is not showing in the new private window');
    75           assert.equal(isShowing(sidebar), false, 'the sidebar is not showing');
    77           sidebar.destroy();
    78           close(window2).then(done);
    79         }, 500);
    80       })
    81     });
    83     sidebar.show();
    84 }
    86 // TEST: edge case where web panel is destroyed while loading
    87 exports.testDestroyEdgeCaseBugWithPrivateWindow = function(assert, done) {
    88   const { Sidebar } = require('sdk/ui/sidebar');
    89   let testName = 'testDestroyEdgeCaseBug';
    90   let window = getMostRecentBrowserWindow();
    92   let sidebar = Sidebar({
    93     id: testName,
    94     title: testName,
    95     url: 'data:text/html;charset=utf-8,'+testName
    96   });
    98   // NOTE: purposely not listening to show event b/c the event happens
    99   //       between now and then.
   100   sidebar.show();
   102   assert.equal(isPrivate(window), false, 'the new window is not private');
   103   assert.equal(isSidebarShowing(window), true, 'the sidebar is showing');
   105   //assert.equal(isShowing(sidebar), true, 'the sidebar is showing');
   107   open(null, { features: { private: true } }).then(focus).then(function(window2) {
   108     assert.equal(isPrivate(window2), true, 'the new window is private');
   109     assert.equal(isSidebarShowing(window2), false, 'the sidebar is not showing');
   110     assert.equal(isShowing(sidebar), false, 'the sidebar is not showing');
   112     sidebar.destroy();
   113     assert.pass('destroying the sidebar');
   115     close(window2).then(function() {
   116       let loader = Loader(module);
   118       assert.equal(isPrivate(window), false, 'the current window is not private');
   120       let sidebar = loader.require('sdk/ui/sidebar').Sidebar({
   121         id: testName,
   122         title: testName,
   123         url:  'data:text/html;charset=utf-8,'+ testName,
   124         onShow: function() {
   125           assert.pass('onShow works for Sidebar');
   126           loader.unload();
   128           let sidebarMI = getSidebarMenuitems();
   129           for (let mi of sidebarMI) {
   130             assert.ok(BUILTIN_SIDEBAR_MENUITEMS.indexOf(mi.getAttribute('id')) >= 0, 'the menuitem is for a built-in sidebar')
   131             assert.ok(!isChecked(mi), 'no sidebar menuitem is checked');
   132           }
   133           assert.ok(!window.document.getElementById(makeID(testName)), 'sidebar id DNE');
   134           assert.equal(isSidebarShowing(window), false, 'the sidebar is not showing');
   136           done();
   137         }
   138       })
   140       sidebar.show();
   141       assert.pass('showing the sidebar');
   143     });
   144   });
   145 }
   147 exports.testShowInPrivateWindow = function(assert, done) {
   148   const { Sidebar } = require('sdk/ui/sidebar');
   149   let testName = 'testShowInPrivateWindow';
   150   let window = getMostRecentBrowserWindow();
   151   let { document } = window;
   152   let url = 'data:text/html;charset=utf-8,'+testName;
   154   let sidebar1 = Sidebar({
   155     id: testName,
   156     title: testName,
   157     url: url
   158   });
   160   assert.equal(sidebar1.url, url, 'url getter works');
   161   assert.equal(isShowing(sidebar1), false, 'the sidebar is not showing');
   162   assert.ok(!isChecked(document.getElementById(makeID(sidebar1.id))),
   163                'the menuitem is not checked');
   164   assert.equal(isSidebarShowing(window), false, 'the new window sidebar is not showing');
   166   windowPromise(window.OpenBrowserWindow({ private: true }), 'load').then(function(window) {
   167     let { document } = window;
   168     assert.equal(isWindowPrivate(window), true, 'new window is private');
   169     assert.equal(isPrivate(window), true, 'new window is private');
   171     sidebar1.show().then(
   172       function bad() {
   173         assert.fail('a successful show should not happen here..');
   174       },
   175       function good() {
   176         assert.equal(isShowing(sidebar1), false, 'the sidebar is still not showing');
   177         assert.equal(document.getElementById(makeID(sidebar1.id)),
   178                      null,
   179                      'the menuitem dne on the private window');
   180         assert.equal(isSidebarShowing(window), false, 'the new window sidebar is not showing');
   182         sidebar1.destroy();
   183         close(window).then(done);
   184       });
   185   }, assert.fail);
   186 }
   188 // If the module doesn't support the app we're being run in, require() will
   189 // throw.  In that case, remove all tests above from exports, and add one dummy
   190 // test that passes.
   191 try {
   192   require('sdk/ui/sidebar');
   193 }
   194 catch (err) {
   195   if (!/^Unsupported Application/.test(err.message))
   196     throw err;
   198   module.exports = {
   199     'test Unsupported Application': assert => assert.pass(err.message)
   200   }
   201 }
   203 require('sdk/test').run(exports);

mercurial