dom/mobilemessage/tests/marionette/test_getthreads.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 = 40000;
michael@0 5
michael@0 6 SpecialPowers.addPermission("sms", true, document);
michael@0 7 SpecialPowers.setBoolPref("dom.sms.enabled", true);
michael@0 8
michael@0 9 let manager = window.navigator.mozMobileMessage;
michael@0 10 ok(manager instanceof MozMobileMessageManager,
michael@0 11 "manager is instance of " + manager.constructor);
michael@0 12
michael@0 13 let pendingEmulatorCmdCount = 0;
michael@0 14 function sendSmsToEmulator(from, text, callback) {
michael@0 15 ++pendingEmulatorCmdCount;
michael@0 16
michael@0 17 let cmd = "sms send " + from + " " + text;
michael@0 18 runEmulatorCmd(cmd, function(result) {
michael@0 19 --pendingEmulatorCmdCount;
michael@0 20
michael@0 21 callback(result[0] == "OK");
michael@0 22 });
michael@0 23 }
michael@0 24
michael@0 25 let tasks = {
michael@0 26 // List of test fuctions. Each of them should call |tasks.next()| when
michael@0 27 // completed or |tasks.finish()| to jump to the last one.
michael@0 28 _tasks: [],
michael@0 29 _nextTaskIndex: 0,
michael@0 30
michael@0 31 push: function(func) {
michael@0 32 this._tasks.push(func);
michael@0 33 },
michael@0 34
michael@0 35 next: function() {
michael@0 36 let index = this._nextTaskIndex++;
michael@0 37 let task = this._tasks[index];
michael@0 38 try {
michael@0 39 task.apply(null, Array.slice(arguments));
michael@0 40 } catch (ex) {
michael@0 41 ok(false, "test task[" + index + "] throws: " + ex);
michael@0 42 // Run last task as clean up if possible.
michael@0 43 if (index != this._tasks.length - 1) {
michael@0 44 this.finish();
michael@0 45 }
michael@0 46 }
michael@0 47 },
michael@0 48
michael@0 49 finish: function() {
michael@0 50 this._tasks[this._tasks.length - 1]();
michael@0 51 },
michael@0 52
michael@0 53 run: function() {
michael@0 54 this.next();
michael@0 55 }
michael@0 56 };
michael@0 57
michael@0 58 function getAllMessages(callback, filter, reverse) {
michael@0 59 if (!filter) {
michael@0 60 filter = new MozSmsFilter;
michael@0 61 }
michael@0 62 let messages = [];
michael@0 63 let request = manager.getMessages(filter, reverse || false);
michael@0 64 request.onsuccess = function(event) {
michael@0 65 if (!request.done) {
michael@0 66 messages.push(request.result);
michael@0 67 request.continue();
michael@0 68 return;
michael@0 69 }
michael@0 70
michael@0 71 window.setTimeout(callback.bind(null, messages), 0);
michael@0 72 }
michael@0 73 }
michael@0 74
michael@0 75 function deleteAllMessages() {
michael@0 76 getAllMessages(function deleteAll(messages) {
michael@0 77 let message = messages.shift();
michael@0 78 if (!message) {
michael@0 79 ok(true, "all messages deleted");
michael@0 80 tasks.next();
michael@0 81 return;
michael@0 82 }
michael@0 83
michael@0 84 let request = manager.delete(message.id);
michael@0 85 request.onsuccess = deleteAll.bind(null, messages);
michael@0 86 request.onerror = function(event) {
michael@0 87 ok(false, "failed to delete all messages");
michael@0 88 tasks.finish();
michael@0 89 }
michael@0 90 });
michael@0 91 }
michael@0 92
michael@0 93 function sendMessage(to, body) {
michael@0 94 manager.onsent = function() {
michael@0 95 manager.onsent = null;
michael@0 96 tasks.next();
michael@0 97 };
michael@0 98 let request = manager.send(to, body);
michael@0 99 request.onerror = tasks.finish.bind(tasks);
michael@0 100 }
michael@0 101
michael@0 102 function receiveMessage(from, body) {
michael@0 103 manager.onreceived = function() {
michael@0 104 manager.onreceived = null;
michael@0 105 tasks.next();
michael@0 106 };
michael@0 107 sendSmsToEmulator(from, body, function(success) {
michael@0 108 if (!success) {
michael@0 109 tasks.finish();
michael@0 110 }
michael@0 111 });
michael@0 112 }
michael@0 113
michael@0 114 function getAllThreads(callback) {
michael@0 115 let threads = [];
michael@0 116
michael@0 117 let cursor = manager.getThreads();
michael@0 118 ok(cursor instanceof DOMCursor,
michael@0 119 "cursor is instanceof " + cursor.constructor);
michael@0 120
michael@0 121 cursor.onsuccess = function(event) {
michael@0 122 if (!cursor.done) {
michael@0 123 threads.push(cursor.result);
michael@0 124 cursor.continue();
michael@0 125 return;
michael@0 126 }
michael@0 127
michael@0 128 window.setTimeout(callback.bind(null, threads), 0);
michael@0 129 };
michael@0 130 }
michael@0 131
michael@0 132 function checkThread(bodies, lastBody, unreadCount, participants,
michael@0 133 thread, callback) {
michael@0 134 log("Validating MozMobileMessageThread attributes " +
michael@0 135 JSON.stringify([bodies, lastBody, unreadCount, participants]));
michael@0 136
michael@0 137 ok(thread, "current thread should be valid.");
michael@0 138
michael@0 139 ok(thread.id, "thread id", "thread.id");
michael@0 140 log("Got thread " + thread.id);
michael@0 141
michael@0 142 if (lastBody != null) {
michael@0 143 is(thread.body, lastBody, "thread.body");
michael@0 144 }
michael@0 145
michael@0 146 is(thread.unreadCount, unreadCount, "thread.unreadCount");
michael@0 147
michael@0 148 ok(Array.isArray(thread.participants), "thread.participants is array");
michael@0 149 is(thread.participants.length, participants.length,
michael@0 150 "thread.participants.length");
michael@0 151 for (let i = 0; i < participants.length; i++) {
michael@0 152 is(thread.participants[i], participants[i],
michael@0 153 "thread.participants[" + i + "]");
michael@0 154 }
michael@0 155
michael@0 156 // Check whether the thread does contain all the messages it supposed to have.
michael@0 157 let filter = new MozSmsFilter;
michael@0 158 filter.threadId = thread.id;
michael@0 159 getAllMessages(function(messages) {
michael@0 160 is(messages.length, bodies.length, "messages.length and bodies.length");
michael@0 161
michael@0 162 for (let message of messages) {
michael@0 163 let index = bodies.indexOf(message.body);
michael@0 164 ok(index >= 0, "message.body '" + message.body +
michael@0 165 "' should be found in bodies array.");
michael@0 166 bodies.splice(index, 1);
michael@0 167 }
michael@0 168
michael@0 169 is(bodies.length, 0, "bodies array length");
michael@0 170
michael@0 171 window.setTimeout(callback, 0);
michael@0 172 }, filter, false);
michael@0 173 }
michael@0 174
michael@0 175 tasks.push(deleteAllMessages);
michael@0 176
michael@0 177 tasks.push(getAllThreads.bind(null, function(threads) {
michael@0 178 is(threads.length, 0, "Empty thread list at beginning.");
michael@0 179 tasks.next();
michael@0 180 }));
michael@0 181
michael@0 182 // Populate MobileMessageDB with messages.
michael@0 183 let checkFuncs = [];
michael@0 184
michael@0 185 // [Thread 1]
michael@0 186 // One message only, body = "thread 1";
michael@0 187 // All sent message, unreadCount = 0;
michael@0 188 // One participant only, participants = ["5555211001"].
michael@0 189 tasks.push(sendMessage.bind(null, "5555211001", "thread 1"));
michael@0 190 checkFuncs.push(checkThread.bind(null, ["thread 1"],
michael@0 191 "thread 1", 0, ["5555211001"]));
michael@0 192
michael@0 193 // [Thread 2]
michael@0 194 // Two messages, body = "thread 2-2";
michael@0 195 // All sent message, unreadCount = 0;
michael@0 196 // One participant with two aliased addresses, participants = ["5555211002"].
michael@0 197 tasks.push(sendMessage.bind(null, "5555211002", "thread 2-1"));
michael@0 198 tasks.push(sendMessage.bind(null, "+15555211002", "thread 2-2"));
michael@0 199 checkFuncs.push(checkThread.bind(null, ["thread 2-1", "thread 2-2"],
michael@0 200 "thread 2-2", 0, ["5555211002"]));
michael@0 201
michael@0 202 // [Thread 3]
michael@0 203 // Two messages, body = "thread 3-2";
michael@0 204 // All sent message, unreadCount = 0;
michael@0 205 // One participant with two aliased addresses, participants = ["+15555211003"].
michael@0 206 tasks.push(sendMessage.bind(null, "+15555211003", "thread 3-1"));
michael@0 207 tasks.push(sendMessage.bind(null, "5555211003", "thread 3-2"));
michael@0 208 checkFuncs.push(checkThread.bind(null, ["thread 3-1", "thread 3-2"],
michael@0 209 "thread 3-2", 0, ["+15555211003"]));
michael@0 210
michael@0 211 // [Thread 4]
michael@0 212 // One message only, body = "thread 4";
michael@0 213 // All received message, unreadCount = 1;
michael@0 214 // One participant only, participants = ["5555211004"].
michael@0 215 tasks.push(receiveMessage.bind(null, "5555211004", "thread 4"));
michael@0 216 checkFuncs.push(checkThread.bind(null, ["thread 4"],
michael@0 217 "thread 4", 1, ["5555211004"]));
michael@0 218
michael@0 219 // [Thread 5]
michael@0 220 //
michael@0 221 // Thread body should be set to text body of the last message in this
michael@0 222 // thread. However due to BUG 840051, we're having SMSC time as message
michael@0 223 // timestamp and SMSC time resolution is 1 second. So it's likely that the two
michael@0 224 // messages have the same timestamp and we just can't tell which is the later
michael@0 225 // one.
michael@0 226 //
michael@0 227 // All received message, unreadCount = 2;
michael@0 228 // One participant with two aliased addresses, participants = ["5555211005"].
michael@0 229 tasks.push(receiveMessage.bind(null, "5555211005", "thread 5-1"));
michael@0 230 tasks.push(receiveMessage.bind(null, "+15555211005", "thread 5-2"));
michael@0 231 checkFuncs.push(checkThread.bind(null, ["thread 5-1", "thread 5-2"],
michael@0 232 null, 2, ["5555211005"]));
michael@0 233
michael@0 234 // [Thread 6]
michael@0 235 //
michael@0 236 // Thread body should be set to text body of the last message in this
michael@0 237 // thread. However due to BUG 840051, we're having SMSC time as message
michael@0 238 // timestamp and SMSC time resolution is 1 second. So it's likely that the two
michael@0 239 // messages have the same timestamp and we just can't tell which is the later
michael@0 240 // one.
michael@0 241 //
michael@0 242 // All received message, unreadCount = 2;
michael@0 243 // One participant with two aliased addresses, participants = ["+15555211006"].
michael@0 244 tasks.push(receiveMessage.bind(null, "+15555211006", "thread 6-1"));
michael@0 245 tasks.push(receiveMessage.bind(null, "5555211006", "thread 6-2"));
michael@0 246 checkFuncs.push(checkThread.bind(null, ["thread 6-1", "thread 6-2"],
michael@0 247 null, 2, ["+15555211006"]));
michael@0 248
michael@0 249 // [Thread 7]
michael@0 250 //
michael@0 251 // Thread body should be set to text body of the last message in this
michael@0 252 // thread. However due to BUG 840051, there might be time difference between
michael@0 253 // SMSC and device time. So the result of comparing the timestamps of sent and
michael@0 254 // received message may not follow the order of requests and may result in
michael@0 255 // UNEXPECTED-FAIL in following tests.
michael@0 256 //
michael@0 257 // Two received message, unreadCount = 2;
michael@0 258 // One participant with two aliased addresses, participants = ["5555211007"].
michael@0 259 tasks.push(sendMessage.bind(null, "5555211007", "thread 7-1"));
michael@0 260 tasks.push(sendMessage.bind(null, "+15555211007", "thread 7-2"));
michael@0 261 tasks.push(receiveMessage.bind(null, "5555211007", "thread 7-3"));
michael@0 262 tasks.push(receiveMessage.bind(null, "+15555211007", "thread 7-4"));
michael@0 263 checkFuncs.push(checkThread.bind(null, ["thread 7-1", "thread 7-2",
michael@0 264 "thread 7-3", "thread 7-4"],
michael@0 265 null, 2, ["5555211007"]));
michael@0 266
michael@0 267 // [Thread 8]
michael@0 268 //
michael@0 269 // Thread body should be set to text body of the last message in this
michael@0 270 // thread. However due to BUG 840051, there might be time difference between
michael@0 271 // SMSC and device time. So the result of comparing the timestamps of sent and
michael@0 272 // received message may not follow the order of requests and may result in
michael@0 273 // UNEXPECTED-FAIL in following tests.
michael@0 274 //
michael@0 275 // Two received message, unreadCount = 2;
michael@0 276 // One participant with two aliased addresses, participants = ["5555211008"].
michael@0 277 tasks.push(receiveMessage.bind(null, "5555211008", "thread 8-1"));
michael@0 278 tasks.push(receiveMessage.bind(null, "+15555211008", "thread 8-2"));
michael@0 279 tasks.push(sendMessage.bind(null, "5555211008", "thread 8-3"));
michael@0 280 tasks.push(sendMessage.bind(null, "+15555211008", "thread 8-4"));
michael@0 281 checkFuncs.push(checkThread.bind(null, ["thread 8-1", "thread 8-2",
michael@0 282 "thread 8-3", "thread 8-4"],
michael@0 283 null, 2, ["5555211008"]));
michael@0 284
michael@0 285 // [Thread 9]
michael@0 286 // Three sent message, unreadCount = 0;
michael@0 287 // One participant with three aliased addresses, participants = ["+15555211009"].
michael@0 288 tasks.push(sendMessage.bind(null, "+15555211009", "thread 9-1"));
michael@0 289 tasks.push(sendMessage.bind(null, "01115555211009", "thread 9-2"));
michael@0 290 tasks.push(sendMessage.bind(null, "5555211009", "thread 9-3"));
michael@0 291 checkFuncs.push(checkThread.bind(null, ["thread 9-1", "thread 9-2",
michael@0 292 "thread 9-3"],
michael@0 293 "thread 9-3", 0, ["+15555211009"]));
michael@0 294
michael@0 295 // [Thread 10]
michael@0 296 // Three sent message, unreadCount = 0;
michael@0 297 // One participant with three aliased addresses, participants = ["+15555211010"].
michael@0 298 tasks.push(sendMessage.bind(null, "+15555211010", "thread 10-1"));
michael@0 299 tasks.push(sendMessage.bind(null, "5555211010", "thread 10-2"));
michael@0 300 tasks.push(sendMessage.bind(null, "01115555211010", "thread 10-3"));
michael@0 301 checkFuncs.push(checkThread.bind(null, ["thread 10-1", "thread 10-2",
michael@0 302 "thread 10-3"],
michael@0 303 "thread 10-3", 0, ["+15555211010"]));
michael@0 304
michael@0 305 // [Thread 11]
michael@0 306 // Three sent message, unreadCount = 0;
michael@0 307 // One participant with three aliased addresses, participants = ["01115555211011"].
michael@0 308 tasks.push(sendMessage.bind(null, "01115555211011", "thread 11-1"));
michael@0 309 tasks.push(sendMessage.bind(null, "5555211011", "thread 11-2"));
michael@0 310 tasks.push(sendMessage.bind(null, "+15555211011", "thread 11-3"));
michael@0 311 checkFuncs.push(checkThread.bind(null, ["thread 11-1", "thread 11-2",
michael@0 312 "thread 11-3"],
michael@0 313 "thread 11-3", 0, ["01115555211011"]));
michael@0 314
michael@0 315 // [Thread 12]
michael@0 316 // Three sent message, unreadCount = 0;
michael@0 317 // One participant with three aliased addresses, participants = ["01115555211012"].
michael@0 318 tasks.push(sendMessage.bind(null, "01115555211012", "thread 12-1"));
michael@0 319 tasks.push(sendMessage.bind(null, "+15555211012", "thread 12-2"));
michael@0 320 tasks.push(sendMessage.bind(null, "5555211012", "thread 12-3"));
michael@0 321 checkFuncs.push(checkThread.bind(null, ["thread 12-1", "thread 12-2",
michael@0 322 "thread 12-3"],
michael@0 323 "thread 12-3", 0, ["01115555211012"]));
michael@0 324
michael@0 325 // [Thread 13]
michael@0 326 // Three sent message, unreadCount = 0;
michael@0 327 // One participant with three aliased addresses, participants = ["5555211013"].
michael@0 328 tasks.push(sendMessage.bind(null, "5555211013", "thread 13-1"));
michael@0 329 tasks.push(sendMessage.bind(null, "+15555211013", "thread 13-2"));
michael@0 330 tasks.push(sendMessage.bind(null, "01115555211013", "thread 13-3"));
michael@0 331 checkFuncs.push(checkThread.bind(null, ["thread 13-1", "thread 13-2",
michael@0 332 "thread 13-3"],
michael@0 333 "thread 13-3", 0, ["5555211013"]));
michael@0 334
michael@0 335 // [Thread 14]
michael@0 336 // Three sent message, unreadCount = 0;
michael@0 337 // One participant with three aliased addresses, participants = ["5555211014"].
michael@0 338 tasks.push(sendMessage.bind(null, "5555211014", "thread 14-1"));
michael@0 339 tasks.push(sendMessage.bind(null, "01115555211014", "thread 14-2"));
michael@0 340 tasks.push(sendMessage.bind(null, "+15555211014", "thread 14-3"));
michael@0 341 checkFuncs.push(checkThread.bind(null, ["thread 14-1", "thread 14-2",
michael@0 342 "thread 14-3"],
michael@0 343 "thread 14-3", 0, ["5555211014"]));
michael@0 344
michael@0 345 //[Thread 15]
michael@0 346 //Three sent message, unreadCount = 0;
michael@0 347 //One participant but might be merged to 555211015, participants = ["5555211015"].
michael@0 348 tasks.push(sendMessage.bind(null, "5555211015", "thread 15-1"));
michael@0 349 checkFuncs.push(checkThread.bind(null, ["thread 15-1"],
michael@0 350 "thread 15-1", 0, ["5555211015"]));
michael@0 351
michael@0 352 //[Thread 16]
michael@0 353 //Three sent message, unreadCount = 0;
michael@0 354 //One participant but might be merged to 5555211015, participants = ["555211015"].
michael@0 355 tasks.push(sendMessage.bind(null, "555211015", "thread 16-1"));
michael@0 356 checkFuncs.push(checkThread.bind(null, ["thread 16-1"],
michael@0 357 "thread 16-1", 0, ["555211015"]));
michael@0 358
michael@0 359 //[Thread 17]
michael@0 360 //Three sent message, unreadCount = 0;
michael@0 361 //One participant with two aliased addresses, participants = ["555211017"].
michael@0 362 tasks.push(sendMessage.bind(null, "+5511555211017", "thread 17-1"));
michael@0 363 tasks.push(sendMessage.bind(null, "555211017", "thread 17-2"));
michael@0 364 checkFuncs.push(checkThread.bind(null, ["thread 17-1", "thread 17-2"],
michael@0 365 "thread 17-2", 0, ["+5511555211017"]));
michael@0 366
michael@0 367 //[Thread 18]
michael@0 368 //Three sent message, unreadCount = 0;
michael@0 369 //One participant with two aliased addresses, participants = ["555211018"].
michael@0 370 tasks.push(sendMessage.bind(null, "555211018", "thread 18-1"));
michael@0 371 tasks.push(sendMessage.bind(null, "+5511555211018", "thread 18-2"));
michael@0 372 checkFuncs.push(checkThread.bind(null, ["thread 18-1", "thread 18-2"],
michael@0 373 "thread 18-2", 0, ["555211018"]));
michael@0 374
michael@0 375 // Check threads.
michael@0 376 tasks.push(getAllThreads.bind(null, function(threads) {
michael@0 377 is(threads.length, checkFuncs.length, "number of threads got");
michael@0 378
michael@0 379 // Reverse threads as we iterate over them in reverse order
michael@0 380 threads.reverse();
michael@0 381
michael@0 382 (function callback() {
michael@0 383 if (!threads.length) {
michael@0 384 tasks.next();
michael@0 385 return;
michael@0 386 }
michael@0 387
michael@0 388 checkFuncs.shift()(threads.shift(), callback);
michael@0 389 })();
michael@0 390 }));
michael@0 391
michael@0 392 tasks.push(deleteAllMessages);
michael@0 393
michael@0 394 tasks.push(getAllThreads.bind(null, function(threads) {
michael@0 395 is(threads.length, 0, "Empty thread list at the end.");
michael@0 396 tasks.next();
michael@0 397 }));
michael@0 398
michael@0 399 // WARNING: All tasks should be pushed before this!!!
michael@0 400 tasks.push(function cleanUp() {
michael@0 401 if (pendingEmulatorCmdCount) {
michael@0 402 window.setTimeout(cleanUp, 100);
michael@0 403 return;
michael@0 404 }
michael@0 405
michael@0 406 SpecialPowers.removePermission("sms", document);
michael@0 407 SpecialPowers.clearUserPref("dom.sms.enabled");
michael@0 408 finish();
michael@0 409 });
michael@0 410
michael@0 411 tasks.run();

mercurial