michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: // getNetworks() can take some time.. michael@0: MARIONETTE_TIMEOUT = 60000; michael@0: MARIONETTE_HEAD_JS = "head.js"; michael@0: michael@0: function isHomeNetwork(network) { michael@0: is(network.longName, "Android"); michael@0: is(network.shortName, "Android"); michael@0: is(network.mcc, "310"); michael@0: is(network.mnc, "260"); michael@0: } michael@0: michael@0: function isRoamingNetwork(network) { michael@0: is(network.longName, "TelKila"); michael@0: is(network.shortName, "TelKila"); michael@0: is(network.mcc, "310"); michael@0: is(network.mnc, "295"); michael@0: } michael@0: michael@0: function testConnectionInfo() { michael@0: log("Validate initial states"); michael@0: let voice = mobileConnection.voice; michael@0: is(voice.connected, true); michael@0: is(voice.state, "registered"); michael@0: is(voice.emergencyCallsOnly, false); michael@0: is(voice.roaming, false); michael@0: isHomeNetwork(voice.network); michael@0: michael@0: let data = mobileConnection.data; michael@0: // data.connected = true means there's an active data call which we michael@0: // can't predict here. michael@0: is(data.state, "registered"); michael@0: is(data.emergencyCallsOnly, false); michael@0: is(data.roaming, false); michael@0: isHomeNetwork(data.network); michael@0: } michael@0: michael@0: function testGetNetworks() { michael@0: log("Enumerating available networks"); michael@0: return getNetworks() michael@0: .then(function resolve(aNetworks) { michael@0: // The emulator RIL server should always return 2 networks: michael@0: // {"longName":"Android","shortName":"Android","mcc":310,"mnc":260,"state":"available"} michael@0: // {"longName":"TelKila","shortName":"TelKila","mcc":310,"mnc":295,"state":"available"} michael@0: is(aNetworks.length, 2); michael@0: michael@0: let network1 = aNetworks[0]; michael@0: isHomeNetwork(network1); michael@0: is(network1.state, "available"); michael@0: michael@0: let network2 = aNetworks[1]; michael@0: isRoamingNetwork(network2); michael@0: is(network2.state, "available"); michael@0: michael@0: return aNetworks; michael@0: }); michael@0: } michael@0: michael@0: function testSelectNetwork(aNetwork, aValidator) { michael@0: log("Selecting network '" + aNetwork.longName + "' manually"); michael@0: isnot(aNetwork.longName, mobileConnection.voice.network.longName, michael@0: "aNetwork.longName"); michael@0: michael@0: return selectNetworkAndWait(aNetwork) michael@0: .then(function() { michael@0: is(mobileConnection.networkSelectionMode, "manual", michael@0: "mobileConnection.networkSelectionMode"); michael@0: is(mobileConnection.voice.network.longName, aNetwork.longName, michael@0: "mobileConnection.voice.network.longName"); michael@0: michael@0: aValidator(mobileConnection.voice.network); michael@0: }); michael@0: } michael@0: michael@0: function testSelectNetworkAutomatically(aHomeNetwork, aValidator) { michael@0: log("Selecting network '" + aHomeNetwork.longName + "' automatically"); michael@0: isnot(aHomeNetwork.longName, mobileConnection.voice.network.longName, michael@0: "aHomeNetwork.longName"); michael@0: michael@0: return selectNetworkAutomaticallyAndWait() michael@0: .then(function() { michael@0: is(mobileConnection.networkSelectionMode, "automatic", michael@0: "mobileConnection.networkSelectionMode"); michael@0: is(mobileConnection.voice.network.longName, aHomeNetwork.longName, michael@0: "mobileConnection.voice.network.longName"); michael@0: michael@0: aValidator(mobileConnection.voice.network); michael@0: }); michael@0: } michael@0: michael@0: function throwsException(fn) { michael@0: try { michael@0: fn(); michael@0: ok(false, "function did not throw an exception: " + fn); michael@0: } catch (e) { michael@0: ok(true, "function successfully caught exception: " + e); michael@0: } michael@0: } michael@0: michael@0: function testSelectNetworkErrors(aNetworkToSelect, aAnotherNetwork) { michael@0: throwsException(() => mobileConnection.selectNetwork(null)); michael@0: throwsException(() => mobileConnection.selectNetwork({})); michael@0: michael@0: isnot(aNetworkToSelect.longName, mobileConnection.voice.network.longName, michael@0: "aNetworkToSelect.longName"); michael@0: let promise = selectNetworkAndWait(aNetworkToSelect); michael@0: michael@0: // attempt to selectNetwork while one request has already been sent michael@0: throwsException(() => mobileConnection.selectNetwork(aAnotherNetwork)); michael@0: michael@0: return promise; michael@0: } michael@0: michael@0: function testSelectExistingNetworkManual(aNetwork) { michael@0: // When the current network is selected again, the DOMRequest's onsuccess michael@0: // should be called, but the network shouldn't actually change michael@0: michael@0: log("Selecting '" + aNetwork.longName + "' manually (should already be selected)"); michael@0: is(aNetwork.longName, mobileConnection.voice.network.longName, michael@0: "aNetwork.longName"); michael@0: michael@0: function voiceChange() { michael@0: let network = mobileConnection.voice.network; michael@0: if (network.longName !== aNetwork.longName) { michael@0: ok(false, "voicechange event emitted while selecting existing '" + michael@0: aNetwork.longName + "' manually"); michael@0: } michael@0: } michael@0: michael@0: mobileConnection.addEventListener("voicechange", voiceChange); michael@0: michael@0: return selectNetwork(aNetwork) michael@0: .then(function resolve() { michael@0: let deferred = Promise.defer(); michael@0: michael@0: // Give the voicechange event another opportunity to fire michael@0: setTimeout(function() { michael@0: mobileConnection.removeEventListener("voicechange", voiceChange); michael@0: deferred.resolve(); michael@0: }, 3000); michael@0: michael@0: return deferred.promise; michael@0: }, function reject() { michael@0: mobileConnection.removeEventListener("voicechange", voiceChange); michael@0: ok(false, "selectNetwork fails"); michael@0: }); michael@0: } michael@0: michael@0: function testSelectExistingNetworkAuto(aHomeNetwork) { michael@0: // Now try the same thing but using automatic selection michael@0: log("Selecting '" + aHomeNetwork.longName + "' automatically (should already be selected)"); michael@0: is(aHomeNetwork.longName, mobileConnection.voice.network.longName, michael@0: "aHomeNetwork.longName"); michael@0: michael@0: function voiceChange() { michael@0: let network = mobileConnection.voice.network; michael@0: if (network.longName !== aHomeNetwork.longName) { michael@0: ok(false, "voicechange event emitted while selecting existing '" + michael@0: aHomeNetwork.longName + "' automatically"); michael@0: } michael@0: } michael@0: michael@0: mobileConnection.addEventListener("voicechange", voiceChange); michael@0: michael@0: return selectNetworkAutomatically() michael@0: .then(function resolve() { michael@0: let deferred = Promise.defer(); michael@0: michael@0: // Give the voicechange event another opportunity to fire michael@0: setTimeout(function() { michael@0: mobileConnection.removeEventListener("voicechange", voiceChange); michael@0: deferred.resolve(); michael@0: }, 3000); michael@0: michael@0: return deferred.promise; michael@0: }, function reject() { michael@0: mobileConnection.removeEventListener("voicechange", voiceChange); michael@0: ok(false, "selectNetwork fails"); michael@0: }); michael@0: } michael@0: michael@0: startTestCommon(function() { michael@0: let promise = Promise.resolve(); michael@0: if (mobileConnection.networkSelectionMode != "automatic") { michael@0: promise = promise.then(selectNetworkAutomatically); michael@0: } michael@0: michael@0: return promise michael@0: .then(() => testConnectionInfo()) michael@0: .then(() => testGetNetworks()) michael@0: .then(function(aNetworks) { michael@0: let homeNetwork = aNetworks[0], michael@0: roamingNetwork = aNetworks[1]; michael@0: michael@0: // We're initially connected to home network, so let's connect to roaming michael@0: // network first. michael@0: return testSelectNetwork(roamingNetwork, isRoamingNetwork) michael@0: michael@0: // Then connect back to home network automatically. michael@0: .then(() => testSelectNetworkAutomatically(homeNetwork, isHomeNetwork)) michael@0: michael@0: // Then try connect to roaming network again. michael@0: .then(() => testSelectNetworkErrors(roamingNetwork, homeNetwork)) michael@0: michael@0: // Roaming network should has been selected, try select it again. michael@0: .then(() => testSelectExistingNetworkManual(roamingNetwork)) michael@0: michael@0: // Switch back to home network and try selecte it again. michael@0: .then(() => selectNetworkAutomaticallyAndWait()) michael@0: .then(() => testSelectExistingNetworkAuto(homeNetwork)); michael@0: }); michael@0: });