|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 * http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
3 |
|
4 MARIONETTE_CONTEXT = "chrome"; |
|
5 |
|
6 let Promise = Cu.import("resource://gre/modules/Promise.jsm").Promise; |
|
7 |
|
8 /** |
|
9 * Name space for MobileMessageDB.jsm. Only initialized after first call to |
|
10 * newMobileMessageDB. |
|
11 */ |
|
12 let MMDB; |
|
13 |
|
14 /** |
|
15 * Create a new MobileMessageDB instance. |
|
16 * |
|
17 * @return A MobileMessageDB instance. |
|
18 */ |
|
19 function newMobileMessageDB() { |
|
20 if (!MMDB) { |
|
21 MMDB = Cu.import("resource://gre/modules/MobileMessageDB.jsm", {}); |
|
22 is(typeof MMDB.MobileMessageDB, "function", "MMDB.MobileMessageDB"); |
|
23 } |
|
24 |
|
25 let mmdb = new MMDB.MobileMessageDB(); |
|
26 ok(mmdb, "MobileMessageDB instance"); |
|
27 return mmdb; |
|
28 } |
|
29 |
|
30 /** |
|
31 * Initialize a MobileMessageDB. Resolve if initialized with success, reject |
|
32 * otherwise. |
|
33 * |
|
34 * Fulfill params: a MobileMessageDB instance. |
|
35 * Reject params: a MobileMessageDB instance. |
|
36 * |
|
37 * @param aMmdb |
|
38 * A MobileMessageDB instance. |
|
39 * @param aDbName |
|
40 * A string name for that database. |
|
41 * @param aDbVersion |
|
42 * The version that MobileMessageDB should upgrade to. 0 for the lastest |
|
43 * version. |
|
44 * |
|
45 * @return A deferred promise. |
|
46 */ |
|
47 function initMobileMessageDB(aMmdb, aDbName, aDbVersion) { |
|
48 let deferred = Promise.defer(); |
|
49 |
|
50 aMmdb.init(aDbName, aDbVersion, function(aError) { |
|
51 if (aError) { |
|
52 deferred.reject(aMmdb); |
|
53 } else { |
|
54 deferred.resolve(aMmdb); |
|
55 } |
|
56 }); |
|
57 |
|
58 return deferred.promise; |
|
59 } |
|
60 |
|
61 /** |
|
62 * Close a MobileMessageDB. |
|
63 * |
|
64 * @param aMmdb |
|
65 * A MobileMessageDB instance. |
|
66 * |
|
67 * @return The passed MobileMessageDB instance. |
|
68 */ |
|
69 function closeMobileMessageDB(aMmdb) { |
|
70 aMmdb.close(); |
|
71 return aMmdb; |
|
72 } |
|
73 |
|
74 /** |
|
75 * Utility function for calling MMDB methods that takes either a |
|
76 * nsIRilMobileMessageDatabaseCallback or a |
|
77 * nsIRilMobileMessageDatabaseRecordCallback. |
|
78 * |
|
79 * Resolve when the target method notifies us with a successful result code; |
|
80 * reject otherwise. In either case, the arguments passed are packed into an |
|
81 * array and propagated to next action. |
|
82 * |
|
83 * Fulfill params: an array whose elements are the arguments of the original |
|
84 * callback. |
|
85 * Reject params: same as fulfill params. |
|
86 * |
|
87 * @param aMmdb |
|
88 * A MobileMessageDB instance. |
|
89 * @param aMethodName |
|
90 * A string name for that target method. |
|
91 * @param ... |
|
92 * Extra arguments to pass to that target method. The last callback |
|
93 * argument should always be excluded. |
|
94 * |
|
95 * @return A deferred promise. |
|
96 */ |
|
97 function callMmdbMethod(aMmdb, aMethodName) { |
|
98 let deferred = Promise.defer(); |
|
99 |
|
100 let args = Array.slice(arguments, 2); |
|
101 args.push({ |
|
102 notify: function(aRv) { |
|
103 if (!Components.isSuccessCode(aRv)) { |
|
104 ok(true, aMethodName + " returns a unsuccessful code: " + aRv); |
|
105 deferred.reject(Array.slice(arguments)); |
|
106 } else { |
|
107 ok(true, aMethodName + " returns a successful code: " + aRv); |
|
108 deferred.resolve(Array.slice(arguments)); |
|
109 } |
|
110 } |
|
111 }); |
|
112 aMmdb[aMethodName].apply(aMmdb, args); |
|
113 |
|
114 return deferred.promise; |
|
115 } |
|
116 |
|
117 /** |
|
118 * A convenient function for calling |mmdb.saveSendingMessage(...)|. |
|
119 * |
|
120 * Fulfill params: [<Cr.NS_ERROR_FOO>, <DOM message>]. |
|
121 * Reject params: same as fulfill params. |
|
122 * |
|
123 * @return A deferred promise. |
|
124 */ |
|
125 function saveSendingMessage(aMmdb, aMessage) { |
|
126 return callMmdbMethod(aMmdb, "saveSendingMessage", aMessage); |
|
127 } |
|
128 |
|
129 /** |
|
130 * A convenient function for calling |mmdb.saveReceivedMessage(...)|. |
|
131 * |
|
132 * Fulfill params: [<Cr.NS_ERROR_FOO>, <DOM message>]. |
|
133 * Reject params: same as fulfill params. |
|
134 * |
|
135 * @return A deferred promise. |
|
136 */ |
|
137 function saveReceivedMessage(aMmdb, aMessage) { |
|
138 return callMmdbMethod(aMmdb, "saveReceivedMessage", aMessage); |
|
139 } |
|
140 |
|
141 /** |
|
142 * A convenient function for calling |mmdb.setMessageDeliveryByMessageId(...)|. |
|
143 * |
|
144 * Fulfill params: [<Cr.NS_ERROR_FOO>, <DOM message>]. |
|
145 * Reject params: same as fulfill params. |
|
146 * |
|
147 * @return A deferred promise. |
|
148 */ |
|
149 function setMessageDeliveryByMessageId(aMmdb, aMessageId, aReceiver, aDelivery, |
|
150 aDeliveryStatus, aEnvelopeId) { |
|
151 return callMmdbMethod(aMmdb, "setMessageDeliveryByMessageId", aMessageId, |
|
152 aReceiver, aDelivery, aDeliveryStatus, aEnvelopeId); |
|
153 } |
|
154 |
|
155 /** |
|
156 * A convenient function for calling |
|
157 * |mmdb.setMessageDeliveryStatusByEnvelopeId(...)|. |
|
158 * |
|
159 * Fulfill params: [<Cr.NS_ERROR_FOO>, <DOM message>]. |
|
160 * Reject params: same as fulfill params. |
|
161 * |
|
162 * @return A deferred promise. |
|
163 */ |
|
164 function setMessageDeliveryStatusByEnvelopeId(aMmdb, aEnvelopeId, aReceiver, |
|
165 aDeliveryStatus) { |
|
166 return callMmdbMethod(aMmdb, "setMessageDeliveryStatusByEnvelopeId", |
|
167 aMmdb, aEnvelopeId, aReceiver, aDeliveryStatus); |
|
168 } |
|
169 |
|
170 /** |
|
171 * A convenient function for calling |
|
172 * |mmdb.setMessageReadStatusByEnvelopeId(...)|. |
|
173 * |
|
174 * Fulfill params: [<Cr.NS_ERROR_FOO>, <DOM message>]. |
|
175 * Reject params: same as fulfill params. |
|
176 * |
|
177 * @return A deferred promise. |
|
178 */ |
|
179 function setMessageReadStatusByEnvelopeId(aMmdb, aEnvelopeId, aReceiver, |
|
180 aReadStatus) { |
|
181 return callMmdbMethod(aMmdb, "setMessageReadStatusByEnvelopeId", |
|
182 aEnvelopeId, aReceiver, aReadStatus); |
|
183 } |
|
184 |
|
185 /** |
|
186 * A convenient function for calling |
|
187 * |mmdb.getMessageRecordByTransactionId(...)|. |
|
188 * |
|
189 * Fulfill params: [<Cr.NS_ERROR_FOO>, <DB Record>, <DOM message>]. |
|
190 * Reject params: same as fulfill params. |
|
191 * |
|
192 * @return A deferred promise. |
|
193 */ |
|
194 function getMessageRecordByTransactionId(aMmdb, aTransactionId) { |
|
195 return callMmdbMethod(aMmdb, "getMessageRecordByTransactionId", |
|
196 aTransactionId); |
|
197 } |
|
198 |
|
199 /** |
|
200 * A convenient function for calling |mmdb.getMessageRecordById(...)|. |
|
201 * |
|
202 * Fulfill params: [<Cr.NS_ERROR_FOO>, <DB Record>, <DOM message>]. |
|
203 * Reject params: same as fulfill params. |
|
204 * |
|
205 * @return A deferred promise. |
|
206 */ |
|
207 function getMessageRecordById(aMmdb, aMessageId) { |
|
208 return callMmdbMethod(aMmdb, "getMessageRecordById", aMessageId); |
|
209 } |
|
210 |
|
211 /** |
|
212 * A convenient function for calling |mmdb.markMessageRead(...)|. |
|
213 * |
|
214 * Fulfill params: Ci.nsIMobileMessageCallback.FOO. |
|
215 * Reject params: same as fulfill params. |
|
216 * |
|
217 * @return A deferred promise. |
|
218 */ |
|
219 function markMessageRead(aMmdb, aMessageId, aRead) { |
|
220 let deferred = Promise.defer(); |
|
221 |
|
222 aMmdb.markMessageRead(aMessageId, aRead, false, { |
|
223 notifyMarkMessageReadFailed: function(aRv) { |
|
224 ok(true, "markMessageRead returns a unsuccessful code: " + aRv); |
|
225 deferred.reject(aRv); |
|
226 }, |
|
227 |
|
228 notifyMessageMarkedRead: function(aRead) { |
|
229 ok(true, "markMessageRead returns a successful code: " + Cr.NS_OK); |
|
230 deferred.resolve(Ci.nsIMobileMessageCallback.SUCCESS_NO_ERROR); |
|
231 } |
|
232 }); |
|
233 |
|
234 return deferred.promise; |
|
235 } |
|
236 |
|
237 /** |
|
238 * Utility function for calling cursor-based MMDB methods. |
|
239 * |
|
240 * Resolve when the target method notifies us with |notifyCursorDone|, |
|
241 * reject otherwise. |
|
242 * |
|
243 * Fulfill params: [<Ci.nsIMobileMessageCallback.FOO>, [<DOM message/thread>]] |
|
244 * Reject params: same as fulfill params. |
|
245 * |
|
246 * @param aMmdb |
|
247 * A MobileMessageDB instance. |
|
248 * @param aMethodName |
|
249 * A string name for that target method. |
|
250 * @param ... |
|
251 * Extra arguments to pass to that target method. The last callback |
|
252 * argument should always be excluded. |
|
253 * |
|
254 * @return A deferred promise. |
|
255 */ |
|
256 function createMmdbCursor(aMmdb, aMethodName) { |
|
257 let deferred = Promise.defer(); |
|
258 |
|
259 let cursor; |
|
260 let results = []; |
|
261 let args = Array.slice(arguments, 2); |
|
262 args.push({ |
|
263 notifyCursorError: function(aRv) { |
|
264 ok(true, "notifyCursorError: " + aRv); |
|
265 deferred.reject([aRv, results]); |
|
266 }, |
|
267 |
|
268 notifyCursorResult: function(aResult) { |
|
269 ok(true, "notifyCursorResult: " + aResult.id); |
|
270 results.push(aResult); |
|
271 cursor.handleContinue(); |
|
272 }, |
|
273 |
|
274 notifyCursorDone: function() { |
|
275 ok(true, "notifyCursorDone"); |
|
276 deferred.resolve([Ci.nsIMobileMessageCallback.SUCCESS_NO_ERROR, results]); |
|
277 } |
|
278 }); |
|
279 |
|
280 cursor = aMmdb[aMethodName].apply(aMmdb, args); |
|
281 |
|
282 return deferred.promise; |
|
283 } |
|
284 |
|
285 /** |
|
286 * A convenient function for calling |mmdb.createMessageCursor(...)|. |
|
287 * |
|
288 * Fulfill params: [<Ci.nsIMobileMessageCallback.FOO>, [<DOM message>]]. |
|
289 * Reject params: same as fulfill params. |
|
290 * |
|
291 * @return A deferred promise. |
|
292 */ |
|
293 function createMessageCursor(aMmdb, aFilter, aReverse) { |
|
294 return createMmdbCursor(aMmdb, "createMessageCursor", aFilter, aReverse); |
|
295 } |
|
296 |
|
297 /** |
|
298 * A convenient function for calling |mmdb.createThreadCursor(...)|. |
|
299 * |
|
300 * Fulfill params: [<Ci.nsIMobileMessageCallback.FOO>, [<DOM thread>]]. |
|
301 * Reject params: same as fulfill params. |
|
302 * |
|
303 * @return A deferred promise. |
|
304 */ |
|
305 function createThreadCursor(aMmdb) { |
|
306 return createMmdbCursor(aMmdb, "createThreadCursor"); |
|
307 } |
|
308 |
|
309 // A reference to a nsIUUIDGenerator service. |
|
310 let _uuidGenerator; |
|
311 |
|
312 /** |
|
313 * Generate a new UUID. |
|
314 * |
|
315 * @return A UUID string. |
|
316 */ |
|
317 function newUUID() { |
|
318 if (!_uuidGenerator) { |
|
319 _uuidGenerator = Cc["@mozilla.org/uuid-generator;1"] |
|
320 .getService(Ci.nsIUUIDGenerator); |
|
321 ok(_uuidGenerator, "uuidGenerator"); |
|
322 } |
|
323 |
|
324 return _uuidGenerator.generateUUID().toString(); |
|
325 } |
|
326 |
|
327 /** |
|
328 * Flush permission settings and call |finish()|. |
|
329 */ |
|
330 function cleanUp() { |
|
331 // Use ok here so that we have at least one test run. |
|
332 ok(true, "permissions flushed"); |
|
333 |
|
334 finish(); |
|
335 } |
|
336 |
|
337 /** |
|
338 * Basic test routine helper for mobile message tests. |
|
339 * |
|
340 * This helper does nothing but clean-ups. |
|
341 * |
|
342 * @param aTestCaseMain |
|
343 * A function that takes no parameter. |
|
344 */ |
|
345 function startTestBase(aTestCaseMain) { |
|
346 Promise.resolve() |
|
347 .then(aTestCaseMain) |
|
348 .then(null, function() { |
|
349 ok(false, 'promise rejects during test.'); |
|
350 }) |
|
351 .then(cleanUp); |
|
352 } |