addon-sdk/source/test/test-event-utils.js

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

mercurial