Bug 973771 - Part 2: Marionette tests for LTE preferred network types. r=hsinyi
authorEdgar Chen <echen@mozilla.com>
Tue, 18 Feb 2014 17:09:25 +0800
changeset 170512 9b820751dd528621dfeba4083b01de1e16caf324
parent 170511 7470b2696fb4ee02c4e8ac212d63926e9130f5ab
child 170513 9f773e25965877718d6c5dca750c017cb801ec6d
push id270
push userpvanderbeken@mozilla.com
push dateThu, 06 Mar 2014 09:24:21 +0000
reviewershsinyi
bugs973771
milestone30.0a1
Bug 973771 - Part 2: Marionette tests for LTE preferred network types. r=hsinyi
dom/mobileconnection/tests/marionette/test_mobile_preferred_network_type.js
--- a/dom/mobileconnection/tests/marionette/test_mobile_preferred_network_type.js
+++ b/dom/mobileconnection/tests/marionette/test_mobile_preferred_network_type.js
@@ -1,153 +1,131 @@
 /* Any copyright is dedicated to the Public Domain.
    http://creativecommons.org/publicdomain/zero/1.0/ */
 
 MARIONETTE_TIMEOUT = 60000;
+MARIONETTE_HEAD_JS = "mobile_header.js";
 
-SpecialPowers.addPermission("mobileconnection", true, document);
+function doSetAndVerifyPreferredNetworkType(preferredNetworkType, callback) {
+  log("setPreferredNetworkType to '" + preferredNetworkType + "'.");
+  let setRequest = mobileConnection.setPreferredNetworkType(preferredNetworkType);
+  ok(setRequest instanceof DOMRequest,
+     "setRequest instanceof " + setRequest.constructor);
+
+  setRequest.onsuccess = function() {
+    log("Verify preferred network.");
+    let getRequest = mobileConnection.getPreferredNetworkType();
+    ok(getRequest instanceof DOMRequest,
+       "getRequest instanceof " + getRequest.constructor);
+
+    getRequest.onsuccess = function() {
+      is(getRequest.result, preferredNetworkType, "Check preferred network type.");
+      callback();
+    };
+
+    getRequest.onerror = function() {
+      ok(false, "getPreferredNetworkType got error: " + getRequest.error.name);
+      callback();
+    };
+  };
 
-let connection = navigator.mozMobileConnections[0];
-ok(connection instanceof MozMobileConnection,
-   "connection is instanceof " + connection.constructor);
+  setRequest.onerror = function() {
+    ok(false, "setPreferredNetwork got error: " + setRequest.error.name);
+    callback();
+  };
+}
+
+function doFailToSetPreferredNetworkType(preferredNetworkType, expectedError, callback) {
+  log("setPreferredNetworkType to '" + preferredNetworkType + "'.");
+  let request = mobileConnection.setPreferredNetworkType(preferredNetworkType);
+  ok(request instanceof DOMRequest,
+     "request instanceof " + request.constructor);
 
-function testSupportedNetworkTypes() {
-  let supportedNetworkTypes = connection.supportedNetworkTypes;
+  request.onsuccess = function() {
+    ok(false, "Should not success");
+    callback();
+  };
+
+  request.onerror = function() {
+    is(request.error.name, expectedError, "Check error message.");
+    callback();
+  };
+}
+
+/* Test supportedNetworkTypes */
+taskHelper.push(function testSupportedNetworkTypes() {
+  let supportedNetworkTypes = mobileConnection.supportedNetworkTypes;
 
   ok(Array.isArray(supportedNetworkTypes), "supportedNetworkTypes should be an array");
   ok(supportedNetworkTypes.indexOf("gsm") >= 0, "Should support 'gsm'");
   ok(supportedNetworkTypes.indexOf("wcdma") >= 0, "Should support 'wcdma'");
   ok(supportedNetworkTypes.indexOf("cdma") >= 0, "Should support 'cdma'");
   ok(supportedNetworkTypes.indexOf("evdo") >= 0, "Should support 'evdo'");
 
-  runNextTest();
-}
-
-function setPreferredNetworkType(type, callback) {
-  log("setPreferredNetworkType: " + type);
-
-  let request = connection.setPreferredNetworkType(type);
-  ok(request instanceof DOMRequest,
-     "request instanceof " + request.constructor);
-
-  request.onsuccess = function onsuccess() {
-    ok(true, "request success");
-    callback();
-  }
-  request.onerror = function onerror() {
-    ok(false, request.error);
-    callback();
-  }
-}
-
-function getPreferredNetworkType(callback) {
-  log("getPreferredNetworkType");
-
-  let request = connection.getPreferredNetworkType();
-  ok(request instanceof DOMRequest,
-     "request instanceof " + request.constructor);
+  taskHelper.runNext();
+});
 
-  request.onsuccess = function onsuccess() {
-    ok(true, "request success");
-    log("getPreferredNetworkType: " + request.result);
-    callback(request.result);
-  }
-  request.onerror = function onerror() {
-    ok(false, request.error);
-    callback();
-  }
-}
-
-function failToSetPreferredNetworkType(type, expectedError, callback) {
-  log("failToSetPreferredNetworkType: " + type + ", expected error: "
-    + expectedError);
-
-  let request = connection.setPreferredNetworkType(type);
-  ok(request instanceof DOMRequest,
-     "request instanceof " + request.constructor);
+/* Test switching to supported preferred types */
+taskHelper.push(function testPreferredNetworkTypes() {
+  let supportedTypes = [
+    'gsm',
+    'wcdma',
+    'wcdma/gsm-auto',
+    'cdma/evdo',
+    'evdo',
+    'cdma',
+    'wcdma/gsm/cdma/evdo',
+    // Restore to default
+    'wcdma/gsm'
+  ];
 
-  request.onsuccess = function onsuccess() {
-    ok(false, "request should not succeed");
-    callback();
-  }
-  request.onerror = function onerror() {
-    ok(true, "request error");
-    is(request.error.name, expectedError);
-    callback();
-  }
-}
-
-function setAndVerifyNetworkType(type) {
-  setPreferredNetworkType(type, function() {
-    getPreferredNetworkType(function(result) {
-      is(result, type);
-      testPreferredNetworkTypes();
-    });
-  });
-}
+  // Run all test data.
+  (function do_call() {
+    let type = supportedTypes.shift();
+    if (!type) {
+      taskHelper.runNext();
+      return;
+    }
+    doSetAndVerifyPreferredNetworkType(type, do_call);
+  })();
+});
 
-function testPreferredNetworkTypes() {
-  let networkType = supportedTypes.shift();
-  if (!networkType) {
-    runNextTest();
-    return;
-  }
-  setAndVerifyNetworkType(networkType);
-}
+/* Test switching to unsupported preferred types */
+taskHelper.push(function testUnsupportedPreferredNetworkTypes() {
+  // Currently emulator doesn't support lte network
+  let unsupportedTypes = [
+    'lte/cdma/evdo',
+    'lte/wcdma/gsm',
+    'lte/wcdma/gsm/cdma/evdo',
+    'lte'
+  ];
 
-function failToSetAndVerifyNetworkType(type, expectedError, previousType) {
-  failToSetPreferredNetworkType(type, expectedError, function() {
-    getPreferredNetworkType(function(result) {
-      // should return the previous selected type.
-      is(result, previousType);
-      testInvalidNetworkTypes();
-    });
-  });
-}
-
-function testInvalidNetworkTypes() {
-  let networkType = invalidTypes.shift();
-  if (!networkType) {
-    runNextTest();
-    return;
-  }
-  failToSetAndVerifyNetworkType(networkType, "InvalidParameter",
-                                "wcdma/gsm");
-}
+  // Run all test data.
+  (function do_call() {
+    let type = unsupportedTypes.shift();
+    if (!type) {
+      taskHelper.runNext();
+      return;
+    }
+    doFailToSetPreferredNetworkType(type, "ModeNotSupported", do_call);
+  })();
+});
 
-let supportedTypes = [
-  'gsm',
-  'wcdma',
-  'wcdma/gsm-auto',
-  'cdma/evdo',
-  'evdo',
-  'cdma',
-  'wcdma/gsm/cdma/evdo',
-  'wcdma/gsm' // restore to default
-];
-
-let invalidTypes = [
-  ' ',
-  'AnInvalidType'
-];
+/* Test switching to invalid preferred types */
+taskHelper.push(function testInvalidPreferredNetworkTypes() {
+  let invalidTypes = [
+    ' ',
+    'AnInvalidType'
+  ];
 
-let tests = [
-  testSupportedNetworkTypes,
-  testPreferredNetworkTypes,
-  testInvalidNetworkTypes
-];
+  // Run all test data.
+  (function do_call() {
+    let type = invalidTypes.shift();
+    if (!type) {
+      taskHelper.runNext();
+      return;
+    }
+    doFailToSetPreferredNetworkType(type, "InvalidParameter", do_call);
+  })();
+});
 
-function runNextTest() {
-  let test = tests.shift();
-  if (!test) {
-    cleanUp();
-    return;
-  }
-
-  test();
-}
-
-function cleanUp() {
-  SpecialPowers.removePermission("mobileconnection", document);
-  finish();
-}
-
-runNextTest();
+// Start test
+taskHelper.runNext();