Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | * http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 3 | |
michael@0 | 4 | MARIONETTE_TIMEOUT = 60000; |
michael@0 | 5 | MARIONETTE_CONTEXT = "chrome"; |
michael@0 | 6 | |
michael@0 | 7 | Cu.import("resource://gre/modules/Promise.jsm"); |
michael@0 | 8 | |
michael@0 | 9 | const DATA_KEY = "ril.data.enabled"; |
michael@0 | 10 | const APN_KEY = "ril.data.apnSettings"; |
michael@0 | 11 | |
michael@0 | 12 | let ril = Cc["@mozilla.org/ril;1"].getService(Ci.nsIRadioInterfaceLayer); |
michael@0 | 13 | ok(ril, "ril.constructor is " + ril.constructor); |
michael@0 | 14 | |
michael@0 | 15 | let radioInterface = ril.getRadioInterface(0); |
michael@0 | 16 | ok(radioInterface, "radioInterface.constructor is " + radioInterface.constrctor); |
michael@0 | 17 | |
michael@0 | 18 | function setSetting(key, value) { |
michael@0 | 19 | log("setSetting: '" + key + "'' -> " + JSON.stringify(value)); |
michael@0 | 20 | |
michael@0 | 21 | let deferred = Promise.defer(); |
michael@0 | 22 | let obj = {}; |
michael@0 | 23 | obj[key] = value; |
michael@0 | 24 | |
michael@0 | 25 | let setRequest = window.navigator.mozSettings.createLock().set(obj); |
michael@0 | 26 | setRequest.addEventListener("success", function() { |
michael@0 | 27 | log("set '" + key + "' to " + JSON.stringify(value) + " success"); |
michael@0 | 28 | deferred.resolve(); |
michael@0 | 29 | }); |
michael@0 | 30 | setRequest.addEventListener("error", function() { |
michael@0 | 31 | ok(false, "cannot set '" + key + "' to " + JSON.stringify(value)); |
michael@0 | 32 | deferred.reject(); |
michael@0 | 33 | }); |
michael@0 | 34 | |
michael@0 | 35 | return deferred.promise; |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | function getSetting(key) { |
michael@0 | 39 | log("getSetting: '" + key + "'"); |
michael@0 | 40 | |
michael@0 | 41 | let deferred = Promise.defer(); |
michael@0 | 42 | |
michael@0 | 43 | let getRequest = window.navigator.mozSettings.createLock().get(key); |
michael@0 | 44 | getRequest.addEventListener("success", function() { |
michael@0 | 45 | let result = getRequest.result[key]; |
michael@0 | 46 | log("setting '" + key + "': " + JSON.stringify(result)); |
michael@0 | 47 | deferred.resolve(result); |
michael@0 | 48 | }); |
michael@0 | 49 | getRequest.addEventListener("error", function() { |
michael@0 | 50 | ok(false, "cannot get '" + key + "'"); |
michael@0 | 51 | deferred.reject(); |
michael@0 | 52 | }); |
michael@0 | 53 | |
michael@0 | 54 | return deferred.promise; |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | function setEmulatorAPN() { |
michael@0 | 58 | let apn = [ |
michael@0 | 59 | [{"carrier":"T-Mobile US", |
michael@0 | 60 | "apn":"epc.tmobile.com", |
michael@0 | 61 | "mmsc":"http://mms.msg.eng.t-mobile.com/mms/wapenc", |
michael@0 | 62 | "types":["default","supl","mms","ims","dun"]}] |
michael@0 | 63 | ]; |
michael@0 | 64 | |
michael@0 | 65 | return setSetting(APN_KEY, apn); |
michael@0 | 66 | } |
michael@0 | 67 | |
michael@0 | 68 | function waitNetworkConnected(networkType) { |
michael@0 | 69 | log("wait network " + networkType + " connected"); |
michael@0 | 70 | |
michael@0 | 71 | let interfaceStateChangeTopic = "network-connection-state-changed"; |
michael@0 | 72 | let obs = Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService); |
michael@0 | 73 | let deferred = Promise.defer(); |
michael@0 | 74 | |
michael@0 | 75 | function observer(subject, topic, data) { |
michael@0 | 76 | let network = subject.QueryInterface(Ci.nsINetworkInterface); |
michael@0 | 77 | log("Network " + network.type + " state changes to " + network.state); |
michael@0 | 78 | if (network.type == networkType && |
michael@0 | 79 | network.state == Ci.nsINetworkInterface.NETWORK_STATE_CONNECTED) { |
michael@0 | 80 | obs.removeObserver(observer, interfaceStateChangeTopic); |
michael@0 | 81 | deferred.resolve(); |
michael@0 | 82 | } |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | obs.addObserver(observer, interfaceStateChangeTopic, false); |
michael@0 | 86 | |
michael@0 | 87 | return deferred.promise; |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | function waitNetworkDisconnected(networkType) { |
michael@0 | 91 | log("wait network " + networkType + " disconnected"); |
michael@0 | 92 | |
michael@0 | 93 | let interfaceStateChangeTopic = "network-connection-state-changed"; |
michael@0 | 94 | let obs = Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService); |
michael@0 | 95 | let deferred = Promise.defer(); |
michael@0 | 96 | |
michael@0 | 97 | function observer(subject, topic, data) { |
michael@0 | 98 | let network = subject.QueryInterface(Ci.nsINetworkInterface); |
michael@0 | 99 | log("Network " + network.type + " state changes to " + network.state); |
michael@0 | 100 | // We can not check network.type here cause network.type would return |
michael@0 | 101 | // NETWORK_TYPE_MOBILE_SUPL (NETWORK_TYPE_MOBILE_OTHERS) when disconnecting |
michael@0 | 102 | // and disconnected, see bug 939046. |
michael@0 | 103 | if (network.state == Ci.nsINetworkInterface.NETWORK_STATE_DISCONNECTED || |
michael@0 | 104 | network.state == Ci.nsINetworkInterface.NETWORK_STATE_UNKNOWN) { |
michael@0 | 105 | obs.removeObserver(observer, interfaceStateChangeTopic); |
michael@0 | 106 | deferred.resolve(); |
michael@0 | 107 | } |
michael@0 | 108 | } |
michael@0 | 109 | |
michael@0 | 110 | obs.addObserver(observer, interfaceStateChangeTopic, false); |
michael@0 | 111 | |
michael@0 | 112 | return deferred.promise; |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | // Test initial State |
michael@0 | 116 | function testInitialState() { |
michael@0 | 117 | log("= testInitialState ="); |
michael@0 | 118 | |
michael@0 | 119 | // Data should be off before starting any test. |
michael@0 | 120 | return Promise.resolve() |
michael@0 | 121 | .then(() => getSetting(DATA_KEY)) |
michael@0 | 122 | .then(value => { |
michael@0 | 123 | is(value, false, "Data must be off"); |
michael@0 | 124 | }) |
michael@0 | 125 | .then(null, () => { |
michael@0 | 126 | ok(false, "promise rejected during test"); |
michael@0 | 127 | }) |
michael@0 | 128 | .then(runNextTest); |
michael@0 | 129 | } |
michael@0 | 130 | |
michael@0 | 131 | // Test default data Connection |
michael@0 | 132 | function testDefaultDataConnection() { |
michael@0 | 133 | log("= testDefaultDataConnection ="); |
michael@0 | 134 | |
michael@0 | 135 | return Promise.resolve() |
michael@0 | 136 | // Enable data |
michael@0 | 137 | .then(() => setSetting(DATA_KEY, true)) |
michael@0 | 138 | .then(() => waitNetworkConnected(Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE)) |
michael@0 | 139 | // Disable data |
michael@0 | 140 | .then(() => setSetting(DATA_KEY, false)) |
michael@0 | 141 | .then(() => waitNetworkDisconnected(Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE)) |
michael@0 | 142 | .then(null, () => { |
michael@0 | 143 | ok(false, "promise rejected during test"); |
michael@0 | 144 | }) |
michael@0 | 145 | .then(runNextTest); |
michael@0 | 146 | } |
michael@0 | 147 | |
michael@0 | 148 | // Test non default data connection |
michael@0 | 149 | function testNonDefaultDataConnection() { |
michael@0 | 150 | log("= testNonDefaultDataConnection ="); |
michael@0 | 151 | |
michael@0 | 152 | function doTestNonDefaultDataConnection(type) { |
michael@0 | 153 | log("doTestNonDefaultDataConnection: " + type); |
michael@0 | 154 | |
michael@0 | 155 | let typeMapping = { |
michael@0 | 156 | "mms": Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE_MMS, |
michael@0 | 157 | "supl": Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE_SUPL, |
michael@0 | 158 | "ims": Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE_IMS, |
michael@0 | 159 | "dun": Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE_DUN |
michael@0 | 160 | }; |
michael@0 | 161 | let networkType = typeMapping[type]; |
michael@0 | 162 | |
michael@0 | 163 | return Promise.resolve() |
michael@0 | 164 | .then(() => radioInterface.setupDataCallByType(type)) |
michael@0 | 165 | .then(() => waitNetworkConnected(networkType)) |
michael@0 | 166 | .then(() => radioInterface.deactivateDataCallByType(type)) |
michael@0 | 167 | .then(() => waitNetworkDisconnected(networkType)); |
michael@0 | 168 | } |
michael@0 | 169 | |
michael@0 | 170 | let currentApn; |
michael@0 | 171 | return Promise.resolve() |
michael@0 | 172 | .then(() => getSetting(APN_KEY)) |
michael@0 | 173 | .then(value => { |
michael@0 | 174 | currentApn = value; |
michael@0 | 175 | }) |
michael@0 | 176 | .then(setEmulatorAPN) |
michael@0 | 177 | .then(() => doTestNonDefaultDataConnection("mms")) |
michael@0 | 178 | .then(() => doTestNonDefaultDataConnection("supl")) |
michael@0 | 179 | .then(() => doTestNonDefaultDataConnection("ims")) |
michael@0 | 180 | .then(() => doTestNonDefaultDataConnection("dun")) |
michael@0 | 181 | // Restore APN settings |
michael@0 | 182 | .then(() => setSetting(APN_KEY, currentApn)) |
michael@0 | 183 | .then(null, () => { |
michael@0 | 184 | ok(false, "promise rejected during test"); |
michael@0 | 185 | }) |
michael@0 | 186 | .then(runNextTest); |
michael@0 | 187 | } |
michael@0 | 188 | |
michael@0 | 189 | let tests = [ |
michael@0 | 190 | testInitialState, |
michael@0 | 191 | testDefaultDataConnection, |
michael@0 | 192 | testNonDefaultDataConnection |
michael@0 | 193 | ]; |
michael@0 | 194 | |
michael@0 | 195 | function runNextTest() { |
michael@0 | 196 | let test = tests.shift(); |
michael@0 | 197 | if (!test) { |
michael@0 | 198 | finish(); |
michael@0 | 199 | return; |
michael@0 | 200 | } |
michael@0 | 201 | |
michael@0 | 202 | test(); |
michael@0 | 203 | } |
michael@0 | 204 | |
michael@0 | 205 | // Start Tests |
michael@0 | 206 | runNextTest(); |