Bug 1079563 (part 1) - replace use of async.js with promises in webchannel tests. r=MattN

This commit is contained in:
Mark Hammond 2014-11-07 18:12:25 +11:00
parent 7173003458
commit 6e9ec64669
2 changed files with 64 additions and 69 deletions

View File

@ -5,7 +5,6 @@
const Cu = Components.utils;
Cu.import("resource://services-common/async.js");
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/WebChannel.jsm");
@ -37,47 +36,45 @@ function run_test() {
/**
* Test channel listening
*/
add_test(function test_web_channel_listen() {
let channel = new WebChannel(VALID_WEB_CHANNEL_ID, VALID_WEB_CHANNEL_ORIGIN, {
broker: MockWebChannelBroker
add_task(function test_web_channel_listen() {
return new Promise((resolve, reject) => {
let channel = new WebChannel(VALID_WEB_CHANNEL_ID, VALID_WEB_CHANNEL_ORIGIN, {
broker: MockWebChannelBroker
});
let delivered = 0;
do_check_eq(channel.id, VALID_WEB_CHANNEL_ID);
do_check_eq(channel.origin.spec, VALID_WEB_CHANNEL_ORIGIN.spec);
do_check_eq(channel._deliverCallback, null);
channel.listen(function(id, message, target) {
do_check_eq(id, VALID_WEB_CHANNEL_ID);
do_check_true(message);
do_check_true(message.command);
do_check_true(target.sender);
delivered++;
// 2 messages should be delivered
if (delivered === 2) {
channel.stopListening();
do_check_eq(channel._deliverCallback, null);
resolve();
}
});
// send two messages
channel.deliver({
id: VALID_WEB_CHANNEL_ID,
message: {
command: "one"
}
}, { sender: true });
channel.deliver({
id: VALID_WEB_CHANNEL_ID,
message: {
command: "two"
}
}, { sender: true });
});
let cb = Async.makeSpinningCallback();
let delivered = 0;
do_check_eq(channel.id, VALID_WEB_CHANNEL_ID);
do_check_eq(channel.origin.spec, VALID_WEB_CHANNEL_ORIGIN.spec);
do_check_eq(channel._deliverCallback, null);
channel.listen(function(id, message, target) {
do_check_eq(id, VALID_WEB_CHANNEL_ID);
do_check_true(message);
do_check_true(message.command);
do_check_true(target.sender);
delivered++;
// 2 messages should be delivered
if (delivered === 2) {
channel.stopListening();
do_check_eq(channel._deliverCallback, null);
cb();
run_next_test();
}
});
// send two messages
channel.deliver({
id: VALID_WEB_CHANNEL_ID,
message: {
command: "one"
}
}, { sender: true });
channel.deliver({
id: VALID_WEB_CHANNEL_ID,
message: {
command: "two"
}
}, { sender: true });
cb.wait();
});

View File

@ -5,7 +5,6 @@
const Cu = Components.utils;
Cu.import("resource://services-common/async.js");
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/WebChannel.jsm");
@ -52,34 +51,33 @@ add_test(function test_web_channel_broker_channel_map() {
/**
* Test WebChannelBroker _listener test
*/
add_test(function test_web_channel_broker_listener() {
let cb = Async.makeSpinningCallback();
var channel = new Object({
id: VALID_WEB_CHANNEL_ID,
origin: VALID_WEB_CHANNEL_ORIGIN,
deliver: function(data, sender) {
do_check_eq(data.id, VALID_WEB_CHANNEL_ID);
do_check_eq(data.message.command, "hello");
WebChannelBroker.unregisterChannel(channel);
cb();
run_next_test();
}
});
WebChannelBroker.registerChannel(channel);
var mockEvent = {
data: {
add_task(function test_web_channel_broker_listener() {
return new Promise((resolve, reject) => {
var channel = new Object({
id: VALID_WEB_CHANNEL_ID,
message: {
command: "hello"
origin: VALID_WEB_CHANNEL_ORIGIN,
deliver: function(data, sender) {
do_check_eq(data.id, VALID_WEB_CHANNEL_ID);
do_check_eq(data.message.command, "hello");
WebChannelBroker.unregisterChannel(channel);
resolve();
}
},
principal: {
origin: URL_STRING
}
};
});
WebChannelBroker._listener(mockEvent);
cb.wait();
WebChannelBroker.registerChannel(channel);
var mockEvent = {
data: {
id: VALID_WEB_CHANNEL_ID,
message: {
command: "hello"
}
},
principal: {
origin: URL_STRING
}
};
WebChannelBroker._listener(mockEvent);
});
});