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 smsList = new Array(); 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: log("SMS read attribute: " + incomingSms.read + "."); michael@0: michael@0: // Add newly received message id to array of msgs michael@0: smsList.push(incomingSms.id); michael@0: michael@0: // Wait for emulator to catch up before continuing michael@0: waitFor(sendSms, function() { michael@0: return(rcvdEmulatorCallback); michael@0: }); michael@0: }; michael@0: michael@0: function sendSms() { michael@0: let gotSmsSent = false; michael@0: let gotRequestSuccess = false; michael@0: let remoteNumber = "5557779999"; michael@0: let text = "Mo Mo Mo Zilla Zilla Zilla!"; michael@0: michael@0: log("Sending an SMS."); michael@0: michael@0: manager.onsent = function(event) { michael@0: log("Received 'onsent' event."); michael@0: gotSmsSent = true; michael@0: let sentSms = event.message; michael@0: log("Sent SMS (id: " + sentSms.id + ")."); michael@0: is(sentSms.read, true, "sent sms read"); michael@0: log("SMS read attribute: " + sentSms.read + "."); michael@0: michael@0: // Add newly received message id to array of msgs michael@0: smsList.push(sentSms.id); michael@0: michael@0: if (gotSmsSent && gotRequestSuccess) { michael@0: test1(); michael@0: } michael@0: }; michael@0: michael@0: let request = manager.send(remoteNumber, text); michael@0: michael@0: request.onsuccess = function(event) { michael@0: log("Received 'onsuccess' smsrequest event."); michael@0: if(event.target.result) { michael@0: gotRequestSuccess = true; michael@0: if (gotSmsSent && gotRequestSuccess) { michael@0: test1(); michael@0: } michael@0: } else { michael@0: log("smsrequest returned false for manager.send"); michael@0: ok(false, "SMS send failed"); michael@0: deleteMsgs(); 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.send request returned unexpected error: " michael@0: + event.target.error.name ); michael@0: deleteMsgs(); michael@0: }; michael@0: } michael@0: michael@0: function markMessageAndVerify(smsId, readBool, nextFunction) { michael@0: let request = manager.markMessageRead(smsId, readBool); 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: michael@0: // Success from MarkMessageRead, the result should match what we set michael@0: is(event.target.result, readBool, "result matches what was set"); michael@0: michael@0: // Message marked read/unread, now verify michael@0: log("Getting SMS message (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, "SMS id matches"); michael@0: log("SMS read attribute: " + foundSms.read + "."); michael@0: let text = readBool ? "read" : "unread"; michael@0: if (foundSms.read == readBool) { michael@0: ok(true, "marked sms " + text); michael@0: } else { michael@0: ok(false, "marking sms " + text + " didn't work"); michael@0: log("Expected SMS (id: " + foundSms.id + ") to be marked " + text michael@0: + " but it is not."); michael@0: } 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: log("Could not get SMS (id: " + outSmsId + ") but should have."); michael@0: ok(false, "Could not get SMS"); michael@0: deleteMsgs(); 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.markMessageRead request returned unexpected error: " michael@0: + event.target.error.name ); michael@0: nextFunction(); michael@0: }; michael@0: } michael@0: michael@0: function test1() { michael@0: rcvdSms = smsList[0]; michael@0: log("Test 1: Marking received SMS (id: " + rcvdSms + ") read."); michael@0: markMessageAndVerify(rcvdSms, true, test2); michael@0: } michael@0: michael@0: function test2() { michael@0: rcvdSms = smsList[0]; michael@0: log("Test 2: Marking received SMS (id: " + rcvdSms + ") unread."); michael@0: markMessageAndVerify(rcvdSms, false, test3); michael@0: } michael@0: michael@0: function test3() { michael@0: sentSms = smsList[1]; michael@0: log("Test 3: Marking sent SMS (id: " + sentSms + ") unread."); michael@0: markMessageAndVerify(sentSms, false, test4); michael@0: } michael@0: michael@0: function test4() { michael@0: sentSms = smsList[1]; michael@0: log("Test 4: Marking sent SMS (id: " + sentSms + ") read."); michael@0: markMessageAndVerify(sentSms, true, test5); michael@0: } michael@0: michael@0: function test5() { michael@0: sentSms = smsList[1]; michael@0: log("Test 5: Marking an already read SMS (id: " + sentSms + ") read."); michael@0: markMessageAndVerify(sentSms, true, test6); michael@0: } michael@0: michael@0: function test6() { michael@0: rcvdSms = smsList[0]; michael@0: log("Test 6: Marking an already unread SMS (id: " + rcvdSms + ") unread."); michael@0: markMessageAndVerify(rcvdSms, false, deleteMsgs); michael@0: } michael@0: michael@0: function deleteMsgs() { michael@0: let smsId = smsList.shift(); michael@0: 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, continue until none are left michael@0: if (smsList.length) { michael@0: deleteMsgs(); michael@0: } else { michael@0: cleanUp(); michael@0: } 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();