dom/mobilemessage/tests/marionette/mmdb_head.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

     1 /* Any copyright is dedicated to the Public Domain.
     2  * http://creativecommons.org/publicdomain/zero/1.0/ */
     4 MARIONETTE_CONTEXT = "chrome";
     6 let Promise = Cu.import("resource://gre/modules/Promise.jsm").Promise;
     8 /**
     9  * Name space for MobileMessageDB.jsm.  Only initialized after first call to
    10  * newMobileMessageDB.
    11  */
    12 let MMDB;
    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   }
    25   let mmdb = new MMDB.MobileMessageDB();
    26   ok(mmdb, "MobileMessageDB instance");
    27   return mmdb;
    28 }
    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();
    50   aMmdb.init(aDbName, aDbVersion, function(aError) {
    51     if (aError) {
    52       deferred.reject(aMmdb);
    53     } else {
    54       deferred.resolve(aMmdb);
    55     }
    56   });
    58   return deferred.promise;
    59 }
    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 }
    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();
   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);
   114   return deferred.promise;
   115 }
   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 }
   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 }
   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 }
   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 }
   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 }
   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 }
   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 }
   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();
   222   aMmdb.markMessageRead(aMessageId, aRead, false, {
   223     notifyMarkMessageReadFailed: function(aRv) {
   224       ok(true, "markMessageRead returns a unsuccessful code: " + aRv);
   225       deferred.reject(aRv);
   226     },
   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   });
   234   return deferred.promise;
   235 }
   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();
   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     },
   268     notifyCursorResult: function(aResult) {
   269       ok(true, "notifyCursorResult: " + aResult.id);
   270       results.push(aResult);
   271       cursor.handleContinue();
   272     },
   274     notifyCursorDone: function() {
   275       ok(true, "notifyCursorDone");
   276       deferred.resolve([Ci.nsIMobileMessageCallback.SUCCESS_NO_ERROR, results]);
   277     }
   278   });
   280   cursor = aMmdb[aMethodName].apply(aMmdb, args);
   282   return deferred.promise;
   283 }
   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 }
   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 }
   309 // A reference to a nsIUUIDGenerator service.
   310 let _uuidGenerator;
   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   }
   324   return _uuidGenerator.generateUUID().toString();
   325 }
   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");
   334   finish();
   335 }
   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 }

mercurial