Bug 1197667 - [NetworkManager] Part 2: add test case for 'allNetworkInfo'. r=echen

This commit is contained in:
Jessica Jong 2015-09-10 14:50:21 +08:00
parent 9d1718bfd2
commit 099c753d40
3 changed files with 136 additions and 0 deletions

View File

@ -5,6 +5,7 @@ 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";
@ -129,6 +130,34 @@ function waitForObserverEvent(aTopic) {
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.

View File

@ -14,3 +14,4 @@ disabled = Bug 808783
[test_multiple_data_connection.js]
[test_data_connection_proxy.js]
[test_network_interface_list_service.js]
[test_all_network_info.js]

View File

@ -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));
});