1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/addon-sdk/source/test/test-events.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,281 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 +'use strict'; 1.8 + 1.9 +const { LoaderWithHookedConsole } = require("sdk/test/loader"); 1.10 + 1.11 +// Exposing private methods as public in order to test 1.12 +const EventEmitter = require('sdk/deprecated/events').EventEmitter.compose({ 1.13 + listeners: function(type) this._listeners(type), 1.14 + emit: function() this._emit.apply(this, arguments), 1.15 + emitOnObject: function() this._emitOnObject.apply(this, arguments), 1.16 + removeAllListeners: function(type) this._removeAllListeners(type) 1.17 +}); 1.18 + 1.19 +exports['test:add listeners'] = function(assert) { 1.20 + let e = new EventEmitter(); 1.21 + 1.22 + let events_new_listener_emited = []; 1.23 + let times_hello_emited = 0; 1.24 + 1.25 + e.on("newListener", function (event, listener) { 1.26 + events_new_listener_emited.push(event) 1.27 + }) 1.28 + 1.29 + e.on("hello", function (a, b) { 1.30 + times_hello_emited += 1 1.31 + assert.equal("a", a) 1.32 + assert.equal("b", b) 1.33 + assert.equal(this, e, '`this` pseudo-variable is bound to instance'); 1.34 + }) 1.35 + 1.36 + e.emit("hello", "a", "b") 1.37 +}; 1.38 + 1.39 +exports['test:removeListener'] = function(assert) { 1.40 + let count = 0; 1.41 + 1.42 + function listener1 () { 1.43 + count++; 1.44 + } 1.45 + function listener2 () { 1.46 + count++; 1.47 + } 1.48 + 1.49 + // test adding and removing listener 1.50 + let e1 = new EventEmitter(); 1.51 + assert.equal(0, e1.listeners('hello').length); 1.52 + e1.on("hello", listener1); 1.53 + assert.equal(1, e1.listeners('hello').length); 1.54 + assert.equal(listener1, e1.listeners('hello')[0]); 1.55 + e1.removeListener("hello", listener1); 1.56 + assert.equal(0, e1.listeners('hello').length); 1.57 + e1.emit("hello", ""); 1.58 + assert.equal(0, count); 1.59 + 1.60 + // test adding one listener and removing another which was not added 1.61 + let e2 = new EventEmitter(); 1.62 + assert.equal(0, e2.listeners('hello').length); 1.63 + e2.on("hello", listener1); 1.64 + assert.equal(1, e2.listeners('hello').length); 1.65 + e2.removeListener("hello", listener2); 1.66 + assert.equal(1, e2.listeners('hello').length); 1.67 + assert.equal(listener1, e2.listeners('hello')[0]); 1.68 + e2.emit("hello", ""); 1.69 + assert.equal(1, count); 1.70 + 1.71 + // test adding 2 listeners, and removing one 1.72 + let e3 = new EventEmitter(); 1.73 + assert.equal(0, e3.listeners('hello').length); 1.74 + e3.on("hello", listener1); 1.75 + assert.equal(1, e3.listeners('hello').length); 1.76 + e3.on("hello", listener2); 1.77 + assert.equal(2, e3.listeners('hello').length); 1.78 + e3.removeListener("hello", listener1); 1.79 + assert.equal(1, e3.listeners('hello').length); 1.80 + assert.equal(listener2, e3.listeners('hello')[0]); 1.81 + e3.emit("hello", ""); 1.82 + assert.equal(2, count); 1.83 +}; 1.84 + 1.85 +exports['test:removeAllListeners'] = function(assert) { 1.86 + let count = 0; 1.87 + 1.88 + function listener1 () { 1.89 + count++; 1.90 + } 1.91 + function listener2 () { 1.92 + count++; 1.93 + } 1.94 + 1.95 + // test adding a listener and removing all of that type 1.96 + let e1 = new EventEmitter(); 1.97 + e1.on("hello", listener1); 1.98 + assert.equal(1, e1.listeners('hello').length); 1.99 + e1.removeAllListeners("hello"); 1.100 + assert.equal(0, e1.listeners('hello').length); 1.101 + e1.emit("hello", ""); 1.102 + assert.equal(0, count); 1.103 + 1.104 + // test adding a listener and removing all of another type 1.105 + let e2 = new EventEmitter(); 1.106 + e2.on("hello", listener1); 1.107 + assert.equal(1, e2.listeners('hello').length); 1.108 + e2.removeAllListeners('goodbye'); 1.109 + assert.equal(1, e2.listeners('hello').length); 1.110 + e2.emit("hello", ""); 1.111 + assert.equal(1, count); 1.112 + 1.113 + // test adding 1+ listeners and removing all of that type 1.114 + let e3 = new EventEmitter(); 1.115 + e3.on("hello", listener1); 1.116 + assert.equal(1, e3.listeners('hello').length); 1.117 + e3.on("hello", listener2); 1.118 + assert.equal(2, e3.listeners('hello').length); 1.119 + e3.removeAllListeners("hello"); 1.120 + assert.equal(0, e3.listeners('hello').length); 1.121 + e3.emit("hello", ""); 1.122 + assert.equal(1, count); 1.123 + 1.124 + // test adding 2 listeners for 2 types and removing all listeners 1.125 + let e4 = new EventEmitter(); 1.126 + e4.on("hello", listener1); 1.127 + assert.equal(1, e4.listeners('hello').length); 1.128 + e4.on('goodbye', listener2); 1.129 + assert.equal(1, e4.listeners('goodbye').length); 1.130 + e4.emit("goodbye", ""); 1.131 + e4.removeAllListeners(); 1.132 + assert.equal(0, e4.listeners('hello').length); 1.133 + assert.equal(0, e4.listeners('goodbye').length); 1.134 + e4.emit("hello", ""); 1.135 + e4.emit("goodbye", ""); 1.136 + assert.equal(2, count); 1.137 +}; 1.138 + 1.139 +exports['test: modify in emit'] = function(assert) { 1.140 + let callbacks_called = [ ]; 1.141 + let e = new EventEmitter(); 1.142 + 1.143 + function callback1() { 1.144 + callbacks_called.push("callback1"); 1.145 + e.on("foo", callback2); 1.146 + e.on("foo", callback3); 1.147 + e.removeListener("foo", callback1); 1.148 + } 1.149 + function callback2() { 1.150 + callbacks_called.push("callback2"); 1.151 + e.removeListener("foo", callback2); 1.152 + } 1.153 + function callback3() { 1.154 + callbacks_called.push("callback3"); 1.155 + e.removeListener("foo", callback3); 1.156 + } 1.157 + 1.158 + e.on("foo", callback1); 1.159 + assert.equal(1, e.listeners("foo").length); 1.160 + 1.161 + e.emit("foo"); 1.162 + assert.equal(2, e.listeners("foo").length); 1.163 + assert.equal(1, callbacks_called.length); 1.164 + assert.equal('callback1', callbacks_called[0]); 1.165 + 1.166 + e.emit("foo"); 1.167 + assert.equal(0, e.listeners("foo").length); 1.168 + assert.equal(3, callbacks_called.length); 1.169 + assert.equal('callback1', callbacks_called[0]); 1.170 + assert.equal('callback2', callbacks_called[1]); 1.171 + assert.equal('callback3', callbacks_called[2]); 1.172 + 1.173 + e.emit("foo"); 1.174 + assert.equal(0, e.listeners("foo").length); 1.175 + assert.equal(3, callbacks_called.length); 1.176 + assert.equal('callback1', callbacks_called[0]); 1.177 + assert.equal('callback2', callbacks_called[1]); 1.178 + assert.equal('callback3', callbacks_called[2]); 1.179 + 1.180 + e.on("foo", callback1); 1.181 + e.on("foo", callback2); 1.182 + assert.equal(2, e.listeners("foo").length); 1.183 + e.removeAllListeners("foo"); 1.184 + assert.equal(0, e.listeners("foo").length); 1.185 + 1.186 + // Verify that removing callbacks while in emit allows emits to propagate to 1.187 + // all listeners 1.188 + callbacks_called = [ ]; 1.189 + 1.190 + e.on("foo", callback2); 1.191 + e.on("foo", callback3); 1.192 + assert.equal(2, e.listeners("foo").length); 1.193 + e.emit("foo"); 1.194 + assert.equal(2, callbacks_called.length); 1.195 + assert.equal('callback2', callbacks_called[0]); 1.196 + assert.equal('callback3', callbacks_called[1]); 1.197 + assert.equal(0, e.listeners("foo").length); 1.198 +}; 1.199 + 1.200 +exports['test:adding same listener'] = function(assert) { 1.201 + function foo() {} 1.202 + let e = new EventEmitter(); 1.203 + e.on("foo", foo); 1.204 + e.on("foo", foo); 1.205 + assert.equal( 1.206 + 1, 1.207 + e.listeners("foo").length, 1.208 + "listener reregistration is ignored" 1.209 + ); 1.210 +} 1.211 + 1.212 +exports['test:errors are reported if listener throws'] = function(assert) { 1.213 + let e = new EventEmitter(), 1.214 + reported = false; 1.215 + e.on('error', function(e) reported = true) 1.216 + e.on('boom', function() { throw new Error('Boom!') }); 1.217 + e.emit('boom', 3); 1.218 + assert.ok(reported, 'error should be reported through event'); 1.219 +}; 1.220 + 1.221 +exports['test:emitOnObject'] = function(assert) { 1.222 + let e = new EventEmitter(); 1.223 + 1.224 + e.on("foo", function() { 1.225 + assert.equal(this, e, "`this` should be emitter"); 1.226 + }); 1.227 + e.emitOnObject(e, "foo"); 1.228 + 1.229 + e.on("bar", function() { 1.230 + assert.equal(this, obj, "`this` should be other object"); 1.231 + }); 1.232 + let obj = {}; 1.233 + e.emitOnObject(obj, "bar"); 1.234 +}; 1.235 + 1.236 +exports['test:once'] = function(assert) { 1.237 + let e = new EventEmitter(); 1.238 + let called = false; 1.239 + 1.240 + e.once('foo', function(value) { 1.241 + assert.ok(!called, "listener called only once"); 1.242 + assert.equal(value, "bar", "correct argument was passed"); 1.243 + }); 1.244 + 1.245 + e.emit('foo', 'bar'); 1.246 + e.emit('foo', 'baz'); 1.247 +}; 1.248 + 1.249 +exports["test:removing once"] = function(assert) { 1.250 + let e = require("sdk/deprecated/events").EventEmitterTrait.create(); 1.251 + e.once("foo", function() { assert.pass("listener was called"); }); 1.252 + e.once("error", function() { assert.fail("error event was emitted"); }); 1.253 + e._emit("foo", "bug-656684"); 1.254 +}; 1.255 + 1.256 +// Bug 726967: Ensure that `emit` doesn't do an infinite loop when `error` 1.257 +// listener throws an exception 1.258 +exports['test:emitLoop'] = function(assert) { 1.259 + // Override the console for this test so it doesn't log the exception to the 1.260 + // test output 1.261 + let { loader } = LoaderWithHookedConsole(module); 1.262 + 1.263 + let EventEmitter = loader.require('sdk/deprecated/events').EventEmitter.compose({ 1.264 + listeners: function(type) this._listeners(type), 1.265 + emit: function() this._emit.apply(this, arguments), 1.266 + emitOnObject: function() this._emitOnObject.apply(this, arguments), 1.267 + removeAllListeners: function(type) this._removeAllListeners(type) 1.268 + }); 1.269 + 1.270 + let e = new EventEmitter(); 1.271 + 1.272 + e.on("foo", function() { 1.273 + throw new Error("foo"); 1.274 + }); 1.275 + 1.276 + e.on("error", function() { 1.277 + throw new Error("error"); 1.278 + }); 1.279 + e.emit("foo"); 1.280 + 1.281 + assert.pass("emit didn't looped"); 1.282 +}; 1.283 + 1.284 +require('sdk/test').run(exports);