dom/mobilemessage/tests/marionette/head.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

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 let Promise = Cu.import("resource://gre/modules/Promise.jsm").Promise;
michael@0 7
michael@0 8 /**
michael@0 9 * Push required permissions and test if |navigator.mozMobileMessage| exists.
michael@0 10 * Resolve if it does, reject otherwise.
michael@0 11 *
michael@0 12 * Fulfill params:
michael@0 13 * manager -- an reference to navigator.mozMobileMessage.
michael@0 14 *
michael@0 15 * Reject params: (none)
michael@0 16 *
michael@0 17 * @return A deferred promise.
michael@0 18 */
michael@0 19 let manager;
michael@0 20 function ensureMobileMessage() {
michael@0 21 let deferred = Promise.defer();
michael@0 22
michael@0 23 let permissions = [{
michael@0 24 "type": "sms",
michael@0 25 "allow": 1,
michael@0 26 "context": document,
michael@0 27 }];
michael@0 28 SpecialPowers.pushPermissions(permissions, function() {
michael@0 29 ok(true, "permissions pushed: " + JSON.stringify(permissions));
michael@0 30
michael@0 31 manager = window.navigator.mozMobileMessage;
michael@0 32 if (manager) {
michael@0 33 log("navigator.mozMobileMessage is instance of " + manager.constructor);
michael@0 34 } else {
michael@0 35 log("navigator.mozMobileMessage is undefined.");
michael@0 36 }
michael@0 37
michael@0 38 if (manager instanceof MozMobileMessageManager) {
michael@0 39 deferred.resolve(manager);
michael@0 40 } else {
michael@0 41 deferred.reject();
michael@0 42 }
michael@0 43 });
michael@0 44
michael@0 45 return deferred.promise;
michael@0 46 }
michael@0 47
michael@0 48 /**
michael@0 49 * Wait for one named MobileMessageManager event.
michael@0 50 *
michael@0 51 * Resolve if that named event occurs. Never reject.
michael@0 52 *
michael@0 53 * Fulfill params: the DOMEvent passed.
michael@0 54 *
michael@0 55 * @param aEventName
michael@0 56 * A string event name.
michael@0 57 *
michael@0 58 * @return A deferred promise.
michael@0 59 */
michael@0 60 function waitForManagerEvent(aEventName) {
michael@0 61 let deferred = Promise.defer();
michael@0 62
michael@0 63 manager.addEventListener(aEventName, function onevent(aEvent) {
michael@0 64 manager.removeEventListener(aEventName, onevent);
michael@0 65
michael@0 66 ok(true, "MobileMessageManager event '" + aEventName + "' got.");
michael@0 67 deferred.resolve(aEvent);
michael@0 68 });
michael@0 69
michael@0 70 return deferred.promise;
michael@0 71 }
michael@0 72
michael@0 73 /**
michael@0 74 * Send a SMS message to a single receiver. Resolve if it succeeds, reject
michael@0 75 * otherwise.
michael@0 76 *
michael@0 77 * Fulfill params:
michael@0 78 * message -- the sent SmsMessage.
michael@0 79 *
michael@0 80 * Reject params:
michael@0 81 * error -- a DOMError.
michael@0 82 *
michael@0 83 * @param aReceiver the address of the receiver.
michael@0 84 * @param aText the text body of the message.
michael@0 85 *
michael@0 86 * @return A deferred promise.
michael@0 87 */
michael@0 88 function sendSmsWithSuccess(aReceiver, aText) {
michael@0 89 let deferred = Promise.defer();
michael@0 90
michael@0 91 let request = manager.send(aReceiver, aText);
michael@0 92 request.onsuccess = function(event) {
michael@0 93 deferred.resolve(event.target.result);
michael@0 94 };
michael@0 95 request.onerror = function(event) {
michael@0 96 deferred.reject(event.target.error);
michael@0 97 };
michael@0 98
michael@0 99 return deferred.promise;
michael@0 100 }
michael@0 101
michael@0 102 /**
michael@0 103 * Send a MMS message with specified parameters. Resolve if it fails, reject
michael@0 104 * otherwise.
michael@0 105 *
michael@0 106 * Fulfill params:
michael@0 107 * {
michael@0 108 * message, -- the failed MmsMessage
michael@0 109 * error, -- error of the send request
michael@0 110 * }
michael@0 111 *
michael@0 112 * Reject params: (none)
michael@0 113 *
michael@0 114 * @param aMmsParameters a MmsParameters instance.
michael@0 115 *
michael@0 116 * @param aSendParameters a MmsSendParameters instance.
michael@0 117 *
michael@0 118 * @return A deferred promise.
michael@0 119 */
michael@0 120 function sendMmsWithFailure(aMmsParameters, aSendParameters) {
michael@0 121 let deferred = Promise.defer();
michael@0 122
michael@0 123 let result = { message: null, error: null };
michael@0 124 function got(which, value) {
michael@0 125 result[which] = value;
michael@0 126 if (result.message != null && result.error != null) {
michael@0 127 deferred.resolve(result);
michael@0 128 }
michael@0 129 }
michael@0 130
michael@0 131 manager.addEventListener("failed", function onfailed(event) {
michael@0 132 manager.removeEventListener("failed", onfailed);
michael@0 133 got("message", event.message);
michael@0 134 });
michael@0 135
michael@0 136 let request = manager.sendMMS(aMmsParameters, aSendParameters);
michael@0 137 request.onsuccess = function(event) {
michael@0 138 deferred.reject();
michael@0 139 };
michael@0 140 request.onerror = function(event) {
michael@0 141 got("error", event.target.error);
michael@0 142 }
michael@0 143
michael@0 144 return deferred.promise;
michael@0 145 }
michael@0 146
michael@0 147 /**
michael@0 148 * Retrieve messages from database.
michael@0 149 *
michael@0 150 * Fulfill params:
michael@0 151 * messages -- an array of {Sms,Mms}Message instances.
michael@0 152 *
michael@0 153 * Reject params:
michael@0 154 * event -- a DOMEvent
michael@0 155 *
michael@0 156 * @param aFilter an optional MozSmsFilter instance.
michael@0 157 * @param aReverse a boolean value indicating whether the order of the messages
michael@0 158 * should be reversed.
michael@0 159 *
michael@0 160 * @return A deferred promise.
michael@0 161 */
michael@0 162 function getMessages(aFilter, aReverse) {
michael@0 163 let deferred = Promise.defer();
michael@0 164
michael@0 165 if (!aFilter) {
michael@0 166 aFilter = new MozSmsFilter;
michael@0 167 }
michael@0 168 let messages = [];
michael@0 169 let cursor = manager.getMessages(aFilter, aReverse || false);
michael@0 170 cursor.onsuccess = function(aEvent) {
michael@0 171 if (cursor.result) {
michael@0 172 messages.push(cursor.result);
michael@0 173 cursor.continue();
michael@0 174 return;
michael@0 175 }
michael@0 176
michael@0 177 deferred.resolve(messages);
michael@0 178 };
michael@0 179 cursor.onerror = deferred.reject.bind(deferred);
michael@0 180
michael@0 181 return deferred.promise;
michael@0 182 }
michael@0 183
michael@0 184 /**
michael@0 185 * Retrieve all messages from database.
michael@0 186 *
michael@0 187 * Fulfill params:
michael@0 188 * messages -- an array of {Sms,Mms}Message instances.
michael@0 189 *
michael@0 190 * Reject params:
michael@0 191 * event -- a DOMEvent
michael@0 192 *
michael@0 193 * @return A deferred promise.
michael@0 194 */
michael@0 195 function getAllMessages() {
michael@0 196 return getMessages(null, false);
michael@0 197 }
michael@0 198
michael@0 199 /**
michael@0 200 * Retrieve all threads from database.
michael@0 201 *
michael@0 202 * Fulfill params:
michael@0 203 * threads -- an array of MozMobileMessageThread instances.
michael@0 204 *
michael@0 205 * Reject params:
michael@0 206 * event -- a DOMEvent
michael@0 207 *
michael@0 208 * @return A deferred promise.
michael@0 209 */
michael@0 210 function getAllThreads() {
michael@0 211 let deferred = Promise.defer();
michael@0 212
michael@0 213 let threads = [];
michael@0 214 let cursor = manager.getThreads();
michael@0 215 cursor.onsuccess = function(aEvent) {
michael@0 216 if (cursor.result) {
michael@0 217 threads.push(cursor.result);
michael@0 218 cursor.continue();
michael@0 219 return;
michael@0 220 }
michael@0 221
michael@0 222 deferred.resolve(threads);
michael@0 223 };
michael@0 224 cursor.onerror = deferred.reject.bind(deferred);
michael@0 225
michael@0 226 return deferred.promise;
michael@0 227 }
michael@0 228
michael@0 229 /**
michael@0 230 * Retrieve a single specified thread from database.
michael@0 231 *
michael@0 232 * Fulfill params:
michael@0 233 * thread -- a MozMobileMessageThread instance.
michael@0 234 *
michael@0 235 * Reject params:
michael@0 236 * event -- a DOMEvent if an error occurs in the retrieving process, or
michael@0 237 * undefined if there's no such thread.
michael@0 238 *
michael@0 239 * @aThreadId a numeric value identifying the target thread.
michael@0 240 *
michael@0 241 * @return A deferred promise.
michael@0 242 */
michael@0 243 function getThreadById(aThreadId) {
michael@0 244 return getAllThreads()
michael@0 245 .then(function(aThreads) {
michael@0 246 for (let thread of aThreads) {
michael@0 247 if (thread.id === aThreadId) {
michael@0 248 return thread;
michael@0 249 }
michael@0 250 }
michael@0 251 throw undefined;
michael@0 252 });
michael@0 253 }
michael@0 254
michael@0 255 /**
michael@0 256 * Delete messages specified from database.
michael@0 257 *
michael@0 258 * Fulfill params:
michael@0 259 * result -- an array of boolean values indicating whether delesion was
michael@0 260 * actually performed on the message record with corresponding id.
michael@0 261 *
michael@0 262 * Reject params:
michael@0 263 * event -- a DOMEvent.
michael@0 264 *
michael@0 265 * @aMessageId an array of numeric values identifying the target messages.
michael@0 266 *
michael@0 267 * @return An empty array if nothing to be deleted; otherwise, a deferred promise.
michael@0 268 */
michael@0 269 function deleteMessagesById(aMessageIds) {
michael@0 270 if (!aMessageIds.length) {
michael@0 271 ok(true, "no message to be deleted");
michael@0 272 return [];
michael@0 273 }
michael@0 274
michael@0 275 let deferred = Promise.defer();
michael@0 276
michael@0 277 let request = manager.delete(aMessageIds);
michael@0 278 request.onsuccess = function(event) {
michael@0 279 deferred.resolve(event.target.result);
michael@0 280 };
michael@0 281 request.onerror = deferred.reject.bind(deferred);
michael@0 282
michael@0 283 return deferred.promise;
michael@0 284 }
michael@0 285
michael@0 286 /**
michael@0 287 * Delete messages specified from database.
michael@0 288 *
michael@0 289 * Fulfill params:
michael@0 290 * result -- an array of boolean values indicating whether delesion was
michael@0 291 * actually performed on the message record with corresponding id.
michael@0 292 *
michael@0 293 * Reject params:
michael@0 294 * event -- a DOMEvent.
michael@0 295 *
michael@0 296 * @aMessages an array of {Sms,Mms}Message instances.
michael@0 297 *
michael@0 298 * @return A deferred promise.
michael@0 299 */
michael@0 300 function deleteMessages(aMessages) {
michael@0 301 let ids = messagesToIds(aMessages);
michael@0 302 return deleteMessagesById(ids);
michael@0 303 }
michael@0 304
michael@0 305 /**
michael@0 306 * Delete all messages from database.
michael@0 307 *
michael@0 308 * Fulfill params:
michael@0 309 * ids -- an array of numeric values identifying those deleted
michael@0 310 * {Sms,Mms}Messages.
michael@0 311 *
michael@0 312 * Reject params:
michael@0 313 * event -- a DOMEvent.
michael@0 314 *
michael@0 315 * @return A deferred promise.
michael@0 316 */
michael@0 317 function deleteAllMessages() {
michael@0 318 return getAllMessages().then(deleteMessages);
michael@0 319 }
michael@0 320
michael@0 321 let pendingEmulatorCmdCount = 0;
michael@0 322
michael@0 323 /**
michael@0 324 * Send emulator command with safe guard.
michael@0 325 *
michael@0 326 * We should only call |finish()| after all emulator command transactions
michael@0 327 * end, so here comes with the pending counter. Resolve when the emulator
michael@0 328 * gives positive response, and reject otherwise.
michael@0 329 *
michael@0 330 * Fulfill params:
michael@0 331 * result -- an array of emulator response lines.
michael@0 332 *
michael@0 333 * Reject params:
michael@0 334 * result -- an array of emulator response lines.
michael@0 335 *
michael@0 336 * @return A deferred promise.
michael@0 337 */
michael@0 338 function runEmulatorCmdSafe(aCommand) {
michael@0 339 let deferred = Promise.defer();
michael@0 340
michael@0 341 ++pendingEmulatorCmdCount;
michael@0 342 runEmulatorCmd(aCommand, function(aResult) {
michael@0 343 --pendingEmulatorCmdCount;
michael@0 344
michael@0 345 ok(true, "Emulator response: " + JSON.stringify(aResult));
michael@0 346 if (Array.isArray(aResult) && aResult[0] === "OK") {
michael@0 347 deferred.resolve(aResult);
michael@0 348 } else {
michael@0 349 deferred.reject(aResult);
michael@0 350 }
michael@0 351 });
michael@0 352
michael@0 353 return deferred.promise;
michael@0 354 }
michael@0 355
michael@0 356 /**
michael@0 357 * Send simple text SMS to emulator.
michael@0 358 *
michael@0 359 * Fulfill params:
michael@0 360 * result -- an array of emulator response lines.
michael@0 361 *
michael@0 362 * Reject params:
michael@0 363 * result -- an array of emulator response lines.
michael@0 364 *
michael@0 365 * @return A deferred promise.
michael@0 366 */
michael@0 367 function sendTextSmsToEmulator(aFrom, aText) {
michael@0 368 let command = "sms send " + aFrom + " " + aText;
michael@0 369 return runEmulatorCmdSafe(command);
michael@0 370 }
michael@0 371
michael@0 372 /**
michael@0 373 * Send raw SMS TPDU to emulator.
michael@0 374 *
michael@0 375 * @param: aPdu
michael@0 376 * A hex string representing the whole SMS T-PDU.
michael@0 377 *
michael@0 378 * Fulfill params:
michael@0 379 * result -- an array of emulator response lines.
michael@0 380 *
michael@0 381 * Reject params:
michael@0 382 * result -- an array of emulator response lines.
michael@0 383 *
michael@0 384 * @return A deferred promise.
michael@0 385 */
michael@0 386 function sendRawSmsToEmulator(aPdu) {
michael@0 387 let command = "sms pdu " + aPdu;
michael@0 388 return runEmulatorCmdSafe(command);
michael@0 389 }
michael@0 390
michael@0 391 /**
michael@0 392 * Send multiple raw SMS TPDU to emulator and wait
michael@0 393 *
michael@0 394 * @param: aPdus
michael@0 395 * A array of hex strings. Each represents a SMS T-PDU.
michael@0 396 *
michael@0 397 * Fulfill params:
michael@0 398 * result -- array of resolved Promise, where
michael@0 399 * result[0].message representing the received message.
michael@0 400 * result[1-n] represents the response of sent emulator command.
michael@0 401 *
michael@0 402 * Reject params:
michael@0 403 * result -- an array of emulator response lines.
michael@0 404 *
michael@0 405 * @return A deferred promise.
michael@0 406 */
michael@0 407 function sendMultipleRawSmsToEmulatorAndWait(aPdus) {
michael@0 408 let promises = [];
michael@0 409
michael@0 410 promises.push(waitForManagerEvent("received"));
michael@0 411 for (let pdu of aPdus) {
michael@0 412 promises.push(sendRawSmsToEmulator(pdu));
michael@0 413 }
michael@0 414
michael@0 415 return Promise.all(promises);
michael@0 416 }
michael@0 417
michael@0 418 /**
michael@0 419 * Create a new array of id attribute of input messages.
michael@0 420 *
michael@0 421 * @param aMessages an array of {Sms,Mms}Message instances.
michael@0 422 *
michael@0 423 * @return an array of numeric values.
michael@0 424 */
michael@0 425 function messagesToIds(aMessages) {
michael@0 426 let ids = [];
michael@0 427 for (let message of aMessages) {
michael@0 428 ids.push(message.id);
michael@0 429 }
michael@0 430 return ids;
michael@0 431 }
michael@0 432
michael@0 433 /**
michael@0 434 * Flush permission settings and call |finish()|.
michael@0 435 */
michael@0 436 function cleanUp() {
michael@0 437 waitFor(function() {
michael@0 438 SpecialPowers.flushPermissions(function() {
michael@0 439 // Use ok here so that we have at least one test run.
michael@0 440 ok(true, "permissions flushed");
michael@0 441
michael@0 442 finish();
michael@0 443 });
michael@0 444 }, function() {
michael@0 445 return pendingEmulatorCmdCount === 0;
michael@0 446 });
michael@0 447 }
michael@0 448
michael@0 449 /**
michael@0 450 * Basic test routine helper for mobile message tests.
michael@0 451 *
michael@0 452 * This helper does nothing but clean-ups.
michael@0 453 *
michael@0 454 * @param aTestCaseMain
michael@0 455 * A function that takes no parameter.
michael@0 456 */
michael@0 457 function startTestBase(aTestCaseMain) {
michael@0 458 Promise.resolve()
michael@0 459 .then(aTestCaseMain)
michael@0 460 .then(cleanUp, function() {
michael@0 461 ok(false, 'promise rejects during test.');
michael@0 462 cleanUp();
michael@0 463 });
michael@0 464 }
michael@0 465
michael@0 466 /**
michael@0 467 * Common test routine helper for mobile message tests.
michael@0 468 *
michael@0 469 * This function ensures global |manager| variable is available during the
michael@0 470 * process and performs clean-ups as well.
michael@0 471 *
michael@0 472 * @param aTestCaseMain
michael@0 473 * A function that takes no parameter.
michael@0 474 */
michael@0 475 function startTestCommon(aTestCaseMain) {
michael@0 476 startTestBase(function() {
michael@0 477 return ensureMobileMessage()
michael@0 478 .then(deleteAllMessages)
michael@0 479 .then(aTestCaseMain)
michael@0 480 .then(deleteAllMessages);
michael@0 481 });
michael@0 482 }
michael@0 483
michael@0 484 /**
michael@0 485 * Helper to run the test case only needed in Multi-SIM environment.
michael@0 486 *
michael@0 487 * @param aTest
michael@0 488 * A function which will be invoked w/o parameter.
michael@0 489 * @return a Promise object.
michael@0 490 */
michael@0 491 function runIfMultiSIM(aTest) {
michael@0 492 let numRIL;
michael@0 493 try {
michael@0 494 numRIL = SpecialPowers.getIntPref("ril.numRadioInterfaces");
michael@0 495 } catch (ex) {
michael@0 496 numRIL = 1; // Pref not set.
michael@0 497 }
michael@0 498
michael@0 499 if (numRIL > 1) {
michael@0 500 return aTest();
michael@0 501 } else {
michael@0 502 log("Not a Multi-SIM environment. Test is skipped.");
michael@0 503 return Promise.resolve();
michael@0 504 }
michael@0 505 }

mercurial