test-events.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 { LoaderWithHookedConsole } = require("sdk/test/loader");
  6. // Exposing private methods as public in order to test
  7. const EventEmitter = require('sdk/deprecated/events').EventEmitter.compose({
  8. listeners: function(type) this._listeners(type),
  9. emit: function() this._emit.apply(this, arguments),
  10. emitOnObject: function() this._emitOnObject.apply(this, arguments),
  11. removeAllListeners: function(type) this._removeAllListeners(type)
  12. });
  13. exports['test:add listeners'] = function(assert) {
  14. let e = new EventEmitter();
  15. let events_new_listener_emited = [];
  16. let times_hello_emited = 0;
  17. e.on("newListener", function (event, listener) {
  18. events_new_listener_emited.push(event)
  19. })
  20. e.on("hello", function (a, b) {
  21. times_hello_emited += 1
  22. assert.equal("a", a)
  23. assert.equal("b", b)
  24. assert.equal(this, e, '`this` pseudo-variable is bound to instance');
  25. })
  26. e.emit("hello", "a", "b")
  27. };
  28. exports['test:removeListener'] = function(assert) {
  29. let count = 0;
  30. function listener1 () {
  31. count++;
  32. }
  33. function listener2 () {
  34. count++;
  35. }
  36. // test adding and removing listener
  37. let e1 = new EventEmitter();
  38. assert.equal(0, e1.listeners('hello').length);
  39. e1.on("hello", listener1);
  40. assert.equal(1, e1.listeners('hello').length);
  41. assert.equal(listener1, e1.listeners('hello')[0]);
  42. e1.removeListener("hello", listener1);
  43. assert.equal(0, e1.listeners('hello').length);
  44. e1.emit("hello", "");
  45. assert.equal(0, count);
  46. // test adding one listener and removing another which was not added
  47. let e2 = new EventEmitter();
  48. assert.equal(0, e2.listeners('hello').length);
  49. e2.on("hello", listener1);
  50. assert.equal(1, e2.listeners('hello').length);
  51. e2.removeListener("hello", listener2);
  52. assert.equal(1, e2.listeners('hello').length);
  53. assert.equal(listener1, e2.listeners('hello')[0]);
  54. e2.emit("hello", "");
  55. assert.equal(1, count);
  56. // test adding 2 listeners, and removing one
  57. let e3 = new EventEmitter();
  58. assert.equal(0, e3.listeners('hello').length);
  59. e3.on("hello", listener1);
  60. assert.equal(1, e3.listeners('hello').length);
  61. e3.on("hello", listener2);
  62. assert.equal(2, e3.listeners('hello').length);
  63. e3.removeListener("hello", listener1);
  64. assert.equal(1, e3.listeners('hello').length);
  65. assert.equal(listener2, e3.listeners('hello')[0]);
  66. e3.emit("hello", "");
  67. assert.equal(2, count);
  68. };
  69. exports['test:removeAllListeners'] = function(assert) {
  70. let count = 0;
  71. function listener1 () {
  72. count++;
  73. }
  74. function listener2 () {
  75. count++;
  76. }
  77. // test adding a listener and removing all of that type
  78. let e1 = new EventEmitter();
  79. e1.on("hello", listener1);
  80. assert.equal(1, e1.listeners('hello').length);
  81. e1.removeAllListeners("hello");
  82. assert.equal(0, e1.listeners('hello').length);
  83. e1.emit("hello", "");
  84. assert.equal(0, count);
  85. // test adding a listener and removing all of another type
  86. let e2 = new EventEmitter();
  87. e2.on("hello", listener1);
  88. assert.equal(1, e2.listeners('hello').length);
  89. e2.removeAllListeners('goodbye');
  90. assert.equal(1, e2.listeners('hello').length);
  91. e2.emit("hello", "");
  92. assert.equal(1, count);
  93. // test adding 1+ listeners and removing all of that type
  94. let e3 = new EventEmitter();
  95. e3.on("hello", listener1);
  96. assert.equal(1, e3.listeners('hello').length);
  97. e3.on("hello", listener2);
  98. assert.equal(2, e3.listeners('hello').length);
  99. e3.removeAllListeners("hello");
  100. assert.equal(0, e3.listeners('hello').length);
  101. e3.emit("hello", "");
  102. assert.equal(1, count);
  103. // test adding 2 listeners for 2 types and removing all listeners
  104. let e4 = new EventEmitter();
  105. e4.on("hello", listener1);
  106. assert.equal(1, e4.listeners('hello').length);
  107. e4.on('goodbye', listener2);
  108. assert.equal(1, e4.listeners('goodbye').length);
  109. e4.emit("goodbye", "");
  110. e4.removeAllListeners();
  111. assert.equal(0, e4.listeners('hello').length);
  112. assert.equal(0, e4.listeners('goodbye').length);
  113. e4.emit("hello", "");
  114. e4.emit("goodbye", "");
  115. assert.equal(2, count);
  116. };
  117. exports['test: modify in emit'] = function(assert) {
  118. let callbacks_called = [ ];
  119. let e = new EventEmitter();
  120. function callback1() {
  121. callbacks_called.push("callback1");
  122. e.on("foo", callback2);
  123. e.on("foo", callback3);
  124. e.removeListener("foo", callback1);
  125. }
  126. function callback2() {
  127. callbacks_called.push("callback2");
  128. e.removeListener("foo", callback2);
  129. }
  130. function callback3() {
  131. callbacks_called.push("callback3");
  132. e.removeListener("foo", callback3);
  133. }
  134. e.on("foo", callback1);
  135. assert.equal(1, e.listeners("foo").length);
  136. e.emit("foo");
  137. assert.equal(2, e.listeners("foo").length);
  138. assert.equal(1, callbacks_called.length);
  139. assert.equal('callback1', callbacks_called[0]);
  140. e.emit("foo");
  141. assert.equal(0, e.listeners("foo").length);
  142. assert.equal(3, callbacks_called.length);
  143. assert.equal('callback1', callbacks_called[0]);
  144. assert.equal('callback2', callbacks_called[1]);
  145. assert.equal('callback3', callbacks_called[2]);
  146. e.emit("foo");
  147. assert.equal(0, e.listeners("foo").length);
  148. assert.equal(3, callbacks_called.length);
  149. assert.equal('callback1', callbacks_called[0]);
  150. assert.equal('callback2', callbacks_called[1]);
  151. assert.equal('callback3', callbacks_called[2]);
  152. e.on("foo", callback1);
  153. e.on("foo", callback2);
  154. assert.equal(2, e.listeners("foo").length);
  155. e.removeAllListeners("foo");
  156. assert.equal(0, e.listeners("foo").length);
  157. // Verify that removing callbacks while in emit allows emits to propagate to
  158. // all listeners
  159. callbacks_called = [ ];
  160. e.on("foo", callback2);
  161. e.on("foo", callback3);
  162. assert.equal(2, e.listeners("foo").length);
  163. e.emit("foo");
  164. assert.equal(2, callbacks_called.length);
  165. assert.equal('callback2', callbacks_called[0]);
  166. assert.equal('callback3', callbacks_called[1]);
  167. assert.equal(0, e.listeners("foo").length);
  168. };
  169. exports['test:adding same listener'] = function(assert) {
  170. function foo() {}
  171. let e = new EventEmitter();
  172. e.on("foo", foo);
  173. e.on("foo", foo);
  174. assert.equal(
  175. 1,
  176. e.listeners("foo").length,
  177. "listener reregistration is ignored"
  178. );
  179. }
  180. exports['test:errors are reported if listener throws'] = function(assert) {
  181. let e = new EventEmitter(),
  182. reported = false;
  183. e.on('error', function(e) reported = true)
  184. e.on('boom', function() { throw new Error('Boom!') });
  185. e.emit('boom', 3);
  186. assert.ok(reported, 'error should be reported through event');
  187. };
  188. exports['test:emitOnObject'] = function(assert) {
  189. let e = new EventEmitter();
  190. e.on("foo", function() {
  191. assert.equal(this, e, "`this` should be emitter");
  192. });
  193. e.emitOnObject(e, "foo");
  194. e.on("bar", function() {
  195. assert.equal(this, obj, "`this` should be other object");
  196. });
  197. let obj = {};
  198. e.emitOnObject(obj, "bar");
  199. };
  200. exports['test:once'] = function(assert) {
  201. let e = new EventEmitter();
  202. let called = false;
  203. e.once('foo', function(value) {
  204. assert.ok(!called, "listener called only once");
  205. assert.equal(value, "bar", "correct argument was passed");
  206. });
  207. e.emit('foo', 'bar');
  208. e.emit('foo', 'baz');
  209. };
  210. exports["test:removing once"] = function(assert) {
  211. let e = require("sdk/deprecated/events").EventEmitterTrait.create();
  212. e.once("foo", function() { assert.pass("listener was called"); });
  213. e.once("error", function() { assert.fail("error event was emitted"); });
  214. e._emit("foo", "bug-656684");
  215. };
  216. // Bug 726967: Ensure that `emit` doesn't do an infinite loop when `error`
  217. // listener throws an exception
  218. exports['test:emitLoop'] = function(assert) {
  219. // Override the console for this test so it doesn't log the exception to the
  220. // test output
  221. let { loader } = LoaderWithHookedConsole(module);
  222. let EventEmitter = loader.require('sdk/deprecated/events').EventEmitter.compose({
  223. listeners: function(type) this._listeners(type),
  224. emit: function() this._emit.apply(this, arguments),
  225. emitOnObject: function() this._emitOnObject.apply(this, arguments),
  226. removeAllListeners: function(type) this._removeAllListeners(type)
  227. });
  228. let e = new EventEmitter();
  229. e.on("foo", function() {
  230. throw new Error("foo");
  231. });
  232. e.on("error", function() {
  233. throw new Error("error");
  234. });
  235. e.emit("foo");
  236. assert.pass("emit didn't looped");
  237. };
  238. require('sdk/test').run(exports);