Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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 | const {Cc: Cc, Ci: Ci, Cr: Cr, Cu: Cu} = SpecialPowers; |
michael@0 | 5 | |
michael@0 | 6 | const SETTINGS_KEY_DATA_ENABLED = "ril.data.enabled"; |
michael@0 | 7 | const SETTINGS_KEY_DATA_ROAMING_ENABLED = "ril.data.roaming_enabled"; |
michael@0 | 8 | const SETTINGS_KEY_DATA_APN_SETTINGS = "ril.data.apnSettings"; |
michael@0 | 9 | |
michael@0 | 10 | let Promise = Cu.import("resource://gre/modules/Promise.jsm").Promise; |
michael@0 | 11 | |
michael@0 | 12 | let _pendingEmulatorCmdCount = 0; |
michael@0 | 13 | |
michael@0 | 14 | /** |
michael@0 | 15 | * Send emulator command with safe guard. |
michael@0 | 16 | * |
michael@0 | 17 | * We should only call |finish()| after all emulator command transactions |
michael@0 | 18 | * end, so here comes with the pending counter. Resolve when the emulator |
michael@0 | 19 | * gives positive response, and reject otherwise. |
michael@0 | 20 | * |
michael@0 | 21 | * Fulfill params: |
michael@0 | 22 | * result -- an array of emulator response lines. |
michael@0 | 23 | * Reject params: |
michael@0 | 24 | * result -- an array of emulator response lines. |
michael@0 | 25 | * |
michael@0 | 26 | * @param aCommand |
michael@0 | 27 | * A string command to be passed to emulator through its telnet console. |
michael@0 | 28 | * |
michael@0 | 29 | * @return A deferred promise. |
michael@0 | 30 | */ |
michael@0 | 31 | function runEmulatorCmdSafe(aCommand) { |
michael@0 | 32 | let deferred = Promise.defer(); |
michael@0 | 33 | |
michael@0 | 34 | ++_pendingEmulatorCmdCount; |
michael@0 | 35 | runEmulatorCmd(aCommand, function(aResult) { |
michael@0 | 36 | --_pendingEmulatorCmdCount; |
michael@0 | 37 | |
michael@0 | 38 | ok(true, "Emulator response: " + JSON.stringify(aResult)); |
michael@0 | 39 | if (Array.isArray(aResult) && |
michael@0 | 40 | aResult[aResult.length - 1] === "OK") { |
michael@0 | 41 | deferred.resolve(aResult); |
michael@0 | 42 | } else { |
michael@0 | 43 | deferred.reject(aResult); |
michael@0 | 44 | } |
michael@0 | 45 | }); |
michael@0 | 46 | |
michael@0 | 47 | return deferred.promise; |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | /** |
michael@0 | 51 | * Wrap DOMRequest onsuccess/onerror events to Promise resolve/reject. |
michael@0 | 52 | * |
michael@0 | 53 | * Fulfill params: A DOMEvent. |
michael@0 | 54 | * Reject params: A DOMEvent. |
michael@0 | 55 | * |
michael@0 | 56 | * @param aRequest |
michael@0 | 57 | * A DOMRequest instance. |
michael@0 | 58 | * |
michael@0 | 59 | * @return A deferred promise. |
michael@0 | 60 | */ |
michael@0 | 61 | function wrapDomRequestAsPromise(aRequest) { |
michael@0 | 62 | let deferred = Promise.defer(); |
michael@0 | 63 | |
michael@0 | 64 | ok(aRequest instanceof DOMRequest, |
michael@0 | 65 | "aRequest is instanceof " + aRequest.constructor); |
michael@0 | 66 | |
michael@0 | 67 | aRequest.addEventListener("success", function(aEvent) { |
michael@0 | 68 | deferred.resolve(aEvent); |
michael@0 | 69 | }); |
michael@0 | 70 | aRequest.addEventListener("error", function(aEvent) { |
michael@0 | 71 | deferred.reject(aEvent); |
michael@0 | 72 | }); |
michael@0 | 73 | |
michael@0 | 74 | return deferred.promise; |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | let workingFrame; |
michael@0 | 78 | |
michael@0 | 79 | /** |
michael@0 | 80 | * Get mozSettings value specified by @aKey. |
michael@0 | 81 | * |
michael@0 | 82 | * Resolve if that mozSettings value is retrieved successfully, reject |
michael@0 | 83 | * otherwise. |
michael@0 | 84 | * |
michael@0 | 85 | * Fulfill params: |
michael@0 | 86 | * The corresponding mozSettings value of the key. |
michael@0 | 87 | * Reject params: (none) |
michael@0 | 88 | * |
michael@0 | 89 | * @param aKey |
michael@0 | 90 | * A string. |
michael@0 | 91 | * @param aAllowError [optional] |
michael@0 | 92 | * A boolean value. If set to true, an error response won't be treated |
michael@0 | 93 | * as test failure. Default: false. |
michael@0 | 94 | * |
michael@0 | 95 | * @return A deferred promise. |
michael@0 | 96 | */ |
michael@0 | 97 | function getSettings(aKey, aAllowError) { |
michael@0 | 98 | let request = |
michael@0 | 99 | workingFrame.contentWindow.navigator.mozSettings.createLock().get(aKey); |
michael@0 | 100 | return wrapDomRequestAsPromise(request) |
michael@0 | 101 | .then(function resolve(aEvent) { |
michael@0 | 102 | ok(true, "getSettings(" + aKey + ") - success"); |
michael@0 | 103 | return aEvent.target.result[aKey]; |
michael@0 | 104 | }, function reject(aEvent) { |
michael@0 | 105 | ok(aAllowError, "getSettings(" + aKey + ") - error"); |
michael@0 | 106 | }); |
michael@0 | 107 | } |
michael@0 | 108 | |
michael@0 | 109 | /** |
michael@0 | 110 | * Set mozSettings values. |
michael@0 | 111 | * |
michael@0 | 112 | * Resolve if that mozSettings value is set successfully, reject otherwise. |
michael@0 | 113 | * |
michael@0 | 114 | * Fulfill params: (none) |
michael@0 | 115 | * Reject params: (none) |
michael@0 | 116 | * |
michael@0 | 117 | * @param aSettings |
michael@0 | 118 | * An object of format |{key1: value1, key2: value2, ...}|. |
michael@0 | 119 | * @param aAllowError [optional] |
michael@0 | 120 | * A boolean value. If set to true, an error response won't be treated |
michael@0 | 121 | * as test failure. Default: false. |
michael@0 | 122 | * |
michael@0 | 123 | * @return A deferred promise. |
michael@0 | 124 | */ |
michael@0 | 125 | function setSettings(aSettings, aAllowError) { |
michael@0 | 126 | let request = |
michael@0 | 127 | workingFrame.contentWindow.navigator.mozSettings.createLock().set(aSettings); |
michael@0 | 128 | return wrapDomRequestAsPromise(request) |
michael@0 | 129 | .then(function resolve() { |
michael@0 | 130 | ok(true, "setSettings(" + JSON.stringify(aSettings) + ")"); |
michael@0 | 131 | }, function reject() { |
michael@0 | 132 | ok(aAllowError, "setSettings(" + JSON.stringify(aSettings) + ")"); |
michael@0 | 133 | }); |
michael@0 | 134 | } |
michael@0 | 135 | |
michael@0 | 136 | /** |
michael@0 | 137 | * Set mozSettings value with only one key. |
michael@0 | 138 | * |
michael@0 | 139 | * Resolve if that mozSettings value is set successfully, reject otherwise. |
michael@0 | 140 | * |
michael@0 | 141 | * Fulfill params: (none) |
michael@0 | 142 | * Reject params: (none) |
michael@0 | 143 | * |
michael@0 | 144 | * @param aKey |
michael@0 | 145 | * A string key. |
michael@0 | 146 | * @param aValue |
michael@0 | 147 | * An object value. |
michael@0 | 148 | * @param aAllowError [optional] |
michael@0 | 149 | * A boolean value. If set to true, an error response won't be treated |
michael@0 | 150 | * as test failure. Default: false. |
michael@0 | 151 | * |
michael@0 | 152 | * @return A deferred promise. |
michael@0 | 153 | */ |
michael@0 | 154 | function setSettings1(aKey, aValue, aAllowError) { |
michael@0 | 155 | let settings = {}; |
michael@0 | 156 | settings[aKey] = aValue; |
michael@0 | 157 | return setSettings(settings, aAllowError); |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | /** |
michael@0 | 161 | * Convenient MozSettings getter for SETTINGS_KEY_DATA_ENABLED. |
michael@0 | 162 | */ |
michael@0 | 163 | function getDataEnabled(aAllowError) { |
michael@0 | 164 | return getSettings(SETTINGS_KEY_DATA_ENABLED, aAllowError); |
michael@0 | 165 | } |
michael@0 | 166 | |
michael@0 | 167 | /** |
michael@0 | 168 | * Convenient MozSettings setter for SETTINGS_KEY_DATA_ENABLED. |
michael@0 | 169 | */ |
michael@0 | 170 | function setDataEnabled(aEnabled, aAllowError) { |
michael@0 | 171 | return setSettings1(SETTINGS_KEY_DATA_ENABLED, aEnabled, aAllowError); |
michael@0 | 172 | } |
michael@0 | 173 | |
michael@0 | 174 | /** |
michael@0 | 175 | * Convenient MozSettings getter for SETTINGS_KEY_DATA_ROAMING_ENABLED. |
michael@0 | 176 | */ |
michael@0 | 177 | function getDataRoamingEnabled(aAllowError) { |
michael@0 | 178 | return getSettings(SETTINGS_KEY_DATA_ROAMING_ENABLED, aAllowError); |
michael@0 | 179 | } |
michael@0 | 180 | |
michael@0 | 181 | /** |
michael@0 | 182 | * Convenient MozSettings setter for SETTINGS_KEY_DATA_ROAMING_ENABLED. |
michael@0 | 183 | */ |
michael@0 | 184 | function setDataRoamingEnabled(aEnabled, aAllowError) { |
michael@0 | 185 | return setSettings1(SETTINGS_KEY_DATA_ROAMING_ENABLED, aEnabled, aAllowError); |
michael@0 | 186 | } |
michael@0 | 187 | |
michael@0 | 188 | /** |
michael@0 | 189 | * Convenient MozSettings getter for SETTINGS_KEY_DATA_APN_SETTINGS. |
michael@0 | 190 | */ |
michael@0 | 191 | function getDataApnSettings(aAllowError) { |
michael@0 | 192 | return getSettings(SETTINGS_KEY_DATA_APN_SETTINGS, aAllowError); |
michael@0 | 193 | } |
michael@0 | 194 | |
michael@0 | 195 | /** |
michael@0 | 196 | * Convenient MozSettings setter for SETTINGS_KEY_DATA_APN_SETTINGS. |
michael@0 | 197 | */ |
michael@0 | 198 | function setDataApnSettings(aApnSettings, aAllowError) { |
michael@0 | 199 | return setSettings1(SETTINGS_KEY_DATA_APN_SETTINGS, aApnSettings, aAllowError); |
michael@0 | 200 | } |
michael@0 | 201 | |
michael@0 | 202 | let mobileConnection; |
michael@0 | 203 | |
michael@0 | 204 | /** |
michael@0 | 205 | * Push required permissions and test if |
michael@0 | 206 | * |navigator.mozMobileConnections[<aServiceId>]| exists. Resolve if it does, |
michael@0 | 207 | * reject otherwise. |
michael@0 | 208 | * |
michael@0 | 209 | * Fulfill params: |
michael@0 | 210 | * mobileConnection -- an reference to navigator.mozMobileMessage. |
michael@0 | 211 | * |
michael@0 | 212 | * Reject params: (none) |
michael@0 | 213 | * |
michael@0 | 214 | * @param aAdditonalPermissions [optional] |
michael@0 | 215 | * An array of permission strings other than "mobileconnection" to be |
michael@0 | 216 | * pushed. Default: empty string. |
michael@0 | 217 | * @param aServiceId [optional] |
michael@0 | 218 | * A numeric DSDS service id. Default: 0. |
michael@0 | 219 | * |
michael@0 | 220 | * @return A deferred promise. |
michael@0 | 221 | */ |
michael@0 | 222 | function ensureMobileConnection(aAdditionalPermissions, aServiceId) { |
michael@0 | 223 | let deferred = Promise.defer(); |
michael@0 | 224 | |
michael@0 | 225 | aAdditionalPermissions = aAdditionalPermissions || []; |
michael@0 | 226 | aServiceId = aServiceId || 0; |
michael@0 | 227 | |
michael@0 | 228 | if (aAdditionalPermissions.indexOf("mobileconnection") < 0) { |
michael@0 | 229 | aAdditionalPermissions.push("mobileconnection"); |
michael@0 | 230 | } |
michael@0 | 231 | let permissions = []; |
michael@0 | 232 | for (let perm of aAdditionalPermissions) { |
michael@0 | 233 | permissions.push({ "type": perm, "allow": 1, "context": document }); |
michael@0 | 234 | } |
michael@0 | 235 | |
michael@0 | 236 | SpecialPowers.pushPermissions(permissions, function() { |
michael@0 | 237 | ok(true, "permissions pushed: " + JSON.stringify(permissions)); |
michael@0 | 238 | |
michael@0 | 239 | // Permission changes can't change existing Navigator.prototype |
michael@0 | 240 | // objects, so grab our objects from a new Navigator. |
michael@0 | 241 | workingFrame = document.createElement("iframe"); |
michael@0 | 242 | workingFrame.addEventListener("load", function load() { |
michael@0 | 243 | workingFrame.removeEventListener("load", load); |
michael@0 | 244 | |
michael@0 | 245 | mobileConnection = |
michael@0 | 246 | workingFrame.contentWindow.navigator.mozMobileConnections[aServiceId]; |
michael@0 | 247 | |
michael@0 | 248 | if (mobileConnection) { |
michael@0 | 249 | log("navigator.mozMobileConnections[" + aServiceId + "] is instance of " + |
michael@0 | 250 | mobileConnection.constructor); |
michael@0 | 251 | } else { |
michael@0 | 252 | log("navigator.mozMobileConnections[" + aServiceId + "] is undefined"); |
michael@0 | 253 | } |
michael@0 | 254 | |
michael@0 | 255 | if (mobileConnection instanceof MozMobileConnection) { |
michael@0 | 256 | deferred.resolve(mobileConnection); |
michael@0 | 257 | } else { |
michael@0 | 258 | deferred.reject(); |
michael@0 | 259 | } |
michael@0 | 260 | }); |
michael@0 | 261 | |
michael@0 | 262 | document.body.appendChild(workingFrame); |
michael@0 | 263 | }); |
michael@0 | 264 | |
michael@0 | 265 | return deferred.promise; |
michael@0 | 266 | } |
michael@0 | 267 | |
michael@0 | 268 | /** |
michael@0 | 269 | * Wait for one named MobileConnection event. |
michael@0 | 270 | * |
michael@0 | 271 | * Resolve if that named event occurs. Never reject. |
michael@0 | 272 | * |
michael@0 | 273 | * Fulfill params: the DOMEvent passed. |
michael@0 | 274 | * |
michael@0 | 275 | * @param aEventName |
michael@0 | 276 | * A string event name. |
michael@0 | 277 | * @param aServiceId [optional] |
michael@0 | 278 | * A numeric DSDS service id. Default: the one indicated in |
michael@0 | 279 | * start*TestCommon() or 0 if not indicated. |
michael@0 | 280 | * |
michael@0 | 281 | * @return A deferred promise. |
michael@0 | 282 | */ |
michael@0 | 283 | function waitForManagerEvent(aEventName, aServiceId) { |
michael@0 | 284 | let deferred = Promise.defer(); |
michael@0 | 285 | |
michael@0 | 286 | let mobileConn = mobileConnection; |
michael@0 | 287 | if (aServiceId !== undefined) { |
michael@0 | 288 | mobileConn = |
michael@0 | 289 | workingFrame.contentWindow.navigator.mozMobileConnections[aServiceId]; |
michael@0 | 290 | } |
michael@0 | 291 | |
michael@0 | 292 | mobileConn.addEventListener(aEventName, function onevent(aEvent) { |
michael@0 | 293 | mobileConn.removeEventListener(aEventName, onevent); |
michael@0 | 294 | |
michael@0 | 295 | ok(true, "MobileConnection event '" + aEventName + "' got."); |
michael@0 | 296 | deferred.resolve(aEvent); |
michael@0 | 297 | }); |
michael@0 | 298 | |
michael@0 | 299 | return deferred.promise; |
michael@0 | 300 | } |
michael@0 | 301 | |
michael@0 | 302 | /** |
michael@0 | 303 | * Get available networks. |
michael@0 | 304 | * |
michael@0 | 305 | * Fulfill params: |
michael@0 | 306 | * An array of nsIDOMMozMobileNetworkInfo. |
michael@0 | 307 | * Reject params: |
michael@0 | 308 | * A DOMEvent. |
michael@0 | 309 | * |
michael@0 | 310 | * @return A deferred promise. |
michael@0 | 311 | */ |
michael@0 | 312 | function getNetworks() { |
michael@0 | 313 | let request = mobileConnection.getNetworks(); |
michael@0 | 314 | return wrapDomRequestAsPromise(request) |
michael@0 | 315 | .then(() => request.result); |
michael@0 | 316 | } |
michael@0 | 317 | |
michael@0 | 318 | /** |
michael@0 | 319 | * Manually select a network. |
michael@0 | 320 | * |
michael@0 | 321 | * Fulfill params: (none) |
michael@0 | 322 | * Reject params: |
michael@0 | 323 | * 'RadioNotAvailable', 'RequestNotSupported', or 'GenericFailure' |
michael@0 | 324 | * |
michael@0 | 325 | * @param aNetwork |
michael@0 | 326 | * A nsIDOMMozMobileNetworkInfo. |
michael@0 | 327 | * |
michael@0 | 328 | * @return A deferred promise. |
michael@0 | 329 | */ |
michael@0 | 330 | function selectNetwork(aNetwork) { |
michael@0 | 331 | let request = mobileConnection.selectNetwork(aNetwork); |
michael@0 | 332 | return wrapDomRequestAsPromise(request) |
michael@0 | 333 | .then(null, () => { throw request.error }); |
michael@0 | 334 | } |
michael@0 | 335 | |
michael@0 | 336 | /** |
michael@0 | 337 | * Manually select a network and wait for a 'voicechange' event. |
michael@0 | 338 | * |
michael@0 | 339 | * Fulfill params: (none) |
michael@0 | 340 | * Reject params: |
michael@0 | 341 | * 'RadioNotAvailable', 'RequestNotSupported', or 'GenericFailure' |
michael@0 | 342 | * |
michael@0 | 343 | * @param aNetwork |
michael@0 | 344 | * A nsIDOMMozMobileNetworkInfo. |
michael@0 | 345 | * |
michael@0 | 346 | * @return A deferred promise. |
michael@0 | 347 | */ |
michael@0 | 348 | function selectNetworkAndWait(aNetwork) { |
michael@0 | 349 | let promises = []; |
michael@0 | 350 | |
michael@0 | 351 | promises.push(waitForManagerEvent("voicechange")); |
michael@0 | 352 | promises.push(selectNetwork(aNetwork)); |
michael@0 | 353 | |
michael@0 | 354 | return Promise.all(promises); |
michael@0 | 355 | } |
michael@0 | 356 | |
michael@0 | 357 | /** |
michael@0 | 358 | * Automatically select a network. |
michael@0 | 359 | * |
michael@0 | 360 | * Fulfill params: (none) |
michael@0 | 361 | * Reject params: |
michael@0 | 362 | * 'RadioNotAvailable', 'RequestNotSupported', or 'GenericFailure' |
michael@0 | 363 | * |
michael@0 | 364 | * @return A deferred promise. |
michael@0 | 365 | */ |
michael@0 | 366 | function selectNetworkAutomatically() { |
michael@0 | 367 | let request = mobileConnection.selectNetworkAutomatically(); |
michael@0 | 368 | return wrapDomRequestAsPromise(request) |
michael@0 | 369 | .then(null, () => { throw request.error }); |
michael@0 | 370 | } |
michael@0 | 371 | |
michael@0 | 372 | /** |
michael@0 | 373 | * Automatically select a network and wait for a 'voicechange' event. |
michael@0 | 374 | * |
michael@0 | 375 | * Fulfill params: (none) |
michael@0 | 376 | * Reject params: |
michael@0 | 377 | * 'RadioNotAvailable', 'RequestNotSupported', or 'GenericFailure' |
michael@0 | 378 | * |
michael@0 | 379 | * @return A deferred promise. |
michael@0 | 380 | */ |
michael@0 | 381 | function selectNetworkAutomaticallyAndWait() { |
michael@0 | 382 | let promises = []; |
michael@0 | 383 | |
michael@0 | 384 | promises.push(waitForManagerEvent("voicechange")); |
michael@0 | 385 | promises.push(selectNetworkAutomatically()); |
michael@0 | 386 | |
michael@0 | 387 | return Promise.all(promises); |
michael@0 | 388 | } |
michael@0 | 389 | |
michael@0 | 390 | /** |
michael@0 | 391 | * Set data connection enabling state and wait for "datachange" event. |
michael@0 | 392 | * |
michael@0 | 393 | * Resolve if data connection state changed to the expected one. Never reject. |
michael@0 | 394 | * |
michael@0 | 395 | * Fulfill params: (none) |
michael@0 | 396 | * |
michael@0 | 397 | * @param aEnabled |
michael@0 | 398 | * A boolean state. |
michael@0 | 399 | * @param aServiceId [optional] |
michael@0 | 400 | * A numeric DSDS service id. Default: the one indicated in |
michael@0 | 401 | * start*TestCommon() or 0 if not indicated. |
michael@0 | 402 | * |
michael@0 | 403 | * @return A deferred promise. |
michael@0 | 404 | */ |
michael@0 | 405 | function setDataEnabledAndWait(aEnabled, aServiceId) { |
michael@0 | 406 | let deferred = Promise.defer(); |
michael@0 | 407 | |
michael@0 | 408 | let promises = []; |
michael@0 | 409 | promises.push(waitForManagerEvent("datachange", aServiceId)); |
michael@0 | 410 | promises.push(setDataEnabled(aEnabled)); |
michael@0 | 411 | Promise.all(promises).then(function keepWaiting() { |
michael@0 | 412 | let mobileConn = mobileConnection; |
michael@0 | 413 | if (aServiceId !== undefined) { |
michael@0 | 414 | mobileConn = |
michael@0 | 415 | workingFrame.contentWindow.navigator.mozMobileConnections[aServiceId]; |
michael@0 | 416 | } |
michael@0 | 417 | // To ignore some transient states, we only resolve that deferred promise |
michael@0 | 418 | // when the |connected| state equals to the expected one and never rejects. |
michael@0 | 419 | let connected = mobileConn.data.connected; |
michael@0 | 420 | if (connected == aEnabled) { |
michael@0 | 421 | deferred.resolve(); |
michael@0 | 422 | return; |
michael@0 | 423 | } |
michael@0 | 424 | |
michael@0 | 425 | return waitForManagerEvent("datachange", aServiceId).then(keepWaiting); |
michael@0 | 426 | }); |
michael@0 | 427 | |
michael@0 | 428 | return deferred.promise; |
michael@0 | 429 | } |
michael@0 | 430 | |
michael@0 | 431 | /** |
michael@0 | 432 | * Set voice/data state and wait for state change. |
michael@0 | 433 | * |
michael@0 | 434 | * Fulfill params: (none) |
michael@0 | 435 | * |
michael@0 | 436 | * @param aWhich |
michael@0 | 437 | * "voice" or "data". |
michael@0 | 438 | * @param aState |
michael@0 | 439 | * "unregistered", "searching", "denied", "roaming", or "home". |
michael@0 | 440 | * @param aServiceId [optional] |
michael@0 | 441 | * A numeric DSDS service id. Default: the one indicated in |
michael@0 | 442 | * start*TestCommon() or 0 if not indicated. |
michael@0 | 443 | * |
michael@0 | 444 | * @return A deferred promise. |
michael@0 | 445 | */ |
michael@0 | 446 | function setEmulatorVoiceDataStateAndWait(aWhich, aState, aServiceId) { |
michael@0 | 447 | let promises = []; |
michael@0 | 448 | promises.push(waitForManagerEvent(aWhich + "change", aServiceId)); |
michael@0 | 449 | |
michael@0 | 450 | let cmd = "gsm " + aWhich + " " + aState; |
michael@0 | 451 | promises.push(runEmulatorCmdSafe(cmd)); |
michael@0 | 452 | return Promise.all(promises); |
michael@0 | 453 | } |
michael@0 | 454 | |
michael@0 | 455 | /** |
michael@0 | 456 | * Set voice and data roaming emulation and wait for state change. |
michael@0 | 457 | * |
michael@0 | 458 | * Fulfill params: (none) |
michael@0 | 459 | * |
michael@0 | 460 | * @param aRoaming |
michael@0 | 461 | * A boolean state. |
michael@0 | 462 | * @param aServiceId [optional] |
michael@0 | 463 | * A numeric DSDS service id. Default: the one indicated in |
michael@0 | 464 | * start*TestCommon() or 0 if not indicated. |
michael@0 | 465 | * |
michael@0 | 466 | * @return A deferred promise. |
michael@0 | 467 | */ |
michael@0 | 468 | function setEmulatorRoamingAndWait(aRoaming, aServiceId) { |
michael@0 | 469 | function doSetAndWait(aWhich, aRoaming, aServiceId) { |
michael@0 | 470 | let state = (aRoaming ? "roaming" : "home"); |
michael@0 | 471 | return setEmulatorVoiceDataStateAndWait(aWhich, state, aServiceId) |
michael@0 | 472 | .then(() => { |
michael@0 | 473 | let mobileConn = mobileConnection; |
michael@0 | 474 | if (aServiceId !== undefined) { |
michael@0 | 475 | mobileConn = |
michael@0 | 476 | workingFrame.contentWindow.navigator.mozMobileConnections[aServiceId]; |
michael@0 | 477 | } |
michael@0 | 478 | is(mobileConn[aWhich].roaming, aRoaming, |
michael@0 | 479 | aWhich + ".roaming") |
michael@0 | 480 | }); |
michael@0 | 481 | } |
michael@0 | 482 | |
michael@0 | 483 | // Set voice registration state first and then data registration state. |
michael@0 | 484 | return doSetAndWait("voice", aRoaming, aServiceId) |
michael@0 | 485 | .then(() => doSetAndWait("data", aRoaming, aServiceId)); |
michael@0 | 486 | } |
michael@0 | 487 | |
michael@0 | 488 | /** |
michael@0 | 489 | * Get GSM location emulation. |
michael@0 | 490 | * |
michael@0 | 491 | * Fulfill params: |
michael@0 | 492 | * { lac: <lac>, cid: <cid> } |
michael@0 | 493 | * Reject params: |
michael@0 | 494 | * result -- an array of emulator response lines. |
michael@0 | 495 | * |
michael@0 | 496 | * @return A deferred promise. |
michael@0 | 497 | */ |
michael@0 | 498 | function getEmulatorGsmLocation() { |
michael@0 | 499 | let cmd = "gsm location"; |
michael@0 | 500 | return runEmulatorCmdSafe(cmd) |
michael@0 | 501 | .then(function(aResults) { |
michael@0 | 502 | // lac: <lac> |
michael@0 | 503 | // ci: <cid> |
michael@0 | 504 | // OK |
michael@0 | 505 | is(aResults[0].substring(0,3), "lac", "lac output"); |
michael@0 | 506 | is(aResults[1].substring(0,2), "ci", "ci output"); |
michael@0 | 507 | |
michael@0 | 508 | let lac = parseInt(aResults[0].substring(5)); |
michael@0 | 509 | lac = (lac < 0 ? 65535 : lac); |
michael@0 | 510 | let cid = parseInt(aResults[1].substring(4)); |
michael@0 | 511 | cid = (cid < 0 ? 268435455 : cid); |
michael@0 | 512 | |
michael@0 | 513 | return { lac: lac, cid: cid }; |
michael@0 | 514 | }); |
michael@0 | 515 | } |
michael@0 | 516 | |
michael@0 | 517 | /** |
michael@0 | 518 | * Set GSM location emulation. |
michael@0 | 519 | * |
michael@0 | 520 | * Fulfill params: (none) |
michael@0 | 521 | * Reject params: (none) |
michael@0 | 522 | * |
michael@0 | 523 | * @param aLac |
michael@0 | 524 | * @param aCid |
michael@0 | 525 | * |
michael@0 | 526 | * @return A deferred promise. |
michael@0 | 527 | */ |
michael@0 | 528 | function setEmulatorGsmLocation(aLac, aCid) { |
michael@0 | 529 | let cmd = "gsm location " + aLac + " " + aCid; |
michael@0 | 530 | return runEmulatorCmdSafe(cmd); |
michael@0 | 531 | } |
michael@0 | 532 | |
michael@0 | 533 | /** |
michael@0 | 534 | * Get emulator operators info. |
michael@0 | 535 | * |
michael@0 | 536 | * Fulfill params: |
michael@0 | 537 | * An array of { longName: <string>, shortName: <string>, mccMnc: <string> }. |
michael@0 | 538 | * Reject params: |
michael@0 | 539 | * result -- an array of emulator response lines. |
michael@0 | 540 | * |
michael@0 | 541 | * @return A deferred promise. |
michael@0 | 542 | */ |
michael@0 | 543 | function getEmulatorOperatorNames() { |
michael@0 | 544 | let cmd = "operator dumpall"; |
michael@0 | 545 | return runEmulatorCmdSafe(cmd) |
michael@0 | 546 | .then(function(aResults) { |
michael@0 | 547 | let operators = []; |
michael@0 | 548 | |
michael@0 | 549 | for (let i = 0; i < aResults.length - 1; i++) { |
michael@0 | 550 | let names = aResults[i].split(','); |
michael@0 | 551 | operators.push({ |
michael@0 | 552 | longName: names[0], |
michael@0 | 553 | shortName: names[1], |
michael@0 | 554 | mccMnc: names[2], |
michael@0 | 555 | }); |
michael@0 | 556 | } |
michael@0 | 557 | |
michael@0 | 558 | ok(true, "emulator operators list: " + JSON.stringify(operators)); |
michael@0 | 559 | return operators; |
michael@0 | 560 | }); |
michael@0 | 561 | } |
michael@0 | 562 | |
michael@0 | 563 | /** |
michael@0 | 564 | * Set emulator operators info. |
michael@0 | 565 | * |
michael@0 | 566 | * Fulfill params: (none) |
michael@0 | 567 | * Reject params: |
michael@0 | 568 | * result -- an array of emulator response lines. |
michael@0 | 569 | * |
michael@0 | 570 | * @param aOperator |
michael@0 | 571 | * "home" or "roaming". |
michael@0 | 572 | * @param aLongName |
michael@0 | 573 | * A string. |
michael@0 | 574 | * @param aShortName |
michael@0 | 575 | * A string. |
michael@0 | 576 | * @param aMcc [optional] |
michael@0 | 577 | * A string. |
michael@0 | 578 | * @param aMnc [optional] |
michael@0 | 579 | * A string. |
michael@0 | 580 | * |
michael@0 | 581 | * @return A deferred promise. |
michael@0 | 582 | */ |
michael@0 | 583 | function setEmulatorOperatorNames(aOperator, aLongName, aShortName, aMcc, aMnc) { |
michael@0 | 584 | const EMULATOR_OPERATORS = [ "home", "roaming" ]; |
michael@0 | 585 | |
michael@0 | 586 | let index = EMULATOR_OPERATORS.indexOf(aOperator); |
michael@0 | 587 | if (index < 0) { |
michael@0 | 588 | throw "invalid operator"; |
michael@0 | 589 | } |
michael@0 | 590 | |
michael@0 | 591 | let cmd = "operator set " + index + " " + aLongName + "," + aShortName; |
michael@0 | 592 | if (aMcc && aMnc) { |
michael@0 | 593 | cmd = cmd + "," + aMcc + aMnc; |
michael@0 | 594 | } |
michael@0 | 595 | return runEmulatorCmdSafe(cmd) |
michael@0 | 596 | .then(function(aResults) { |
michael@0 | 597 | let exp = "^" + aLongName + "," + aShortName + ","; |
michael@0 | 598 | if (aMcc && aMnc) { |
michael@0 | 599 | cmd = cmd + aMcc + aMnc; |
michael@0 | 600 | } |
michael@0 | 601 | |
michael@0 | 602 | let re = new RegExp(exp); |
michael@0 | 603 | ok(aResults[index].match(new RegExp(exp)), |
michael@0 | 604 | "Long/short name and/or mcc/mnc should be changed."); |
michael@0 | 605 | }); |
michael@0 | 606 | } |
michael@0 | 607 | |
michael@0 | 608 | let _networkManager; |
michael@0 | 609 | |
michael@0 | 610 | /** |
michael@0 | 611 | * Get internal NetworkManager service. |
michael@0 | 612 | */ |
michael@0 | 613 | function getNetworkManager() { |
michael@0 | 614 | if (!_networkManager) { |
michael@0 | 615 | _networkManager = Cc["@mozilla.org/network/manager;1"] |
michael@0 | 616 | .getService(Ci.nsINetworkManager); |
michael@0 | 617 | ok(_networkManager, "NetworkManager"); |
michael@0 | 618 | } |
michael@0 | 619 | |
michael@0 | 620 | return _networkManager; |
michael@0 | 621 | } |
michael@0 | 622 | |
michael@0 | 623 | let _numOfRadioInterfaces; |
michael@0 | 624 | |
michael@0 | 625 | /* |
michael@0 | 626 | * Get number of radio interfaces. Default is 1 if preference is not set. |
michael@0 | 627 | */ |
michael@0 | 628 | function getNumOfRadioInterfaces() { |
michael@0 | 629 | if (!_numOfRadioInterfaces) { |
michael@0 | 630 | try { |
michael@0 | 631 | _numOfRadioInterfaces = SpecialPowers.getIntPref("ril.numRadioInterfaces"); |
michael@0 | 632 | } catch (ex) { |
michael@0 | 633 | _numOfRadioInterfaces = 1; // Pref not set. |
michael@0 | 634 | } |
michael@0 | 635 | } |
michael@0 | 636 | |
michael@0 | 637 | return _numOfRadioInterfaces; |
michael@0 | 638 | } |
michael@0 | 639 | |
michael@0 | 640 | /** |
michael@0 | 641 | * Flush permission settings and call |finish()|. |
michael@0 | 642 | */ |
michael@0 | 643 | function cleanUp() { |
michael@0 | 644 | waitFor(function() { |
michael@0 | 645 | SpecialPowers.flushPermissions(function() { |
michael@0 | 646 | // Use ok here so that we have at least one test run. |
michael@0 | 647 | ok(true, "permissions flushed"); |
michael@0 | 648 | |
michael@0 | 649 | finish(); |
michael@0 | 650 | }); |
michael@0 | 651 | }, function() { |
michael@0 | 652 | return _pendingEmulatorCmdCount === 0; |
michael@0 | 653 | }); |
michael@0 | 654 | } |
michael@0 | 655 | |
michael@0 | 656 | /** |
michael@0 | 657 | * Basic test routine helper for mobile connection tests. |
michael@0 | 658 | * |
michael@0 | 659 | * This helper does nothing but clean-ups. |
michael@0 | 660 | * |
michael@0 | 661 | * @param aTestCaseMain |
michael@0 | 662 | * A function that takes no parameter. |
michael@0 | 663 | */ |
michael@0 | 664 | function startTestBase(aTestCaseMain) { |
michael@0 | 665 | Promise.resolve() |
michael@0 | 666 | .then(aTestCaseMain) |
michael@0 | 667 | .then(cleanUp, function() { |
michael@0 | 668 | ok(false, 'promise rejects during test.'); |
michael@0 | 669 | cleanUp(); |
michael@0 | 670 | }); |
michael@0 | 671 | } |
michael@0 | 672 | |
michael@0 | 673 | /** |
michael@0 | 674 | * Common test routine helper for mobile connection tests. |
michael@0 | 675 | * |
michael@0 | 676 | * This function ensures global |mobileConnection| variable is available during |
michael@0 | 677 | * the process and performs clean-ups as well. |
michael@0 | 678 | * |
michael@0 | 679 | * @param aTestCaseMain |
michael@0 | 680 | * A function that takes one parameter -- mobileConnection. |
michael@0 | 681 | * @param aAdditonalPermissions [optional] |
michael@0 | 682 | * An array of permission strings other than "mobileconnection" to be |
michael@0 | 683 | * pushed. Default: empty string. |
michael@0 | 684 | * @param aServiceId [optional] |
michael@0 | 685 | * A numeric DSDS service id. Default: 0. |
michael@0 | 686 | */ |
michael@0 | 687 | function startTestCommon(aTestCaseMain, aAdditionalPermissions, aServiceId) { |
michael@0 | 688 | startTestBase(function() { |
michael@0 | 689 | return ensureMobileConnection(aAdditionalPermissions, aServiceId) |
michael@0 | 690 | .then(aTestCaseMain); |
michael@0 | 691 | }); |
michael@0 | 692 | } |
michael@0 | 693 | |
michael@0 | 694 | /** |
michael@0 | 695 | * Common test routine helper for multi-sim mobile connection tests. The test |
michael@0 | 696 | * ends immediately if the device tested is not multi-sim. |
michael@0 | 697 | * |
michael@0 | 698 | * This function ensures global |mobileConnection| variable is available during |
michael@0 | 699 | * the process and performs clean-ups as well. |
michael@0 | 700 | * |
michael@0 | 701 | * @param aTestCaseMain |
michael@0 | 702 | * A function that takes one parameter -- mobileConnection. |
michael@0 | 703 | * @param aAdditonalPermissions [optional] |
michael@0 | 704 | * An array of permission strings other than "mobileconnection" to be |
michael@0 | 705 | * pushed. Default: empty string. |
michael@0 | 706 | * @param aServiceId [optional] |
michael@0 | 707 | * A numeric DSDS service id. Default: 0. |
michael@0 | 708 | */ |
michael@0 | 709 | function startDSDSTestCommon(aTestCaseMain, aAdditionalPermissions, aServiceId) { |
michael@0 | 710 | if (getNumOfRadioInterfaces() > 1) { |
michael@0 | 711 | startTestBase(function() { |
michael@0 | 712 | return ensureMobileConnection(aAdditionalPermissions, aServiceId) |
michael@0 | 713 | .then(aTestCaseMain); |
michael@0 | 714 | }); |
michael@0 | 715 | } else { |
michael@0 | 716 | log("Skipping DSDS tests on single SIM device.") |
michael@0 | 717 | ok(true); // We should run at least one test. |
michael@0 | 718 | cleanUp(); |
michael@0 | 719 | } |
michael@0 | 720 | } |