1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/mobileconnection/tests/marionette/test_mobile_data_connection.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,351 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + * http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +MARIONETTE_TIMEOUT = 60000; 1.8 + 1.9 +const DATA_KEY = "ril.data.enabled"; 1.10 +const DATA_ROAMING_KEY = "ril.data.roaming_enabled"; 1.11 +const APN_KEY = "ril.data.apnSettings"; 1.12 + 1.13 +SpecialPowers.setBoolPref("dom.mozSettings.enabled", true); 1.14 +SpecialPowers.addPermission("mobileconnection", true, document); 1.15 +SpecialPowers.addPermission("settings-read", true, document); 1.16 +SpecialPowers.addPermission("settings-write", true, document); 1.17 + 1.18 +let settings = window.navigator.mozSettings; 1.19 +let connection = window.navigator.mozMobileConnections[0]; 1.20 +ok(connection instanceof MozMobileConnection, 1.21 + "connection is instanceof " + connection.constructor); 1.22 + 1.23 + 1.24 +let pendingEmulatorCmdCount = 0; 1.25 +function sendCmdToEmulator(cmd, callback) { 1.26 + ++pendingEmulatorCmdCount; 1.27 + 1.28 + runEmulatorCmd(cmd, function(result) { 1.29 + --pendingEmulatorCmdCount; 1.30 + 1.31 + is(result[0], "OK", "Emulator response"); 1.32 + 1.33 + if (callback) { 1.34 + callback(); 1.35 + } 1.36 + }); 1.37 +} 1.38 + 1.39 +let tasks = { 1.40 + // List of test fuctions. Each of them should call |tasks.next()| when 1.41 + // completed or |tasks.finish()| to jump to the last one. 1.42 + _tasks: [], 1.43 + _nextTaskIndex: 0, 1.44 + 1.45 + push: function(func) { 1.46 + this._tasks.push(func); 1.47 + }, 1.48 + 1.49 + next: function() { 1.50 + let index = this._nextTaskIndex++; 1.51 + let task = this._tasks[index]; 1.52 + try { 1.53 + task(); 1.54 + } catch (ex) { 1.55 + ok(false, "test task[" + index + "] throws: " + ex); 1.56 + // Run last task as clean up if possible. 1.57 + if (index != this._tasks.length - 1) { 1.58 + this.finish(); 1.59 + } 1.60 + } 1.61 + }, 1.62 + 1.63 + finish: function() { 1.64 + this._tasks[this._tasks.length - 1](); 1.65 + }, 1.66 + 1.67 + run: function() { 1.68 + this.next(); 1.69 + } 1.70 +}; 1.71 + 1.72 +function setSetting(key, value, callback) { 1.73 + let setLock = settings.createLock(); 1.74 + let obj = {}; 1.75 + obj[key] = value; 1.76 + 1.77 + let setReq = setLock.set(obj); 1.78 + setReq.addEventListener("success", function onSetSuccess() { 1.79 + ok(true, "set '" + key + "' to " + obj[key]); 1.80 + if (callback) { 1.81 + callback(); 1.82 + } 1.83 + }); 1.84 + setReq.addEventListener("error", function onSetError() { 1.85 + ok(false, "cannot set '" + key + "'"); 1.86 + tasks.finish(); 1.87 + }); 1.88 +} 1.89 + 1.90 +function getSetting(key, callback) { 1.91 + let getLock = settings.createLock(); 1.92 + 1.93 + let getReq = getLock.get(key); 1.94 + getReq.addEventListener("success", function onGetSuccess() { 1.95 + ok(true, "get " + key + " setting okay"); 1.96 + let value = getReq.result[key]; 1.97 + callback(value); 1.98 + }); 1.99 + getReq.addEventListener("error", function onGetError() { 1.100 + ok(false, "cannot get '" + key + "'"); 1.101 + tasks.finish(); 1.102 + }); 1.103 +} 1.104 + 1.105 +function setEmulatorAPN(callback) { 1.106 + let apn = 1.107 + [ 1.108 + [ 1.109 + {"carrier":"T-Mobile US", 1.110 + "apn":"epc.tmobile.com", 1.111 + "mmsc":"http://mms.msg.eng.t-mobile.com/mms/wapenc", 1.112 + "types":["default","supl","mms"]} 1.113 + ] 1.114 + ]; 1.115 + setSetting(APN_KEY, apn, callback); 1.116 +} 1.117 + 1.118 +function setEmulatorRoaming(roaming, callback) { 1.119 + log("Setting emulator roaming state: " + roaming + "."); 1.120 + 1.121 + // Set voice registration state first and then data registration state. 1.122 + let cmd = "gsm voice " + (roaming ? "roaming" : "home"); 1.123 + sendCmdToEmulator(cmd, function() { 1.124 + 1.125 + connection.addEventListener("voicechange", function onvoicechange() { 1.126 + connection.removeEventListener("voicechange", onvoicechange); 1.127 + log("mobileConnection.voice.roaming is now '" 1.128 + + connection.voice.roaming + "'."); 1.129 + is(connection.voice.roaming, roaming, "voice.roaming"); 1.130 + 1.131 + let cmd = "gsm data " + (roaming ? "roaming" : "home"); 1.132 + sendCmdToEmulator(cmd, function() { 1.133 + 1.134 + connection.addEventListener("datachange", function ondatachange() { 1.135 + connection.removeEventListener("datachange", ondatachange); 1.136 + log("mobileConnection.data.roaming is now '" 1.137 + + connection.data.roaming + "'."); 1.138 + is(connection.data.roaming, roaming, "data.roaming"); 1.139 + if (callback) { 1.140 + callback(); 1.141 + } 1.142 + }); 1.143 + }); 1.144 + }); 1.145 + }); 1.146 +} 1.147 + 1.148 +function setEmulatorHome(callback) { 1.149 + let voiceRegistration = false; 1.150 + let dataRegistration = false; 1.151 + 1.152 + if (connection.voice.state != "registered") { 1.153 + sendCmdToEmulator("gsm voice home", function() { 1.154 + connection.addEventListener("voicechange", function onvoicechange() { 1.155 + connection.removeEventListener("voicechange", onvoicechange); 1.156 + log("mobileConnection.voice.state is now '" 1.157 + + connection.voice.state + "'."); 1.158 + is(connection.voice.state, "registered", "voice.state"); 1.159 + voiceRegistration = true; 1.160 + }); 1.161 + }); 1.162 + } else { 1.163 + voiceRegistration = true; 1.164 + } 1.165 + 1.166 + if (connection.data.state != "registered") { 1.167 + sendCmdToEmulator("gsm data home", function() { 1.168 + connection.addEventListener("datachange", function ondatachange() { 1.169 + connection.removeEventListener("datachange", ondatachange); 1.170 + log("mobileConnection.data.state is now '" 1.171 + + connection.data.state + "'."); 1.172 + is(connection.data.state, "registered", "data.state"); 1.173 + dataRegistration = true; 1.174 + }); 1.175 + }); 1.176 + } else { 1.177 + dataRegistration = true; 1.178 + } 1.179 + 1.180 + waitFor(callback, function() { 1.181 + return (voiceRegistration && dataRegistration); 1.182 + }); 1.183 +} 1.184 + 1.185 + 1.186 +tasks.push(function verifyInitialState() { 1.187 + log("Verifying initial state."); 1.188 + 1.189 + // Want to start test with mobileConnection.data.state 'registered', 1.190 + // This is the default state; if it is not currently this value then set it. 1.191 + setEmulatorHome(function() { 1.192 + // Want to start test with data off, 1.193 + // This is the default state; if it is not currently this value then set it. 1.194 + getSetting(DATA_KEY, function(result) { 1.195 + let value = result; 1.196 + log("Starting data enabled: " + value); 1.197 + if (value) { 1.198 + setSetting(DATA_KEY, false); 1.199 + 1.200 + connection.addEventListener("datachange", function ondatachange() { 1.201 + connection.removeEventListener("datachange", ondatachange); 1.202 + log("mobileConnection.data.connected is now '" 1.203 + + connection.data.connected + "'."); 1.204 + is(connection.data.connected, false, "data.connected"); 1.205 + setEmulatorAPN(function() { 1.206 + tasks.next(); 1.207 + }); 1.208 + }); 1.209 + } else { 1.210 + setEmulatorAPN(function() { 1.211 + tasks.next(); 1.212 + }); 1.213 + } 1.214 + }); 1.215 + }); 1.216 +}); 1.217 + 1.218 +tasks.push(function testEnableData() { 1.219 + log("Turn data on."); 1.220 + 1.221 + connection.addEventListener("datachange", function ondatachange() { 1.222 + connection.removeEventListener("datachange", ondatachange); 1.223 + log("mobileConnection.data.connected is now '" 1.224 + + connection.data.connected + "'."); 1.225 + is(connection.data.connected, true, "data.connected"); 1.226 + tasks.next(); 1.227 + }); 1.228 + 1.229 + setSetting(DATA_KEY, true); 1.230 +}); 1.231 + 1.232 +tasks.push(function testUnregisterDataWhileDataEnabled() { 1.233 + log("Set data registration unregistered while data enabled."); 1.234 + 1.235 + // When data registration is unregistered, all data calls 1.236 + // will be automatically deactivated. 1.237 + sendCmdToEmulator("gsm data unregistered", function() { 1.238 + connection.addEventListener("datachange", function ondatachange() { 1.239 + log("mobileConnection.data.state is now '" 1.240 + + connection.data.state + "'."); 1.241 + if (connection.data.state == "notSearching") { 1.242 + connection.removeEventListener("datachange", ondatachange); 1.243 + log("mobileConnection.data.connected is now '" 1.244 + + connection.data.connected + "'."); 1.245 + is(connection.data.connected, false, "data.connected"); 1.246 + tasks.next(); 1.247 + } 1.248 + }); 1.249 + }); 1.250 +}); 1.251 + 1.252 +tasks.push(function testRegisterDataWhileDataEnabled() { 1.253 + log("Set data registration home while data enabled."); 1.254 + 1.255 + // When data registration is registered, data call will be 1.256 + // (re)activated by gecko if ril.data.enabled is set to true. 1.257 + sendCmdToEmulator("gsm data home", function() { 1.258 + connection.addEventListener("datachange", function ondatachange() { 1.259 + connection.removeEventListener("datachange", ondatachange); 1.260 + log("mobileConnection.data.state is now '" 1.261 + + connection.data.state + "'."); 1.262 + is(connection.data.state, "registered", "data.state"); 1.263 + 1.264 + connection.addEventListener("datachange", function ondatachange() { 1.265 + connection.removeEventListener("datachange", ondatachange); 1.266 + log("mobileConnection.data.connected is now '" 1.267 + + connection.data.connected + "'."); 1.268 + is(connection.data.connected, true, "data.connected"); 1.269 + tasks.next(); 1.270 + }); 1.271 + }); 1.272 + }); 1.273 +}); 1.274 + 1.275 +tasks.push(function testDisableDataRoamingWhileRoaming() { 1.276 + log("Disable data roaming while roaming."); 1.277 + 1.278 + setSetting(DATA_ROAMING_KEY, false); 1.279 + 1.280 + // Wait for roaming state to change, then data connection should 1.281 + // be disconnected due to data roaming set to off. 1.282 + setEmulatorRoaming(true, function() { 1.283 + connection.addEventListener("datachange", function ondatachange() { 1.284 + connection.removeEventListener("datachange", ondatachange); 1.285 + log("mobileConnection.data.connected is now '" 1.286 + + connection.data.connected + "'."); 1.287 + is(connection.data.connected, false, "data.connected"); 1.288 + tasks.next(); 1.289 + }); 1.290 + }); 1.291 +}); 1.292 + 1.293 +tasks.push(function testEnableDataRoamingWhileRoaming() { 1.294 + log("Enable data roaming while roaming."); 1.295 + 1.296 + // Data should be re-connected as we enabled data roaming. 1.297 + connection.addEventListener("datachange", function ondatachange() { 1.298 + connection.removeEventListener("datachange", ondatachange); 1.299 + log("mobileConnection.data.connected is now '" 1.300 + + connection.data.connected + "'."); 1.301 + is(connection.data.connected, true, "data.connected"); 1.302 + tasks.next(); 1.303 + }); 1.304 + 1.305 + setSetting(DATA_ROAMING_KEY, true); 1.306 +}); 1.307 + 1.308 +tasks.push(function testDisableDataRoamingWhileNotRoaming() { 1.309 + log("Disable data roaming while not roaming."); 1.310 + 1.311 + // Wait for roaming state to change then set data roaming back 1.312 + // to off. 1.313 + setEmulatorRoaming(false, function() { 1.314 + setSetting(DATA_ROAMING_KEY, false); 1.315 + 1.316 + // No change event will be received cause data connection state 1.317 + // remains the same. 1.318 + window.setTimeout(function() { 1.319 + is(connection.data.connected, true, "data.connected"); 1.320 + tasks.next(); 1.321 + }, 1000); 1.322 + }); 1.323 +}); 1.324 + 1.325 +tasks.push(function testDisableData() { 1.326 + log("Turn data off."); 1.327 + 1.328 + connection.addEventListener("datachange", function ondatachange() { 1.329 + connection.removeEventListener("datachange", ondatachange); 1.330 + log("mobileConnection.data.connected is now '" 1.331 + + connection.data.connected + "'."); 1.332 + is(connection.data.connected, false, "data.connected"); 1.333 + tasks.next(); 1.334 + }); 1.335 + 1.336 + setSetting(DATA_KEY, false); 1.337 +}); 1.338 + 1.339 +// WARNING: All tasks should be pushed before this!!! 1.340 +tasks.push(function cleanUp() { 1.341 + if (pendingEmulatorCmdCount) { 1.342 + window.setTimeout(cleanUp, 100); 1.343 + return; 1.344 + } 1.345 + 1.346 + SpecialPowers.removePermission("mobileconnection", document); 1.347 + SpecialPowers.removePermission("settings-write", document); 1.348 + SpecialPowers.removePermission("settings-read", document); 1.349 + SpecialPowers.clearUserPref("dom.mozSettings.enabled"); 1.350 + finish(); 1.351 +}); 1.352 + 1.353 +tasks.run(); 1.354 +