addon-sdk/source/test/addons/private-browsing-supported/test-sidebar.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 { Loader } = require('sdk/test/loader');
     7 const { show, hide } = require('sdk/ui/sidebar/actions');
     8 const { isShowing } = require('sdk/ui/sidebar/utils');
     9 const { getMostRecentBrowserWindow, isWindowPrivate } = require('sdk/window/utils');
    10 const { open, close, focus, promise: windowPromise } = require('sdk/window/helpers');
    11 const { setTimeout } = require('sdk/timers');
    12 const { isPrivate } = require('sdk/private-browsing');
    13 const { data } = require('sdk/self');
    14 const { URL } = require('sdk/url');
    16 const { BUILTIN_SIDEBAR_MENUITEMS, isSidebarShowing,
    17         getSidebarMenuitems, getExtraSidebarMenuitems, makeID, simulateCommand,
    18         simulateClick, isChecked } = require('./sidebar/utils');
    20 exports.testSideBarIsInNewPrivateWindows = function(assert, done) {
    21   const { Sidebar } = require('sdk/ui/sidebar');
    22   let testName = 'testSideBarIsInNewPrivateWindows';
    23   let sidebar = Sidebar({
    24     id: testName,
    25     title: testName,
    26     url: 'data:text/html;charset=utf-8,'+testName
    27   });
    29   let startWindow = getMostRecentBrowserWindow();
    30   let ele = startWindow.document.getElementById(makeID(testName));
    31   assert.ok(ele, 'sidebar element was added');
    33   open(null, { features: { private: true } }).then(function(window) {
    34       let ele = window.document.getElementById(makeID(testName));
    35       assert.ok(isPrivate(window), 'the new window is private');
    36       assert.ok(!!ele, 'sidebar element was added');
    38       sidebar.destroy();
    39       assert.ok(!window.document.getElementById(makeID(testName)), 'sidebar id DNE');
    40       assert.ok(!startWindow.document.getElementById(makeID(testName)), 'sidebar id DNE');
    42       return close(window);
    43   }).then(done).then(null, assert.fail);
    44 }
    46 // Disabled in order to land other fixes, see bug 910647 for further details.
    47 /*
    48 exports.testSidebarIsOpenInNewPrivateWindow = function(assert, done) {
    49   const { Sidebar } = require('sdk/ui/sidebar');
    50   let testName = 'testSidebarIsOpenInNewPrivateWindow';
    51   let window = getMostRecentBrowserWindow();
    53   let sidebar = Sidebar({
    54     id: testName,
    55     title: testName,
    56     url: 'data:text/html;charset=utf-8,'+testName
    57   });
    59   assert.equal(isPrivate(window), false, 'the window is not private');
    61   sidebar.on('show', function() {
    62     assert.equal(isSidebarShowing(window), true, 'the sidebar is showing');
    63     assert.equal(isShowing(sidebar), true, 'the sidebar is showing');
    65     windowPromise(window.OpenBrowserWindow({private: true}), 'DOMContentLoaded').then(function(window2) {
    66       assert.equal(isPrivate(window2), true, 'the new window is private');
    68       let sidebarEle = window2.document.getElementById('sidebar');
    70       // wait for the sidebar to load something
    71       function onSBLoad() {
    72         sidebarEle.contentDocument.getElementById('web-panels-browser').addEventListener('load', function() {
    73           assert.equal(isSidebarShowing(window), true, 'the sidebar is showing in old window still');
    74           assert.equal(isSidebarShowing(window2), true, 'the sidebar is showing in the new private window');
    75           assert.equal(isShowing(sidebar), true, 'the sidebar is showing');
    77           sidebar.destroy();
    78           close(window2).then(done);
    79         }, true);
    80       }
    82       sidebarEle.addEventListener('load', onSBLoad, true);
    84       assert.pass('waiting for the sidebar to open...');
    85     }, assert.fail).then(null, assert.fail);
    86   });
    88   sidebar.show();
    89 }
    90 */
    91 // TEST: edge case where web panel is destroyed while loading
    92 exports.testDestroyEdgeCaseBugWithPrivateWindow = function(assert, done) {
    93   const { Sidebar } = require('sdk/ui/sidebar');
    94   let testName = 'testDestroyEdgeCaseBug';
    95   let window = getMostRecentBrowserWindow();
    96   let sidebar = Sidebar({
    97     id: testName,
    98     title: testName,
    99     url: 'data:text/html;charset=utf-8,'+testName
   100   });
   102   // NOTE: purposely not listening to show event b/c the event happens
   103   //       between now and then.
   104   sidebar.show();
   106   assert.equal(isPrivate(window), false, 'the new window is not private');
   107   assert.equal(isSidebarShowing(window), true, 'the sidebar is showing');
   109   //assert.equal(isShowing(sidebar), true, 'the sidebar is showing');
   111   open(null, { features: { private: true } }).then(focus).then(function(window2) {
   112     assert.equal(isPrivate(window2), true, 'the new window is private');
   113     assert.equal(isSidebarShowing(window2), false, 'the sidebar is not showing');
   114     assert.equal(isShowing(sidebar), false, 'the sidebar is not showing');
   116     sidebar.destroy();
   117     assert.pass('destroying the sidebar');
   119     close(window2).then(function() {
   120       let loader = Loader(module);
   122       assert.equal(isPrivate(window), false, 'the current window is not private');
   124       let sidebar = loader.require('sdk/ui/sidebar').Sidebar({
   125         id: testName,
   126         title: testName,
   127         url:  'data:text/html;charset=utf-8,'+ testName,
   128         onShow: function() {
   129           assert.pass('onShow works for Sidebar');
   130           loader.unload();
   132           let sidebarMI = getSidebarMenuitems();
   133           for (let mi of sidebarMI) {
   134             assert.ok(BUILTIN_SIDEBAR_MENUITEMS.indexOf(mi.getAttribute('id')) >= 0, 'the menuitem is for a built-in sidebar')
   135             assert.ok(!isChecked(mi), 'no sidebar menuitem is checked');
   136           }
   137           assert.ok(!window.document.getElementById(makeID(testName)), 'sidebar id DNE');
   138           assert.equal(isSidebarShowing(window), false, 'the sidebar is not showing');
   140           done();
   141         }
   142       })
   144       sidebar.show();
   145       assert.pass('showing the sidebar');
   146     }).then(null, assert.fail);
   147   }).then(null, assert.fail);
   148 }
   150 exports.testShowInPrivateWindow = function(assert, done) {
   151   const { Sidebar } = require('sdk/ui/sidebar');
   152   let testName = 'testShowInPrivateWindow';
   153   let window1 = getMostRecentBrowserWindow();
   154   let url = 'data:text/html;charset=utf-8,'+testName;
   156   let sidebar1 = Sidebar({
   157     id: testName,
   158     title: testName,
   159     url: url
   160   });
   161   let menuitemID = makeID(sidebar1.id);
   163   assert.equal(sidebar1.url, url, 'url getter works');
   164   assert.equal(isShowing(sidebar1), false, 'the sidebar is not showing');
   165   assert.ok(!isChecked(window1.document.getElementById(menuitemID)),
   166                'the menuitem is not checked');
   167   assert.equal(isSidebarShowing(window1), false, 'the new window sidebar is not showing');
   169   windowPromise(window1.OpenBrowserWindow({ private: true }), 'load').then(function(window) {
   170     let { document } = window;
   171     assert.equal(isWindowPrivate(window), true, 'new window is private');
   172     assert.equal(isPrivate(window), true, 'new window is private');
   174     sidebar1.show().then(
   175       function good() {
   176         assert.equal(isShowing(sidebar1), true, 'the sidebar is showing');
   177         assert.ok(!!document.getElementById(menuitemID),
   178                   'the menuitem exists on the private window');
   179         assert.equal(isSidebarShowing(window), true, 'the new window sidebar is showing');
   181         sidebar1.destroy();
   182         assert.equal(isSidebarShowing(window), false, 'the new window sidebar is showing');
   183         assert.ok(!window1.document.getElementById(menuitemID),
   184                   'the menuitem on the new window dne');
   186         // test old window state
   187         assert.equal(isSidebarShowing(window1), false, 'the old window sidebar is not showing');
   188         assert.equal(window1.document.getElementById(menuitemID),
   189                      null,
   190                      'the menuitem on the old window dne');
   192         close(window).then(done).then(null, assert.fail);
   193       },
   194       function bad() {
   195         assert.fail('a successful show should not happen here..');
   196       });
   197   }).then(null, assert.fail);
   198 }
   200 // If the module doesn't support the app we're being run in, require() will
   201 // throw.  In that case, remove all tests above from exports, and add one dummy
   202 // test that passes.
   203 try {
   204   require('sdk/ui/sidebar');
   205 }
   206 catch (err) {
   207   if (!/^Unsupported Application/.test(err.message))
   208     throw err;
   210   module.exports = {
   211     'test Unsupported Application': assert => assert.pass(err.message)
   212   }
   213 }

mercurial