gecko/dom/mobileconnection/tests/marionette/test_mobile_data_connection.js
Edgar Chen 5284ea1427 Bug 956655 - Part 1: Move MobileConnection related files to dom/mobileconnection. r=smaug,khuey
--HG--
rename : dom/network/interfaces/nsIDOMMobileConnection.idl => dom/mobileconnection/interfaces/nsIDOMMobileConnection.idl
rename : dom/network/interfaces/nsIMobileConnectionProvider.idl => dom/mobileconnection/interfaces/nsIMobileConnectionProvider.idl
rename : dom/network/tests/marionette/manifest.ini => dom/mobileconnection/tests/marionette/manifest.ini
rename : dom/network/tests/marionette/test_call_barring_change_password.js => dom/mobileconnection/tests/marionette/test_call_barring_change_password.js
rename : dom/network/tests/marionette/test_call_barring_get_option.js => dom/mobileconnection/tests/marionette/test_call_barring_get_option.js
rename : dom/network/tests/marionette/test_call_barring_set_error.js => dom/mobileconnection/tests/marionette/test_call_barring_set_error.js
rename : dom/network/tests/marionette/test_mobile_connections_array_uninitialized.js => dom/mobileconnection/tests/marionette/test_mobile_connections_array_uninitialized.js
rename : dom/network/tests/marionette/test_mobile_data_connection.js => dom/mobileconnection/tests/marionette/test_mobile_data_connection.js
rename : dom/network/tests/marionette/test_mobile_data_location.js => dom/mobileconnection/tests/marionette/test_mobile_data_location.js
rename : dom/network/tests/marionette/test_mobile_data_state.js => dom/mobileconnection/tests/marionette/test_mobile_data_state.js
rename : dom/network/tests/marionette/test_mobile_icc_change.js => dom/mobileconnection/tests/marionette/test_mobile_icc_change.js
rename : dom/network/tests/marionette/test_mobile_last_known_network.js => dom/mobileconnection/tests/marionette/test_mobile_last_known_network.js
rename : dom/network/tests/marionette/test_mobile_mmi.js => dom/mobileconnection/tests/marionette/test_mobile_mmi.js
rename : dom/network/tests/marionette/test_mobile_networks.js => dom/mobileconnection/tests/marionette/test_mobile_networks.js
rename : dom/network/tests/marionette/test_mobile_operator_names.js => dom/mobileconnection/tests/marionette/test_mobile_operator_names.js
rename : dom/network/tests/marionette/test_mobile_preferred_network_type.js => dom/mobileconnection/tests/marionette/test_mobile_preferred_network_type.js
rename : dom/network/tests/marionette/test_mobile_preferred_network_type_by_setting.js => dom/mobileconnection/tests/marionette/test_mobile_preferred_network_type_by_setting.js
rename : dom/network/tests/marionette/test_mobile_roaming_preference.js => dom/mobileconnection/tests/marionette/test_mobile_roaming_preference.js
rename : dom/network/tests/marionette/test_mobile_set_radio.js => dom/mobileconnection/tests/marionette/test_mobile_set_radio.js
rename : dom/network/tests/marionette/test_mobile_voice_state.js => dom/mobileconnection/tests/marionette/test_mobile_voice_state.js
2013-12-24 15:55:52 +08:00

352 lines
11 KiB
JavaScript

/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */
MARIONETTE_TIMEOUT = 60000;
const DATA_KEY = "ril.data.enabled";
const DATA_ROAMING_KEY = "ril.data.roaming_enabled";
const APN_KEY = "ril.data.apnSettings";
SpecialPowers.setBoolPref("dom.mozSettings.enabled", true);
SpecialPowers.addPermission("mobileconnection", true, document);
SpecialPowers.addPermission("settings-read", true, document);
SpecialPowers.addPermission("settings-write", true, document);
let settings = window.navigator.mozSettings;
let connection = window.navigator.mozMobileConnections[0];
ok(connection instanceof MozMobileConnection,
"connection is instanceof " + connection.constructor);
let pendingEmulatorCmdCount = 0;
function sendCmdToEmulator(cmd, callback) {
++pendingEmulatorCmdCount;
runEmulatorCmd(cmd, function(result) {
--pendingEmulatorCmdCount;
is(result[0], "OK", "Emulator response");
if (callback) {
callback();
}
});
}
let tasks = {
// List of test fuctions. Each of them should call |tasks.next()| when
// completed or |tasks.finish()| to jump to the last one.
_tasks: [],
_nextTaskIndex: 0,
push: function(func) {
this._tasks.push(func);
},
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.finish();
}
}
},
finish: function() {
this._tasks[this._tasks.length - 1]();
},
run: function() {
this.next();
}
};
function setSetting(key, value, callback) {
let setLock = settings.createLock();
let obj = {};
obj[key] = value;
let setReq = setLock.set(obj);
setReq.addEventListener("success", function onSetSuccess() {
ok(true, "set '" + key + "' to " + obj[key]);
if (callback) {
callback();
}
});
setReq.addEventListener("error", function onSetError() {
ok(false, "cannot set '" + key + "'");
tasks.finish();
});
}
function getSetting(key, callback) {
let getLock = settings.createLock();
let getReq = getLock.get(key);
getReq.addEventListener("success", function onGetSuccess() {
ok(true, "get " + key + " setting okay");
let value = getReq.result[key];
callback(value);
});
getReq.addEventListener("error", function onGetError() {
ok(false, "cannot get '" + key + "'");
tasks.finish();
});
}
function setEmulatorAPN(callback) {
let apn =
[
[
{"carrier":"T-Mobile US",
"apn":"epc.tmobile.com",
"mmsc":"http://mms.msg.eng.t-mobile.com/mms/wapenc",
"types":["default","supl","mms"]}
]
];
setSetting(APN_KEY, apn, callback);
}
function setEmulatorRoaming(roaming, callback) {
log("Setting emulator roaming state: " + roaming + ".");
// Set voice registration state first and then data registration state.
let cmd = "gsm voice " + (roaming ? "roaming" : "home");
sendCmdToEmulator(cmd, function() {
connection.addEventListener("voicechange", function onvoicechange() {
connection.removeEventListener("voicechange", onvoicechange);
log("mobileConnection.voice.roaming is now '"
+ connection.voice.roaming + "'.");
is(connection.voice.roaming, roaming, "voice.roaming");
let cmd = "gsm data " + (roaming ? "roaming" : "home");
sendCmdToEmulator(cmd, function() {
connection.addEventListener("datachange", function ondatachange() {
connection.removeEventListener("datachange", ondatachange);
log("mobileConnection.data.roaming is now '"
+ connection.data.roaming + "'.");
is(connection.data.roaming, roaming, "data.roaming");
if (callback) {
callback();
}
});
});
});
});
}
function setEmulatorHome(callback) {
let voiceRegistration = false;
let dataRegistration = false;
if (connection.voice.state != "registered") {
sendCmdToEmulator("gsm voice home", function() {
connection.addEventListener("voicechange", function onvoicechange() {
connection.removeEventListener("voicechange", onvoicechange);
log("mobileConnection.voice.state is now '"
+ connection.voice.state + "'.");
is(connection.voice.state, "registered", "voice.state");
voiceRegistration = true;
});
});
} else {
voiceRegistration = true;
}
if (connection.data.state != "registered") {
sendCmdToEmulator("gsm data home", function() {
connection.addEventListener("datachange", function ondatachange() {
connection.removeEventListener("datachange", ondatachange);
log("mobileConnection.data.state is now '"
+ connection.data.state + "'.");
is(connection.data.state, "registered", "data.state");
dataRegistration = true;
});
});
} else {
dataRegistration = true;
}
waitFor(callback, function() {
return (voiceRegistration && dataRegistration);
});
}
tasks.push(function verifyInitialState() {
log("Verifying initial state.");
// Want to start test with mobileConnection.data.state 'registered',
// This is the default state; if it is not currently this value then set it.
setEmulatorHome(function() {
// Want to start test with data off,
// This is the default state; if it is not currently this value then set it.
getSetting(DATA_KEY, function(result) {
let value = result;
log("Starting data enabled: " + value);
if (value) {
setSetting(DATA_KEY, false);
connection.addEventListener("datachange", function ondatachange() {
connection.removeEventListener("datachange", ondatachange);
log("mobileConnection.data.connected is now '"
+ connection.data.connected + "'.");
is(connection.data.connected, false, "data.connected");
setEmulatorAPN(function() {
tasks.next();
});
});
} else {
setEmulatorAPN(function() {
tasks.next();
});
}
});
});
});
tasks.push(function testEnableData() {
log("Turn data on.");
connection.addEventListener("datachange", function ondatachange() {
connection.removeEventListener("datachange", ondatachange);
log("mobileConnection.data.connected is now '"
+ connection.data.connected + "'.");
is(connection.data.connected, true, "data.connected");
tasks.next();
});
setSetting(DATA_KEY, true);
});
tasks.push(function testUnregisterDataWhileDataEnabled() {
log("Set data registration unregistered while data enabled.");
// When data registration is unregistered, all data calls
// will be automatically deactivated.
sendCmdToEmulator("gsm data unregistered", function() {
connection.addEventListener("datachange", function ondatachange() {
log("mobileConnection.data.state is now '"
+ connection.data.state + "'.");
if (connection.data.state == "notSearching") {
connection.removeEventListener("datachange", ondatachange);
log("mobileConnection.data.connected is now '"
+ connection.data.connected + "'.");
is(connection.data.connected, false, "data.connected");
tasks.next();
}
});
});
});
tasks.push(function testRegisterDataWhileDataEnabled() {
log("Set data registration home while data enabled.");
// When data registration is registered, data call will be
// (re)activated by gecko if ril.data.enabled is set to true.
sendCmdToEmulator("gsm data home", function() {
connection.addEventListener("datachange", function ondatachange() {
connection.removeEventListener("datachange", ondatachange);
log("mobileConnection.data.state is now '"
+ connection.data.state + "'.");
is(connection.data.state, "registered", "data.state");
connection.addEventListener("datachange", function ondatachange() {
connection.removeEventListener("datachange", ondatachange);
log("mobileConnection.data.connected is now '"
+ connection.data.connected + "'.");
is(connection.data.connected, true, "data.connected");
tasks.next();
});
});
});
});
tasks.push(function testDisableDataRoamingWhileRoaming() {
log("Disable data roaming while roaming.");
setSetting(DATA_ROAMING_KEY, false);
// Wait for roaming state to change, then data connection should
// be disconnected due to data roaming set to off.
setEmulatorRoaming(true, function() {
connection.addEventListener("datachange", function ondatachange() {
connection.removeEventListener("datachange", ondatachange);
log("mobileConnection.data.connected is now '"
+ connection.data.connected + "'.");
is(connection.data.connected, false, "data.connected");
tasks.next();
});
});
});
tasks.push(function testEnableDataRoamingWhileRoaming() {
log("Enable data roaming while roaming.");
// Data should be re-connected as we enabled data roaming.
connection.addEventListener("datachange", function ondatachange() {
connection.removeEventListener("datachange", ondatachange);
log("mobileConnection.data.connected is now '"
+ connection.data.connected + "'.");
is(connection.data.connected, true, "data.connected");
tasks.next();
});
setSetting(DATA_ROAMING_KEY, true);
});
tasks.push(function testDisableDataRoamingWhileNotRoaming() {
log("Disable data roaming while not roaming.");
// Wait for roaming state to change then set data roaming back
// to off.
setEmulatorRoaming(false, function() {
setSetting(DATA_ROAMING_KEY, false);
// No change event will be received cause data connection state
// remains the same.
window.setTimeout(function() {
is(connection.data.connected, true, "data.connected");
tasks.next();
}, 1000);
});
});
tasks.push(function testDisableData() {
log("Turn data off.");
connection.addEventListener("datachange", function ondatachange() {
connection.removeEventListener("datachange", ondatachange);
log("mobileConnection.data.connected is now '"
+ connection.data.connected + "'.");
is(connection.data.connected, false, "data.connected");
tasks.next();
});
setSetting(DATA_KEY, false);
});
// WARNING: All tasks should be pushed before this!!!
tasks.push(function cleanUp() {
if (pendingEmulatorCmdCount) {
window.setTimeout(cleanUp, 100);
return;
}
SpecialPowers.removePermission("mobileconnection", document);
SpecialPowers.removePermission("settings-write", document);
SpecialPowers.removePermission("settings-read", document);
SpecialPowers.clearUserPref("dom.mozSettings.enabled");
finish();
});
tasks.run();