test-windows.js 9.0 KB

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