Bug 877141 - add test cases for sending SMS with continuous space characters. r=gene
authorVicamo Yang <vyang@mozilla.com>
Wed, 05 Jun 2013 17:42:42 +0800
changeset 134135 307d7abbf1c01e522a1cea8b6ba66421f2c2fcb3
parent 134134 afb71a02b5c571db38d01dde057acce376ef619d
child 134136 e49f78051ab4dc9cc0b00806a2c97f12102a1297
push id29067
push userryanvm@gmail.com
push dateWed, 05 Jun 2013 20:37:20 +0000
treeherdermozilla-inbound@72fbfb2f8e51 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgene
bugs877141
milestone24.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 877141 - add test cases for sending SMS with continuous space characters. r=gene
dom/mobilemessage/tests/marionette/test_strict_7bit_encoding.js
--- a/dom/mobilemessage/tests/marionette/test_strict_7bit_encoding.js
+++ b/dom/mobilemessage/tests/marionette/test_strict_7bit_encoding.js
@@ -111,36 +111,61 @@ const GSM_SMS_STRICT_7BIT_CHARMAP = {
 const SELF = "5554";
 
 SpecialPowers.setBoolPref("dom.sms.enabled", true);
 SpecialPowers.addPermission("sms", true, document);
 
 let sms = window.navigator.mozSms;
 ok(sms instanceof MozSmsManager);
 
-function repeat(func, array, oncomplete) {
-  (function do_call(index) {
-    let next = index < (array.length - 1) ? do_call.bind(null, index + 1) : oncomplete;
-    array[index].push(next);
-    func.apply(null, array[index]);
-  })(0);
-}
+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);
+  },
 
-function testStrict7BitEncodingHelper(sent, received, next) {
+  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();
+  }
+};
+
+function testStrict7BitEncodingHelper(sent, received) {
   // The log message contains unicode and Marionette seems unable to process
   // it and throws: |UnicodeEncodeError: 'ascii' codec can't encode character
   // u'\xa5' in position 14: ordinal not in range(128)|.
   //
   //log("Testing '" + sent + "' => '" + received + "'");
 
   let count = 0;
   function done(step) {
     count += step;
     if (count >= 2) {
-      window.setTimeout(next, 0);
+      window.setTimeout(tasks.next.bind(tasks), 0);
     }
   }
 
   sms.addEventListener("received", function onReceived(event) {
     event.target.removeEventListener("received", onReceived);
 
     let message = event.message;
     is(message.body, received, "received message.body");
@@ -156,58 +181,80 @@ function testStrict7BitEncodingHelper(se
     done(1);
   });
   request.addEventListener("error", function onRequestError(event) {
     ok(false, "Can't send message out!!!");
     done(2);
   });
 }
 
-function test_enabled() {
+// Bug 877141 - If you send several spaces together in a sms, the other
+//              dipositive receives a "*" for each space.
+//
+// This function is called twice, with strict 7bit encoding enabled or
+// disabled.  Expect the same result in both sent and received text and with
+// either strict 7bit encoding enabled or disabled.
+function testBug877141() {
+  log("Testing bug 877141");
+  let sent = "1 2     3";
+  testStrict7BitEncodingHelper(sent, sent);
+}
+
+tasks.push(function () {
   log("Testing with dom.sms.strict7BitEncoding enabled");
-
   SpecialPowers.setBoolPref("dom.sms.strict7BitEncoding", true);
+  tasks.next();
+});
 
-  let cases = [];
 
-  // Test for combined string.
+// Test for combined string.
+tasks.push(function () {
   let sent = "", received = "";
   for (let c in GSM_SMS_STRICT_7BIT_CHARMAP) {
     sent += c;
     received += GSM_SMS_STRICT_7BIT_CHARMAP[c];
   }
-  cases.push([sent, received]);
+  testStrict7BitEncodingHelper(sent, received);
+});
 
-  // When strict7BitEncoding is enabled, we should replace characters that
-  // can't be encoded with GSM 7-Bit alphabets with '*'.
-  cases.push(["\u65b0\u5e74\u5feb\u6a02", "****"]); // "Happy New Year" in Chinese.
-
-  repeat(testStrict7BitEncodingHelper, cases, test_disabled);
-}
+// When strict7BitEncoding is enabled, we should replace characters that
+// can't be encoded with GSM 7-Bit alphabets with '*'.
+tasks.push(function () {
+  // "Happy New Year" in Chinese.
+  let sent = "\u65b0\u5e74\u5feb\u6a02", received = "****";
+  testStrict7BitEncodingHelper(sent, received);
+});
 
-function test_disabled() {
+tasks.push(testBug877141);
+
+tasks.push(function () {
   log("Testing with dom.sms.strict7BitEncoding disabled");
-
   SpecialPowers.setBoolPref("dom.sms.strict7BitEncoding", false);
+  tasks.next();
+});
 
-  let cases = [];
-
-  // Test for combined string.
+// Test for combined string.
+tasks.push(function () {
   let sent = "";
   for (let c in GSM_SMS_STRICT_7BIT_CHARMAP) {
     sent += c;
   }
-  cases.push([sent, sent]);
-
-  cases.push(["\u65b0\u5e74\u5feb\u6a02", "\u65b0\u5e74\u5feb\u6a02"]);
+  testStrict7BitEncodingHelper(sent, sent);
+});
 
-  repeat(testStrict7BitEncodingHelper, cases, cleanUp);
-}
+tasks.push(function () {
+  // "Happy New Year" in Chinese.
+  let sent = "\u65b0\u5e74\u5feb\u6a02";
+  testStrict7BitEncodingHelper(sent, sent);
+});
 
-function cleanUp() {
+tasks.push(testBug877141);
+
+// WARNING: All tasks should be pushed before this!!!
+tasks.push(function cleanUp() {
   SpecialPowers.removePermission("sms", document);
   SpecialPowers.clearUserPref("dom.sms.enabled");
   SpecialPowers.clearUserPref("dom.sms.strict7BitEncoding");
 
   finish();
-}
+});
 
-test_enabled();
+tasks.run();