dom/mobilemessage/tests/marionette/test_phone_number_normalization.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 MARIONETTE_TIMEOUT = 60000;
michael@0 5
michael@0 6 SpecialPowers.setBoolPref("dom.sms.enabled", true);
michael@0 7 SpecialPowers.addPermission("sms", true, document);
michael@0 8
michael@0 9 let pendingEmulatorCmdCount = 0;
michael@0 10 function sendSmsToEmulator(from, text) {
michael@0 11 ++pendingEmulatorCmdCount;
michael@0 12
michael@0 13 let cmd = "sms send " + from + " " + text;
michael@0 14 runEmulatorCmd(cmd, function(result) {
michael@0 15 --pendingEmulatorCmdCount;
michael@0 16
michael@0 17 is(result[0], "OK", "Emulator response");
michael@0 18 });
michael@0 19 }
michael@0 20
michael@0 21 let tasks = {
michael@0 22 // List of test fuctions. Each of them should call |tasks.next()| when
michael@0 23 // completed or |tasks.finish()| to jump to the last one.
michael@0 24 _tasks: [],
michael@0 25 _nextTaskIndex: 0,
michael@0 26
michael@0 27 push: function(func) {
michael@0 28 this._tasks.push(func);
michael@0 29 },
michael@0 30
michael@0 31 next: function() {
michael@0 32 let index = this._nextTaskIndex++;
michael@0 33 let task = this._tasks[index];
michael@0 34 try {
michael@0 35 task();
michael@0 36 } catch (ex) {
michael@0 37 ok(false, "test task[" + index + "] throws: " + ex);
michael@0 38 // Run last task as clean up if possible.
michael@0 39 if (index != this._tasks.length - 1) {
michael@0 40 this.finish();
michael@0 41 }
michael@0 42 }
michael@0 43 },
michael@0 44
michael@0 45 finish: function() {
michael@0 46 this._tasks[this._tasks.length - 1]();
michael@0 47 },
michael@0 48
michael@0 49 run: function() {
michael@0 50 this.next();
michael@0 51 }
michael@0 52 };
michael@0 53
michael@0 54 function getAllMessages(callback, filter, reverse) {
michael@0 55 if (!filter) {
michael@0 56 filter = new MozSmsFilter;
michael@0 57 }
michael@0 58 let messages = [];
michael@0 59 let request = manager.getMessages(filter, reverse || false);
michael@0 60 request.onsuccess = function(event) {
michael@0 61 if (request.result) {
michael@0 62 messages.push(request.result);
michael@0 63 request.continue();
michael@0 64 return;
michael@0 65 }
michael@0 66
michael@0 67 window.setTimeout(callback.bind(null, messages), 0);
michael@0 68 }
michael@0 69 }
michael@0 70
michael@0 71 function deleteAllMessages() {
michael@0 72 getAllMessages(function deleteAll(messages) {
michael@0 73 let message = messages.shift();
michael@0 74 if (!message) {
michael@0 75 ok(true, "all messages deleted");
michael@0 76 tasks.next();
michael@0 77 return;
michael@0 78 }
michael@0 79
michael@0 80 let request = manager.delete(message.id);
michael@0 81 request.onsuccess = deleteAll.bind(null, messages);
michael@0 82 request.onerror = function(event) {
michael@0 83 ok(false, "failed to delete all messages");
michael@0 84 tasks.finish();
michael@0 85 }
michael@0 86 });
michael@0 87 }
michael@0 88
michael@0 89 function validate(number, normalizedNumber) {
michael@0 90 log("Checking ('" + number + "', '" + normalizedNumber + "')");
michael@0 91
michael@0 92 let sendRequest = manager.send(number, "ping");
michael@0 93 sendRequest.onsuccess = function onSendSuccess(event) {
michael@0 94 let sentMessage = event.target.result;
michael@0 95
michael@0 96 manager.onreceived = function onreceived(event) {
michael@0 97 let receivedMessage = event.message;
michael@0 98 is(sentMessage.threadId, receivedMessage.threadId,
michael@0 99 "message threadIds are supposed to be matched");
michael@0 100
michael@0 101 tasks.next();
michael@0 102 };
michael@0 103 sendSmsToEmulator(normalizedNumber, "pong");
michael@0 104 };
michael@0 105 sendRequest.onerror = function onSendError(event) {
michael@0 106 ok(false, "failed to send message.");
michael@0 107 tasks.finish();
michael@0 108 };
michael@0 109 }
michael@0 110
michael@0 111 let manager = window.navigator.mozMobileMessage;
michael@0 112 tasks.push(function() {
michael@0 113 log("Verifying initial state.");
michael@0 114 ok(manager instanceof MozMobileMessageManager,
michael@0 115 "manager is instance of " + manager.constructor);
michael@0 116 tasks.next();
michael@0 117 });
michael@0 118
michael@0 119 tasks.push(deleteAllMessages);
michael@0 120
michael@0 121 tasks.push(validate.bind(null, "+886-9-87-654-321", "+886987654321"));
michael@0 122 tasks.push(validate.bind(null, "+886 9 87654321", "+886987654321"));
michael@0 123 tasks.push(validate.bind(null, "+886(9)87654321", "+886987654321"));
michael@0 124
michael@0 125 tasks.push(deleteAllMessages);
michael@0 126
michael@0 127 // WARNING: All tasks should be pushed before this!!!
michael@0 128 tasks.push(function cleanUp() {
michael@0 129 if (pendingEmulatorCmdCount) {
michael@0 130 window.setTimeout(cleanUp, 100);
michael@0 131 return;
michael@0 132 }
michael@0 133
michael@0 134 SpecialPowers.removePermission("sms", document);
michael@0 135 SpecialPowers.clearUserPref("dom.sms.enabled");
michael@0 136 finish();
michael@0 137 });
michael@0 138
michael@0 139 tasks.run();

mercurial