From dfb19367234bdb8f6aef57131c3029e9a7d672fa Mon Sep 17 00:00:00 2001 From: Bevis Tseng Date: Tue, 29 Apr 2014 17:04:42 +0800 Subject: [PATCH] Bug 824717 - Part 7: Modify and Add test cases to check the message id and message content when failed to send SMS/MMS message. r=vyang --- dom/mobilemessage/tests/marionette/head.js | 108 ++++++++++++------ .../tests/marionette/manifest.ini | 1 + .../marionette/test_error_of_mms_send.js | 22 +++- .../marionette/test_error_of_sms_send.js | 105 +++++++++++++++++ 4 files changed, 197 insertions(+), 39 deletions(-) create mode 100644 dom/mobilemessage/tests/marionette/test_error_of_sms_send.js diff --git a/dom/mobilemessage/tests/marionette/head.js b/dom/mobilemessage/tests/marionette/head.js index a9cdc51bd52..79850980160 100644 --- a/dom/mobilemessage/tests/marionette/head.js +++ b/dom/mobilemessage/tests/marionette/head.js @@ -70,6 +70,33 @@ function waitForManagerEvent(aEventName) { return deferred.promise; } +/** + * Wrap DOMRequest onsuccess/onerror events to Promise resolve/reject. + * + * Fulfill params: A DOMEvent. + * Reject params: A DOMEvent. + * + * @param aRequest + * A DOMRequest instance. + * + * @return A deferred promise. + */ +function wrapDomRequestAsPromise(aRequest) { + let deferred = Promise.defer(); + + ok(aRequest instanceof DOMRequest, + "aRequest is instanceof " + aRequest.constructor); + + aRequest.addEventListener("success", function(aEvent) { + deferred.resolve(aEvent); + }); + aRequest.addEventListener("error", function(aEvent) { + deferred.reject(aEvent); + }); + + return deferred.promise; +} + /** * Send a SMS message to a single receiver. Resolve if it succeeds, reject * otherwise. @@ -86,17 +113,42 @@ function waitForManagerEvent(aEventName) { * @return A deferred promise. */ function sendSmsWithSuccess(aReceiver, aText) { - let deferred = Promise.defer(); + let request = manager.send(aReceiver, aText); + return wrapDomRequestAsPromise(request) + .then((aEvent) => { return aEvent.target.result; }, + (aEvent) => { throw aEvent.target.error; }); +} + +/** + * Send a SMS message to a single receiver. + * Resolve if it fails, reject otherwise. + * + * Fulfill params: + * { + * message, -- the failed MmsMessage + * error, -- error of the send request + * } + * + * Reject params: (none) + * + * @param aReceiver the address of the receiver. + * @param aText the text body of the message. + * + * @return A deferred promise. + */ +function sendSmsWithFailure(aReceiver, aText) { + let promises = []; + promises.push(waitForManagerEvent("failed") + .then((aEvent) => { return aEvent.message; })); let request = manager.send(aReceiver, aText); - request.onsuccess = function(event) { - deferred.resolve(event.target.result); - }; - request.onerror = function(event) { - deferred.reject(event.target.error); - }; + promises.push(wrapDomRequestAsPromise(request) + .then((aEvent) => { throw aEvent; }, + (aEvent) => { return aEvent.target.error; })); - return deferred.promise; + return Promise.all(promises) + .then((aResults) => { return { message: aResults[0], + error: aResults[1] }; }); } /** @@ -118,30 +170,18 @@ function sendSmsWithSuccess(aReceiver, aText) { * @return A deferred promise. */ function sendMmsWithFailure(aMmsParameters, aSendParameters) { - let deferred = Promise.defer(); - - let result = { message: null, error: null }; - function got(which, value) { - result[which] = value; - if (result.message != null && result.error != null) { - deferred.resolve(result); - } - } - - manager.addEventListener("failed", function onfailed(event) { - manager.removeEventListener("failed", onfailed); - got("message", event.message); - }); + let promises = []; + promises.push(waitForManagerEvent("failed") + .then((aEvent) => { return aEvent.message; })); let request = manager.sendMMS(aMmsParameters, aSendParameters); - request.onsuccess = function(event) { - deferred.reject(); - }; - request.onerror = function(event) { - got("error", event.target.error); - } + promises.push(wrapDomRequestAsPromise(request) + .then((aEvent) => { throw aEvent; }, + (aEvent) => { return aEvent.target.error; })); - return deferred.promise; + return Promise.all(promises) + .then((aResults) => { return { message: aResults[0], + error: aResults[1] }; }); } /** @@ -272,15 +312,9 @@ function deleteMessagesById(aMessageIds) { return []; } - let deferred = Promise.defer(); - let request = manager.delete(aMessageIds); - request.onsuccess = function(event) { - deferred.resolve(event.target.result); - }; - request.onerror = deferred.reject.bind(deferred); - - return deferred.promise; + return wrapDomRequestAsPromise(request) + .then((aEvent) => { return aEvent.target.result; }); } /** diff --git a/dom/mobilemessage/tests/marionette/manifest.ini b/dom/mobilemessage/tests/marionette/manifest.ini index e7f3dc38056..9d9bdd242cb 100644 --- a/dom/mobilemessage/tests/marionette/manifest.ini +++ b/dom/mobilemessage/tests/marionette/manifest.ini @@ -49,3 +49,4 @@ qemu = true [test_mt_sms_concatenation.js] [test_error_of_mms_manual_retrieval.js] [test_error_of_mms_send.js] +[test_error_of_sms_send.js] diff --git a/dom/mobilemessage/tests/marionette/test_error_of_mms_send.js b/dom/mobilemessage/tests/marionette/test_error_of_mms_send.js index 83096b20959..406555b3a07 100644 --- a/dom/mobilemessage/tests/marionette/test_error_of_mms_send.js +++ b/dom/mobilemessage/tests/marionette/test_error_of_mms_send.js @@ -14,13 +14,31 @@ function testSendFailed(aCause, aServiceId) { sendParameters = { serviceId: aServiceId }; } - let mmsParameters = { subject: "Test", - receivers: ["+0987654321"], + let testSubject = "Test"; + let testReceivers = ["+0987654321"]; + + let mmsParameters = { subject: testSubject, + receivers: testReceivers, attachments: [] }; return sendMmsWithFailure(mmsParameters, sendParameters) .then((result) => { is(result.error.name, aCause, "Checking failure cause."); + + let domMessage = result.error.data; + is(domMessage.id, result.message.id, "Checking message id."); + is(domMessage.subject, testSubject, "Checking subject."); + is(domMessage.receivers.length, testReceivers.length, "Checking no. of receivers."); + for (let i = 0; i < testReceivers.length; i++) { + is(domMessage.receivers[i], testReceivers[i], "Checking receiver address."); + } + + let deliveryInfo = domMessage.deliveryInfo; + is(deliveryInfo.length, testReceivers.length, "Checking no. of deliveryInfo."); + for (let i = 0; i < deliveryInfo.length; i++) { + is(deliveryInfo[i].receiver, testReceivers[i], "Checking receiver address."); + is(deliveryInfo[i].deliveryStatus, "error", "Checking deliveryStatus."); + } }); } diff --git a/dom/mobilemessage/tests/marionette/test_error_of_sms_send.js b/dom/mobilemessage/tests/marionette/test_error_of_sms_send.js new file mode 100644 index 00000000000..9c29c08ad4f --- /dev/null +++ b/dom/mobilemessage/tests/marionette/test_error_of_sms_send.js @@ -0,0 +1,105 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ + +MARIONETTE_TIMEOUT = 60000; +MARIONETTE_HEAD_JS = 'head.js'; + +const kPrefRilRadioDisabled = "ril.radio.disabled"; + +let connection; +function ensureMobileConnection() { + let deferred = Promise.defer(); + + let permissions = [{ + "type": "mobileconnection", + "allow": true, + "context": document, + }]; + SpecialPowers.pushPermissions(permissions, function() { + ok(true, "permissions pushed: " + JSON.stringify(permissions)); + + connection = window.navigator.mozMobileConnections[0]; + if (connection) { + log("navigator.mozMobileConnections[0] is instance of " + connection.constructor); + } else { + log("navigator.mozMobileConnections[0] is undefined."); + } + + if (connection instanceof MozMobileConnection) { + deferred.resolve(connection); + } else { + deferred.reject(); + } + }); + + return deferred.promise; +} + +function waitRadioState(state) { + let deferred = Promise.defer(); + + waitFor(function() { + deferred.resolve(); + }, function() { + return connection.radioState == state; + }); + + return deferred.promise; +} + +function setRadioEnabled(enabled) { + log("setRadioEnabled to " + enabled); + + let deferred = Promise.defer(); + + let finalState = (enabled) ? "enabled" : "disabled"; + connection.onradiostatechange = function() { + let state = connection.radioState; + log("Received 'radiostatechange' event, radioState: " + state); + + if (state == finalState) { + deferred.resolve(); + connection.onradiostatechange = null; + } + }; + + let req = connection.setRadioEnabled(enabled); + + req.onsuccess = function() { + log("setRadioEnabled success"); + }; + + req.onerror = function() { + ok(false, "setRadioEnabled should not fail"); + deferred.reject(); + }; + + return deferred.promise; +} + +function testSendFailed(aCause) { + log("testSendFailed, aCause: " + aCause); + + let testReceiver = "+0987654321"; + let testMessage = "quick fox jump over the lazy dog"; + + return sendSmsWithFailure(testReceiver, testMessage) + .then((result) => { + is(result.error.name, aCause, "Checking failure cause."); + + let domMessage = result.error.data; + is(domMessage.id, result.message.id, "Checking message id."); + is(domMessage.receiver, testReceiver, "Checking receiver address."); + is(domMessage.body, testMessage, "Checking message body."); + is(domMessage.delivery, "error", "Checking delivery."); + is(domMessage.deliveryStatus, "error", "Checking deliveryStatus."); + }); +} + +startTestCommon(function testCaseMain() { + return ensureMobileConnection() + .then(() => waitRadioState("enabled")) + .then(() => setRadioEnabled(false)) + .then(() => testSendFailed("RadioDisabledError")) + .then(() => setRadioEnabled(true)); +});