1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/mobileconnection/tests/marionette/test_mobile_networks.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,214 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +// getNetworks() can take some time.. 1.8 +MARIONETTE_TIMEOUT = 60000; 1.9 +MARIONETTE_HEAD_JS = "head.js"; 1.10 + 1.11 +function isHomeNetwork(network) { 1.12 + is(network.longName, "Android"); 1.13 + is(network.shortName, "Android"); 1.14 + is(network.mcc, "310"); 1.15 + is(network.mnc, "260"); 1.16 +} 1.17 + 1.18 +function isRoamingNetwork(network) { 1.19 + is(network.longName, "TelKila"); 1.20 + is(network.shortName, "TelKila"); 1.21 + is(network.mcc, "310"); 1.22 + is(network.mnc, "295"); 1.23 +} 1.24 + 1.25 +function testConnectionInfo() { 1.26 + log("Validate initial states"); 1.27 + let voice = mobileConnection.voice; 1.28 + is(voice.connected, true); 1.29 + is(voice.state, "registered"); 1.30 + is(voice.emergencyCallsOnly, false); 1.31 + is(voice.roaming, false); 1.32 + isHomeNetwork(voice.network); 1.33 + 1.34 + let data = mobileConnection.data; 1.35 + // data.connected = true means there's an active data call which we 1.36 + // can't predict here. 1.37 + is(data.state, "registered"); 1.38 + is(data.emergencyCallsOnly, false); 1.39 + is(data.roaming, false); 1.40 + isHomeNetwork(data.network); 1.41 +} 1.42 + 1.43 +function testGetNetworks() { 1.44 + log("Enumerating available networks"); 1.45 + return getNetworks() 1.46 + .then(function resolve(aNetworks) { 1.47 + // The emulator RIL server should always return 2 networks: 1.48 + // {"longName":"Android","shortName":"Android","mcc":310,"mnc":260,"state":"available"} 1.49 + // {"longName":"TelKila","shortName":"TelKila","mcc":310,"mnc":295,"state":"available"} 1.50 + is(aNetworks.length, 2); 1.51 + 1.52 + let network1 = aNetworks[0]; 1.53 + isHomeNetwork(network1); 1.54 + is(network1.state, "available"); 1.55 + 1.56 + let network2 = aNetworks[1]; 1.57 + isRoamingNetwork(network2); 1.58 + is(network2.state, "available"); 1.59 + 1.60 + return aNetworks; 1.61 + }); 1.62 +} 1.63 + 1.64 +function testSelectNetwork(aNetwork, aValidator) { 1.65 + log("Selecting network '" + aNetwork.longName + "' manually"); 1.66 + isnot(aNetwork.longName, mobileConnection.voice.network.longName, 1.67 + "aNetwork.longName"); 1.68 + 1.69 + return selectNetworkAndWait(aNetwork) 1.70 + .then(function() { 1.71 + is(mobileConnection.networkSelectionMode, "manual", 1.72 + "mobileConnection.networkSelectionMode"); 1.73 + is(mobileConnection.voice.network.longName, aNetwork.longName, 1.74 + "mobileConnection.voice.network.longName"); 1.75 + 1.76 + aValidator(mobileConnection.voice.network); 1.77 + }); 1.78 +} 1.79 + 1.80 +function testSelectNetworkAutomatically(aHomeNetwork, aValidator) { 1.81 + log("Selecting network '" + aHomeNetwork.longName + "' automatically"); 1.82 + isnot(aHomeNetwork.longName, mobileConnection.voice.network.longName, 1.83 + "aHomeNetwork.longName"); 1.84 + 1.85 + return selectNetworkAutomaticallyAndWait() 1.86 + .then(function() { 1.87 + is(mobileConnection.networkSelectionMode, "automatic", 1.88 + "mobileConnection.networkSelectionMode"); 1.89 + is(mobileConnection.voice.network.longName, aHomeNetwork.longName, 1.90 + "mobileConnection.voice.network.longName"); 1.91 + 1.92 + aValidator(mobileConnection.voice.network); 1.93 + }); 1.94 +} 1.95 + 1.96 +function throwsException(fn) { 1.97 + try { 1.98 + fn(); 1.99 + ok(false, "function did not throw an exception: " + fn); 1.100 + } catch (e) { 1.101 + ok(true, "function successfully caught exception: " + e); 1.102 + } 1.103 +} 1.104 + 1.105 +function testSelectNetworkErrors(aNetworkToSelect, aAnotherNetwork) { 1.106 + throwsException(() => mobileConnection.selectNetwork(null)); 1.107 + throwsException(() => mobileConnection.selectNetwork({})); 1.108 + 1.109 + isnot(aNetworkToSelect.longName, mobileConnection.voice.network.longName, 1.110 + "aNetworkToSelect.longName"); 1.111 + let promise = selectNetworkAndWait(aNetworkToSelect); 1.112 + 1.113 + // attempt to selectNetwork while one request has already been sent 1.114 + throwsException(() => mobileConnection.selectNetwork(aAnotherNetwork)); 1.115 + 1.116 + return promise; 1.117 +} 1.118 + 1.119 +function testSelectExistingNetworkManual(aNetwork) { 1.120 + // When the current network is selected again, the DOMRequest's onsuccess 1.121 + // should be called, but the network shouldn't actually change 1.122 + 1.123 + log("Selecting '" + aNetwork.longName + "' manually (should already be selected)"); 1.124 + is(aNetwork.longName, mobileConnection.voice.network.longName, 1.125 + "aNetwork.longName"); 1.126 + 1.127 + function voiceChange() { 1.128 + let network = mobileConnection.voice.network; 1.129 + if (network.longName !== aNetwork.longName) { 1.130 + ok(false, "voicechange event emitted while selecting existing '" + 1.131 + aNetwork.longName + "' manually"); 1.132 + } 1.133 + } 1.134 + 1.135 + mobileConnection.addEventListener("voicechange", voiceChange); 1.136 + 1.137 + return selectNetwork(aNetwork) 1.138 + .then(function resolve() { 1.139 + let deferred = Promise.defer(); 1.140 + 1.141 + // Give the voicechange event another opportunity to fire 1.142 + setTimeout(function() { 1.143 + mobileConnection.removeEventListener("voicechange", voiceChange); 1.144 + deferred.resolve(); 1.145 + }, 3000); 1.146 + 1.147 + return deferred.promise; 1.148 + }, function reject() { 1.149 + mobileConnection.removeEventListener("voicechange", voiceChange); 1.150 + ok(false, "selectNetwork fails"); 1.151 + }); 1.152 +} 1.153 + 1.154 +function testSelectExistingNetworkAuto(aHomeNetwork) { 1.155 + // Now try the same thing but using automatic selection 1.156 + log("Selecting '" + aHomeNetwork.longName + "' automatically (should already be selected)"); 1.157 + is(aHomeNetwork.longName, mobileConnection.voice.network.longName, 1.158 + "aHomeNetwork.longName"); 1.159 + 1.160 + function voiceChange() { 1.161 + let network = mobileConnection.voice.network; 1.162 + if (network.longName !== aHomeNetwork.longName) { 1.163 + ok(false, "voicechange event emitted while selecting existing '" + 1.164 + aHomeNetwork.longName + "' automatically"); 1.165 + } 1.166 + } 1.167 + 1.168 + mobileConnection.addEventListener("voicechange", voiceChange); 1.169 + 1.170 + return selectNetworkAutomatically() 1.171 + .then(function resolve() { 1.172 + let deferred = Promise.defer(); 1.173 + 1.174 + // Give the voicechange event another opportunity to fire 1.175 + setTimeout(function() { 1.176 + mobileConnection.removeEventListener("voicechange", voiceChange); 1.177 + deferred.resolve(); 1.178 + }, 3000); 1.179 + 1.180 + return deferred.promise; 1.181 + }, function reject() { 1.182 + mobileConnection.removeEventListener("voicechange", voiceChange); 1.183 + ok(false, "selectNetwork fails"); 1.184 + }); 1.185 +} 1.186 + 1.187 +startTestCommon(function() { 1.188 + let promise = Promise.resolve(); 1.189 + if (mobileConnection.networkSelectionMode != "automatic") { 1.190 + promise = promise.then(selectNetworkAutomatically); 1.191 + } 1.192 + 1.193 + return promise 1.194 + .then(() => testConnectionInfo()) 1.195 + .then(() => testGetNetworks()) 1.196 + .then(function(aNetworks) { 1.197 + let homeNetwork = aNetworks[0], 1.198 + roamingNetwork = aNetworks[1]; 1.199 + 1.200 + // We're initially connected to home network, so let's connect to roaming 1.201 + // network first. 1.202 + return testSelectNetwork(roamingNetwork, isRoamingNetwork) 1.203 + 1.204 + // Then connect back to home network automatically. 1.205 + .then(() => testSelectNetworkAutomatically(homeNetwork, isHomeNetwork)) 1.206 + 1.207 + // Then try connect to roaming network again. 1.208 + .then(() => testSelectNetworkErrors(roamingNetwork, homeNetwork)) 1.209 + 1.210 + // Roaming network should has been selected, try select it again. 1.211 + .then(() => testSelectExistingNetworkManual(roamingNetwork)) 1.212 + 1.213 + // Switch back to home network and try selecte it again. 1.214 + .then(() => selectNetworkAutomaticallyAndWait()) 1.215 + .then(() => testSelectExistingNetworkAuto(homeNetwork)); 1.216 + }); 1.217 +});