1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/mobilemessage/tests/marionette/mmdb_head.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,352 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + * http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +MARIONETTE_CONTEXT = "chrome"; 1.8 + 1.9 +let Promise = Cu.import("resource://gre/modules/Promise.jsm").Promise; 1.10 + 1.11 +/** 1.12 + * Name space for MobileMessageDB.jsm. Only initialized after first call to 1.13 + * newMobileMessageDB. 1.14 + */ 1.15 +let MMDB; 1.16 + 1.17 +/** 1.18 + * Create a new MobileMessageDB instance. 1.19 + * 1.20 + * @return A MobileMessageDB instance. 1.21 + */ 1.22 +function newMobileMessageDB() { 1.23 + if (!MMDB) { 1.24 + MMDB = Cu.import("resource://gre/modules/MobileMessageDB.jsm", {}); 1.25 + is(typeof MMDB.MobileMessageDB, "function", "MMDB.MobileMessageDB"); 1.26 + } 1.27 + 1.28 + let mmdb = new MMDB.MobileMessageDB(); 1.29 + ok(mmdb, "MobileMessageDB instance"); 1.30 + return mmdb; 1.31 +} 1.32 + 1.33 +/** 1.34 + * Initialize a MobileMessageDB. Resolve if initialized with success, reject 1.35 + * otherwise. 1.36 + * 1.37 + * Fulfill params: a MobileMessageDB instance. 1.38 + * Reject params: a MobileMessageDB instance. 1.39 + * 1.40 + * @param aMmdb 1.41 + * A MobileMessageDB instance. 1.42 + * @param aDbName 1.43 + * A string name for that database. 1.44 + * @param aDbVersion 1.45 + * The version that MobileMessageDB should upgrade to. 0 for the lastest 1.46 + * version. 1.47 + * 1.48 + * @return A deferred promise. 1.49 + */ 1.50 +function initMobileMessageDB(aMmdb, aDbName, aDbVersion) { 1.51 + let deferred = Promise.defer(); 1.52 + 1.53 + aMmdb.init(aDbName, aDbVersion, function(aError) { 1.54 + if (aError) { 1.55 + deferred.reject(aMmdb); 1.56 + } else { 1.57 + deferred.resolve(aMmdb); 1.58 + } 1.59 + }); 1.60 + 1.61 + return deferred.promise; 1.62 +} 1.63 + 1.64 +/** 1.65 + * Close a MobileMessageDB. 1.66 + * 1.67 + * @param aMmdb 1.68 + * A MobileMessageDB instance. 1.69 + * 1.70 + * @return The passed MobileMessageDB instance. 1.71 + */ 1.72 +function closeMobileMessageDB(aMmdb) { 1.73 + aMmdb.close(); 1.74 + return aMmdb; 1.75 +} 1.76 + 1.77 +/** 1.78 + * Utility function for calling MMDB methods that takes either a 1.79 + * nsIRilMobileMessageDatabaseCallback or a 1.80 + * nsIRilMobileMessageDatabaseRecordCallback. 1.81 + * 1.82 + * Resolve when the target method notifies us with a successful result code; 1.83 + * reject otherwise. In either case, the arguments passed are packed into an 1.84 + * array and propagated to next action. 1.85 + * 1.86 + * Fulfill params: an array whose elements are the arguments of the original 1.87 + * callback. 1.88 + * Reject params: same as fulfill params. 1.89 + * 1.90 + * @param aMmdb 1.91 + * A MobileMessageDB instance. 1.92 + * @param aMethodName 1.93 + * A string name for that target method. 1.94 + * @param ... 1.95 + * Extra arguments to pass to that target method. The last callback 1.96 + * argument should always be excluded. 1.97 + * 1.98 + * @return A deferred promise. 1.99 + */ 1.100 +function callMmdbMethod(aMmdb, aMethodName) { 1.101 + let deferred = Promise.defer(); 1.102 + 1.103 + let args = Array.slice(arguments, 2); 1.104 + args.push({ 1.105 + notify: function(aRv) { 1.106 + if (!Components.isSuccessCode(aRv)) { 1.107 + ok(true, aMethodName + " returns a unsuccessful code: " + aRv); 1.108 + deferred.reject(Array.slice(arguments)); 1.109 + } else { 1.110 + ok(true, aMethodName + " returns a successful code: " + aRv); 1.111 + deferred.resolve(Array.slice(arguments)); 1.112 + } 1.113 + } 1.114 + }); 1.115 + aMmdb[aMethodName].apply(aMmdb, args); 1.116 + 1.117 + return deferred.promise; 1.118 +} 1.119 + 1.120 +/** 1.121 + * A convenient function for calling |mmdb.saveSendingMessage(...)|. 1.122 + * 1.123 + * Fulfill params: [<Cr.NS_ERROR_FOO>, <DOM message>]. 1.124 + * Reject params: same as fulfill params. 1.125 + * 1.126 + * @return A deferred promise. 1.127 + */ 1.128 +function saveSendingMessage(aMmdb, aMessage) { 1.129 + return callMmdbMethod(aMmdb, "saveSendingMessage", aMessage); 1.130 +} 1.131 + 1.132 +/** 1.133 + * A convenient function for calling |mmdb.saveReceivedMessage(...)|. 1.134 + * 1.135 + * Fulfill params: [<Cr.NS_ERROR_FOO>, <DOM message>]. 1.136 + * Reject params: same as fulfill params. 1.137 + * 1.138 + * @return A deferred promise. 1.139 + */ 1.140 +function saveReceivedMessage(aMmdb, aMessage) { 1.141 + return callMmdbMethod(aMmdb, "saveReceivedMessage", aMessage); 1.142 +} 1.143 + 1.144 +/** 1.145 + * A convenient function for calling |mmdb.setMessageDeliveryByMessageId(...)|. 1.146 + * 1.147 + * Fulfill params: [<Cr.NS_ERROR_FOO>, <DOM message>]. 1.148 + * Reject params: same as fulfill params. 1.149 + * 1.150 + * @return A deferred promise. 1.151 + */ 1.152 +function setMessageDeliveryByMessageId(aMmdb, aMessageId, aReceiver, aDelivery, 1.153 + aDeliveryStatus, aEnvelopeId) { 1.154 + return callMmdbMethod(aMmdb, "setMessageDeliveryByMessageId", aMessageId, 1.155 + aReceiver, aDelivery, aDeliveryStatus, aEnvelopeId); 1.156 +} 1.157 + 1.158 +/** 1.159 + * A convenient function for calling 1.160 + * |mmdb.setMessageDeliveryStatusByEnvelopeId(...)|. 1.161 + * 1.162 + * Fulfill params: [<Cr.NS_ERROR_FOO>, <DOM message>]. 1.163 + * Reject params: same as fulfill params. 1.164 + * 1.165 + * @return A deferred promise. 1.166 + */ 1.167 +function setMessageDeliveryStatusByEnvelopeId(aMmdb, aEnvelopeId, aReceiver, 1.168 + aDeliveryStatus) { 1.169 + return callMmdbMethod(aMmdb, "setMessageDeliveryStatusByEnvelopeId", 1.170 + aMmdb, aEnvelopeId, aReceiver, aDeliveryStatus); 1.171 +} 1.172 + 1.173 +/** 1.174 + * A convenient function for calling 1.175 + * |mmdb.setMessageReadStatusByEnvelopeId(...)|. 1.176 + * 1.177 + * Fulfill params: [<Cr.NS_ERROR_FOO>, <DOM message>]. 1.178 + * Reject params: same as fulfill params. 1.179 + * 1.180 + * @return A deferred promise. 1.181 + */ 1.182 +function setMessageReadStatusByEnvelopeId(aMmdb, aEnvelopeId, aReceiver, 1.183 + aReadStatus) { 1.184 + return callMmdbMethod(aMmdb, "setMessageReadStatusByEnvelopeId", 1.185 + aEnvelopeId, aReceiver, aReadStatus); 1.186 +} 1.187 + 1.188 +/** 1.189 + * A convenient function for calling 1.190 + * |mmdb.getMessageRecordByTransactionId(...)|. 1.191 + * 1.192 + * Fulfill params: [<Cr.NS_ERROR_FOO>, <DB Record>, <DOM message>]. 1.193 + * Reject params: same as fulfill params. 1.194 + * 1.195 + * @return A deferred promise. 1.196 + */ 1.197 +function getMessageRecordByTransactionId(aMmdb, aTransactionId) { 1.198 + return callMmdbMethod(aMmdb, "getMessageRecordByTransactionId", 1.199 + aTransactionId); 1.200 +} 1.201 + 1.202 +/** 1.203 + * A convenient function for calling |mmdb.getMessageRecordById(...)|. 1.204 + * 1.205 + * Fulfill params: [<Cr.NS_ERROR_FOO>, <DB Record>, <DOM message>]. 1.206 + * Reject params: same as fulfill params. 1.207 + * 1.208 + * @return A deferred promise. 1.209 + */ 1.210 +function getMessageRecordById(aMmdb, aMessageId) { 1.211 + return callMmdbMethod(aMmdb, "getMessageRecordById", aMessageId); 1.212 +} 1.213 + 1.214 +/** 1.215 + * A convenient function for calling |mmdb.markMessageRead(...)|. 1.216 + * 1.217 + * Fulfill params: Ci.nsIMobileMessageCallback.FOO. 1.218 + * Reject params: same as fulfill params. 1.219 + * 1.220 + * @return A deferred promise. 1.221 + */ 1.222 +function markMessageRead(aMmdb, aMessageId, aRead) { 1.223 + let deferred = Promise.defer(); 1.224 + 1.225 + aMmdb.markMessageRead(aMessageId, aRead, false, { 1.226 + notifyMarkMessageReadFailed: function(aRv) { 1.227 + ok(true, "markMessageRead returns a unsuccessful code: " + aRv); 1.228 + deferred.reject(aRv); 1.229 + }, 1.230 + 1.231 + notifyMessageMarkedRead: function(aRead) { 1.232 + ok(true, "markMessageRead returns a successful code: " + Cr.NS_OK); 1.233 + deferred.resolve(Ci.nsIMobileMessageCallback.SUCCESS_NO_ERROR); 1.234 + } 1.235 + }); 1.236 + 1.237 + return deferred.promise; 1.238 +} 1.239 + 1.240 +/** 1.241 + * Utility function for calling cursor-based MMDB methods. 1.242 + * 1.243 + * Resolve when the target method notifies us with |notifyCursorDone|, 1.244 + * reject otherwise. 1.245 + * 1.246 + * Fulfill params: [<Ci.nsIMobileMessageCallback.FOO>, [<DOM message/thread>]] 1.247 + * Reject params: same as fulfill params. 1.248 + * 1.249 + * @param aMmdb 1.250 + * A MobileMessageDB instance. 1.251 + * @param aMethodName 1.252 + * A string name for that target method. 1.253 + * @param ... 1.254 + * Extra arguments to pass to that target method. The last callback 1.255 + * argument should always be excluded. 1.256 + * 1.257 + * @return A deferred promise. 1.258 + */ 1.259 +function createMmdbCursor(aMmdb, aMethodName) { 1.260 + let deferred = Promise.defer(); 1.261 + 1.262 + let cursor; 1.263 + let results = []; 1.264 + let args = Array.slice(arguments, 2); 1.265 + args.push({ 1.266 + notifyCursorError: function(aRv) { 1.267 + ok(true, "notifyCursorError: " + aRv); 1.268 + deferred.reject([aRv, results]); 1.269 + }, 1.270 + 1.271 + notifyCursorResult: function(aResult) { 1.272 + ok(true, "notifyCursorResult: " + aResult.id); 1.273 + results.push(aResult); 1.274 + cursor.handleContinue(); 1.275 + }, 1.276 + 1.277 + notifyCursorDone: function() { 1.278 + ok(true, "notifyCursorDone"); 1.279 + deferred.resolve([Ci.nsIMobileMessageCallback.SUCCESS_NO_ERROR, results]); 1.280 + } 1.281 + }); 1.282 + 1.283 + cursor = aMmdb[aMethodName].apply(aMmdb, args); 1.284 + 1.285 + return deferred.promise; 1.286 +} 1.287 + 1.288 +/** 1.289 + * A convenient function for calling |mmdb.createMessageCursor(...)|. 1.290 + * 1.291 + * Fulfill params: [<Ci.nsIMobileMessageCallback.FOO>, [<DOM message>]]. 1.292 + * Reject params: same as fulfill params. 1.293 + * 1.294 + * @return A deferred promise. 1.295 + */ 1.296 +function createMessageCursor(aMmdb, aFilter, aReverse) { 1.297 + return createMmdbCursor(aMmdb, "createMessageCursor", aFilter, aReverse); 1.298 +} 1.299 + 1.300 +/** 1.301 + * A convenient function for calling |mmdb.createThreadCursor(...)|. 1.302 + * 1.303 + * Fulfill params: [<Ci.nsIMobileMessageCallback.FOO>, [<DOM thread>]]. 1.304 + * Reject params: same as fulfill params. 1.305 + * 1.306 + * @return A deferred promise. 1.307 + */ 1.308 +function createThreadCursor(aMmdb) { 1.309 + return createMmdbCursor(aMmdb, "createThreadCursor"); 1.310 +} 1.311 + 1.312 +// A reference to a nsIUUIDGenerator service. 1.313 +let _uuidGenerator; 1.314 + 1.315 +/** 1.316 + * Generate a new UUID. 1.317 + * 1.318 + * @return A UUID string. 1.319 + */ 1.320 +function newUUID() { 1.321 + if (!_uuidGenerator) { 1.322 + _uuidGenerator = Cc["@mozilla.org/uuid-generator;1"] 1.323 + .getService(Ci.nsIUUIDGenerator); 1.324 + ok(_uuidGenerator, "uuidGenerator"); 1.325 + } 1.326 + 1.327 + return _uuidGenerator.generateUUID().toString(); 1.328 +} 1.329 + 1.330 +/** 1.331 + * Flush permission settings and call |finish()|. 1.332 + */ 1.333 +function cleanUp() { 1.334 + // Use ok here so that we have at least one test run. 1.335 + ok(true, "permissions flushed"); 1.336 + 1.337 + finish(); 1.338 +} 1.339 + 1.340 +/** 1.341 + * Basic test routine helper for mobile message tests. 1.342 + * 1.343 + * This helper does nothing but clean-ups. 1.344 + * 1.345 + * @param aTestCaseMain 1.346 + * A function that takes no parameter. 1.347 + */ 1.348 +function startTestBase(aTestCaseMain) { 1.349 + Promise.resolve() 1.350 + .then(aTestCaseMain) 1.351 + .then(null, function() { 1.352 + ok(false, 'promise rejects during test.'); 1.353 + }) 1.354 + .then(cleanUp); 1.355 +}