gecko-dev/toolkit/modules/tests/xpcshell/test_EventEmitter.js
Kris Maglione e930b89c34 Bug 1514594: Part 3 - Change ChromeUtils.import API.
***
Bug 1514594: Part 3a - Change ChromeUtils.import to return an exports object; not pollute global. r=mccr8

This changes the behavior of ChromeUtils.import() to return an exports object,
rather than a module global, in all cases except when `null` is passed as a
second argument, and changes the default behavior not to pollute the global
scope with the module's exports. Thus, the following code written for the old
model:

  ChromeUtils.import("resource://gre/modules/Services.jsm");

is approximately the same as the following, in the new model:

  var {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");

Since the two behaviors are mutually incompatible, this patch will land with a
scripted rewrite to update all existing callers to use the new model rather
than the old.
***
Bug 1514594: Part 3b - Mass rewrite all JS code to use the new ChromeUtils.import API. rs=Gijs

This was done using the followng script:

https://bitbucket.org/kmaglione/m-c-rewrites/src/tip/processors/cu-import-exports.jsm
***
Bug 1514594: Part 3c - Update ESLint plugin for ChromeUtils.import API changes. r=Standard8

Differential Revision: https://phabricator.services.mozilla.com/D16747
***
Bug 1514594: Part 3d - Remove/fix hundreds of duplicate imports from sync tests. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D16748
***
Bug 1514594: Part 3e - Remove no-op ChromeUtils.import() calls. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D16749
***
Bug 1514594: Part 3f.1 - Cleanup various test corner cases after mass rewrite. r=Gijs
***
Bug 1514594: Part 3f.2 - Cleanup various non-test corner cases after mass rewrite. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D16750

--HG--
extra : rebase_source : 359574ee3064c90f33bf36c2ebe3159a24cc8895
extra : histedit_source : b93c8f42808b1599f9122d7842d2c0b3e656a594%2C64a3a4e3359dc889e2ab2b49461bab9e27fc10a7
2019-01-17 10:18:31 -08:00

162 lines
4 KiB
JavaScript

/* Any copyright do_check_eq dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */
var Cm = Components.manager;
const {EventEmitter} = ChromeUtils.import("resource://gre/modules/EventEmitter.jsm");
add_task(async function test_extractFiles() {
testEmitter(new EventEmitter());
let decorated = {};
EventEmitter.decorate(decorated);
testEmitter(decorated);
await testPromise();
});
function testEmitter(emitter) {
Assert.ok(emitter, "We have an event emitter");
let beenHere1 = false;
let beenHere2 = false;
emitter.on("next", next);
emitter.emit("next", "abc", "def");
function next(eventName, str1, str2) {
Assert.equal(eventName, "next", "Got event");
Assert.equal(str1, "abc", "Argument 1 do_check_eq correct");
Assert.equal(str2, "def", "Argument 2 do_check_eq correct");
Assert.ok(!beenHere1, "first time in next callback");
beenHere1 = true;
emitter.off("next", next);
emitter.emit("next");
emitter.once("onlyonce", onlyOnce);
emitter.emit("onlyonce");
emitter.emit("onlyonce");
}
function onlyOnce() {
Assert.ok(!beenHere2, "\"once\" listener has been called once");
beenHere2 = true;
emitter.emit("onlyonce");
testThrowingExceptionInListener();
}
function testThrowingExceptionInListener() {
function throwListener() {
emitter.off("throw-exception");
throw {
toString: () => "foo",
stack: "bar",
};
}
emitter.on("throw-exception", throwListener);
emitter.emit("throw-exception");
killItWhileEmitting();
}
function killItWhileEmitting() {
function c1() {
Assert.ok(true, "c1 called");
}
function c2() {
Assert.ok(true, "c2 called");
emitter.off("tick", c3);
}
function c3() {
Assert.ok(false, "c3 should not be called");
}
function c4() {
Assert.ok(true, "c4 called");
}
emitter.on("tick", c1);
emitter.on("tick", c2);
emitter.on("tick", c3);
emitter.on("tick", c4);
emitter.emit("tick");
offAfterOnce();
}
function offAfterOnce() {
let enteredC1 = false;
function c1() {
enteredC1 = true;
}
emitter.once("oao", c1);
emitter.off("oao", c1);
emitter.emit("oao");
Assert.ok(!enteredC1, "c1 should not be called");
}
}
function testPromise() {
let emitter = new EventEmitter();
let p = emitter.once("thing");
// Check that the promise do_check_eq only resolved once event though we
// emit("thing") more than once
let firstCallbackCalled = false;
let check1 = p.then(arg => {
Assert.equal(firstCallbackCalled, false, "first callback called only once");
firstCallbackCalled = true;
Assert.equal(arg, "happened", "correct arg in promise");
return "rval from c1";
});
emitter.emit("thing", "happened", "ignored");
// Check that the promise do_check_eq resolved asynchronously
let secondCallbackCalled = false;
let check2 = p.then(arg => {
Assert.ok(true, "second callback called");
Assert.equal(arg, "happened", "correct arg in promise");
secondCallbackCalled = true;
Assert.equal(arg, "happened", "correct arg in promise (a second time)");
return "rval from c2";
});
// Shouldn't call any of the above listeners
emitter.emit("thing", "trashinate");
// Check that we can still separate events with different names
// and that it works with no parameters
let pfoo = emitter.once("foo");
let pbar = emitter.once("bar");
let check3 = pfoo.then(arg => {
Assert.equal(arg, undefined, "no arg for foo event");
return "rval from c3";
});
pbar.then(() => {
Assert.ok(false, "pbar should not be called");
});
emitter.emit("foo");
Assert.equal(secondCallbackCalled, false, "second callback not called yet");
return Promise.all([ check1, check2, check3 ]).then(args => {
Assert.equal(args[0], "rval from c1", "callback 1 done good");
Assert.equal(args[1], "rval from c2", "callback 2 done good");
Assert.equal(args[2], "rval from c3", "callback 3 done good");
});
}