Bug 1197667 - [NetworkManager] Part 2: add test case for 'allNetworkInfo'. r=echen
authorJessica Jong <jjong@mozilla.com>
Thu, 10 Sep 2015 14:50:21 +0800
changeset 294234 ab2b5214e3256f9ad018305c3c09d0913019688b
parent 294233 115f1a4faea667fb4519ff484ba14c3ca2e9972e
child 294235 84d3df460bc72a9290dedc94edf85335f7b35f6a
push id5245
push userraliiev@mozilla.com
push dateThu, 29 Oct 2015 11:30:51 +0000
treeherdermozilla-beta@dac831dc1bd0 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersechen
bugs1197667
milestone43.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 1197667 - [NetworkManager] Part 2: add test case for 'allNetworkInfo'. r=echen
dom/system/gonk/tests/marionette/head.js
dom/system/gonk/tests/marionette/manifest.ini
dom/system/gonk/tests/marionette/test_all_network_info.js
--- a/dom/system/gonk/tests/marionette/head.js
+++ b/dom/system/gonk/tests/marionette/head.js
@@ -1,15 +1,16 @@
 /* Any copyright is dedicated to the Public Domain.
  * http://creativecommons.org/publicdomain/zero/1.0/ */
 
 MARIONETTE_CONTEXT = "chrome";
 
 const SETTINGS_KEY_DATA_ENABLED = "ril.data.enabled";
 const SETTINGS_KEY_DATA_APN_SETTINGS  = "ril.data.apnSettings";
+const SETTINGS_KEY_WIFI_ENABLED = "wifi.enabled";
 
 const TOPIC_CONNECTION_STATE_CHANGED = "network-connection-state-changed";
 const TOPIC_NETWORK_ACTIVE_CHANGED = "network-active-changed";
 
 const NETWORK_STATE_UNKNOWN = Ci.nsINetworkInfo.NETWORK_STATE_UNKNOWN;
 const NETWORK_STATE_CONNECTING = Ci.nsINetworkInfo.NETWORK_STATE_CONNECTING;
 const NETWORK_STATE_CONNECTED = Ci.nsINetworkInfo.NETWORK_STATE_CONNECTED;
 const NETWORK_STATE_DISCONNECTING = Ci.nsINetworkInfo.NETWORK_STATE_DISCONNECTING;
@@ -125,16 +126,44 @@ function waitForObserverEvent(aTopic) {
       deferred.resolve(subject);
     }
   }, aTopic, false);
 
   return deferred.promise;
 }
 
 /**
+ * Wait for one named event.
+ *
+ * Resolve if that named event occurs.  Never reject.
+ *
+ * Fulfill params: the DOMEvent passed.
+ *
+ * @param aEventTarget
+ *        An EventTarget object.
+ * @param aEventName
+ *        A string event name.
+ * @param aMatchFun [optional]
+ *        A matching function returns true or false to filter the event.
+ *
+ * @return A deferred promise.
+ */
+function waitForTargetEvent(aEventTarget, aEventName, aMatchFun) {
+  return new Promise(function(aResolve, aReject) {
+    aEventTarget.addEventListener(aEventName, function onevent(aEvent) {
+      if (!aMatchFun || aMatchFun(aEvent)) {
+        aEventTarget.removeEventListener(aEventName, onevent);
+        ok(true, "Event '" + aEventName + "' got.");
+        aResolve(aEvent);
+      }
+    });
+  });
+}
+
+/**
  * Set the default data connection enabling state, wait for
  * "network-connection-state-changed" event and verify state.
  *
  * Fulfill params: (none)
  *
  * @param aEnabled
  *        A boolean state.
  *
--- a/dom/system/gonk/tests/marionette/manifest.ini
+++ b/dom/system/gonk/tests/marionette/manifest.ini
@@ -9,8 +9,9 @@ disabled = Bug 808783
 [test_ril_code_quality.py]
 [test_screen_state.js]
 [test_dsds_numRadioInterfaces.js]
 [test_data_connection.js]
 [test_network_active_changed.js]
 [test_multiple_data_connection.js]
 [test_data_connection_proxy.js]
 [test_network_interface_list_service.js]
+[test_all_network_info.js]
new file mode 100644
--- /dev/null
+++ b/dom/system/gonk/tests/marionette/test_all_network_info.js
@@ -0,0 +1,106 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+MARIONETTE_TIMEOUT = 60000;
+MARIONETTE_HEAD_JS = "head.js";
+
+let networkManager =
+  Cc["@mozilla.org/network/manager;1"].getService(Ci.nsINetworkManager);
+ok(networkManager,
+   "networkManager.constructor is " + networkManager.constructor);
+
+let wifiManager = window.navigator.mozWifiManager;
+ok(wifiManager, "wifiManager.constructor is " + wifiManager.constructor);
+
+function setEmulatorAPN() {
+  let apn = [
+    [{"carrier":"T-Mobile US",
+      "apn":"epc.tmobile.com",
+      "mmsc":"http://mms.msg.eng.t-mobile.com/mms/wapenc",
+      "types":["default","supl","mms","ims","dun", "fota"]}]
+  ];
+
+  return setSettings(SETTINGS_KEY_DATA_APN_SETTINGS, apn);
+}
+
+function ensureWifiEnabled(aEnabled) {
+  if (wifiManager.enabled === aEnabled) {
+    log('Already ' + (aEnabled ? 'enabled' : 'disabled'));
+    return Promise.resolve();
+  }
+  return requestWifiEnabled(aEnabled);
+}
+
+function requestWifiEnabled(aEnabled) {
+  let promises = [];
+
+  promises.push(waitForTargetEvent(wifiManager, aEnabled ? 'enabled' : 'disabled',
+    function() {
+      return wifiManager.enabled === aEnabled ? true : false;
+  }));
+  promises.push(setSettings(SETTINGS_KEY_WIFI_ENABLED, aEnabled));
+
+  return Promise.all(promises);
+}
+
+// Test initial State
+function verifyInitialState() {
+  log("= verifyInitialState =");
+
+  // Data and wifi should be off before starting any test.
+  return getSettings(SETTINGS_KEY_DATA_ENABLED)
+    .then(value => {
+      is(value, false, "Data must be off");
+    })
+    .then(() => ensureWifiEnabled(false));
+}
+
+function testAllNetworkInfo(aAnyConnected) {
+  log("= testAllNetworkInfo = " + aAnyConnected);
+
+  let allNetworkInfo = networkManager.allNetworkInfo;
+  ok(allNetworkInfo, "NetworkManager.allNetworkInfo");
+
+  let count = Object.keys(allNetworkInfo).length;
+  ok(count > 0, "NetworkManager.allNetworkInfo count");
+
+  let connected = false;
+  for (let networkId in allNetworkInfo) {
+    if (allNetworkInfo.hasOwnProperty(networkId)) {
+      let networkInfo = allNetworkInfo[networkId];
+      if (networkInfo.state == Ci.nsINetworkInfo.NETWORK_STATE_CONNECTED) {
+        connected = true;
+        break;
+      }
+    }
+  }
+
+  is(aAnyConnected, connected, "NetworkManager.allNetworkInfo any connected");
+}
+
+// Start test
+startTestBase(function() {
+
+  let origApnSettings, origWifiEnabled;
+  return Promise.resolve()
+    .then(() => {
+      origWifiEnabled = wifiManager.enabled;
+    })
+    .then(() => verifyInitialState())
+    .then(() => getSettings(SETTINGS_KEY_DATA_APN_SETTINGS))
+    .then(value => {
+      origApnSettings = value;
+    })
+    .then(() => setEmulatorAPN())
+    .then(() => setDataEnabledAndWait(true))
+    .then(() => testAllNetworkInfo(true))
+    .then(() => setDataEnabledAndWait(false))
+    .then(() => testAllNetworkInfo(false))
+    // Restore original apn settings and wifi state.
+    .then(() => {
+      if (origApnSettings) {
+        return setSettings(SETTINGS_KEY_DATA_APN_SETTINGS, origApnSettings);
+      }
+    })
+    .then(() => ensureWifiEnabled(origWifiEnabled));
+});