test-window-utils-global-private-browsing.js 4.7 KB

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