2016-10-10 00:48:35 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const {assert} = require("chai");
|
|
|
|
const sinon = require("sinon");
|
|
|
|
|
|
|
|
const {setupTestDOMWindow} = require("./setup");
|
|
|
|
|
|
|
|
describe("browser-polyfill", () => {
|
|
|
|
describe("wrapped runtime.onMessage listener", () => {
|
2016-10-11 12:27:26 +00:00
|
|
|
it("keeps track of the listeners added", () => {
|
2016-10-10 00:48:35 +00:00
|
|
|
const messageListener = sinon.spy();
|
|
|
|
|
|
|
|
const fakeChrome = {
|
|
|
|
runtime: {
|
2016-10-11 11:56:00 +00:00
|
|
|
lastError: null,
|
2016-10-10 00:48:35 +00:00
|
|
|
onMessage: {
|
|
|
|
addListener: sinon.spy(),
|
|
|
|
hasListener: sinon.stub(),
|
|
|
|
removeListener: sinon.spy(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
return setupTestDOMWindow(fakeChrome).then(window => {
|
|
|
|
fakeChrome.runtime.onMessage.hasListener
|
2016-10-11 12:34:39 +00:00
|
|
|
// Fake the hasListener result for "listener not yet registered".
|
2016-10-10 00:48:35 +00:00
|
|
|
.onFirstCall().returns(false)
|
2016-10-11 12:34:39 +00:00
|
|
|
// Fake the hasListener result for "listener registered".
|
2016-10-11 12:27:26 +00:00
|
|
|
.onSecondCall().returns(true)
|
2016-10-11 12:34:39 +00:00
|
|
|
// Fake the hasListener result for "listener unregistered".
|
2016-10-11 12:27:26 +00:00
|
|
|
.onThirdCall().returns(false);
|
2016-10-10 00:48:35 +00:00
|
|
|
|
|
|
|
assert.equal(window.browser.runtime.onMessage.hasListener(messageListener),
|
|
|
|
false, "Got hasListener==false before the listener has been added");
|
2016-10-11 12:34:39 +00:00
|
|
|
|
2016-10-10 00:48:35 +00:00
|
|
|
window.browser.runtime.onMessage.addListener(messageListener);
|
2016-10-11 12:34:39 +00:00
|
|
|
|
2016-10-10 00:48:35 +00:00
|
|
|
assert.equal(window.browser.runtime.onMessage.hasListener(messageListener),
|
2016-10-11 12:27:26 +00:00
|
|
|
true, "Got hasListener==true once the listener has been added");
|
2016-10-11 12:34:39 +00:00
|
|
|
|
|
|
|
// Add the same listener again to test that it will be called with the
|
|
|
|
// same wrapped listener.
|
2016-10-10 00:48:35 +00:00
|
|
|
window.browser.runtime.onMessage.addListener(messageListener);
|
|
|
|
|
|
|
|
assert.ok(fakeChrome.runtime.onMessage.addListener.calledTwice,
|
|
|
|
"addListener has been called twice");
|
|
|
|
assert.equal(fakeChrome.runtime.onMessage.addListener.secondCall.args[0],
|
|
|
|
fakeChrome.runtime.onMessage.addListener.firstCall.args[0],
|
|
|
|
"both the addListener calls received the same wrapped listener");
|
|
|
|
|
2016-10-11 12:34:39 +00:00
|
|
|
// Retrieve the wrapped listener and execute it to fake a received message.
|
2016-10-10 00:48:35 +00:00
|
|
|
const wrappedListener = fakeChrome.runtime.onMessage.addListener.firstCall.args[0];
|
|
|
|
wrappedListener("msg", {name: "sender"}, function sendResponse() {});
|
|
|
|
assert.ok(messageListener.calledOnce, "The listener has been called once");
|
|
|
|
|
2016-10-11 12:34:39 +00:00
|
|
|
// Remove the listener.
|
2016-10-10 00:48:35 +00:00
|
|
|
window.browser.runtime.onMessage.removeListener(messageListener);
|
|
|
|
assert.ok(fakeChrome.runtime.onMessage.removeListener.calledOnce,
|
|
|
|
"removeListener has been called once");
|
|
|
|
assert.equal(fakeChrome.runtime.onMessage.addListener.secondCall.args[0],
|
|
|
|
fakeChrome.runtime.onMessage.removeListener.firstCall.args[0],
|
|
|
|
"both the addListener and removeListenercalls received the same wrapped listener");
|
2016-10-11 12:27:26 +00:00
|
|
|
assert.equal(fakeChrome.runtime.onMessage.hasListener(messageListener), false,
|
|
|
|
"Got hasListener==false once the listener has been removed");
|
2016-10-10 00:48:35 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("sends the returned value as a message response", () => {
|
|
|
|
const fakeChrome = {
|
|
|
|
runtime: {
|
2016-10-11 11:56:00 +00:00
|
|
|
lastError: null,
|
2016-10-10 00:48:35 +00:00
|
|
|
onMessage: {
|
|
|
|
addListener: sinon.spy(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const messageListener = sinon.stub();
|
|
|
|
const firstResponse = "fake reply";
|
|
|
|
const secondResponse = Promise.resolve("fake reply2");
|
|
|
|
const sendResponseSpy = sinon.spy();
|
|
|
|
|
|
|
|
messageListener.onFirstCall().returns(firstResponse)
|
|
|
|
.onSecondCall().returns(secondResponse);
|
|
|
|
|
|
|
|
return setupTestDOMWindow(fakeChrome).then(window => {
|
|
|
|
window.browser.runtime.onMessage.addListener(messageListener);
|
|
|
|
|
|
|
|
assert.ok(fakeChrome.runtime.onMessage.addListener.calledOnce);
|
|
|
|
|
|
|
|
const wrappedListener = fakeChrome.runtime.onMessage.addListener.firstCall.args[0];
|
|
|
|
|
|
|
|
wrappedListener("fake message", {name: "fake sender"}, sendResponseSpy);
|
|
|
|
|
|
|
|
assert.ok(messageListener.calledOnce, "The unwrapped message listener has been called");
|
|
|
|
assert.deepEqual(messageListener.firstCall.args,
|
|
|
|
["fake message", {name: "fake sender"}],
|
|
|
|
"The unwrapped message listener has received the expected parameters");
|
|
|
|
|
|
|
|
assert.ok(sendResponseSpy.calledOnce, "The sendResponse function has been called");
|
|
|
|
assert.equal(sendResponseSpy.firstCall.args[0], "fake reply",
|
|
|
|
"sendResponse callback has been called with the expected parameters");
|
|
|
|
|
|
|
|
wrappedListener("fake message2", {name: "fake sender2"}, sendResponseSpy);
|
|
|
|
|
|
|
|
// Wait the second response promise to be resolved.
|
|
|
|
return secondResponse;
|
|
|
|
}).then(() => {
|
|
|
|
assert.ok(messageListener.calledTwice,
|
|
|
|
"The unwrapped message listener has been called");
|
|
|
|
assert.deepEqual(messageListener.secondCall.args,
|
|
|
|
["fake message2", {name: "fake sender2"}],
|
|
|
|
"The unwrapped message listener has received the expected parameters");
|
|
|
|
|
|
|
|
assert.ok(sendResponseSpy.calledTwice, "The sendResponse function has been called");
|
|
|
|
assert.equal(sendResponseSpy.secondCall.args[0], "fake reply2",
|
|
|
|
"sendResponse callback has been called with the expected parameters");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|