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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

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, once, off, emit, count } = require('sdk/event/core');
michael@0 8 const { LoaderWithHookedConsole } = require("sdk/test/loader");
michael@0 9
michael@0 10 exports['test add a listener'] = function(assert) {
michael@0 11 let events = [ { name: 'event#1' }, 'event#2' ];
michael@0 12 let target = { name: 'target' };
michael@0 13
michael@0 14 on(target, 'message', function(message) {
michael@0 15 assert.equal(this, target, 'this is a target object');
michael@0 16 assert.equal(message, events.shift(), 'message is emitted event');
michael@0 17 });
michael@0 18 emit(target, 'message', events[0]);
michael@0 19 emit(target, 'message', events[0]);
michael@0 20 };
michael@0 21
michael@0 22 exports['test that listener is unique per type'] = function(assert) {
michael@0 23 let actual = []
michael@0 24 let target = {}
michael@0 25 function listener() { actual.push(1) }
michael@0 26 on(target, 'message', listener);
michael@0 27 on(target, 'message', listener);
michael@0 28 on(target, 'message', listener);
michael@0 29 on(target, 'foo', listener);
michael@0 30 on(target, 'foo', listener);
michael@0 31
michael@0 32 emit(target, 'message');
michael@0 33 assert.deepEqual([ 1 ], actual, 'only one message listener added');
michael@0 34 emit(target, 'foo');
michael@0 35 assert.deepEqual([ 1, 1 ], actual, 'same listener added for other event');
michael@0 36 };
michael@0 37
michael@0 38 exports['test event type matters'] = function(assert) {
michael@0 39 let target = { name: 'target' }
michael@0 40 on(target, 'message', function() {
michael@0 41 assert.fail('no event is expected');
michael@0 42 });
michael@0 43 on(target, 'done', function() {
michael@0 44 assert.pass('event is emitted');
michael@0 45 });
michael@0 46 emit(target, 'foo')
michael@0 47 emit(target, 'done');
michael@0 48 };
michael@0 49
michael@0 50 exports['test all arguments are pasesd'] = function(assert) {
michael@0 51 let foo = { name: 'foo' }, bar = 'bar';
michael@0 52 let target = { name: 'target' };
michael@0 53 on(target, 'message', function(a, b) {
michael@0 54 assert.equal(a, foo, 'first argument passed');
michael@0 55 assert.equal(b, bar, 'second argument passed');
michael@0 56 });
michael@0 57 emit(target, 'message', foo, bar);
michael@0 58 };
michael@0 59
michael@0 60 exports['test no side-effects in emit'] = function(assert) {
michael@0 61 let target = { name: 'target' };
michael@0 62 on(target, 'message', function() {
michael@0 63 assert.pass('first listener is called');
michael@0 64 on(target, 'message', function() {
michael@0 65 assert.fail('second listener is called');
michael@0 66 });
michael@0 67 });
michael@0 68 emit(target, 'message');
michael@0 69 };
michael@0 70
michael@0 71 exports['test can remove next listener'] = function(assert) {
michael@0 72 let target = { name: 'target' };
michael@0 73 function fail() assert.fail('Listener should be removed');
michael@0 74
michael@0 75 on(target, 'data', function() {
michael@0 76 assert.pass('first litener called');
michael@0 77 off(target, 'data', fail);
michael@0 78 });
michael@0 79 on(target, 'data', fail);
michael@0 80
michael@0 81 emit(target, 'data', 'hello');
michael@0 82 };
michael@0 83
michael@0 84 exports['test order of propagation'] = function(assert) {
michael@0 85 let actual = [];
michael@0 86 let target = { name: 'target' };
michael@0 87 on(target, 'message', function() { actual.push(1); });
michael@0 88 on(target, 'message', function() { actual.push(2); });
michael@0 89 on(target, 'message', function() { actual.push(3); });
michael@0 90 emit(target, 'message');
michael@0 91 assert.deepEqual([ 1, 2, 3 ], actual, 'called in order they were added');
michael@0 92 };
michael@0 93
michael@0 94 exports['test remove a listener'] = function(assert) {
michael@0 95 let target = { name: 'target' };
michael@0 96 let actual = [];
michael@0 97 on(target, 'message', function listener() {
michael@0 98 actual.push(1);
michael@0 99 on(target, 'message', function() {
michael@0 100 off(target, 'message', listener);
michael@0 101 actual.push(2);
michael@0 102 })
michael@0 103 });
michael@0 104
michael@0 105 emit(target, 'message');
michael@0 106 assert.deepEqual([ 1 ], actual, 'first listener called');
michael@0 107 emit(target, 'message');
michael@0 108 assert.deepEqual([ 1, 1, 2 ], actual, 'second listener called');
michael@0 109
michael@0 110 emit(target, 'message');
michael@0 111 assert.deepEqual([ 1, 1, 2, 2, 2 ], actual, 'first listener removed');
michael@0 112 };
michael@0 113
michael@0 114 exports['test remove all listeners for type'] = function(assert) {
michael@0 115 let actual = [];
michael@0 116 let target = { name: 'target' }
michael@0 117 on(target, 'message', function() { actual.push(1); });
michael@0 118 on(target, 'message', function() { actual.push(2); });
michael@0 119 on(target, 'message', function() { actual.push(3); });
michael@0 120 on(target, 'bar', function() { actual.push('b') });
michael@0 121 off(target, 'message');
michael@0 122
michael@0 123 emit(target, 'message');
michael@0 124 emit(target, 'bar');
michael@0 125
michael@0 126 assert.deepEqual([ 'b' ], actual, 'all message listeners were removed');
michael@0 127 };
michael@0 128
michael@0 129 exports['test remove all listeners'] = function(assert) {
michael@0 130 let actual = [];
michael@0 131 let target = { name: 'target' }
michael@0 132 on(target, 'message', function() { actual.push(1); });
michael@0 133 on(target, 'message', function() { actual.push(2); });
michael@0 134 on(target, 'message', function() { actual.push(3); });
michael@0 135 on(target, 'bar', function() { actual.push('b') });
michael@0 136 off(target);
michael@0 137
michael@0 138 emit(target, 'message');
michael@0 139 emit(target, 'bar');
michael@0 140
michael@0 141 assert.deepEqual([], actual, 'all listeners events were removed');
michael@0 142 };
michael@0 143
michael@0 144 exports['test falsy arguments are fine'] = function(assert) {
michael@0 145 let type, listener, actual = [];
michael@0 146 let target = { name: 'target' }
michael@0 147 on(target, 'bar', function() { actual.push(0) });
michael@0 148
michael@0 149 off(target, 'bar', listener);
michael@0 150 emit(target, 'bar');
michael@0 151 assert.deepEqual([ 0 ], actual, '3rd bad ard will keep listeners');
michael@0 152
michael@0 153 off(target, type);
michael@0 154 emit(target, 'bar');
michael@0 155 assert.deepEqual([ 0, 0 ], actual, '2nd bad arg will keep listener');
michael@0 156
michael@0 157 off(target, type, listener);
michael@0 158 emit(target, 'bar');
michael@0 159 assert.deepEqual([ 0, 0, 0 ], actual, '2nd&3rd bad args will keep listener');
michael@0 160 };
michael@0 161
michael@0 162 exports['test error handling'] = function(assert) {
michael@0 163 let target = Object.create(null);
michael@0 164 let error = Error('boom!');
michael@0 165
michael@0 166 on(target, 'message', function() { throw error; })
michael@0 167 on(target, 'error', function(boom) {
michael@0 168 assert.equal(boom, error, 'thrown exception causes error event');
michael@0 169 });
michael@0 170 emit(target, 'message');
michael@0 171 };
michael@0 172
michael@0 173 exports['test unhandled exceptions'] = function(assert) {
michael@0 174 let exceptions = [];
michael@0 175 let { loader, messages } = LoaderWithHookedConsole(module);
michael@0 176
michael@0 177 let { emit, on } = loader.require('sdk/event/core');
michael@0 178 let target = {};
michael@0 179 let boom = Error('Boom!');
michael@0 180 let drax = Error('Draax!!');
michael@0 181
michael@0 182 on(target, 'message', function() { throw boom; });
michael@0 183
michael@0 184 emit(target, 'message');
michael@0 185 assert.equal(messages.length, 1, 'Got the first exception');
michael@0 186 assert.equal(messages[0].type, 'exception', 'The console message is exception');
michael@0 187 assert.ok(~String(messages[0].msg).indexOf('Boom!'),
michael@0 188 'unhandled exception is logged');
michael@0 189
michael@0 190 on(target, 'error', function() { throw drax; });
michael@0 191 emit(target, 'message');
michael@0 192 assert.equal(messages.length, 2, 'Got the second exception');
michael@0 193 assert.equal(messages[1].type, 'exception', 'The console message is exception');
michael@0 194 assert.ok(~String(messages[1].msg).indexOf('Draax!'),
michael@0 195 'error in error handler is logged');
michael@0 196 };
michael@0 197
michael@0 198 exports['test unhandled errors'] = function(assert) {
michael@0 199 let exceptions = [];
michael@0 200 let { loader, messages } = LoaderWithHookedConsole(module);
michael@0 201
michael@0 202 let { emit, on } = loader.require('sdk/event/core');
michael@0 203 let target = {};
michael@0 204 let boom = Error('Boom!');
michael@0 205
michael@0 206 emit(target, 'error', boom);
michael@0 207 assert.equal(messages.length, 1, 'Error was logged');
michael@0 208 assert.equal(messages[0].type, 'exception', 'The console message is exception');
michael@0 209 assert.ok(~String(messages[0].msg).indexOf('Boom!'),
michael@0 210 'unhandled exception is logged');
michael@0 211 };
michael@0 212
michael@0 213
michael@0 214 exports['test count'] = function(assert) {
michael@0 215 let target = {};
michael@0 216
michael@0 217 assert.equal(count(target, 'foo'), 0, 'no listeners for "foo" events');
michael@0 218 on(target, 'foo', function() {});
michael@0 219 assert.equal(count(target, 'foo'), 1, 'listener registered');
michael@0 220 on(target, 'foo', function() {}, 2, 'another listener registered');
michael@0 221 off(target)
michael@0 222 assert.equal(count(target, 'foo'), 0, 'listeners unregistered');
michael@0 223 };
michael@0 224
michael@0 225 exports['test listen to all events'] = function(assert) {
michael@0 226 let actual = [];
michael@0 227 let target = {};
michael@0 228
michael@0 229 on(target, 'foo', message => actual.push(message));
michael@0 230 on(target, '*', (type, ...message) => {
michael@0 231 actual.push([type].concat(message));
michael@0 232 });
michael@0 233
michael@0 234 emit(target, 'foo', 'hello');
michael@0 235 assert.equal(actual[0], 'hello',
michael@0 236 'non-wildcard listeners still work');
michael@0 237 assert.deepEqual(actual[1], ['foo', 'hello'],
michael@0 238 'wildcard listener called');
michael@0 239
michael@0 240 emit(target, 'bar', 'goodbye');
michael@0 241 assert.deepEqual(actual[2], ['bar', 'goodbye'],
michael@0 242 'wildcard listener called for unbound event name');
michael@0 243 };
michael@0 244
michael@0 245 require('test').run(exports);

mercurial