test-content-events.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 { Loader } = require("sdk/test/loader");
  6. const { getMostRecentBrowserWindow, getInnerId } = require("sdk/window/utils");
  7. const { openTab, closeTab, getBrowserForTab } = require("sdk/tabs/utils");
  8. const { defer } = require("sdk/core/promise");
  9. const { curry, identity, partial } = require("sdk/lang/functional");
  10. let when = curry(function(options, tab) {
  11. let type = options.type || options;
  12. let capture = options.capture || false;
  13. let target = getBrowserForTab(tab);
  14. let { promise, resolve } = defer();
  15. target.addEventListener(type, function handler(event) {
  16. if (!event.target.defaultView.frameElement) {
  17. target.removeEventListener(type, handler, capture);
  18. resolve(tab);
  19. }
  20. }, capture);
  21. return promise;
  22. });
  23. let use = function(value) function() value;
  24. let open = curry(function(url, window) openTab(window, url));
  25. let close = function(tab) {
  26. let promise = when("pagehide", tab);
  27. closeTab(tab);
  28. return promise;
  29. }
  30. exports["test multiple tabs"] = function(assert, done) {
  31. let loader = Loader(module);
  32. let { events } = loader.require("sdk/content/events");
  33. let { on, off } = loader.require("sdk/event/core");
  34. let actual = [];
  35. on(events, "data", handler);
  36. function handler ({type, target, timeStamp}) {
  37. eventFilter(type, target, () => {
  38. actual.push(type + " -> " + target.URL)
  39. });
  40. }
  41. let window = getMostRecentBrowserWindow();
  42. let firstTab = open("data:text/html,first-tab", window);
  43. when("pageshow", firstTab).
  44. then(close).
  45. then(use(window)).
  46. then(open("data:text/html,second-tab")).
  47. then(when("pageshow")).
  48. then(close).
  49. then(function() {
  50. assert.deepEqual(actual, [
  51. "document-element-inserted -> data:text/html,first-tab",
  52. "DOMContentLoaded -> data:text/html,first-tab",
  53. "load -> data:text/html,first-tab",
  54. "pageshow -> data:text/html,first-tab",
  55. "document-element-inserted -> data:text/html,second-tab",
  56. "DOMContentLoaded -> data:text/html,second-tab",
  57. "load -> data:text/html,second-tab",
  58. "pageshow -> data:text/html,second-tab"
  59. ], "all events dispatche as expeced")
  60. }, function(reason) {
  61. assert.fail(Error(reason));
  62. }).then(function() {
  63. loader.unload();
  64. off(events, "data", handler);
  65. done();
  66. });
  67. };
  68. exports["test nested frames"] = function(assert, done) {
  69. let loader = Loader(module);
  70. let { events } = loader.require("sdk/content/events");
  71. let { on, off } = loader.require("sdk/event/core");
  72. let actual = [];
  73. on(events, "data", handler);
  74. function handler ({type, target, timeStamp}) {
  75. eventFilter(type, target, () => {
  76. actual.push(type + " -> " + target.URL)
  77. });
  78. }
  79. let window = getMostRecentBrowserWindow();
  80. let uri = encodeURI("data:text/html,<iframe src='data:text/html,iframe'>");
  81. let tab = open(uri, window);
  82. when("pageshow", tab).
  83. then(close).
  84. then(function() {
  85. assert.deepEqual(actual, [
  86. "document-element-inserted -> " + uri,
  87. "DOMContentLoaded -> " + uri,
  88. "document-element-inserted -> data:text/html,iframe",
  89. "DOMContentLoaded -> data:text/html,iframe",
  90. "load -> data:text/html,iframe",
  91. "pageshow -> data:text/html,iframe",
  92. "load -> " + uri,
  93. "pageshow -> " + uri
  94. ], "events where dispatched")
  95. }, function(reason) {
  96. assert.fail(Error(reason))
  97. }).then(function() {
  98. loader.unload();
  99. off(events, "data", handler);
  100. done();
  101. });
  102. };
  103. // ignore about:blank pages and *-document-global-created
  104. // events that are not very consistent.
  105. // ignore http:// requests, as Fennec's `about:home` page
  106. // displays add-ons a user could install
  107. // ignore local `searchplugins` files loaded
  108. // Calls callback if passes filter
  109. function eventFilter (type, target, callback) {
  110. if (target.URL !== "about:blank" &&
  111. target.URL !== "about:home" &&
  112. !target.URL.match(/^https?:\/\//i) &&
  113. !target.URL.match(/searchplugins/) &&
  114. type !== "chrome-document-global-created" &&
  115. type !== "content-document-global-created")
  116. callback();
  117. }
  118. require("test").run(exports);