diff --git a/dom/telephony/test/marionette/head.js b/dom/telephony/test/marionette/head.js index 2578de45b3de..6ec06b4280de 100644 --- a/dom/telephony/test/marionette/head.js +++ b/dom/telephony/test/marionette/head.js @@ -118,6 +118,44 @@ let emulator = (function() { }); } + /** + * Wait for one named system message. + * + * Resolve if that named message is received. Never reject. + * + * Fulfill params: the message passed. + * + * @param aEventName + * A string message name. + * @param aMatchFun [optional] + * A matching function returns true or false to filter the message. If no + * matching function passed the promise is resolved after receiving the + * first message. + * + * @return Promise + */ + function waitForSystemMessage(aMessageName, aMatchFun = null) { + // Current page may not register to receiving the message. We should + // register it first. + let systemMessenger = SpecialPowers.Cc["@mozilla.org/system-message-internal;1"] + .getService(SpecialPowers.Ci.nsISystemMessagesInternal); + + // TODO: Find a better way to get current pageURI and manifestURI. + systemMessenger.registerPage(aMessageName, + SpecialPowers.Services.io.newURI("app://system.gaiamobile.org/index.html", null, null), + SpecialPowers.Services.io.newURI("app://system.gaiamobile.org/manifest.webapp", null, null)); + + return new Promise(function(aResolve, aReject) { + window.navigator.mozSetMessageHandler(aMessageName, function(aMessage) { + if (!aMatchFun || aMatchFun(aMessage)) { + log("System message '" + aMessageName + "' got."); + window.navigator.mozSetMessageHandler(aMessageName, null); + aResolve(aMessage); + } + }); + }); + } + /** * Wait for one named event. * @@ -1098,6 +1136,7 @@ let emulator = (function() { */ this.gDelay = delay; + this.gWaitForSystemMessage = waitForSystemMessage; this.gWaitForEvent = waitForEvent; this.gWaitForCallsChangedEvent = waitForCallsChangedEvent; this.gWaitForNamedStateEvent = waitForNamedStateEvent; diff --git a/dom/telephony/test/marionette/manifest.ini b/dom/telephony/test/marionette/manifest.ini index 9513741c5274..0bf86a93453f 100644 --- a/dom/telephony/test/marionette/manifest.ini +++ b/dom/telephony/test/marionette/manifest.ini @@ -51,4 +51,5 @@ qemu = true [test_ready.js] [test_redundant_operations.js] [test_swap_held_and_active.js] +[test_system_message_telephony_call_ended.js] [test_temporary_clir.js] diff --git a/dom/telephony/test/marionette/test_system_message_telephony_call_ended.js b/dom/telephony/test/marionette/test_system_message_telephony_call_ended.js new file mode 100644 index 000000000000..8fbfdfdb0395 --- /dev/null +++ b/dom/telephony/test/marionette/test_system_message_telephony_call_ended.js @@ -0,0 +1,104 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ + +MARIONETTE_TIMEOUT = 90000; +MARIONETTE_HEAD_JS = 'head.js'; + +const inNumber = "5555552222"; +const inInfo = gInCallStrPool(inNumber); +let inCall; + +/** + * telephony-call-ended message contains the following fields: + * - serviceId + * - number + * - emergency + * - duration + * - direction + * - hangUpLocal + */ + +function testIncomingReject() { + log("= testIncomingReject ="); + + let p1 = gWaitForSystemMessage("telephony-call-ended") + .then(message => { + is(message.number, inNumber); + is(message.duration, 0); + is(message.direction, "incoming"); + is(message.hangUpLocal, true); + }); + + let p2 = gRemoteDial(inNumber) + .then(call => inCall = call) + .then(() => gHangUp(inCall)); + + return Promise.all([p1, p2]); +} + +function testIncomingCancel() { + log("= testIncomingCancel ="); + + let p1 = gWaitForSystemMessage("telephony-call-ended") + .then(message => { + is(message.number, inNumber); + is(message.duration, 0); + is(message.direction, "incoming"); + is(message.hangUpLocal, false); + }); + + let p2 = gRemoteDial(inNumber) + .then(call => inCall = call) + .then(() => gRemoteHangUp(inCall)); + + return Promise.all([p1, p2]); +} + +function testIncomingAnswerHangUp() { + log("= testIncomingAnswerHangUp ="); + + p1 = gWaitForSystemMessage("telephony-call-ended") + .then(message => { + is(message.number, inNumber); + ok(message.duration > 0); + is(message.direction, "incoming"); + is(message.hangUpLocal, true); + }); + + p2 = gRemoteDial(inNumber) + .then(call => inCall = call) + .then(() => gAnswer(inCall)) + .then(() => gHangUp(inCall)); + + return Promise.all([p1, p2]); +} + +function testIncomingAnswerRemoteHangUp() { + log("= testIncomingAnswerRemoteHangUp ="); + + p1 = gWaitForSystemMessage("telephony-call-ended") + .then(message => { + is(message.number, inNumber); + ok(message.duration > 0); + is(message.direction, "incoming"); + is(message.hangUpLocal, false); + }); + + p2 = gRemoteDial(inNumber) + .then(call => inCall = call) + .then(() => gAnswer(inCall)) + .then(() => gRemoteHangUp(inCall)); + + return Promise.all([p1, p2]); +} + +startTest(function() { + Promise.resolve() + .then(() => testIncomingReject()) + .then(() => testIncomingCancel()) + .then(() => testIncomingAnswerHangUp()) + .then(() => testIncomingAnswerRemoteHangUp()) + + .catch(error => ok(false, "Promise reject: " + error)) + .then(finish); +});