Bug 929701 - Part 4-1: Convert test_mobile_mmi.js to Promise. r=hsinyi
authorEdgar Chen <echen@mozilla.com>
Wed, 23 Apr 2014 15:07:17 +0800
changeset 181394 e77760fd73329f5a620175ba6773e637508bc4c7
parent 181393 1c1e3bae34ba98351691fbb9de119a7dd11098d6
child 181395 f8e0340f374aa473bdf8da303d7f29dcc342c460
push id272
push userpvanderbeken@mozilla.com
push dateMon, 05 May 2014 16:31:18 +0000
reviewershsinyi
bugs929701
milestone32.0a1
Bug 929701 - Part 4-1: Convert test_mobile_mmi.js to Promise. r=hsinyi
dom/mobileconnection/tests/marionette/head.js
dom/mobileconnection/tests/marionette/test_mobile_mmi.js
--- a/dom/mobileconnection/tests/marionette/head.js
+++ b/dom/mobileconnection/tests/marionette/head.js
@@ -383,16 +383,33 @@ function selectNetworkAutomaticallyAndWa
 
   promises.push(waitForManagerEvent("voicechange"));
   promises.push(selectNetworkAutomatically());
 
   return Promise.all(promises);
 }
 
 /**
+ * Send a MMI message.
+ *
+ * Fulfill params: An object contains MMI result.
+ * Reject params: A DOMMMIError.
+ *
+ * @param aMmi
+ *        A MMI string.
+ *
+ * @return A deferred promise.
+ */
+function sendMMI(aMmi) {
+  let request = mobileConnection.sendMMI(aMmi);
+  return wrapDomRequestAsPromise(request)
+    .then(() => request.result, () => { throw request.error });
+}
+
+/**
  * Set data connection enabling state and wait for "datachange" event.
  *
  * Resolve if data connection state changed to the expected one.  Never reject.
  *
  * Fulfill params: (none)
  *
  * @param aEnabled
  *        A boolean state.
--- a/dom/mobileconnection/tests/marionette/test_mobile_mmi.js
+++ b/dom/mobileconnection/tests/marionette/test_mobile_mmi.js
@@ -1,107 +1,46 @@
 /* Any copyright is dedicated to the Public Domain.
  * http://creativecommons.org/publicdomain/zero/1.0/ */
 
-MARIONETTE_TIMEOUT = 20000;
-
-SpecialPowers.addPermission("mobileconnection", true, document);
-
-// Permission changes can't change existing Navigator.prototype
-// objects, so grab our objects from a new Navigator
-let ifr = document.createElement("iframe");
-let mobileConnection;
-ifr.onload = function() {
-  mobileConnection = ifr.contentWindow.navigator.mozMobileConnections[0];
-
-  tasks.run();
-};
-document.body.appendChild(ifr);
-
-let tasks = {
-  // List of test functions. Each of them should call |tasks.next()| when
-  // completed or |tasks.abort()| to jump to the last one.
-  _tasks: [],
-  _nextTaskIndex: 0,
-
-  push: function(func) {
-    this._tasks.push(func);
-  },
+MARIONETTE_TIMEOUT = 60000;
+MARIONETTE_HEAD_JS = "head.js";
 
-  next: function() {
-    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.abort();
-      }
-    }
-  },
-
-  abort: function() {
-    this._tasks[this._tasks.length - 1]();
-  },
-
-  run: function() {
-    this.next();
-  }
-};
-
-tasks.push(function verifyInitialState() {
-  log("Verifying initial state.");
-
-  ok(mobileConnection instanceof ifr.contentWindow.MozMobileConnection,
-      "mobileConnection is instanceof " + mobileConnection.constructor);
-
-  tasks.next();
-});
-
-tasks.push(function testGettingIMEI() {
+function testGettingIMEI() {
   log("Test *#06# ...");
 
-  let request = mobileConnection.sendMMI("*#06#");
-  ok(request instanceof DOMRequest,
-     "request is instanceof " + request.constructor);
+  let MMI_CODE = "*#06#";
+  return sendMMI(MMI_CODE)
+    .then(function resolve(aResult) {
+      ok(true, MMI_CODE + " success");
+      is(aResult.serviceCode, "scImei", "Service code IMEI");
+      // IMEI is hardcoded as "000000000000000".
+      // See it here {B2G_HOME}/external/qemu/telephony/android_modem.c
+      // (The result of +CGSN).
+      is(aResult.statusMessage, "000000000000000", "Emulator IMEI");
+      is(aResult.additionalInformation, undefined, "No additional information");
+    }, function reject() {
+      ok(false, MMI_CODE + " should not fail");
+    });
+}
 
-  request.onsuccess = function onsuccess(event) {
-    ok(true, "request success");
-    is(typeof event.target.result, "object", "typeof result object");
-    ok(event.target.result instanceof Object, "result instanceof Object");
-    is(event.target.result.statusMessage, "000000000000000", "Emulator IMEI");
-    is(event.target.result.serviceCode, "scImei", "Service code IMEI");
-    is(event.target.result.additionalInformation, undefined,
-       "No additional information");
-    tasks.next();
-  }
-  request.onerror = function onerror() {
-    ok(false, "request should not error");
-    tasks.abort();
-  };
-});
-
-tasks.push(function testInvalidMMICode(){
+function testInvalidMMICode() {
   log("Test invalid MMI code ...");
 
-  let request = mobileConnection.sendMMI("InvalidMMICode");
-  ok(request instanceof DOMRequest,
-     "request is instanceof " + request.constructor);
-
-  request.onsuccess = function onsuccess(event) {
-    ok(false, "request should not success");
-    tasks.abort();
-  };
+  let MMI_CODE = "InvalidMMICode";
+  return sendMMI(MMI_CODE)
+    .then(function resolve() {
+      ok(false, MMI_CODE + " should not success");
+    }, function reject(aError) {
+      ok(true, MMI_CODE + " fail");
+      is(aError.name, "emMmiError", "MMI error name");
+      is(aError.message, "", "No message");
+      is(aError.serviceCode, "", "No serviceCode");
+      is(aError.additionalInformation, null, "No additional information");
+    });
+}
 
-  request.onerror = function onerror() {
-    ok(true, "request error");
-    is(request.error.name, "emMmiError", "MMI error name");
-    tasks.next();
-  };
+// Start test
+startTestCommon(function() {
+   return Promise.resolve()
+    .then(() => testGettingIMEI())
+    .then(() => testInvalidMMICode());
 });
-
-// WARNING: All tasks should be pushed before this!!!
-tasks.push(function cleanUp() {
-  SpecialPowers.removePermission("mobileconnection", document);
-  finish();
-});