addon-sdk/source/test/test-events.js

Thu, 15 Jan 2015 15:59:08 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:59:08 +0100
branch
TOR_BUG_9701
changeset 10
ac0c01689b40
permissions
-rw-r--r--

Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

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 'use strict';
michael@0 5
michael@0 6 const { LoaderWithHookedConsole } = require("sdk/test/loader");
michael@0 7
michael@0 8 // Exposing private methods as public in order to test
michael@0 9 const EventEmitter = require('sdk/deprecated/events').EventEmitter.compose({
michael@0 10 listeners: function(type) this._listeners(type),
michael@0 11 emit: function() this._emit.apply(this, arguments),
michael@0 12 emitOnObject: function() this._emitOnObject.apply(this, arguments),
michael@0 13 removeAllListeners: function(type) this._removeAllListeners(type)
michael@0 14 });
michael@0 15
michael@0 16 exports['test:add listeners'] = function(assert) {
michael@0 17 let e = new EventEmitter();
michael@0 18
michael@0 19 let events_new_listener_emited = [];
michael@0 20 let times_hello_emited = 0;
michael@0 21
michael@0 22 e.on("newListener", function (event, listener) {
michael@0 23 events_new_listener_emited.push(event)
michael@0 24 })
michael@0 25
michael@0 26 e.on("hello", function (a, b) {
michael@0 27 times_hello_emited += 1
michael@0 28 assert.equal("a", a)
michael@0 29 assert.equal("b", b)
michael@0 30 assert.equal(this, e, '`this` pseudo-variable is bound to instance');
michael@0 31 })
michael@0 32
michael@0 33 e.emit("hello", "a", "b")
michael@0 34 };
michael@0 35
michael@0 36 exports['test:removeListener'] = function(assert) {
michael@0 37 let count = 0;
michael@0 38
michael@0 39 function listener1 () {
michael@0 40 count++;
michael@0 41 }
michael@0 42 function listener2 () {
michael@0 43 count++;
michael@0 44 }
michael@0 45
michael@0 46 // test adding and removing listener
michael@0 47 let e1 = new EventEmitter();
michael@0 48 assert.equal(0, e1.listeners('hello').length);
michael@0 49 e1.on("hello", listener1);
michael@0 50 assert.equal(1, e1.listeners('hello').length);
michael@0 51 assert.equal(listener1, e1.listeners('hello')[0]);
michael@0 52 e1.removeListener("hello", listener1);
michael@0 53 assert.equal(0, e1.listeners('hello').length);
michael@0 54 e1.emit("hello", "");
michael@0 55 assert.equal(0, count);
michael@0 56
michael@0 57 // test adding one listener and removing another which was not added
michael@0 58 let e2 = new EventEmitter();
michael@0 59 assert.equal(0, e2.listeners('hello').length);
michael@0 60 e2.on("hello", listener1);
michael@0 61 assert.equal(1, e2.listeners('hello').length);
michael@0 62 e2.removeListener("hello", listener2);
michael@0 63 assert.equal(1, e2.listeners('hello').length);
michael@0 64 assert.equal(listener1, e2.listeners('hello')[0]);
michael@0 65 e2.emit("hello", "");
michael@0 66 assert.equal(1, count);
michael@0 67
michael@0 68 // test adding 2 listeners, and removing one
michael@0 69 let e3 = new EventEmitter();
michael@0 70 assert.equal(0, e3.listeners('hello').length);
michael@0 71 e3.on("hello", listener1);
michael@0 72 assert.equal(1, e3.listeners('hello').length);
michael@0 73 e3.on("hello", listener2);
michael@0 74 assert.equal(2, e3.listeners('hello').length);
michael@0 75 e3.removeListener("hello", listener1);
michael@0 76 assert.equal(1, e3.listeners('hello').length);
michael@0 77 assert.equal(listener2, e3.listeners('hello')[0]);
michael@0 78 e3.emit("hello", "");
michael@0 79 assert.equal(2, count);
michael@0 80 };
michael@0 81
michael@0 82 exports['test:removeAllListeners'] = function(assert) {
michael@0 83 let count = 0;
michael@0 84
michael@0 85 function listener1 () {
michael@0 86 count++;
michael@0 87 }
michael@0 88 function listener2 () {
michael@0 89 count++;
michael@0 90 }
michael@0 91
michael@0 92 // test adding a listener and removing all of that type
michael@0 93 let e1 = new EventEmitter();
michael@0 94 e1.on("hello", listener1);
michael@0 95 assert.equal(1, e1.listeners('hello').length);
michael@0 96 e1.removeAllListeners("hello");
michael@0 97 assert.equal(0, e1.listeners('hello').length);
michael@0 98 e1.emit("hello", "");
michael@0 99 assert.equal(0, count);
michael@0 100
michael@0 101 // test adding a listener and removing all of another type
michael@0 102 let e2 = new EventEmitter();
michael@0 103 e2.on("hello", listener1);
michael@0 104 assert.equal(1, e2.listeners('hello').length);
michael@0 105 e2.removeAllListeners('goodbye');
michael@0 106 assert.equal(1, e2.listeners('hello').length);
michael@0 107 e2.emit("hello", "");
michael@0 108 assert.equal(1, count);
michael@0 109
michael@0 110 // test adding 1+ listeners and removing all of that type
michael@0 111 let e3 = new EventEmitter();
michael@0 112 e3.on("hello", listener1);
michael@0 113 assert.equal(1, e3.listeners('hello').length);
michael@0 114 e3.on("hello", listener2);
michael@0 115 assert.equal(2, e3.listeners('hello').length);
michael@0 116 e3.removeAllListeners("hello");
michael@0 117 assert.equal(0, e3.listeners('hello').length);
michael@0 118 e3.emit("hello", "");
michael@0 119 assert.equal(1, count);
michael@0 120
michael@0 121 // test adding 2 listeners for 2 types and removing all listeners
michael@0 122 let e4 = new EventEmitter();
michael@0 123 e4.on("hello", listener1);
michael@0 124 assert.equal(1, e4.listeners('hello').length);
michael@0 125 e4.on('goodbye', listener2);
michael@0 126 assert.equal(1, e4.listeners('goodbye').length);
michael@0 127 e4.emit("goodbye", "");
michael@0 128 e4.removeAllListeners();
michael@0 129 assert.equal(0, e4.listeners('hello').length);
michael@0 130 assert.equal(0, e4.listeners('goodbye').length);
michael@0 131 e4.emit("hello", "");
michael@0 132 e4.emit("goodbye", "");
michael@0 133 assert.equal(2, count);
michael@0 134 };
michael@0 135
michael@0 136 exports['test: modify in emit'] = function(assert) {
michael@0 137 let callbacks_called = [ ];
michael@0 138 let e = new EventEmitter();
michael@0 139
michael@0 140 function callback1() {
michael@0 141 callbacks_called.push("callback1");
michael@0 142 e.on("foo", callback2);
michael@0 143 e.on("foo", callback3);
michael@0 144 e.removeListener("foo", callback1);
michael@0 145 }
michael@0 146 function callback2() {
michael@0 147 callbacks_called.push("callback2");
michael@0 148 e.removeListener("foo", callback2);
michael@0 149 }
michael@0 150 function callback3() {
michael@0 151 callbacks_called.push("callback3");
michael@0 152 e.removeListener("foo", callback3);
michael@0 153 }
michael@0 154
michael@0 155 e.on("foo", callback1);
michael@0 156 assert.equal(1, e.listeners("foo").length);
michael@0 157
michael@0 158 e.emit("foo");
michael@0 159 assert.equal(2, e.listeners("foo").length);
michael@0 160 assert.equal(1, callbacks_called.length);
michael@0 161 assert.equal('callback1', callbacks_called[0]);
michael@0 162
michael@0 163 e.emit("foo");
michael@0 164 assert.equal(0, e.listeners("foo").length);
michael@0 165 assert.equal(3, callbacks_called.length);
michael@0 166 assert.equal('callback1', callbacks_called[0]);
michael@0 167 assert.equal('callback2', callbacks_called[1]);
michael@0 168 assert.equal('callback3', callbacks_called[2]);
michael@0 169
michael@0 170 e.emit("foo");
michael@0 171 assert.equal(0, e.listeners("foo").length);
michael@0 172 assert.equal(3, callbacks_called.length);
michael@0 173 assert.equal('callback1', callbacks_called[0]);
michael@0 174 assert.equal('callback2', callbacks_called[1]);
michael@0 175 assert.equal('callback3', callbacks_called[2]);
michael@0 176
michael@0 177 e.on("foo", callback1);
michael@0 178 e.on("foo", callback2);
michael@0 179 assert.equal(2, e.listeners("foo").length);
michael@0 180 e.removeAllListeners("foo");
michael@0 181 assert.equal(0, e.listeners("foo").length);
michael@0 182
michael@0 183 // Verify that removing callbacks while in emit allows emits to propagate to
michael@0 184 // all listeners
michael@0 185 callbacks_called = [ ];
michael@0 186
michael@0 187 e.on("foo", callback2);
michael@0 188 e.on("foo", callback3);
michael@0 189 assert.equal(2, e.listeners("foo").length);
michael@0 190 e.emit("foo");
michael@0 191 assert.equal(2, callbacks_called.length);
michael@0 192 assert.equal('callback2', callbacks_called[0]);
michael@0 193 assert.equal('callback3', callbacks_called[1]);
michael@0 194 assert.equal(0, e.listeners("foo").length);
michael@0 195 };
michael@0 196
michael@0 197 exports['test:adding same listener'] = function(assert) {
michael@0 198 function foo() {}
michael@0 199 let e = new EventEmitter();
michael@0 200 e.on("foo", foo);
michael@0 201 e.on("foo", foo);
michael@0 202 assert.equal(
michael@0 203 1,
michael@0 204 e.listeners("foo").length,
michael@0 205 "listener reregistration is ignored"
michael@0 206 );
michael@0 207 }
michael@0 208
michael@0 209 exports['test:errors are reported if listener throws'] = function(assert) {
michael@0 210 let e = new EventEmitter(),
michael@0 211 reported = false;
michael@0 212 e.on('error', function(e) reported = true)
michael@0 213 e.on('boom', function() { throw new Error('Boom!') });
michael@0 214 e.emit('boom', 3);
michael@0 215 assert.ok(reported, 'error should be reported through event');
michael@0 216 };
michael@0 217
michael@0 218 exports['test:emitOnObject'] = function(assert) {
michael@0 219 let e = new EventEmitter();
michael@0 220
michael@0 221 e.on("foo", function() {
michael@0 222 assert.equal(this, e, "`this` should be emitter");
michael@0 223 });
michael@0 224 e.emitOnObject(e, "foo");
michael@0 225
michael@0 226 e.on("bar", function() {
michael@0 227 assert.equal(this, obj, "`this` should be other object");
michael@0 228 });
michael@0 229 let obj = {};
michael@0 230 e.emitOnObject(obj, "bar");
michael@0 231 };
michael@0 232
michael@0 233 exports['test:once'] = function(assert) {
michael@0 234 let e = new EventEmitter();
michael@0 235 let called = false;
michael@0 236
michael@0 237 e.once('foo', function(value) {
michael@0 238 assert.ok(!called, "listener called only once");
michael@0 239 assert.equal(value, "bar", "correct argument was passed");
michael@0 240 });
michael@0 241
michael@0 242 e.emit('foo', 'bar');
michael@0 243 e.emit('foo', 'baz');
michael@0 244 };
michael@0 245
michael@0 246 exports["test:removing once"] = function(assert) {
michael@0 247 let e = require("sdk/deprecated/events").EventEmitterTrait.create();
michael@0 248 e.once("foo", function() { assert.pass("listener was called"); });
michael@0 249 e.once("error", function() { assert.fail("error event was emitted"); });
michael@0 250 e._emit("foo", "bug-656684");
michael@0 251 };
michael@0 252
michael@0 253 // Bug 726967: Ensure that `emit` doesn't do an infinite loop when `error`
michael@0 254 // listener throws an exception
michael@0 255 exports['test:emitLoop'] = function(assert) {
michael@0 256 // Override the console for this test so it doesn't log the exception to the
michael@0 257 // test output
michael@0 258 let { loader } = LoaderWithHookedConsole(module);
michael@0 259
michael@0 260 let EventEmitter = loader.require('sdk/deprecated/events').EventEmitter.compose({
michael@0 261 listeners: function(type) this._listeners(type),
michael@0 262 emit: function() this._emit.apply(this, arguments),
michael@0 263 emitOnObject: function() this._emitOnObject.apply(this, arguments),
michael@0 264 removeAllListeners: function(type) this._removeAllListeners(type)
michael@0 265 });
michael@0 266
michael@0 267 let e = new EventEmitter();
michael@0 268
michael@0 269 e.on("foo", function() {
michael@0 270 throw new Error("foo");
michael@0 271 });
michael@0 272
michael@0 273 e.on("error", function() {
michael@0 274 throw new Error("error");
michael@0 275 });
michael@0 276 e.emit("foo");
michael@0 277
michael@0 278 assert.pass("emit didn't looped");
michael@0 279 };
michael@0 280
michael@0 281 require('sdk/test').run(exports);

mercurial