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