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