test-dom.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 events = require("sdk/dom/events");
  6. const { activeBrowserWindow: { document } } = require("sdk/deprecated/window-utils");
  7. const window = document.window;
  8. /*
  9. exports["test on / emit"] = function (assert, done) {
  10. let element = document.createElement("div");
  11. events.on(element, "click", function listener(event) {
  12. assert.equal(event.target, element, "event has correct target");
  13. events.removeListener(element, "click", listener);
  14. done();
  15. });
  16. events.emit(element, "click", {
  17. category: "MouseEvents",
  18. settings: [
  19. true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null
  20. ]
  21. });
  22. };
  23. exports["test remove"] = function (assert, done) {
  24. let element = document.createElement("span");
  25. let l1 = 0;
  26. let l2 = 0;
  27. let options = {
  28. category: "MouseEvents",
  29. settings: [
  30. true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null
  31. ]
  32. };
  33. events.on(element, "click", function listener1(event) {
  34. l1 ++;
  35. assert.equal(event.target, element, "event has correct target");
  36. events.removeListener(element, "click", listener1);
  37. });
  38. events.on(element, "click", function listener2(event) {
  39. l2 ++;
  40. if (l1 < l2) {
  41. assert.equal(l1, 1, "firs listener was called and then romeved");
  42. events.removeListener(element, "click", listener2);
  43. done();
  44. }
  45. events.emit(element, "click", options);
  46. });
  47. events.emit(element, "click", options);
  48. };
  49. exports["test once"] = function (assert, done) {
  50. let element = document.createElement("h1");
  51. let l1 = 0;
  52. let l2 = 0;
  53. let options = {
  54. category: "MouseEvents",
  55. settings: [
  56. true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null
  57. ]
  58. };
  59. events.once(element, "click", function listener(event) {
  60. assert.equal(event.target, element, "event target is a correct element");
  61. l1 ++;
  62. });
  63. events.on(element, "click", function listener(event) {
  64. l2 ++;
  65. if (l2 > 3) {
  66. events.removeListener(element, "click", listener);
  67. assert.equal(event.target, element, "event has correct target");
  68. assert.equal(l1, 1, "once was called only once");
  69. done();
  70. }
  71. events.emit(element, "click", options);
  72. });
  73. events.emit(element, "click", options);
  74. }
  75. */
  76. require("test").run(exports);