Bug 800377 - Develop a test to verify sms timestamps, update manifest; r=jgriffin, DONTBUILD(NPOTB)
authorRob Wood <rwood@mozilla.com>
Thu, 25 Oct 2012 09:50:56 -0400
changeset 111702 c45e5390c3cfa51f6cca23cb5ec0ecb5b87c8a19
parent 111701 a20a5429e7a5b998de5314073219e98c71b7155f
child 111747 c5428231b6f7b17a29ebd6ded683ed9b312c06b1
push id93
push usernmatsakis@mozilla.com
push dateWed, 31 Oct 2012 21:26:57 +0000
reviewersjgriffin, DONTBUILD
bugs800377
milestone19.0a1
Bug 800377 - Develop a test to verify sms timestamps, update manifest; r=jgriffin, DONTBUILD(NPOTB)
dom/sms/tests/marionette/manifest.ini
dom/sms/tests/marionette/test_timestamp.js
--- a/dom/sms/tests/marionette/manifest.ini
+++ b/dom/sms/tests/marionette/manifest.ini
@@ -6,8 +6,9 @@ qemu = true
 ;[test_between_emulators.py]
 [test_incoming.js]
 [test_outgoing.js]
 [test_message_classes.js]
 [test_incoming_delete.js]
 [test_outgoing_delete.js]
 [test_getmessage.js]
 [test_getmessage_notfound.js]
+[test_timestamp.js]
new file mode 100644
--- /dev/null
+++ b/dom/sms/tests/marionette/test_timestamp.js
@@ -0,0 +1,187 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+MARIONETTE_TIMEOUT = 15000;
+
+SpecialPowers.setBoolPref("dom.sms.enabled", true);
+SpecialPowers.addPermission("sms", true, document);
+
+let sms = window.navigator.mozSms;
+let inText;
+let remoteNumber = "5559997777";
+let outText;
+let gotSmsOnsent;
+let gotReqOnsuccess;
+let inSmsId = 0;
+let outSmsId = 0;
+let inSmsTime = 0;
+let outSmsTime = 0;
+let testCount = 10;
+
+function verifyInitialState() {
+  log("Verifying initial state.");
+  ok(sms, "mozSms");
+  simulateIncomingSms();  
+}
+
+function simulateIncomingSms() {
+  log("Simulating incoming SMS.");
+
+  sms.onreceived = function onreceived(event) {
+    log("Received 'onreceived' smsmanager event.");
+    let incomingSms = event.message;
+    ok(incomingSms, "incoming sms");
+    ok(incomingSms.id, "sms id");
+    inSmsId = incomingSms.id;
+    is(incomingSms.body, inText, "msg body");
+    is(incomingSms.delivery, "received", "delivery");
+    is(incomingSms.read, false, "read");
+    is(incomingSms.receiver, null, "receiver");
+    is(incomingSms.sender, remoteNumber, "sender");
+    ok(incomingSms.timestamp instanceof Date, "timestamp is instanceof date");
+    // The max resolution of the SCTS (SMS Centre TimeStamp) is one second
+    // therefore we want to compare in seconds not milliseconds. No need to
+    // round received sms timestamp, the value is in milliseconds but at
+    // seconds resolution already (always ends with 000) so just convert
+    inSmsTime = Math.floor(incomingSms.timestamp.getTime() / 1000);
+    log("Received SMS (id: " + inSmsId + ") timestamp: " + inSmsTime + ".");
+    if(outSmsTime) {
+      // Test is repeating; compare received SMS timestamp with last sent sms.
+      // Can be some time drift between the SCTS and host/emulator, but we are
+      // comparing seconds (not milliseconds) so should be ok. If this test
+      // goes random orange, then may need to add a 1 second lee-way here.
+      if(inSmsTime >= outSmsTime) {
+        log("Timestamp in sms " + inSmsId + " is >= timestamp in sms "
+            + outSmsId + ".");
+      } else {
+        log("* Timestamp in sms " + inSmsId + " is < timestamp in sms "
+            + outSmsId + ".");
+        ok(false, "sms timestamp is incorrect");
+      }
+    }
+    sendSms();
+  };
+  // Simulate incoming sms sent from remoteNumber to our emulator
+  inText = "Incoming SMS " + Date.now();
+  runEmulatorCmd("sms send " + remoteNumber + " " + inText, function(result) {
+    is(result[0], "OK", "emulator output");
+  });
+}
+
+function sendSms() {
+  log("Sending an SMS.");
+  let gotSmsOnsent = false;
+  let gotReqOnsuccess = false;  
+  sms.onsent = function(event) {
+    log("Received 'onsent' smsmanager event.");
+    gotSmsOnsent = true;
+    let sentSms = event.message;
+    ok(sentSms, "outgoing sms");
+    ok(sentSms.id, "sms id");
+    outSmsId = sentSms.id;
+    is(sentSms.body, outText, "msg body");
+    is(sentSms.delivery, "sent", "delivery");
+    is(sentSms.read, true, "read");
+    is(sentSms.receiver, remoteNumber, "receiver");
+    is(sentSms.sender, null, "sender");
+    ok(sentSms.timestamp instanceof Date, "timestamp is instanceof date");
+    // The max resolution of the SCTS (SMS Centre TimeStamp) is one second
+    // therefore we want to compare in seconds not milliseconds. Round the
+    // sent sms timestamp to nearest second.
+    outSmsTime = Math.round(sentSms.timestamp.getTime() / 1000);
+    log("Sent SMS (id: " + outSmsId + ") timestamp: " + outSmsTime + ".");
+
+    if (gotSmsOnsent && gotReqOnsuccess) { verifyTimeStamps(); }
+  };
+  outText = "Outgoing SMS " + Date.now();
+  let requestRet = sms.send(remoteNumber, outText);
+  ok(requestRet, "smsrequest obj returned");
+
+  requestRet.onsuccess = function(event) {
+    log("Received 'onsuccess' smsrequest event.");
+    gotReqOnsuccess = true;
+    if(event.target.result){
+      if (gotSmsOnsent && gotReqOnsuccess) { verifyTimeStamps(); }
+    } else {
+      log("smsrequest returned false for sms.send");
+      ok(false,"SMS send failed");
+      cleanUp();
+    }
+  };
+
+  requestRet.onerror = function(event) {
+    log("Received 'onerror' smsrequest event.");
+    ok(event.target.error, "domerror obj");
+    ok(false, "sms.send request returned unexpected error: "
+        + event.target.error.name );
+    cleanUp();
+  };
+}
+
+function verifyTimeStamps() {
+  // Compare sent sms timestamp with that of the previously received sms.
+  // Can be some time drift between the SCTS and host/emulator, but we are
+  // comparing seconds (not milliseconds) so should be ok. If this test
+  // goes random orange, then may need to add a 1 second lee-way here.
+  if(outSmsTime >= inSmsTime) {
+    log("Timestamp in sms " + outSmsId + " is >= timestamp in sms "
+        + inSmsId + ".");
+  } else {
+    log("* Timestamp in sms " + outSmsId + " is < timestamp in sms "
+        + inSmsId + ".");
+    ok(false, "sms timestamp is incorrect");
+  }
+  deleteMsgs();
+}
+
+function deleteMsgs() {
+  log("Deleting SMS (id: " + inSmsId + ").");
+  let requestRet = sms.delete(inSmsId);
+  ok(requestRet,"smsrequest obj returned");
+
+  requestRet.onsuccess = function(event) {
+    log("Received 'onsuccess' smsrequest event.");
+    if(event.target.result){
+      log("Deleting SMS (id: " + outSmsId + ").");
+      let nextReqRet = sms.delete(outSmsId);
+      ok(nextReqRet,"smsrequest obj returned");
+
+      nextReqRet.onsuccess = function(event) {
+        log("Received 'onsuccess' smsrequest event.");
+        if(event.target.result) {
+          if(--testCount) {
+            simulateIncomingSms();
+          } else {
+            cleanUp();
+          }
+        } else {
+          log("smsrequest returned false for sms.delete");
+          ok(false,"SMS delete failed");
+          cleanUp();
+        }
+      };
+    } else {
+      log("smsrequest returned false for sms.delete");
+      ok(false,"SMS delete failed");
+      cleanUp();
+    }
+  };
+
+  requestRet.onerror = function(event) {
+    log("Received 'onerror' smsrequest event.");
+    ok(event.target.error, "domerror obj");
+    ok(false, "sms.delete request returned unexpected error: "
+        + event.target.error.name );
+    cleanUp();
+  };
+}
+
+function cleanUp() {
+  sms.onreceived = null;
+  SpecialPowers.removePermission("sms", document);
+  SpecialPowers.setBoolPref("dom.sms.enabled", false);
+  finish();
+}
+
+// Start the test
+verifyInitialState();