1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/mobilemessage/tests/marionette/test_getthreads.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,411 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + * http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +MARIONETTE_TIMEOUT = 40000; 1.8 + 1.9 +SpecialPowers.addPermission("sms", true, document); 1.10 +SpecialPowers.setBoolPref("dom.sms.enabled", true); 1.11 + 1.12 +let manager = window.navigator.mozMobileMessage; 1.13 +ok(manager instanceof MozMobileMessageManager, 1.14 + "manager is instance of " + manager.constructor); 1.15 + 1.16 +let pendingEmulatorCmdCount = 0; 1.17 +function sendSmsToEmulator(from, text, callback) { 1.18 + ++pendingEmulatorCmdCount; 1.19 + 1.20 + let cmd = "sms send " + from + " " + text; 1.21 + runEmulatorCmd(cmd, function(result) { 1.22 + --pendingEmulatorCmdCount; 1.23 + 1.24 + callback(result[0] == "OK"); 1.25 + }); 1.26 +} 1.27 + 1.28 +let tasks = { 1.29 + // List of test fuctions. Each of them should call |tasks.next()| when 1.30 + // completed or |tasks.finish()| to jump to the last one. 1.31 + _tasks: [], 1.32 + _nextTaskIndex: 0, 1.33 + 1.34 + push: function(func) { 1.35 + this._tasks.push(func); 1.36 + }, 1.37 + 1.38 + next: function() { 1.39 + let index = this._nextTaskIndex++; 1.40 + let task = this._tasks[index]; 1.41 + try { 1.42 + task.apply(null, Array.slice(arguments)); 1.43 + } catch (ex) { 1.44 + ok(false, "test task[" + index + "] throws: " + ex); 1.45 + // Run last task as clean up if possible. 1.46 + if (index != this._tasks.length - 1) { 1.47 + this.finish(); 1.48 + } 1.49 + } 1.50 + }, 1.51 + 1.52 + finish: function() { 1.53 + this._tasks[this._tasks.length - 1](); 1.54 + }, 1.55 + 1.56 + run: function() { 1.57 + this.next(); 1.58 + } 1.59 +}; 1.60 + 1.61 +function getAllMessages(callback, filter, reverse) { 1.62 + if (!filter) { 1.63 + filter = new MozSmsFilter; 1.64 + } 1.65 + let messages = []; 1.66 + let request = manager.getMessages(filter, reverse || false); 1.67 + request.onsuccess = function(event) { 1.68 + if (!request.done) { 1.69 + messages.push(request.result); 1.70 + request.continue(); 1.71 + return; 1.72 + } 1.73 + 1.74 + window.setTimeout(callback.bind(null, messages), 0); 1.75 + } 1.76 +} 1.77 + 1.78 +function deleteAllMessages() { 1.79 + getAllMessages(function deleteAll(messages) { 1.80 + let message = messages.shift(); 1.81 + if (!message) { 1.82 + ok(true, "all messages deleted"); 1.83 + tasks.next(); 1.84 + return; 1.85 + } 1.86 + 1.87 + let request = manager.delete(message.id); 1.88 + request.onsuccess = deleteAll.bind(null, messages); 1.89 + request.onerror = function(event) { 1.90 + ok(false, "failed to delete all messages"); 1.91 + tasks.finish(); 1.92 + } 1.93 + }); 1.94 +} 1.95 + 1.96 +function sendMessage(to, body) { 1.97 + manager.onsent = function() { 1.98 + manager.onsent = null; 1.99 + tasks.next(); 1.100 + }; 1.101 + let request = manager.send(to, body); 1.102 + request.onerror = tasks.finish.bind(tasks); 1.103 +} 1.104 + 1.105 +function receiveMessage(from, body) { 1.106 + manager.onreceived = function() { 1.107 + manager.onreceived = null; 1.108 + tasks.next(); 1.109 + }; 1.110 + sendSmsToEmulator(from, body, function(success) { 1.111 + if (!success) { 1.112 + tasks.finish(); 1.113 + } 1.114 + }); 1.115 +} 1.116 + 1.117 +function getAllThreads(callback) { 1.118 + let threads = []; 1.119 + 1.120 + let cursor = manager.getThreads(); 1.121 + ok(cursor instanceof DOMCursor, 1.122 + "cursor is instanceof " + cursor.constructor); 1.123 + 1.124 + cursor.onsuccess = function(event) { 1.125 + if (!cursor.done) { 1.126 + threads.push(cursor.result); 1.127 + cursor.continue(); 1.128 + return; 1.129 + } 1.130 + 1.131 + window.setTimeout(callback.bind(null, threads), 0); 1.132 + }; 1.133 +} 1.134 + 1.135 +function checkThread(bodies, lastBody, unreadCount, participants, 1.136 + thread, callback) { 1.137 + log("Validating MozMobileMessageThread attributes " + 1.138 + JSON.stringify([bodies, lastBody, unreadCount, participants])); 1.139 + 1.140 + ok(thread, "current thread should be valid."); 1.141 + 1.142 + ok(thread.id, "thread id", "thread.id"); 1.143 + log("Got thread " + thread.id); 1.144 + 1.145 + if (lastBody != null) { 1.146 + is(thread.body, lastBody, "thread.body"); 1.147 + } 1.148 + 1.149 + is(thread.unreadCount, unreadCount, "thread.unreadCount"); 1.150 + 1.151 + ok(Array.isArray(thread.participants), "thread.participants is array"); 1.152 + is(thread.participants.length, participants.length, 1.153 + "thread.participants.length"); 1.154 + for (let i = 0; i < participants.length; i++) { 1.155 + is(thread.participants[i], participants[i], 1.156 + "thread.participants[" + i + "]"); 1.157 + } 1.158 + 1.159 + // Check whether the thread does contain all the messages it supposed to have. 1.160 + let filter = new MozSmsFilter; 1.161 + filter.threadId = thread.id; 1.162 + getAllMessages(function(messages) { 1.163 + is(messages.length, bodies.length, "messages.length and bodies.length"); 1.164 + 1.165 + for (let message of messages) { 1.166 + let index = bodies.indexOf(message.body); 1.167 + ok(index >= 0, "message.body '" + message.body + 1.168 + "' should be found in bodies array."); 1.169 + bodies.splice(index, 1); 1.170 + } 1.171 + 1.172 + is(bodies.length, 0, "bodies array length"); 1.173 + 1.174 + window.setTimeout(callback, 0); 1.175 + }, filter, false); 1.176 +} 1.177 + 1.178 +tasks.push(deleteAllMessages); 1.179 + 1.180 +tasks.push(getAllThreads.bind(null, function(threads) { 1.181 + is(threads.length, 0, "Empty thread list at beginning."); 1.182 + tasks.next(); 1.183 +})); 1.184 + 1.185 +// Populate MobileMessageDB with messages. 1.186 +let checkFuncs = []; 1.187 + 1.188 +// [Thread 1] 1.189 +// One message only, body = "thread 1"; 1.190 +// All sent message, unreadCount = 0; 1.191 +// One participant only, participants = ["5555211001"]. 1.192 +tasks.push(sendMessage.bind(null, "5555211001", "thread 1")); 1.193 +checkFuncs.push(checkThread.bind(null, ["thread 1"], 1.194 + "thread 1", 0, ["5555211001"])); 1.195 + 1.196 +// [Thread 2] 1.197 +// Two messages, body = "thread 2-2"; 1.198 +// All sent message, unreadCount = 0; 1.199 +// One participant with two aliased addresses, participants = ["5555211002"]. 1.200 +tasks.push(sendMessage.bind(null, "5555211002", "thread 2-1")); 1.201 +tasks.push(sendMessage.bind(null, "+15555211002", "thread 2-2")); 1.202 +checkFuncs.push(checkThread.bind(null, ["thread 2-1", "thread 2-2"], 1.203 + "thread 2-2", 0, ["5555211002"])); 1.204 + 1.205 +// [Thread 3] 1.206 +// Two messages, body = "thread 3-2"; 1.207 +// All sent message, unreadCount = 0; 1.208 +// One participant with two aliased addresses, participants = ["+15555211003"]. 1.209 +tasks.push(sendMessage.bind(null, "+15555211003", "thread 3-1")); 1.210 +tasks.push(sendMessage.bind(null, "5555211003", "thread 3-2")); 1.211 +checkFuncs.push(checkThread.bind(null, ["thread 3-1", "thread 3-2"], 1.212 + "thread 3-2", 0, ["+15555211003"])); 1.213 + 1.214 +// [Thread 4] 1.215 +// One message only, body = "thread 4"; 1.216 +// All received message, unreadCount = 1; 1.217 +// One participant only, participants = ["5555211004"]. 1.218 +tasks.push(receiveMessage.bind(null, "5555211004", "thread 4")); 1.219 +checkFuncs.push(checkThread.bind(null, ["thread 4"], 1.220 + "thread 4", 1, ["5555211004"])); 1.221 + 1.222 +// [Thread 5] 1.223 +// 1.224 +// Thread body should be set to text body of the last message in this 1.225 +// thread. However due to BUG 840051, we're having SMSC time as message 1.226 +// timestamp and SMSC time resolution is 1 second. So it's likely that the two 1.227 +// messages have the same timestamp and we just can't tell which is the later 1.228 +// one. 1.229 +// 1.230 +// All received message, unreadCount = 2; 1.231 +// One participant with two aliased addresses, participants = ["5555211005"]. 1.232 +tasks.push(receiveMessage.bind(null, "5555211005", "thread 5-1")); 1.233 +tasks.push(receiveMessage.bind(null, "+15555211005", "thread 5-2")); 1.234 +checkFuncs.push(checkThread.bind(null, ["thread 5-1", "thread 5-2"], 1.235 + null, 2, ["5555211005"])); 1.236 + 1.237 +// [Thread 6] 1.238 +// 1.239 +// Thread body should be set to text body of the last message in this 1.240 +// thread. However due to BUG 840051, we're having SMSC time as message 1.241 +// timestamp and SMSC time resolution is 1 second. So it's likely that the two 1.242 +// messages have the same timestamp and we just can't tell which is the later 1.243 +// one. 1.244 +// 1.245 +// All received message, unreadCount = 2; 1.246 +// One participant with two aliased addresses, participants = ["+15555211006"]. 1.247 +tasks.push(receiveMessage.bind(null, "+15555211006", "thread 6-1")); 1.248 +tasks.push(receiveMessage.bind(null, "5555211006", "thread 6-2")); 1.249 +checkFuncs.push(checkThread.bind(null, ["thread 6-1", "thread 6-2"], 1.250 + null, 2, ["+15555211006"])); 1.251 + 1.252 +// [Thread 7] 1.253 +// 1.254 +// Thread body should be set to text body of the last message in this 1.255 +// thread. However due to BUG 840051, there might be time difference between 1.256 +// SMSC and device time. So the result of comparing the timestamps of sent and 1.257 +// received message may not follow the order of requests and may result in 1.258 +// UNEXPECTED-FAIL in following tests. 1.259 +// 1.260 +// Two received message, unreadCount = 2; 1.261 +// One participant with two aliased addresses, participants = ["5555211007"]. 1.262 +tasks.push(sendMessage.bind(null, "5555211007", "thread 7-1")); 1.263 +tasks.push(sendMessage.bind(null, "+15555211007", "thread 7-2")); 1.264 +tasks.push(receiveMessage.bind(null, "5555211007", "thread 7-3")); 1.265 +tasks.push(receiveMessage.bind(null, "+15555211007", "thread 7-4")); 1.266 +checkFuncs.push(checkThread.bind(null, ["thread 7-1", "thread 7-2", 1.267 + "thread 7-3", "thread 7-4"], 1.268 + null, 2, ["5555211007"])); 1.269 + 1.270 +// [Thread 8] 1.271 +// 1.272 +// Thread body should be set to text body of the last message in this 1.273 +// thread. However due to BUG 840051, there might be time difference between 1.274 +// SMSC and device time. So the result of comparing the timestamps of sent and 1.275 +// received message may not follow the order of requests and may result in 1.276 +// UNEXPECTED-FAIL in following tests. 1.277 +// 1.278 +// Two received message, unreadCount = 2; 1.279 +// One participant with two aliased addresses, participants = ["5555211008"]. 1.280 +tasks.push(receiveMessage.bind(null, "5555211008", "thread 8-1")); 1.281 +tasks.push(receiveMessage.bind(null, "+15555211008", "thread 8-2")); 1.282 +tasks.push(sendMessage.bind(null, "5555211008", "thread 8-3")); 1.283 +tasks.push(sendMessage.bind(null, "+15555211008", "thread 8-4")); 1.284 +checkFuncs.push(checkThread.bind(null, ["thread 8-1", "thread 8-2", 1.285 + "thread 8-3", "thread 8-4"], 1.286 + null, 2, ["5555211008"])); 1.287 + 1.288 +// [Thread 9] 1.289 +// Three sent message, unreadCount = 0; 1.290 +// One participant with three aliased addresses, participants = ["+15555211009"]. 1.291 +tasks.push(sendMessage.bind(null, "+15555211009", "thread 9-1")); 1.292 +tasks.push(sendMessage.bind(null, "01115555211009", "thread 9-2")); 1.293 +tasks.push(sendMessage.bind(null, "5555211009", "thread 9-3")); 1.294 +checkFuncs.push(checkThread.bind(null, ["thread 9-1", "thread 9-2", 1.295 + "thread 9-3"], 1.296 + "thread 9-3", 0, ["+15555211009"])); 1.297 + 1.298 +// [Thread 10] 1.299 +// Three sent message, unreadCount = 0; 1.300 +// One participant with three aliased addresses, participants = ["+15555211010"]. 1.301 +tasks.push(sendMessage.bind(null, "+15555211010", "thread 10-1")); 1.302 +tasks.push(sendMessage.bind(null, "5555211010", "thread 10-2")); 1.303 +tasks.push(sendMessage.bind(null, "01115555211010", "thread 10-3")); 1.304 +checkFuncs.push(checkThread.bind(null, ["thread 10-1", "thread 10-2", 1.305 + "thread 10-3"], 1.306 + "thread 10-3", 0, ["+15555211010"])); 1.307 + 1.308 +// [Thread 11] 1.309 +// Three sent message, unreadCount = 0; 1.310 +// One participant with three aliased addresses, participants = ["01115555211011"]. 1.311 +tasks.push(sendMessage.bind(null, "01115555211011", "thread 11-1")); 1.312 +tasks.push(sendMessage.bind(null, "5555211011", "thread 11-2")); 1.313 +tasks.push(sendMessage.bind(null, "+15555211011", "thread 11-3")); 1.314 +checkFuncs.push(checkThread.bind(null, ["thread 11-1", "thread 11-2", 1.315 + "thread 11-3"], 1.316 + "thread 11-3", 0, ["01115555211011"])); 1.317 + 1.318 +// [Thread 12] 1.319 +// Three sent message, unreadCount = 0; 1.320 +// One participant with three aliased addresses, participants = ["01115555211012"]. 1.321 +tasks.push(sendMessage.bind(null, "01115555211012", "thread 12-1")); 1.322 +tasks.push(sendMessage.bind(null, "+15555211012", "thread 12-2")); 1.323 +tasks.push(sendMessage.bind(null, "5555211012", "thread 12-3")); 1.324 +checkFuncs.push(checkThread.bind(null, ["thread 12-1", "thread 12-2", 1.325 + "thread 12-3"], 1.326 + "thread 12-3", 0, ["01115555211012"])); 1.327 + 1.328 +// [Thread 13] 1.329 +// Three sent message, unreadCount = 0; 1.330 +// One participant with three aliased addresses, participants = ["5555211013"]. 1.331 +tasks.push(sendMessage.bind(null, "5555211013", "thread 13-1")); 1.332 +tasks.push(sendMessage.bind(null, "+15555211013", "thread 13-2")); 1.333 +tasks.push(sendMessage.bind(null, "01115555211013", "thread 13-3")); 1.334 +checkFuncs.push(checkThread.bind(null, ["thread 13-1", "thread 13-2", 1.335 + "thread 13-3"], 1.336 + "thread 13-3", 0, ["5555211013"])); 1.337 + 1.338 +// [Thread 14] 1.339 +// Three sent message, unreadCount = 0; 1.340 +// One participant with three aliased addresses, participants = ["5555211014"]. 1.341 +tasks.push(sendMessage.bind(null, "5555211014", "thread 14-1")); 1.342 +tasks.push(sendMessage.bind(null, "01115555211014", "thread 14-2")); 1.343 +tasks.push(sendMessage.bind(null, "+15555211014", "thread 14-3")); 1.344 +checkFuncs.push(checkThread.bind(null, ["thread 14-1", "thread 14-2", 1.345 + "thread 14-3"], 1.346 + "thread 14-3", 0, ["5555211014"])); 1.347 + 1.348 +//[Thread 15] 1.349 +//Three sent message, unreadCount = 0; 1.350 +//One participant but might be merged to 555211015, participants = ["5555211015"]. 1.351 +tasks.push(sendMessage.bind(null, "5555211015", "thread 15-1")); 1.352 +checkFuncs.push(checkThread.bind(null, ["thread 15-1"], 1.353 + "thread 15-1", 0, ["5555211015"])); 1.354 + 1.355 +//[Thread 16] 1.356 +//Three sent message, unreadCount = 0; 1.357 +//One participant but might be merged to 5555211015, participants = ["555211015"]. 1.358 +tasks.push(sendMessage.bind(null, "555211015", "thread 16-1")); 1.359 +checkFuncs.push(checkThread.bind(null, ["thread 16-1"], 1.360 + "thread 16-1", 0, ["555211015"])); 1.361 + 1.362 +//[Thread 17] 1.363 +//Three sent message, unreadCount = 0; 1.364 +//One participant with two aliased addresses, participants = ["555211017"]. 1.365 +tasks.push(sendMessage.bind(null, "+5511555211017", "thread 17-1")); 1.366 +tasks.push(sendMessage.bind(null, "555211017", "thread 17-2")); 1.367 +checkFuncs.push(checkThread.bind(null, ["thread 17-1", "thread 17-2"], 1.368 + "thread 17-2", 0, ["+5511555211017"])); 1.369 + 1.370 +//[Thread 18] 1.371 +//Three sent message, unreadCount = 0; 1.372 +//One participant with two aliased addresses, participants = ["555211018"]. 1.373 +tasks.push(sendMessage.bind(null, "555211018", "thread 18-1")); 1.374 +tasks.push(sendMessage.bind(null, "+5511555211018", "thread 18-2")); 1.375 +checkFuncs.push(checkThread.bind(null, ["thread 18-1", "thread 18-2"], 1.376 + "thread 18-2", 0, ["555211018"])); 1.377 + 1.378 +// Check threads. 1.379 +tasks.push(getAllThreads.bind(null, function(threads) { 1.380 + is(threads.length, checkFuncs.length, "number of threads got"); 1.381 + 1.382 + // Reverse threads as we iterate over them in reverse order 1.383 + threads.reverse(); 1.384 + 1.385 + (function callback() { 1.386 + if (!threads.length) { 1.387 + tasks.next(); 1.388 + return; 1.389 + } 1.390 + 1.391 + checkFuncs.shift()(threads.shift(), callback); 1.392 + })(); 1.393 +})); 1.394 + 1.395 +tasks.push(deleteAllMessages); 1.396 + 1.397 +tasks.push(getAllThreads.bind(null, function(threads) { 1.398 + is(threads.length, 0, "Empty thread list at the end."); 1.399 + tasks.next(); 1.400 +})); 1.401 + 1.402 +// WARNING: All tasks should be pushed before this!!! 1.403 +tasks.push(function cleanUp() { 1.404 + if (pendingEmulatorCmdCount) { 1.405 + window.setTimeout(cleanUp, 100); 1.406 + return; 1.407 + } 1.408 + 1.409 + SpecialPowers.removePermission("sms", document); 1.410 + SpecialPowers.clearUserPref("dom.sms.enabled"); 1.411 + finish(); 1.412 +}); 1.413 + 1.414 +tasks.run();