Bug 787370 - Part 3/3: test script, r=marshall_law
authorVicamo Yang <vyang@mozilla.com>
Mon, 10 Sep 2012 13:56:08 +0800
changeset 108286 21244f4a873b89c23886889d6780dca74f98adcf
parent 108285 3eb1c445608212ff33b9101653591a59a1f2a521
child 108287 12dad118c02f3d20b142999462db065b539f6875
child 108298 144e050b27ccf0de0995b81db1d8d0327f7359cb
push id2248
push userakeybl@mozilla.com
push dateMon, 08 Oct 2012 19:23:44 +0000
treeherdermozilla-aurora@118a3b748323 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmarshall_law
bugs787370
milestone18.0a1
Bug 787370 - Part 3/3: test script, r=marshall_law
dom/sms/tests/marionette/manifest.ini
dom/sms/tests/marionette/test_outgoing.js
--- a/dom/sms/tests/marionette/manifest.ini
+++ b/dom/sms/tests/marionette/manifest.ini
@@ -1,7 +1,8 @@
 [DEFAULT]
 b2g = true
 browser = false
 qemu = true
 
 [test_between_emulators.py]
 [test_incoming.js]
+[test_outgoing.js]
new file mode 100644
--- /dev/null
+++ b/dom/sms/tests/marionette/test_outgoing.js
@@ -0,0 +1,143 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+MARIONETTE_TIMEOUT = 20000;
+
+SpecialPowers.setBoolPref("dom.sms.enabled", true);
+SpecialPowers.addPermission("sms", true, document);
+SpecialPowers.addPermission("mobileconnection", true, document);
+
+let sms = window.navigator.mozSms;
+let receiver = "5555552368";
+let body = "Hello SMS world!";
+
+function checkSentMessage(message, sentDate) {
+  ok(message, "message is valid");
+  ok(message instanceof MozSmsMessage,
+     "message is instanceof " + message.constructor);
+
+  ok(message.id, "message.id is valid");
+  is(message.delivery, "sent", "message.delivery");
+  is(message.sender, null, "message.sender");
+  is(message.receiver, receiver, "message.receiver");
+  is(message.body, body, "message.body");
+  ok(message.timestamp instanceof Date,
+     "message.timestamp is instanceof " + message.timestamp.constructor);
+  // SMSC timestamp is in seconds.
+  ok(Math.floor(message.timestamp.getTime() / 1000) >= Math.floor(sentDate / 1000),
+     "sent timestamp is valid");
+  is(message.read, true, "message.read");
+}
+
+let sentMessages = null;
+function checkSameSentMessage(message, now) {
+  checkSentMessage(message, now);
+
+  let sentMessage = sentMessages[message.id];
+  if (!sentMessage) {
+    sentMessages[message.id] = message;
+    return;
+  }
+
+  // The two message instance should be exactly the same, but since we had
+  // already checked most attributes of them, we only compare their
+  // timestamps here.
+  ok(sentMessage.timestamp.getTime() == message.timestamp.getTime(),
+     "the messages got from onsent event and request result must be the same");
+}
+
+function doSendMessageAndCheckSuccess(receivers, callback) {
+  sentMessages = [];
+
+  let now = Date.now();
+  let count;
+
+  function done() {
+    if (--count > 0) {
+      return;
+    }
+
+    sms.removeEventListener("sent", onSmsSent);
+    setTimeout(callback, 0);
+  }
+
+  function onSmsSent(event) {
+    log("SmsManager.onsent event received.");
+
+    ok(event instanceof MozSmsEvent,
+       "event is instanceof " + event.constructor);
+    // Event listener is removed in done().
+
+    checkSameSentMessage(event.message, now);
+
+    done();
+  }
+
+  function onRequestSuccess(event) {
+    log("SmsRequest.onsuccess event received.");
+
+    ok(event.target instanceof MozSmsRequest,
+       "event.target is instanceof " + event.target.constructor);
+    event.target.removeEventListener("success", onRequestSuccess);
+
+    checkSameSentMessage(event.target.result, now);
+
+    done();
+  }
+
+  sms.addEventListener("sent", onSmsSent);
+
+  let result = sms.send(receivers, body);
+  if (Array.isArray(receivers)) {
+    ok(Array.isArray(result),
+      "send() returns an array of requests if receivers is an array");
+    is(result.length, receivers.length, "returned array length");
+
+    // `receivers` is an array, so we have N request.onsuccess and N sms.onsent.
+    count = result.length * 2;
+
+    for (let i = 0; i < result.length; i++) {
+      let request = result[i];
+      ok(request instanceof MozSmsRequest,
+         "request is instanceof " + request.constructor);
+      request.addEventListener("success", onRequestSuccess);
+    }
+
+    return;
+  }
+
+  // `receivers` is not an array, so we have one request.onsuccess and one
+  // sms.onsent.
+  count = 2;
+
+  let request = result;
+  ok(request instanceof MozSmsRequest,
+     "request is instanceof " + request.constructor);
+  request.addEventListener("success", onRequestSuccess);
+}
+
+function testSendMessage() {
+  log("Testing sending message to one receiver:");
+  doSendMessageAndCheckSuccess(receiver, testSendMessageToMultipleRecipients);
+}
+
+function testSendMessageToMultipleRecipients() {
+  log("Testing sending message to multiple receivers:");
+  // TODO: bug 788928 - add test cases for nsIDOMSmsManager.ondelivered event
+  doSendMessageAndCheckSuccess([receiver, receiver], cleanUp);
+}
+
+function cleanUp() {
+  SpecialPowers.removePermission("sms", document);
+  SpecialPowers.removePermission("mobileconnection", document);
+  finish();
+}
+
+waitFor(testSendMessage, function () {
+  let connected = navigator.mozMobileConnection.voice.connected;
+  if (!connected) {
+    log("MobileConnection is not ready yet.");
+  }
+  return connected;
+});
+