test-event-utils.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 { on, emit } = require("sdk/event/core");
  6. const { filter, map, merge, expand, pipe, stripListeners } = require("sdk/event/utils");
  7. const $ = require("./event/helpers");
  8. function isEven(x) !(x % 2)
  9. function inc(x) x + 1
  10. exports["test filter events"] = function(assert) {
  11. let input = {};
  12. let evens = filter(input, isEven);
  13. let actual = [];
  14. on(evens, "data", function(e) actual.push(e));
  15. [1, 2, 3, 4, 5, 6, 7].forEach(function(x) emit(input, "data", x));
  16. assert.deepEqual(actual, [2, 4, 6], "only even numbers passed through");
  17. };
  18. exports["test filter emits"] = $.emits(function(input, assert) {
  19. let output = filter(input, isEven);
  20. assert(output, [1, 2, 3, 4, 5], [2, 4], "this is `output` & evens passed");
  21. });;
  22. exports["test filter reg once"] = $.registerOnce(function(input, assert) {
  23. assert(filter(input, isEven), [1, 2, 3, 4, 5, 6], [2, 4, 6],
  24. "listener can be registered only once");
  25. });
  26. exports["test filter ignores new"] = $.ignoreNew(function(input, assert) {
  27. assert(filter(input, isEven), [1, 2, 3], [2],
  28. "new listener is ignored")
  29. });
  30. exports["test filter is FIFO"] = $.FIFO(function(input, assert) {
  31. assert(filter(input, isEven), [1, 2, 3, 4], [2, 4],
  32. "listeners are invoked in fifo order")
  33. });
  34. exports["test map events"] = function(assert) {
  35. let input = {};
  36. let incs = map(input, inc);
  37. let actual = [];
  38. on(incs, "data", function(e) actual.push(e));
  39. [1, 2, 3, 4].forEach(function(x) emit(input, "data", x));
  40. assert.deepEqual(actual, [2, 3, 4, 5], "all numbers were incremented");
  41. };
  42. exports["test map emits"] = $.emits(function(input, assert) {
  43. let output = map(input, inc);
  44. assert(output, [1, 2, 3], [2, 3, 4], "this is `output` & evens passed");
  45. });;
  46. exports["test map reg once"] = $.registerOnce(function(input, assert) {
  47. assert(map(input, inc), [1, 2, 3], [2, 3, 4],
  48. "listener can be registered only once");
  49. });
  50. exports["test map ignores new"] = $.ignoreNew(function(input, assert) {
  51. assert(map(input, inc), [1], [2],
  52. "new listener is ignored")
  53. });
  54. exports["test map is FIFO"] = $.FIFO(function(input, assert) {
  55. assert(map(input, inc), [1, 2, 3, 4], [2, 3, 4, 5],
  56. "listeners are invoked in fifo order")
  57. });
  58. exports["test merge stream[stream]"] = function(assert) {
  59. let a = {}, b = {}, c = {};
  60. let inputs = {};
  61. let actual = [];
  62. on(merge(inputs), "data", function($) actual.push($))
  63. emit(inputs, "data", a);
  64. emit(a, "data", "a1");
  65. emit(inputs, "data", b);
  66. emit(b, "data", "b1");
  67. emit(a, "data", "a2");
  68. emit(inputs, "data", c);
  69. emit(c, "data", "c1");
  70. emit(c, "data", "c2");
  71. emit(b, "data", "b2");
  72. emit(a, "data", "a3");
  73. assert.deepEqual(actual, ["a1", "b1", "a2", "c1", "c2", "b2", "a3"],
  74. "all inputs data merged into one");
  75. };
  76. exports["test merge array[stream]"] = function(assert) {
  77. let a = {}, b = {}, c = {};
  78. let inputs = {};
  79. let actual = [];
  80. on(merge([a, b, c]), "data", function($) actual.push($))
  81. emit(a, "data", "a1");
  82. emit(b, "data", "b1");
  83. emit(a, "data", "a2");
  84. emit(c, "data", "c1");
  85. emit(c, "data", "c2");
  86. emit(b, "data", "b2");
  87. emit(a, "data", "a3");
  88. assert.deepEqual(actual, ["a1", "b1", "a2", "c1", "c2", "b2", "a3"],
  89. "all inputs data merged into one");
  90. };
  91. exports["test merge emits"] = $.emits(function(input, assert) {
  92. let evens = filter(input, isEven)
  93. let output = merge([evens, input]);
  94. assert(output, [1, 2, 3], [1, 2, 2, 3], "this is `output` & evens passed");
  95. });
  96. exports["test merge reg once"] = $.registerOnce(function(input, assert) {
  97. let evens = filter(input, isEven)
  98. let output = merge([input, evens]);
  99. assert(output, [1, 2, 3, 4], [1, 2, 2, 3, 4, 4],
  100. "listener can be registered only once");
  101. });
  102. exports["test merge ignores new"] = $.ignoreNew(function(input, assert) {
  103. let evens = filter(input, isEven)
  104. let output = merge([input, evens])
  105. assert(output, [1], [1],
  106. "new listener is ignored")
  107. });
  108. exports["test marge is FIFO"] = $.FIFO(function(input, assert) {
  109. let evens = filter(input, isEven)
  110. let output = merge([input, evens])
  111. assert(output, [1, 2, 3, 4], [1, 2, 2, 3, 4, 4],
  112. "listeners are invoked in fifo order")
  113. });
  114. exports["test expand"] = function(assert) {
  115. let a = {}, b = {}, c = {};
  116. let inputs = {};
  117. let actual = [];
  118. on(expand(inputs, function($) $()), "data", function($) actual.push($))
  119. emit(inputs, "data", function() a);
  120. emit(a, "data", "a1");
  121. emit(inputs, "data", function() b);
  122. emit(b, "data", "b1");
  123. emit(a, "data", "a2");
  124. emit(inputs, "data", function() c);
  125. emit(c, "data", "c1");
  126. emit(c, "data", "c2");
  127. emit(b, "data", "b2");
  128. emit(a, "data", "a3");
  129. assert.deepEqual(actual, ["a1", "b1", "a2", "c1", "c2", "b2", "a3"],
  130. "all inputs data merged into one");
  131. };
  132. exports["test pipe"] = function (assert, done) {
  133. let src = {};
  134. let dest = {};
  135. let aneventCount = 0, multiargsCount = 0;
  136. let wildcardCount = {};
  137. on(dest, 'an-event', arg => {
  138. assert.equal(arg, 'my-arg', 'piped argument to event');
  139. ++aneventCount;
  140. check();
  141. });
  142. on(dest, 'multiargs', (...data) => {
  143. assert.equal(data[0], 'a', 'multiple arguments passed via pipe');
  144. assert.equal(data[1], 'b', 'multiple arguments passed via pipe');
  145. assert.equal(data[2], 'c', 'multiple arguments passed via pipe');
  146. ++multiargsCount;
  147. check();
  148. });
  149. on(dest, '*', (name, ...data) => {
  150. wildcardCount[name] = (wildcardCount[name] || 0) + 1;
  151. if (name === 'multiargs') {
  152. assert.equal(data[0], 'a', 'multiple arguments passed via pipe, wildcard');
  153. assert.equal(data[1], 'b', 'multiple arguments passed via pipe, wildcard');
  154. assert.equal(data[2], 'c', 'multiple arguments passed via pipe, wildcard');
  155. }
  156. if (name === 'an-event')
  157. assert.equal(data[0], 'my-arg', 'argument passed via pipe, wildcard');
  158. check();
  159. });
  160. pipe(src, dest);
  161. for (let i = 0; i < 3; i++)
  162. emit(src, 'an-event', 'my-arg');
  163. emit(src, 'multiargs', 'a', 'b', 'c');
  164. function check () {
  165. if (aneventCount === 3 && multiargsCount === 1 &&
  166. wildcardCount['an-event'] === 3 &&
  167. wildcardCount['multiargs'] === 1)
  168. done();
  169. }
  170. };
  171. exports["test pipe multiple targets"] = function (assert) {
  172. let src1 = {};
  173. let src2 = {};
  174. let middle = {};
  175. let dest = {};
  176. pipe(src1, middle);
  177. pipe(src2, middle);
  178. pipe(middle, dest);
  179. let middleFired = 0;
  180. let destFired = 0;
  181. let src1Fired = 0;
  182. let src2Fired = 0;
  183. on(src1, '*', () => src1Fired++);
  184. on(src2, '*', () => src2Fired++);
  185. on(middle, '*', () => middleFired++);
  186. on(dest, '*', () => destFired++);
  187. emit(src1, 'ev');
  188. assert.equal(src1Fired, 1, 'event triggers in source in pipe chain');
  189. assert.equal(middleFired, 1, 'event passes through the middle of pipe chain');
  190. assert.equal(destFired, 1, 'event propagates to end of pipe chain');
  191. assert.equal(src2Fired, 0, 'event does not fire on alternative chain routes');
  192. emit(src2, 'ev');
  193. assert.equal(src2Fired, 1, 'event triggers in source in pipe chain');
  194. assert.equal(middleFired, 2,
  195. 'event passes through the middle of pipe chain from different src');
  196. assert.equal(destFired, 2,
  197. 'event propagates to end of pipe chain from different src');
  198. assert.equal(src1Fired, 1, 'event does not fire on alternative chain routes');
  199. emit(middle, 'ev');
  200. assert.equal(middleFired, 3,
  201. 'event triggers in source of pipe chain');
  202. assert.equal(destFired, 3,
  203. 'event propagates to end of pipe chain from middle src');
  204. assert.equal(src1Fired, 1, 'event does not fire on alternative chain routes');
  205. assert.equal(src2Fired, 1, 'event does not fire on alternative chain routes');
  206. };
  207. exports['test stripListeners'] = function (assert) {
  208. var options = {
  209. onAnEvent: noop1,
  210. onMessage: noop2,
  211. verb: noop1,
  212. value: 100
  213. };
  214. var stripped = stripListeners(options);
  215. assert.ok(stripped !== options, 'stripListeners should return a new object');
  216. assert.equal(options.onAnEvent, noop1, 'stripListeners does not affect original');
  217. assert.equal(options.onMessage, noop2, 'stripListeners does not affect original');
  218. assert.equal(options.verb, noop1, 'stripListeners does not affect original');
  219. assert.equal(options.value, 100, 'stripListeners does not affect original');
  220. assert.ok(!stripped.onAnEvent, 'stripListeners removes `on*` values');
  221. assert.ok(!stripped.onMessage, 'stripListeners removes `on*` values');
  222. assert.equal(stripped.verb, noop1, 'stripListeners leaves not `on*` values');
  223. assert.equal(stripped.value, 100, 'stripListeners leaves not `on*` values');
  224. function noop1 () {}
  225. function noop2 () {}
  226. };
  227. require('test').run(exports);