|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 * http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
3 |
|
4 MARIONETTE_TIMEOUT = 60000; |
|
5 |
|
6 const MMS_MAX_LENGTH_SUBJECT = 40; |
|
7 |
|
8 SpecialPowers.addPermission("sms", true, document); |
|
9 SpecialPowers.setBoolPref("dom.sms.enabled", true); |
|
10 |
|
11 let tasks = { |
|
12 // List of test fuctions. Each of them should call |tasks.next()| when |
|
13 // completed or |tasks.finish()| to jump to the last one. |
|
14 _tasks: [], |
|
15 _nextTaskIndex: 0, |
|
16 |
|
17 push: function(func) { |
|
18 this._tasks.push(func); |
|
19 }, |
|
20 |
|
21 next: function() { |
|
22 let index = this._nextTaskIndex++; |
|
23 let task = this._tasks[index]; |
|
24 try { |
|
25 task(); |
|
26 } catch (ex) { |
|
27 ok(false, "test task[" + index + "] throws: " + ex); |
|
28 // Run last task as clean up if possible. |
|
29 if (index != this._tasks.length - 1) { |
|
30 this.finish(); |
|
31 } |
|
32 } |
|
33 }, |
|
34 |
|
35 finish: function() { |
|
36 this._tasks[this._tasks.length - 1](); |
|
37 }, |
|
38 |
|
39 run: function() { |
|
40 this.next(); |
|
41 } |
|
42 }; |
|
43 |
|
44 let manager; |
|
45 |
|
46 function getAllMessages(callback, filter, reverse) { |
|
47 if (!filter) { |
|
48 filter = new MozSmsFilter; |
|
49 } |
|
50 let messages = []; |
|
51 let request = manager.getMessages(filter, reverse || false); |
|
52 request.onsuccess = function(event) { |
|
53 if (request.result) { |
|
54 messages.push(request.result); |
|
55 request.continue(); |
|
56 return; |
|
57 } |
|
58 |
|
59 window.setTimeout(callback.bind(null, messages), 0); |
|
60 } |
|
61 } |
|
62 |
|
63 function deleteAllMessages() { |
|
64 getAllMessages(function deleteAll(messages) { |
|
65 let message = messages.shift(); |
|
66 if (!message) { |
|
67 ok(true, "all messages deleted"); |
|
68 tasks.next(); |
|
69 return; |
|
70 } |
|
71 |
|
72 let request = manager.delete(message.id); |
|
73 request.onsuccess = deleteAll.bind(null, messages); |
|
74 request.onerror = function(event) { |
|
75 ok(false, "failed to delete all messages"); |
|
76 tasks.finish(); |
|
77 } |
|
78 }); |
|
79 } |
|
80 |
|
81 tasks.push(function() { |
|
82 log("Verifying initial state."); |
|
83 |
|
84 manager = window.navigator.mozMobileMessage; |
|
85 ok(manager instanceof MozMobileMessageManager, |
|
86 "manager is instance of " + manager.constructor); |
|
87 |
|
88 tasks.next(); |
|
89 }); |
|
90 |
|
91 tasks.push(function() { |
|
92 log("MmsMessage.attachments should be an empty array."); |
|
93 |
|
94 manager.onfailed = function(event) { |
|
95 manager.onfailed = null; |
|
96 |
|
97 let message = event.message; |
|
98 ok(Array.isArray(message.attachments) && message.attachments.length === 0, |
|
99 "message.attachments should be an empty array."); |
|
100 |
|
101 tasks.next(); |
|
102 }; |
|
103 |
|
104 // Have a long long subject causes the send fails, so we don't need |
|
105 // networking here. |
|
106 manager.sendMMS({ |
|
107 subject: new Array(MMS_MAX_LENGTH_SUBJECT + 2).join("a"), |
|
108 receivers: ["1", "2"], |
|
109 attachments: [], |
|
110 }); |
|
111 }); |
|
112 |
|
113 tasks.push(deleteAllMessages); |
|
114 |
|
115 // WARNING: All tasks should be pushed before this!!! |
|
116 tasks.push(function cleanUp() { |
|
117 SpecialPowers.removePermission("sms", document); |
|
118 SpecialPowers.clearUserPref("dom.sms.enabled"); |
|
119 finish(); |
|
120 }); |
|
121 |
|
122 tasks.run(); |