Bug 891756 - [sms][mms] Gecko needs to return proper error code to notify Gaia the address is invalid (part 2, test case). r=vicamo, a=leo+
authorGene Lian <clian@mozilla.com>
Thu, 11 Jul 2013 11:03:05 +0800
changeset 119796 c7e0efd51559cc7636e4b7baf44b66a94d38d564
parent 119795 6ef8f8777c69aceed14f2e9a7f3ceb093a433e2a
child 119797 b3190912589ea10753a12613d133bbeb831d3973
push id996
push userryanvm@gmail.com
push dateTue, 23 Jul 2013 18:57:33 +0000
reviewersvicamo, leo
bugs891756
milestone18.1
Bug 891756 - [sms][mms] Gecko needs to return proper error code to notify Gaia the address is invalid (part 2, test case). r=vicamo, a=leo+
dom/mobilemessage/tests/marionette/manifest.ini
dom/mobilemessage/tests/marionette/test_invalid_address.js
--- a/dom/mobilemessage/tests/marionette/manifest.ini
+++ b/dom/mobilemessage/tests/marionette/manifest.ini
@@ -25,9 +25,10 @@ qemu = true
 [test_segment_info.js]
 [test_mark_msg_read.js]
 [test_mark_msg_read_error.js]
 [test_bug814761.js]
 [test_update_thread_record_in_delete.js]
 [test_massive_incoming_delete.js]
 [test_getsegmentinfofortext.js]
 [test_phone_number_normalization.js]
+[test_invalid_address.js]
 [test_getthreads.js]
new file mode 100644
--- /dev/null
+++ b/dom/mobilemessage/tests/marionette/test_invalid_address.js
@@ -0,0 +1,142 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+MARIONETTE_TIMEOUT = 60000;
+
+const MMS_MAX_LENGTH_SUBJECT = 40;
+
+SpecialPowers.addPermission("sms", true, document);
+SpecialPowers.setBoolPref("dom.sms.enabled", true);
+
+let tasks = {
+  // List of test fuctions. Each of them should call |tasks.next()| when
+  // completed or |tasks.finish()| to jump to the last one.
+  _tasks: [],
+  _nextTaskIndex: 0,
+
+  push: function push(func) {
+    this._tasks.push(func);
+  },
+
+  next: function next() {
+    let index = this._nextTaskIndex++;
+    let task = this._tasks[index];
+    try {
+      task();
+    } catch (ex) {
+      ok(false, "test task[" + index + "] throws: " + ex);
+      // Run last task as clean up if possible.
+      if (index != this._tasks.length - 1) {
+        this.finish();
+      }
+    }
+  },
+
+  finish: function finish() {
+    this._tasks[this._tasks.length - 1]();
+  },
+
+  run: function run() {
+    this.next();
+  }
+};
+
+let mozMobileMessage;
+
+function getAllMessages(callback, filter, reverse) {
+  if (!filter) {
+    filter = new MozSmsFilter;
+  }
+  let messages = [];
+  let request = mozMobileMessage.getMessages(filter, reverse || false);
+  request.onsuccess = function(event) {
+    if (request.result) {
+      messages.push(request.result);
+      request.continue();
+      return;
+    }
+
+    window.setTimeout(callback.bind(null, messages), 0);
+  }
+}
+
+function deleteAllMessages() {
+  getAllMessages(function deleteAll(messages) {
+    let message = messages.shift();
+    if (!message) {
+      ok(true, "all messages deleted");
+      tasks.next();
+      return;
+    }
+
+    let request = mozMobileMessage.delete(message.id);
+    request.onsuccess = deleteAll.bind(null, messages);
+    request.onerror = function (event) {
+      ok(false, "failed to delete all messages");
+      tasks.finish();
+    }
+  });
+}
+
+function testInvalidAddressForSMS(aInvalidAddr)  {
+  log("mozMobileMessage.send(...) should get 'InvalidAddressError' error " +
+      "when attempting to send SMS to: " + aInvalidAddr);
+
+  let request = mozMobileMessage.send(aInvalidAddr, "Test");
+
+  request.onerror = function(event) {
+    log("Received 'onerror' DOMRequest event.");
+    let error = event.target.error;
+    ok(error instanceof DOMError, "should be a valid DOMError object");
+    ok(error.name === "InvalidAddressError", "should be 'InvalidAddressError'");
+    tasks.next();
+  };
+}
+
+function testInvalidAddressForMMS(aInvalidAddrs)  {
+  log("mozMobileMessage.sendMMS(...) should get 'InvalidAddressError' error " +
+      "when attempting to send MMS to: " + aInvalidAddrs);
+
+  let request = mozMobileMessage.sendMMS({
+    subject: "Test",
+    receivers: aInvalidAddrs,
+    attachments: [],
+  });
+
+  request.onerror = function(event) {
+    log("Received 'onerror' DOMRequest event.");
+    let error = event.target.error;
+    ok(error instanceof DOMError, "should be a valid DOMError object");
+    ok(error.name === "InvalidAddressError", "should be 'InvalidAddressError'");
+    tasks.next();
+  };
+}
+
+tasks.push(function () {
+  log("Verifying initial state.");
+
+  mozMobileMessage = window.navigator.mozMobileMessage;
+  ok(mozMobileMessage instanceof MozMobileMessageManager);
+
+  tasks.next();
+});
+
+// Test sending SMS to invalid addresses.
+tasks.push(testInvalidAddressForSMS.bind(this, "&%&"));
+tasks.push(testInvalidAddressForSMS.bind(this, ""));
+
+// Test sending MMS to invalid addresses.
+tasks.push(testInvalidAddressForMMS.bind(this, ["&%&"]));
+tasks.push(testInvalidAddressForMMS.bind(this, [""]));
+tasks.push(testInvalidAddressForMMS.bind(this, ["123", "&%&", "456"]));
+
+tasks.push(deleteAllMessages);
+
+// WARNING: All tasks should be pushed before this!!!
+tasks.push(function cleanUp() {
+  SpecialPowers.removePermission("sms", document);
+  SpecialPowers.clearUserPref("dom.sms.enabled");
+  finish();
+});
+
+tasks.run();