PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/packages/addon-kit/tests/test-windows.js

https://github.com/ehsan/addon-sdk
JavaScript | 308 lines | 236 code | 47 blank | 25 comment | 14 complexity | 047817df9adc54f741279f244f41ba01 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. const {Cc, Ci} = require("chrome");
  5. const { setTimeout } = require("timer");
  6. const { Loader } = require('test-harness/loader');
  7. const wm = Cc["@mozilla.org/appshell/window-mediator;1"].
  8. getService(Ci.nsIWindowMediator);
  9. let browserWindows;
  10. function getTestRunnerWindow() wm.getMostRecentWindow("test:runner")
  11. exports.testOpenAndCloseWindow = function(test) {
  12. test.waitUntilDone();
  13. test.assertEqual(browserWindows.length, 1, "Only one window open");
  14. browserWindows.open({
  15. url: "data:text/html;charset=utf-8,<title>windows API test</title>",
  16. onOpen: function(window) {
  17. test.assertEqual(this, browserWindows,
  18. "The 'this' object is the windows object.");
  19. test.assertEqual(window.tabs.length, 1, "Only one tab open");
  20. test.assertEqual(browserWindows.length, 2, "Two windows open");
  21. window.tabs.activeTab.on('ready', function onReady(tab) {
  22. tab.removeListener('ready', onReady);
  23. test.assert(window.title.indexOf("windows API test") != -1,
  24. "URL correctly loaded");
  25. window.close();
  26. });
  27. },
  28. onClose: function(window) {
  29. test.assertEqual(window.tabs.length, 0, "Tabs were cleared");
  30. test.assertEqual(browserWindows.length, 1, "Only one window open");
  31. test.done();
  32. }
  33. });
  34. };
  35. exports.testAutomaticDestroy = function(test) {
  36. test.waitUntilDone();
  37. let windows = browserWindows;
  38. // Create a second windows instance that we will unload
  39. let called = false;
  40. let loader = Loader(module);
  41. let windows2 = loader.require("windows").browserWindows;
  42. windows2.on("open", function() {
  43. called = true;
  44. });
  45. loader.unload();
  46. // Fire a windows event and check that this unloaded instance is inactive
  47. windows.open({
  48. url: "data:text/html;charset=utf-8,foo",
  49. onOpen: function(window) {
  50. setTimeout(function () {
  51. test.assert(!called,
  52. "Unloaded windows instance is destroyed and inactive");
  53. window.close(function () {
  54. test.done();
  55. });
  56. });
  57. }
  58. });
  59. };
  60. exports.testOnOpenOnCloseListeners = function(test) {
  61. test.waitUntilDone();
  62. let windows = browserWindows;
  63. test.assertEqual(browserWindows.length, 1, "Only one window open");
  64. let received = {
  65. listener1: false,
  66. listener2: false,
  67. listener3: false,
  68. listener4: false
  69. }
  70. function listener1() {
  71. test.assertEqual(this, windows, "The 'this' object is the windows object.");
  72. if (received.listener1)
  73. test.fail("Event received twice");
  74. received.listener1 = true;
  75. }
  76. function listener2() {
  77. if (received.listener2)
  78. test.fail("Event received twice");
  79. received.listener2 = true;
  80. }
  81. function listener3() {
  82. test.assertEqual(this, windows, "The 'this' object is the windows object.");
  83. if (received.listener3)
  84. test.fail("Event received twice");
  85. received.listener3 = true;
  86. }
  87. function listener4() {
  88. if (received.listener4)
  89. test.fail("Event received twice");
  90. received.listener4 = true;
  91. }
  92. windows.on('open', listener1);
  93. windows.on('open', listener2);
  94. windows.on('close', listener3);
  95. windows.on('close', listener4);
  96. function verify() {
  97. test.assert(received.listener1, "onOpen handler called");
  98. test.assert(received.listener2, "onOpen handler called");
  99. test.assert(received.listener3, "onClose handler called");
  100. test.assert(received.listener4, "onClose handler called");
  101. windows.removeListener('open', listener1);
  102. windows.removeListener('open', listener2);
  103. windows.removeListener('close', listener3);
  104. windows.removeListener('close', listener4);
  105. test.done();
  106. }
  107. windows.open({
  108. url: "data:text/html;charset=utf-8,foo",
  109. onOpen: function(window) {
  110. window.close(verify);
  111. }
  112. });
  113. };
  114. exports.testWindowTabsObject = function(test) {
  115. test.waitUntilDone();
  116. browserWindows.open({
  117. url: "data:text/html;charset=utf-8,<title>tab 1</title>",
  118. onOpen: function onOpen(window) {
  119. test.assertEqual(window.tabs.length, 1, "Only 1 tab open");
  120. window.tabs.open({
  121. url: "data:text/html;charset=utf-8,<title>tab 2</title>",
  122. inBackground: true,
  123. onReady: function onReady(newTab) {
  124. test.assertEqual(window.tabs.length, 2, "New tab open");
  125. test.assertEqual(newTab.title, "tab 2", "Correct new tab title");
  126. test.assertEqual(window.tabs.activeTab.title, "tab 1", "Correct active tab");
  127. let i = 1;
  128. for each (let tab in window.tabs)
  129. test.assertEqual(tab.title, "tab " + i++, "Correct title");
  130. window.close();
  131. }
  132. });
  133. },
  134. onClose: function onClose(window) {
  135. test.assertEqual(window.tabs.length, 0, "No more tabs on closed window");
  136. test.done();
  137. }
  138. });
  139. };
  140. exports.testActiveWindow = function(test) {
  141. const xulApp = require("xul-app");
  142. if (xulApp.versionInRange(xulApp.platformVersion, "1.9.2", "1.9.2.*")) {
  143. test.pass("This test is disabled on 3.6. For more information, see bug 598525");
  144. return;
  145. }
  146. let windows = browserWindows;
  147. // API window objects
  148. let window2, window3;
  149. // Raw window objects
  150. let nonBrowserWindow = getTestRunnerWindow(), rawWindow2, rawWindow3;
  151. test.waitUntilDone();
  152. let testSteps = [
  153. function() {
  154. test.assertEqual(windows.length, 3, "Correct number of browser windows");
  155. let count = 0;
  156. for (let window in windows)
  157. count++;
  158. test.assertEqual(count, 3, "Correct number of windows returned by iterator");
  159. rawWindow2.focus();
  160. continueAfterFocus(rawWindow2);
  161. },
  162. function() {
  163. nonBrowserWindow.focus();
  164. continueAfterFocus(nonBrowserWindow);
  165. },
  166. function() {
  167. /**
  168. * Bug 614079: This test fails intermittently on some specific linux
  169. * environnements, without being able to reproduce it in same
  170. * distribution with same window manager.
  171. * Disable it until being able to reproduce it easily.
  172. // On linux, focus is not consistent, so we can't be sure
  173. // what window will be on top.
  174. // Here when we focus "non-browser" window,
  175. // Any Browser window may be selected as "active".
  176. test.assert(windows.activeWindow == window2 || windows.activeWindow == window3,
  177. "Non-browser windows aren't handled by this module");
  178. */
  179. window2.activate();
  180. continueAfterFocus(rawWindow2);
  181. },
  182. function() {
  183. test.assertEqual(windows.activeWindow.title, window2.title, "Correct active window - 2");
  184. window3.activate();
  185. continueAfterFocus(rawWindow3);
  186. },
  187. function() {
  188. test.assertEqual(windows.activeWindow.title, window3.title, "Correct active window - 3");
  189. nonBrowserWindow.focus();
  190. finishTest();
  191. }
  192. ];
  193. windows.open({
  194. url: "data:text/html;charset=utf-8,<title>window 2</title>",
  195. onOpen: function(window) {
  196. window2 = window;
  197. rawWindow2 = wm.getMostRecentWindow("navigator:browser");
  198. windows.open({
  199. url: "data:text/html;charset=utf-8,<title>window 3</title>",
  200. onOpen: function(window) {
  201. window.tabs.activeTab.on('ready', function onReady() {
  202. window3 = window;
  203. rawWindow3 = wm.getMostRecentWindow("navigator:browser");
  204. nextStep()
  205. });
  206. }
  207. });
  208. }
  209. });
  210. function nextStep() {
  211. if (testSteps.length > 0)
  212. testSteps.shift()();
  213. }
  214. function continueAfterFocus(targetWindow) {
  215. // Based on SimpleTest.waitForFocus
  216. var fm = Cc["@mozilla.org/focus-manager;1"].
  217. getService(Ci.nsIFocusManager);
  218. var childTargetWindow = {};
  219. fm.getFocusedElementForWindow(targetWindow, true, childTargetWindow);
  220. childTargetWindow = childTargetWindow.value;
  221. var focusedChildWindow = {};
  222. if (fm.activeWindow) {
  223. fm.getFocusedElementForWindow(fm.activeWindow, true, focusedChildWindow);
  224. focusedChildWindow = focusedChildWindow.value;
  225. }
  226. var focused = (focusedChildWindow == childTargetWindow);
  227. if (focused) {
  228. nextStep();
  229. } else {
  230. childTargetWindow.addEventListener("focus", function focusListener() {
  231. childTargetWindow.removeEventListener("focus", focusListener, true);
  232. nextStep();
  233. }, true);
  234. }
  235. }
  236. function finishTest() {
  237. window3.close(function() {
  238. window2.close(function() {
  239. test.done();
  240. });
  241. });
  242. }
  243. };
  244. // If the module doesn't support the app we're being run in, require() will
  245. // throw. In that case, remove all tests above from exports, and add one dummy
  246. // test that passes.
  247. try {
  248. browserWindows = require("windows").browserWindows;
  249. }
  250. catch (err) {
  251. // This bug should be mentioned in the error message.
  252. let bug = "https://bugzilla.mozilla.org/show_bug.cgi?id=571449";
  253. if (err.message.indexOf(bug) < 0)
  254. throw err;
  255. module.exports = {
  256. testAppNotSupported: function (test) {
  257. test.pass("the windows module does not support this application.");
  258. }
  259. }
  260. }