PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/test/windows/test-firefox-windows.js

https://github.com/past/addon-sdk
JavaScript | 410 lines | 245 code | 53 blank | 112 comment | 16 complexity | 9921889cb7f3854f09ee50b1bca837f6 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception
  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 { setTimeout } = require('sdk/timers');
  7. const { Loader } = require('sdk/test/loader');
  8. const { onFocus, getMostRecentWindow, windows } = require('sdk/window/utils');
  9. const { open, close, focus } = require('sdk/window/helpers');
  10. const { browserWindows } = require("sdk/windows");
  11. const tabs = require("sdk/tabs");
  12. const winUtils = require("sdk/deprecated/window-utils");
  13. const { WindowTracker } = winUtils;
  14. const { isPrivate } = require('sdk/private-browsing');
  15. const { isWindowPBSupported } = require('sdk/private-browsing/utils');
  16. // TEST: open & close window
  17. exports.testOpenAndCloseWindow = function(test) {
  18. test.waitUntilDone();
  19. test.assertEqual(browserWindows.length, 1, "Only one window open");
  20. browserWindows.open({
  21. url: "data:text/html;charset=utf-8,<title>windows API test</title>",
  22. onOpen: function(window) {
  23. test.assertEqual(this, browserWindows,
  24. "The 'this' object is the windows object.");
  25. test.assertEqual(window.tabs.length, 1, "Only one tab open");
  26. test.assertEqual(browserWindows.length, 2, "Two windows open");
  27. window.tabs.activeTab.on('ready', function onReady(tab) {
  28. tab.removeListener('ready', onReady);
  29. test.assert(window.title.indexOf("windows API test") != -1,
  30. "URL correctly loaded");
  31. window.close();
  32. });
  33. },
  34. onClose: function(window) {
  35. test.assertEqual(window.tabs.length, 0, "Tabs were cleared");
  36. test.assertEqual(browserWindows.length, 1, "Only one window open");
  37. test.done();
  38. }
  39. });
  40. };
  41. exports.testAutomaticDestroy = function(test) {
  42. test.waitUntilDone();
  43. let windows = browserWindows;
  44. // Create a second windows instance that we will unload
  45. let called = false;
  46. let loader = Loader(module);
  47. let windows2 = loader.require("sdk/windows").browserWindows;
  48. windows2.on("open", function() {
  49. called = true;
  50. });
  51. loader.unload();
  52. // Fire a windows event and check that this unloaded instance is inactive
  53. windows.open({
  54. url: "data:text/html;charset=utf-8,foo",
  55. onOpen: function(window) {
  56. setTimeout(function () {
  57. test.assert(!called,
  58. "Unloaded windows instance is destroyed and inactive");
  59. window.close(function () {
  60. test.done();
  61. });
  62. });
  63. }
  64. });
  65. };
  66. exports.testWindowTabsObject = function(test) {
  67. test.waitUntilDone();
  68. let count = 0;
  69. let window;
  70. function runTest() {
  71. if (++count != 2)
  72. return;
  73. test.assertEqual(window.tabs.length, 1, "Only 1 tab open");
  74. test.assertEqual(window.tabs.activeTab.title, "tab 1", "Correct active tab");
  75. window.tabs.open({
  76. url: "data:text/html;charset=utf-8,<title>tab 2</title>",
  77. inBackground: true,
  78. onReady: function onReady(newTab) {
  79. test.assertEqual(window.tabs.length, 2, "New tab open");
  80. test.assertEqual(newTab.title, "tab 2", "Correct new tab title");
  81. test.assertEqual(window.tabs.activeTab.title, "tab 1", "Correct active tab");
  82. let i = 1;
  83. for each (let tab in window.tabs)
  84. test.assertEqual(tab.title, "tab " + i++, "Correct title");
  85. window.close();
  86. }
  87. });
  88. }
  89. browserWindows.open({
  90. url: "data:text/html;charset=utf-8,<title>tab 1</title>",
  91. onActivate: function onActivate(win) {
  92. window = win;
  93. runTest();
  94. },
  95. onClose: function onClose(window) {
  96. test.assertEqual(window.tabs.length, 0, "No more tabs on closed window");
  97. test.done();
  98. }
  99. });
  100. tabs.once("ready", runTest);
  101. };
  102. exports.testOnOpenOnCloseListeners = function(test) {
  103. test.waitUntilDone();
  104. let windows = browserWindows;
  105. test.assertEqual(browserWindows.length, 1, "Only one window open");
  106. let received = {
  107. listener1: false,
  108. listener2: false,
  109. listener3: false,
  110. listener4: false
  111. }
  112. function listener1() {
  113. test.assertEqual(this, windows, "The 'this' object is the windows object.");
  114. if (received.listener1)
  115. test.fail("Event received twice");
  116. received.listener1 = true;
  117. }
  118. function listener2() {
  119. if (received.listener2)
  120. test.fail("Event received twice");
  121. received.listener2 = true;
  122. }
  123. function listener3() {
  124. test.assertEqual(this, windows, "The 'this' object is the windows object.");
  125. if (received.listener3)
  126. test.fail("Event received twice");
  127. received.listener3 = true;
  128. }
  129. function listener4() {
  130. if (received.listener4)
  131. test.fail("Event received twice");
  132. received.listener4 = true;
  133. }
  134. windows.on('open', listener1);
  135. windows.on('open', listener2);
  136. windows.on('close', listener3);
  137. windows.on('close', listener4);
  138. function verify() {
  139. test.assert(received.listener1, "onOpen handler called");
  140. test.assert(received.listener2, "onOpen handler called");
  141. test.assert(received.listener3, "onClose handler called");
  142. test.assert(received.listener4, "onClose handler called");
  143. windows.removeListener('open', listener1);
  144. windows.removeListener('open', listener2);
  145. windows.removeListener('close', listener3);
  146. windows.removeListener('close', listener4);
  147. test.done();
  148. }
  149. windows.open({
  150. url: "data:text/html;charset=utf-8,foo",
  151. onOpen: function(window) {
  152. window.close(verify);
  153. }
  154. });
  155. };
  156. /*
  157. Disabled due to all of the Win8 PGO bustage in bug 873007.
  158. exports.testActiveWindow = function(test) {
  159. let windows = browserWindows;
  160. // API window objects
  161. let window2, window3;
  162. // Raw window objects
  163. let rawWindow2, rawWindow3;
  164. test.waitUntilDone();
  165. let testSteps = [
  166. function() {
  167. test.assertEqual(windows.length, 3, "Correct number of browser windows");
  168. let count = 0;
  169. for (let window in windows)
  170. count++;
  171. test.assertEqual(count, 3, "Correct number of windows returned by iterator");
  172. test.assertEqual(windows.activeWindow.title, window3.title, "Correct active window - 3");
  173. continueAfterFocus(rawWindow2);
  174. rawWindow2.focus();
  175. },
  176. function() {
  177. nextStep();
  178. },
  179. function() {
  180. test.assertEqual(windows.activeWindow.title, window2.title, "Correct active window - 2");
  181. continueAfterFocus(rawWindow2);
  182. window2.activate();
  183. },
  184. function() {
  185. test.assertEqual(windows.activeWindow.title, window2.title, "Correct active window - 2");
  186. continueAfterFocus(rawWindow3);
  187. window3.activate();
  188. },
  189. function() {
  190. test.assertEqual(windows.activeWindow.title, window3.title, "Correct active window - 3");
  191. finishTest();
  192. }
  193. ];
  194. let newWindow = null;
  195. let tracker = new WindowTracker({
  196. onTrack: function(window) {
  197. newWindow = window;
  198. }
  199. });
  200. windows.open({
  201. url: "data:text/html;charset=utf-8,<title>window 2</title>",
  202. onOpen: function(window) {
  203. window.tabs.activeTab.on('ready', function() {
  204. window2 = window;
  205. test.assert(newWindow, "A new window was opened");
  206. rawWindow2 = newWindow;
  207. newWindow = null;
  208. test.assertEqual(rawWindow2.content.document.title, "window 2", "Got correct raw window 2");
  209. test.assertEqual(rawWindow2.document.title, window2.title, "Saw correct title on window 2");
  210. windows.open({
  211. url: "data:text/html;charset=utf-8,<title>window 3</title>",
  212. onOpen: function(window) {
  213. window.tabs.activeTab.on('ready', function onReady() {
  214. window3 = window;
  215. test.assert(newWindow, "A new window was opened");
  216. rawWindow3 = newWindow;
  217. tracker.unload();
  218. test.assertEqual(rawWindow3.content.document.title, "window 3", "Got correct raw window 3");
  219. test.assertEqual(rawWindow3.document.title, window3.title, "Saw correct title on window 3");
  220. continueAfterFocus(rawWindow3);
  221. rawWindow3.focus();
  222. });
  223. }
  224. });
  225. });
  226. }
  227. });
  228. function nextStep() {
  229. if (testSteps.length)
  230. testSteps.shift()();
  231. }
  232. let continueAfterFocus = function(w) onFocus(w).then(nextStep);
  233. function finishTest() {
  234. window3.close(function() {
  235. window2.close(function() {
  236. test.done();
  237. });
  238. });
  239. }
  240. };
  241. */
  242. exports.testTrackWindows = function(test) {
  243. test.waitUntilDone();
  244. let windows = [];
  245. let actions = [];
  246. let expects = [
  247. "activate 0", "global activate 0", "deactivate 0", "global deactivate 0",
  248. "activate 1", "global activate 1", "deactivate 1", "global deactivate 1",
  249. "activate 2", "global activate 2"
  250. ];
  251. function openWindow() {
  252. windows.push(browserWindows.open({
  253. url: "data:text/html;charset=utf-8,<i>testTrackWindows</i>",
  254. onActivate: function(window) {
  255. let index = windows.indexOf(window);
  256. test.assertEqual(actions.join(), expects.slice(0, index*4).join(), expects[index*4]);
  257. actions.push("activate " + index);
  258. if (windows.length < 3) {
  259. openWindow()
  260. }
  261. else {
  262. (function closeWindows(windows) {
  263. if (!windows.length)
  264. return test.done();
  265. return windows.pop().close(function() {
  266. test.pass('window was closed');
  267. closeWindows(windows);
  268. });
  269. })(windows)
  270. }
  271. },
  272. onDeactivate: function(window) {
  273. let index = windows.indexOf(window);
  274. test.assertEqual(actions.join(), expects.slice(0, index*4 + 2).join(), expects[index*4 + 2]);
  275. actions.push("deactivate " + index)
  276. }
  277. }));
  278. }
  279. browserWindows.on("activate", function (window) {
  280. let index = windows.indexOf(window);
  281. // only concerned with windows opened for this test
  282. if (index < 0)
  283. return;
  284. test.assertEqual(actions.join(), expects.slice(0, index*4 + 1).join(), expects[index*4 + 1]);
  285. actions.push("global activate " + index)
  286. })
  287. browserWindows.on("deactivate", function (window) {
  288. let index = windows.indexOf(window);
  289. // only concerned with windows opened for this test
  290. if (index < 0)
  291. return;
  292. test.assertEqual(actions.join(), expects.slice(0, index*4 + 3).join(), expects[index*4 + 3]);
  293. actions.push("global deactivate " + index)
  294. })
  295. openWindow();
  296. }
  297. // test that it is not possible to open a private window by default
  298. exports.testWindowOpenPrivateDefault = function(test) {
  299. test.waitUntilDone();
  300. browserWindows.open({
  301. url: 'about:mozilla',
  302. isPrivate: true,
  303. onOpen: function(window) {
  304. let tab = window.tabs[0];
  305. tab.once('ready', function() {
  306. test.assertEqual(tab.url, 'about:mozilla', 'opened correct tab');
  307. test.assertEqual(isPrivate(tab), false, 'tab is not private');
  308. window.close(test.done.bind(test));
  309. });
  310. }
  311. });
  312. }
  313. // test that it is not possible to find a private window in
  314. // windows module's iterator
  315. exports.testWindowIteratorPrivateDefault = function(test) {
  316. test.waitUntilDone();
  317. test.assertEqual(browserWindows.length, 1, 'only one window open');
  318. open('chrome://browser/content/browser.xul', {
  319. features: {
  320. private: true,
  321. chrome: true
  322. }
  323. }).then(function(window) focus(window).then(function() {
  324. // test that there is a private window opened
  325. test.assertEqual(isPrivate(window), isWindowPBSupported, 'there is a private window open');
  326. test.assertStrictEqual(window, winUtils.activeWindow);
  327. test.assertStrictEqual(window, getMostRecentWindow());
  328. test.assert(!isPrivate(browserWindows.activeWindow));
  329. if (isWindowPBSupported) {
  330. test.assertEqual(browserWindows.length, 1, 'only one window in browserWindows');
  331. test.assertEqual(windows().length, 1, 'only one window in windows()');
  332. }
  333. else {
  334. test.assertEqual(browserWindows.length, 2, 'two windows open');
  335. test.assertEqual(windows().length, 2, 'two windows in windows()');
  336. }
  337. test.assertEqual(windows(null, { includePrivate: true }).length, 2);
  338. for each(let window in browserWindows) {
  339. // test that all windows in iterator are not private
  340. test.assert(!isPrivate(window), 'no window in browserWindows is private');
  341. }
  342. close(window).then(test.done.bind(test));
  343. }));
  344. }