Bug 1169159 - Make xpcshells run_test_in_child() and do_await_remote_message() return promises. r=ted,gfritzsche

This commit is contained in:
Alessio Placitelli 2015-06-03 04:41:00 +02:00
parent 6ae071f17a
commit b1961bbde0

View File

@ -1232,54 +1232,74 @@ function do_load_child_test_harness()
* Runs an entire xpcshell unit test in a child process (rather than in chrome, * Runs an entire xpcshell unit test in a child process (rather than in chrome,
* which is the default). * which is the default).
* *
* This function returns immediately, before the test has completed. * This function returns immediately, before the test has completed.
* *
* @param testFile * @param testFile
* The name of the script to run. Path format same as load(). * The name of the script to run. Path format same as load().
* @param optionalCallback. * @param optionalCallback.
* Optional function to be called (in parent) when test on child is * Optional function to be called (in parent) when test on child is
* complete. If provided, the function must call do_test_finished(); * complete. If provided, the function must call do_test_finished();
* @return Promise Resolved when the test in the child is complete.
*/ */
function run_test_in_child(testFile, optionalCallback) function run_test_in_child(testFile, optionalCallback)
{ {
var callback = (typeof optionalCallback == 'undefined') ? return new Promise((resolve) => {
do_test_finished : optionalCallback; var callback = () => {
resolve();
if (typeof optionalCallback == 'undefined') {
do_test_finished();
} else {
optionalCallback();
}
};
do_load_child_test_harness(); do_load_child_test_harness();
var testPath = do_get_file(testFile).path.replace(/\\/g, "/"); var testPath = do_get_file(testFile).path.replace(/\\/g, "/");
do_test_pending("run in child"); do_test_pending("run in child");
sendCommand("_testLogger.info('CHILD-TEST-STARTED'); " sendCommand("_testLogger.info('CHILD-TEST-STARTED'); "
+ "const _TEST_FILE=['" + testPath + "']; " + "const _TEST_FILE=['" + testPath + "']; "
+ "_execute_test(); " + "_execute_test(); "
+ "_testLogger.info('CHILD-TEST-COMPLETED');", + "_testLogger.info('CHILD-TEST-COMPLETED');",
callback); callback);
});
} }
/** /**
* Execute a given function as soon as a particular cross-process message is received. * Execute a given function as soon as a particular cross-process message is received.
* Must be paired with do_send_remote_message or equivalent ProcessMessageManager calls. * Must be paired with do_send_remote_message or equivalent ProcessMessageManager calls.
*
* @param optionalCallback
* Optional callback that is invoked when the message is received. If provided,
* the function must call do_test_finished().
* @return Promise Promise that is resolved when the message is received.
*/ */
function do_await_remote_message(name, callback) function do_await_remote_message(name, optionalCallback)
{ {
var listener = { return new Promise((resolve) => {
receiveMessage: function(message) { var listener = {
if (message.name == name) { receiveMessage: function(message) {
mm.removeMessageListener(name, listener); if (message.name == name) {
callback(); mm.removeMessageListener(name, listener);
do_test_finished(); resolve();
if (optionalCallback) {
optionalCallback();
} else {
do_test_finished();
}
}
} }
} };
};
var mm; var mm;
if (runningInParent) { if (runningInParent) {
mm = Cc["@mozilla.org/parentprocessmessagemanager;1"].getService(Ci.nsIMessageBroadcaster); mm = Cc["@mozilla.org/parentprocessmessagemanager;1"].getService(Ci.nsIMessageBroadcaster);
} else { } else {
mm = Cc["@mozilla.org/childprocessmessagemanager;1"].getService(Ci.nsISyncMessageSender); mm = Cc["@mozilla.org/childprocessmessagemanager;1"].getService(Ci.nsISyncMessageSender);
} }
do_test_pending(); do_test_pending();
mm.addMessageListener(name, listener); mm.addMessageListener(name, listener);
});
} }
/** /**