test-window-utils-private-browsing.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. // Fennec support tracked in bug #809412
  6. module.metadata = {
  7. 'engines': {
  8. 'Firefox': '*'
  9. }
  10. };
  11. const windowUtils = require('sdk/deprecated/window-utils');
  12. const { Cc, Ci } = require('chrome');
  13. const { isWindowPBSupported } = require('sdk/private-browsing/utils');
  14. const { getFrames, getWindowTitle, onFocus, isWindowPrivate } = require('sdk/window/utils');
  15. const { open, close, focus } = require('sdk/window/helpers');
  16. const WM = Cc['@mozilla.org/appshell/window-mediator;1'].getService(Ci.nsIWindowMediator);
  17. const { isPrivate } = require('sdk/private-browsing');
  18. const { fromIterator: toArray } = require('sdk/util/array');
  19. function makeEmptyBrowserWindow(options) {
  20. options = options || {};
  21. return open('chrome://browser/content/browser.xul', {
  22. features: {
  23. chrome: true,
  24. private: !!options.private
  25. }
  26. });
  27. }
  28. exports.testWindowTrackerIgnoresPrivateWindows = function(assert, done) {
  29. var myNonPrivateWindow, myPrivateWindow;
  30. var finished = false;
  31. var privateWindow;
  32. var privateWindowClosed = false;
  33. let wt = windowUtils.WindowTracker({
  34. onTrack: function(window) {
  35. assert.ok(!isWindowPrivate(window), 'private window was not tracked!');
  36. },
  37. onUntrack: function(window) {
  38. assert.ok(!isWindowPrivate(window), 'private window was not tracked!');
  39. // PWPB case
  40. if (window === myPrivateWindow && isWindowPBSupported) {
  41. privateWindowClosed = true;
  42. }
  43. if (window === myNonPrivateWindow) {
  44. assert.ok(!privateWindowClosed);
  45. wt.unload();
  46. done();
  47. }
  48. }
  49. });
  50. // make a new private window
  51. makeEmptyBrowserWindow({
  52. private: true
  53. }).then(function(window) {
  54. myPrivateWindow = window;
  55. assert.equal(isWindowPrivate(window), isWindowPBSupported);
  56. assert.ok(getFrames(window).length > 1, 'there are frames for private window');
  57. assert.equal(getWindowTitle(window), window.document.title,
  58. 'getWindowTitle works');
  59. close(window).then(function() {
  60. makeEmptyBrowserWindow().then(function(window) {
  61. myNonPrivateWindow = window;
  62. assert.pass('opened new window');
  63. window.close();
  64. });
  65. });
  66. });
  67. };
  68. // Test setting activeWIndow and onFocus for private windows
  69. exports.testSettingActiveWindowDoesNotIgnorePrivateWindow = function(assert, done) {
  70. let browserWindow = WM.getMostRecentWindow("navigator:browser");
  71. let testSteps;
  72. assert.equal(windowUtils.activeBrowserWindow, browserWindow,
  73. "Browser window is the active browser window.");
  74. assert.ok(!isPrivate(browserWindow), "Browser window is not private.");
  75. // make a new private window
  76. makeEmptyBrowserWindow({
  77. private: true
  78. }).then(focus).then(function(window) {
  79. let continueAfterFocus = function(window) onFocus(window).then(nextTest);
  80. // PWPB case
  81. if (isWindowPBSupported) {
  82. assert.ok(isPrivate(window), "window is private");
  83. assert.notDeepEqual(windowUtils.activeBrowserWindow, browserWindow);
  84. }
  85. // Global case
  86. else {
  87. assert.ok(!isPrivate(window), "window is not private");
  88. }
  89. assert.strictEqual(windowUtils.activeBrowserWindow, window,
  90. "Correct active browser window pb supported");
  91. assert.notStrictEqual(browserWindow, window,
  92. "The window is not the old browser window");
  93. testSteps = [
  94. function() {
  95. // test setting a non private window
  96. continueAfterFocus(windowUtils.activeWindow = browserWindow);
  97. },
  98. function() {
  99. assert.strictEqual(windowUtils.activeWindow, browserWindow,
  100. "Correct active window [1]");
  101. assert.strictEqual(windowUtils.activeBrowserWindow, browserWindow,
  102. "Correct active browser window [1]");
  103. // test focus(window)
  104. focus(window).then(nextTest);
  105. },
  106. function(w) {
  107. assert.strictEqual(w, window, 'require("sdk/window/helpers").focus on window works');
  108. assert.strictEqual(windowUtils.activeBrowserWindow, window,
  109. "Correct active browser window [2]");
  110. assert.strictEqual(windowUtils.activeWindow, window,
  111. "Correct active window [2]");
  112. // test setting a private window
  113. continueAfterFocus(windowUtils.activeWindow = window);
  114. },
  115. function() {
  116. assert.deepEqual(windowUtils.activeBrowserWindow, window,
  117. "Correct active browser window [3]");
  118. assert.deepEqual(windowUtils.activeWindow, window,
  119. "Correct active window [3]");
  120. // just to get back to original state
  121. continueAfterFocus(windowUtils.activeWindow = browserWindow);
  122. },
  123. function() {
  124. assert.deepEqual(windowUtils.activeBrowserWindow, browserWindow,
  125. "Correct active browser window when pb mode is supported [4]");
  126. assert.deepEqual(windowUtils.activeWindow, browserWindow,
  127. "Correct active window when pb mode is supported [4]");
  128. close(window).then(done);
  129. }
  130. ];
  131. function nextTest() {
  132. let args = arguments;
  133. if (testSteps.length) {
  134. require('sdk/timers').setTimeout(function() {
  135. (testSteps.shift()).apply(null, args);
  136. }, 0);
  137. }
  138. }
  139. nextTest();
  140. });
  141. };
  142. exports.testActiveWindowDoesNotIgnorePrivateWindow = function(assert, done) {
  143. // make a new private window
  144. makeEmptyBrowserWindow({
  145. private: true
  146. }).then(function(window) {
  147. // PWPB case
  148. if (isWindowPBSupported) {
  149. assert.equal(isPrivate(windowUtils.activeWindow), true,
  150. "active window is private");
  151. assert.equal(isPrivate(windowUtils.activeBrowserWindow), true,
  152. "active browser window is private");
  153. assert.ok(isWindowPrivate(window), "window is private");
  154. assert.ok(isPrivate(window), "window is private");
  155. // pb mode is supported
  156. assert.ok(
  157. isWindowPrivate(windowUtils.activeWindow),
  158. "active window is private when pb mode is supported");
  159. assert.ok(
  160. isWindowPrivate(windowUtils.activeBrowserWindow),
  161. "active browser window is private when pb mode is supported");
  162. assert.ok(isPrivate(windowUtils.activeWindow),
  163. "active window is private when pb mode is supported");
  164. assert.ok(isPrivate(windowUtils.activeBrowserWindow),
  165. "active browser window is private when pb mode is supported");
  166. }
  167. // Global case
  168. else {
  169. assert.equal(isPrivate(windowUtils.activeWindow), false,
  170. "active window is not private");
  171. assert.equal(isPrivate(windowUtils.activeBrowserWindow), false,
  172. "active browser window is not private");
  173. assert.equal(isWindowPrivate(window), false, "window is not private");
  174. assert.equal(isPrivate(window), false, "window is not private");
  175. }
  176. close(window).then(done);
  177. });
  178. }
  179. exports.testWindowIteratorIgnoresPrivateWindows = function(assert, done) {
  180. // make a new private window
  181. makeEmptyBrowserWindow({
  182. private: true
  183. }).then(function(window) {
  184. // PWPB case
  185. if (isWindowPBSupported) {
  186. assert.ok(isWindowPrivate(window), "window is private");
  187. assert.equal(toArray(windowUtils.windowIterator()).indexOf(window), -1,
  188. "window is not in windowIterator()");
  189. }
  190. // Global case
  191. else {
  192. assert.equal(isWindowPrivate(window), false, "window is not private");
  193. assert.ok(toArray(windowUtils.windowIterator()).indexOf(window) > -1,
  194. "window is in windowIterator()");
  195. }
  196. close(window).then(done);
  197. });
  198. };
  199. require("test").run(exports);