michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: MARIONETTE_TIMEOUT = 60000; michael@0: michael@0: SpecialPowers.setBoolPref("dom.sms.enabled", true); michael@0: SpecialPowers.addPermission("sms", true, document); michael@0: michael@0: const SENDER = "5555552368"; // the remote number michael@0: const RECEIVER = "15555215554"; // the emulator's number michael@0: michael@0: let manager = window.navigator.mozMobileMessage; michael@0: let MSG_TEXT = "Mozilla Firefox OS!"; michael@0: let SMS_NUMBER = 100; michael@0: michael@0: let SmsList = []; michael@0: let checkDone = true; michael@0: let emulatorReady = true; michael@0: michael@0: let pendingEmulatorCmdCount = 0; michael@0: function sendSmsToEmulator(from, text) { michael@0: ++pendingEmulatorCmdCount; michael@0: michael@0: let cmd = "sms send " + from + " " + text; michael@0: runEmulatorCmd(cmd, function(result) { michael@0: --pendingEmulatorCmdCount; michael@0: michael@0: is(result[0], "OK", "Emulator response"); michael@0: }); michael@0: } michael@0: michael@0: let tasks = { michael@0: // List of test fuctions. Each of them should call |tasks.next()| when michael@0: // completed or |tasks.finish()| to jump to the last one. michael@0: _tasks: [], michael@0: _nextTaskIndex: 0, michael@0: michael@0: push: function(func) { michael@0: this._tasks.push(func); michael@0: }, michael@0: michael@0: next: function() { michael@0: let index = this._nextTaskIndex++; michael@0: let task = this._tasks[index]; michael@0: try { michael@0: task(); michael@0: } catch (ex) { michael@0: ok(false, "test task[" + index + "] throws: " + ex); michael@0: // Run last task as clean up if possible. michael@0: if (index != this._tasks.length - 1) { michael@0: this.finish(); michael@0: } michael@0: } michael@0: }, michael@0: michael@0: finish: function() { michael@0: this._tasks[this._tasks.length - 1](); michael@0: }, michael@0: michael@0: run: function() { michael@0: this.next(); michael@0: } michael@0: }; michael@0: michael@0: function taskNextWrapper() { michael@0: tasks.next(); michael@0: } michael@0: michael@0: function verifySmsExists(incomingSms) { michael@0: log("Getting SMS (id: " + incomingSms.id + ")."); michael@0: let requestRet = manager.getMessage(incomingSms.id); michael@0: ok(requestRet, "smsrequest obj returned"); michael@0: michael@0: requestRet.onsuccess = function(event) { michael@0: log("Received 'onsuccess' smsrequest event."); michael@0: ok(event.target.result, "smsrequest event.target.result"); michael@0: let foundSms = event.target.result; michael@0: is(foundSms.id, incomingSms.id, "found SMS id matches"); michael@0: is(foundSms.threadId, incomingSms.threadId, "found SMS thread id matches"); michael@0: is(foundSms.body, MSG_TEXT, "found SMS msg text matches"); michael@0: is(foundSms.delivery, "received", "delivery"); michael@0: is(foundSms.deliveryStatus, "success", "deliveryStatus"); michael@0: is(foundSms.read, false, "read"); michael@0: is(foundSms.receiver, RECEIVER, "receiver"); michael@0: is(foundSms.sender, SENDER, "sender"); michael@0: is(foundSms.messageClass, "normal", "messageClass"); michael@0: log("Got SMS (id: " + foundSms.id + ") as expected."); michael@0: michael@0: SmsList.push(incomingSms); michael@0: }; michael@0: michael@0: requestRet.onerror = function(event) { michael@0: log("Received 'onerror' smsrequest event."); michael@0: ok(event.target.error, "domerror obj"); michael@0: is(event.target.error.name, "NotFoundError", "error returned"); michael@0: log("Could not get SMS (id: " + incomingSms.id + ") but should have."); michael@0: ok(false,"SMS was not found"); michael@0: tasks.finish(); michael@0: }; michael@0: } michael@0: michael@0: let verifDeletedCount = 0; michael@0: function verifySmsDeleted(smsId) { michael@0: log("Getting SMS (id: " + smsId + ")."); michael@0: let requestRet = manager.getMessage(smsId); michael@0: ok(requestRet, "smsrequest obj returned"); michael@0: michael@0: requestRet.onsuccess = function(event) { michael@0: log("Received 'onsuccess' smsrequest event."); michael@0: ok(event.target.result, "smsrequest event.target.result"); michael@0: let foundSms = event.target.result; michael@0: is(foundSms.id, smsId, "found SMS id matches"); michael@0: is(foundSms.body, MSG_TEXT, "found SMS msg text matches"); michael@0: log("Got SMS (id: " + foundSms.id + ") but should not have."); michael@0: ok(false, "SMS was not deleted"); michael@0: tasks.finish(); michael@0: }; michael@0: michael@0: requestRet.onerror = function(event) { michael@0: log("Received 'onerror' smsrequest event."); michael@0: ok(event.target.error, "domerror obj"); michael@0: is(event.target.error.name, "NotFoundError", "error returned"); michael@0: log("Could not get SMS (id: " + smsId + ") as expected."); michael@0: verifDeletedCount++; michael@0: }; michael@0: } michael@0: michael@0: tasks.push(function init() { michael@0: log("Initialize test object."); michael@0: ok(manager instanceof MozMobileMessageManager, michael@0: "manager is instance of " + manager.constructor); michael@0: michael@0: // Callback for incoming sms michael@0: manager.onreceived = function onreceived(event) { michael@0: log("Received 'onreceived' event."); michael@0: let incomingSms = event.message; michael@0: ok(incomingSms, "incoming sms"); michael@0: ok(incomingSms.id, "sms id"); michael@0: log("Received SMS (id: " + incomingSms.id + ")."); michael@0: ok(incomingSms.threadId, "thread id"); michael@0: is(incomingSms.body, MSG_TEXT, "msg body"); michael@0: is(incomingSms.delivery, "received", "delivery"); michael@0: is(incomingSms.deliveryStatus, "success", "deliveryStatus"); michael@0: is(incomingSms.read, false, "read"); michael@0: is(incomingSms.receiver, RECEIVER, "receiver"); michael@0: is(incomingSms.sender, SENDER, "sender"); michael@0: is(incomingSms.messageClass, "normal", "messageClass"); michael@0: is(incomingSms.deliveryTimestamp, 0, "deliveryTimestamp is 0"); michael@0: michael@0: verifySmsExists(incomingSms); michael@0: }; michael@0: michael@0: tasks.next(); michael@0: }); michael@0: michael@0: tasks.push(function sendAllSms() { michael@0: log("Send " + SMS_NUMBER + " SMS"); michael@0: for (let i = 0; i < SMS_NUMBER; i++) { michael@0: sendSmsToEmulator(SENDER, MSG_TEXT); michael@0: } michael@0: michael@0: waitFor(taskNextWrapper, function() { michael@0: return (pendingEmulatorCmdCount === 0) && (SmsList.length === SMS_NUMBER); michael@0: }); michael@0: }); michael@0: michael@0: tasks.push(function deleteAllSms() { michael@0: log("Deleting SMS using smsmsg obj array parameter."); michael@0: let deleteStart = Date.now(); michael@0: log("deleteStart: " + deleteStart); michael@0: log("SmsList: " + JSON.stringify(SmsList)); michael@0: let requestRet = manager.delete(SmsList); michael@0: ok(requestRet,"smsrequest obj returned"); michael@0: michael@0: requestRet.onsuccess = function(event) { michael@0: let deleteDone = Date.now(); michael@0: log("Delete " + SMS_NUMBER + " SMS takes " + (deleteDone - deleteStart) + " ms."); michael@0: log("Received 'onsuccess' smsrequest event."); michael@0: if (event.target.result) { michael@0: for (let i = 0; i < SmsList.length; i++) { michael@0: verifySmsDeleted(SmsList[i].id); michael@0: } michael@0: } else { michael@0: log("smsrequest returned false for manager.delete"); michael@0: ok(false, "SMS delete failed"); michael@0: } michael@0: }; michael@0: michael@0: requestRet.onerror = function(event) { michael@0: log("Received 'onerror' smsrequest event."); michael@0: ok(event.target.error, "domerror obj"); michael@0: ok(false, "manager.delete request returned unexpected error: " michael@0: + event.target.error.name); michael@0: tasks.finish(); michael@0: }; michael@0: michael@0: waitFor(taskNextWrapper, function() { michael@0: return verifDeletedCount === SMS_NUMBER; michael@0: }); michael@0: }); michael@0: michael@0: // WARNING: All tasks should be pushed before this!!! michael@0: tasks.push(function cleanUp() { michael@0: if (pendingEmulatorCmdCount) { michael@0: window.setTimeout(cleanUp, 100); michael@0: return; michael@0: } michael@0: michael@0: manager.onreceived = null; michael@0: SpecialPowers.removePermission("sms", document); michael@0: SpecialPowers.setBoolPref("dom.sms.enabled", false); michael@0: log("Finish!!!"); michael@0: finish(); michael@0: }); michael@0: michael@0: // Start the test michael@0: tasks.run();