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.addPermission("sms", true, document); michael@0: SpecialPowers.setBoolPref("dom.sms.enabled", true); michael@0: michael@0: let manager = window.navigator.mozMobileMessage; michael@0: let smsId; michael@0: michael@0: function verifyInitialState() { michael@0: log("Verifying initial state."); michael@0: ok(manager instanceof MozMobileMessageManager, michael@0: "manager is instance of " + manager.constructor); michael@0: simulateIncomingSms(); michael@0: } michael@0: michael@0: function simulateIncomingSms() { michael@0: let text = "Incoming SMS courtesy of Firefox OS"; michael@0: let remoteNumber = "5557779999"; michael@0: michael@0: log("Simulating incoming SMS."); michael@0: michael@0: // Simulate incoming SMS sent from remoteNumber to our emulator michael@0: rcvdEmulatorCallback = false; michael@0: runEmulatorCmd("sms send " + remoteNumber + " " + text, function(result) { michael@0: is(result[0], "OK", "emulator callback"); michael@0: rcvdEmulatorCallback = true; michael@0: }); michael@0: } michael@0: michael@0: // Callback for incoming SMS michael@0: manager.onreceived = function onreceived(event) { michael@0: log("Received 'onreceived' sms event."); michael@0: let incomingSms = event.message; michael@0: log("Received SMS (id: " + incomingSms.id + ")."); michael@0: is(incomingSms.read, false, "incoming message read"); michael@0: smsId = incomingSms.id; michael@0: michael@0: // Wait for emulator to catch up before continuing michael@0: waitFor(test1, function() { michael@0: return(rcvdEmulatorCallback); michael@0: }); michael@0: }; michael@0: michael@0: function markMsgError(invalidId, readBool, nextFunction) { michael@0: let requestRet = manager.markMessageRead(invalidId, readBool); michael@0: ok(requestRet, "smsrequest obj returned"); michael@0: michael@0: requestRet.onsuccess = function(event) { michael@0: log("Received 'onsuccess' smsrequest event, but expected error."); michael@0: ok(false, "Smsrequest should have returned error but did not"); michael@0: nextFunction(); 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: nextFunction(); michael@0: }; michael@0: } michael@0: michael@0: function test1() { michael@0: // Mark message read for a message that doesn't exist, expect error michael@0: let msgIdNoExist = smsId + 1; michael@0: log("Attempting to mark non-existent sms (id: " + msgIdNoExist michael@0: + ") read, expect error."); michael@0: markMsgError(msgIdNoExist, true, test2); michael@0: } michael@0: michael@0: function test2() { michael@0: // Mark message read using invalid SMS id, expect error michael@0: invalidId = -1; michael@0: log("Attempting to mark sms unread using an invalid id (id: " + invalidId michael@0: + "), expect error."); michael@0: markMsgError(invalidId, false, deleteMsg); michael@0: } michael@0: michael@0: function deleteMsg() { michael@0: log("Deleting SMS (id: " + smsId + ")."); michael@0: let request = manager.delete(smsId); michael@0: ok(request instanceof DOMRequest, michael@0: "request is instanceof " + request.constructor); michael@0: michael@0: request.onsuccess = function(event) { michael@0: log("Received 'onsuccess' smsrequest event."); michael@0: if (event.target.result) { michael@0: // Message deleted michael@0: cleanUp(); michael@0: } else { michael@0: log("SMS delete failed."); michael@0: ok(false,"manager.delete request returned false"); michael@0: cleanUp(); michael@0: } michael@0: }; michael@0: michael@0: request.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: cleanUp(); michael@0: }; michael@0: } michael@0: michael@0: function cleanUp() { michael@0: manager.onreceived = null; michael@0: SpecialPowers.removePermission("sms", document); michael@0: SpecialPowers.clearUserPref("dom.sms.enabled"); michael@0: finish(); michael@0: } michael@0: michael@0: // Start the test michael@0: verifyInitialState();