test-global-private-browsing.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. const windowUtils = require('sdk/deprecated/window-utils');
  6. const { isWindowPBSupported, isGlobalPBSupported } = require('sdk/private-browsing/utils');
  7. const { getFrames, getWindowTitle, onFocus, isWindowPrivate, windows, isBrowser } = require('sdk/window/utils');
  8. const { open, close, focus } = require('sdk/window/helpers');
  9. const { isPrivate } = require('sdk/private-browsing');
  10. const { Panel } = require('sdk/panel');
  11. const { Widget } = require('sdk/widget');
  12. const { fromIterator: toArray } = require('sdk/util/array');
  13. let { Loader } = require('sdk/test/loader');
  14. let loader = Loader(module, {
  15. console: Object.create(console, {
  16. error: {
  17. value: function(e) !/DEPRECATED:/.test(e) ? console.error(e) : undefined
  18. }
  19. })
  20. });
  21. const pb = loader.require('sdk/private-browsing');
  22. function makeEmptyBrowserWindow(options) {
  23. options = options || {};
  24. return open('chrome://browser/content/browser.xul', {
  25. features: {
  26. chrome: true,
  27. private: !!options.private,
  28. toolbar: true
  29. }
  30. });
  31. }
  32. exports.testShowPanelAndWidgetOnPrivateWindow = function(assert, done) {
  33. var myPrivateWindow;
  34. var finished = false;
  35. var privateWindow;
  36. var privateWindowClosed = false;
  37. pb.once('start', function() {
  38. assert.pass('private browsing mode started');
  39. // make a new private window
  40. makeEmptyBrowserWindow().then(function(window) {
  41. myPrivateWindow = window;
  42. let wt = windowUtils.WindowTracker({
  43. onTrack: function(window) {
  44. if (!isBrowser(window) || window !== myPrivateWindow) return;
  45. assert.ok(isWindowPrivate(window), 'window is private onTrack!');
  46. let panel = Panel({
  47. onShow: function() {
  48. assert.ok(this.isShowing, 'the panel is showing on the private window');
  49. let count = 0;
  50. let widget = Widget({
  51. id: "testShowPanelAndWidgetOnPrivateWindow-id",
  52. label: "My Hello Widget",
  53. content: "Hello!",
  54. onAttach: function(mod) {
  55. count++;
  56. if (count == 2) {
  57. panel.destroy();
  58. widget.destroy();
  59. close(window);
  60. }
  61. }
  62. });
  63. }
  64. }).show(null, window.gBrowser);
  65. },
  66. onUntrack: function(window) {
  67. if (window === myPrivateWindow) {
  68. wt.unload();
  69. pb.once('stop', function() {
  70. assert.pass('private browsing mode end');
  71. done();
  72. });
  73. pb.deactivate();
  74. }
  75. }
  76. });
  77. assert.equal(isWindowPrivate(window), true, 'the opened window is private');
  78. assert.equal(isPrivate(window), true, 'the opened window is private');
  79. assert.ok(getFrames(window).length > 1, 'there are frames for private window');
  80. assert.equal(getWindowTitle(window), window.document.title,
  81. 'getWindowTitle works');
  82. });
  83. });
  84. pb.activate();
  85. };
  86. exports.testWindowTrackerDoesNotIgnorePrivateWindows = function(assert, done) {
  87. var myPrivateWindow;
  88. var count = 0;
  89. let wt = windowUtils.WindowTracker({
  90. onTrack: function(window) {
  91. if (!isBrowser(window) || !isWindowPrivate(window)) return;
  92. assert.ok(isWindowPrivate(window), 'window is private onTrack!');
  93. if (++count == 1)
  94. close(window);
  95. },
  96. onUntrack: function(window) {
  97. if (count == 1 && isWindowPrivate(window)) {
  98. wt.unload();
  99. pb.once('stop', function() {
  100. assert.pass('private browsing mode end');
  101. done();
  102. });
  103. pb.deactivate();
  104. }
  105. }
  106. });
  107. pb.once('start', function() {
  108. assert.pass('private browsing mode started');
  109. makeEmptyBrowserWindow();
  110. });
  111. pb.activate();
  112. }
  113. exports.testWindowIteratorDoesNotIgnorePrivateWindows = function(assert, done) {
  114. pb.once('start', function() {
  115. // make a new private window
  116. makeEmptyBrowserWindow().then(function(window) {
  117. assert.ok(isWindowPrivate(window), "window is private");
  118. assert.equal(isPrivate(window), true, 'the opened window is private');
  119. assert.ok(toArray(windowUtils.windowIterator()).indexOf(window) > -1,
  120. "window is in windowIterator()");
  121. assert.ok(windows(null, { includePrivate: true }).indexOf(window) > -1,
  122. "window is in windows()");
  123. close(window).then(function() {
  124. pb.once('stop', function() {
  125. done();
  126. });
  127. pb.deactivate();
  128. });
  129. });
  130. });
  131. pb.activate();
  132. };