Bug 996409 - Part 2: Marionette test for 'network-active-changed'. r=hsinyi
authorEdgar Chen <echen@mozilla.com>
Mon, 14 Apr 2014 18:15:20 +0800
changeset 179295 c48e44a0e5d128ba198b7da24ba1876f4bd1d613
parent 179294 fe7d26409026ac7c3fa756d160556eaf82ef1f65
child 179296 c36ef908c673266fdb5f26a95d76f20874d30a7f
push id272
push userpvanderbeken@mozilla.com
push dateMon, 05 May 2014 16:31:18 +0000
reviewershsinyi
bugs996409
milestone31.0a1
Bug 996409 - Part 2: Marionette test for 'network-active-changed'. r=hsinyi
dom/system/gonk/tests/marionette/head.js
dom/system/gonk/tests/marionette/manifest.ini
dom/system/gonk/tests/marionette/test_network_active_changed.js
new file mode 100644
--- /dev/null
+++ b/dom/system/gonk/tests/marionette/head.js
@@ -0,0 +1,134 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+MARIONETTE_CONTEXT = "chrome";
+
+let Promise = Cu.import("resource://gre/modules/Promise.jsm").Promise;
+
+/**
+ * Wrap DOMRequest onsuccess/onerror events to Promise resolve/reject.
+ *
+ * Fulfill params: A DOMEvent.
+ * Reject params: A DOMEvent.
+ *
+ * @param aRequest
+ *        A DOMRequest instance.
+ *
+ * @return A deferred promise.
+ */
+function wrapDomRequestAsPromise(aRequest) {
+  let deferred = Promise.defer();
+
+  ok(aRequest instanceof DOMRequest,
+     "aRequest is instanceof " + aRequest.constructor);
+
+  aRequest.addEventListener("success", function(aEvent) {
+    deferred.resolve(aEvent);
+  });
+  aRequest.addEventListener("error", function(aEvent) {
+    deferred.reject(aEvent);
+  });
+
+  return deferred.promise;
+}
+
+/**
+ * Get mozSettings value specified by @aKey.
+ *
+ * Resolve if that mozSettings value is retrieved successfully, reject
+ * otherwise.
+ *
+ * Fulfill params: The corresponding mozSettings value of the key.
+ * Reject params: (none)
+ *
+ * @param aKey
+ *        A string.
+ * @param aAllowError [optional]
+ *        A boolean value.  If set to true, an error response won't be treated
+ *        as test failure.  Default: false.
+ *
+ * @return A deferred promise.
+ */
+function getSettings(aKey, aAllowError) {
+  let request = window.navigator.mozSettings.createLock().get(aKey);
+  return wrapDomRequestAsPromise(request)
+    .then(function resolve(aEvent) {
+      log("getSettings(" + aKey + ") - success");
+      return aEvent.target.result[aKey];
+    }, function reject(aEvent) {
+      ok(aAllowError, "getSettings(" + aKey + ") - error");
+    });
+}
+
+/**
+ * Set mozSettings values.
+ *
+ * Resolve if that mozSettings value is set successfully, reject otherwise.
+ *
+ * Fulfill params: (none)
+ * Reject params: (none)
+ *
+ * @param aKey
+ *        A string key.
+ * @param aValue
+ *        An object value.
+ * @param aAllowError [optional]
+ *        A boolean value.  If set to true, an error response won't be treated
+ *        as test failure.  Default: false.
+ *
+ * @return A deferred promise.
+ */
+function setSettings(aKey, aValue, aAllowError) {
+  let settings = {};
+  settings[aKey] = aValue;
+  let request = window.navigator.mozSettings.createLock().set(settings);
+  return wrapDomRequestAsPromise(request)
+    .then(function resolve() {
+      log("setSettings(" + JSON.stringify(settings) + ") - success");
+    }, function reject() {
+      ok(aAllowError, "setSettings(" + JSON.stringify(settings) + ") - error");
+    });
+}
+
+/**
+ * Wait for observer event.
+ *
+ * Resolve if that topic event occurs.  Never reject.
+ *
+ * Fulfill params: the subject passed.
+ *
+ * @param aTopic
+ *        A string topic name.
+ *
+ * @return A deferred promise.
+ */
+function waitForObserverEvent(aTopic) {
+  let obs = Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService);
+  let deferred = Promise.defer();
+
+  obs.addObserver(function observer(subject, topic, data) {
+    if (topic === aTopic) {
+      obs.removeObserver(observer, aTopic);
+      deferred.resolve(subject);
+    }
+  }, aTopic, false);
+
+  return deferred.promise;
+}
+
+/**
+ * Basic test routine helper.
+ *
+ * This helper does nothing but clean-ups.
+ *
+ * @param aTestCaseMain
+ *        A function that takes no parameter.
+ */
+function startTestBase(aTestCaseMain) {
+  Promise.resolve()
+    .then(aTestCaseMain)
+    .then(finish, function() {
+      ok(false, 'promise rejects during test.');
+      finish();
+    });
+}
--- a/dom/system/gonk/tests/marionette/manifest.ini
+++ b/dom/system/gonk/tests/marionette/manifest.ini
@@ -5,8 +5,9 @@ qemu = true
 
 [test_geolocation.js]
 disabled = Bug 808783
 [test_fakevolume.js]
 [test_ril_code_quality.py]
 [test_screen_state.js]
 [test_dsds_numRadioInterfaces.js]
 [test_data_connection.js]
+[test_network_active_changed.js]
new file mode 100644
--- /dev/null
+++ b/dom/system/gonk/tests/marionette/test_network_active_changed.js
@@ -0,0 +1,54 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+MARIONETTE_TIMEOUT = 60000;
+MARIONETTE_HEAD_JS = "head.js";
+
+const SETTINGS_KEY_DATA_ENABLED = "ril.data.enabled";
+const TOPIC_NETWORK_ACTIVE_CHANGED = "network-active-changed";
+
+let networkManager =
+  Cc["@mozilla.org/network/manager;1"].getService(Ci.nsINetworkManager);
+ok(networkManager,
+   "networkManager.constructor is " + networkManager.constructor);
+
+function testInitialState() {
+  return Promise.resolve()
+    .then(() => getSettings(SETTINGS_KEY_DATA_ENABLED))
+    .then((enabled) => {
+      is(enabled, false, "data should be off by default");
+      is(networkManager.active, null,
+         "networkManager.active should be null by default");
+    });
+}
+
+function testActiveNetworkChangedBySwitchingDataCall(aDataCallEnabled) {
+  log("Test active network by switching dataCallEnabled to " + aDataCallEnabled);
+
+  return Promise.resolve()
+    .then(() => setSettings(SETTINGS_KEY_DATA_ENABLED, aDataCallEnabled))
+    .then(() => waitForObserverEvent(TOPIC_NETWORK_ACTIVE_CHANGED))
+    .then((subject) => {
+      if (aDataCallEnabled) {
+        ok(subject instanceof Ci.nsINetworkInterface,
+           "subject should be an instance of nsINetworkInterface");
+        ok(subject instanceof Ci.nsIRilNetworkInterface,
+           "subject should be an instance of nsIRILNetworkInterface");
+        is(subject.type, Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE,
+           "subject.type should be NETWORK_TYPE_MOBILE");
+      }
+
+      is(subject, networkManager.active,
+         "subject should be equal with networkManager.active");
+    });
+}
+
+// Start test
+startTestBase(function() {
+  return Promise.resolve()
+    .then(() => testInitialState())
+    // Test active network changed by enabling data call.
+    .then(() => testActiveNetworkChangedBySwitchingDataCall(true))
+    // Test active network changed by disabling data call.
+    .then(() => testActiveNetworkChangedBySwitchingDataCall(false));
+});