toolkit/components/satchel/test/unit/head_satchel.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/satchel/test/unit/head_satchel.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,102 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
     1.9 +Components.utils.import("resource://gre/modules/Services.jsm");
    1.10 +Components.utils.import("resource://gre/modules/FormHistory.jsm");
    1.11 +
    1.12 +const Ci = Components.interfaces;
    1.13 +const Cc = Components.classes;
    1.14 +const Cu = Components.utils;
    1.15 +
    1.16 +const CURRENT_SCHEMA = 4;
    1.17 +const PR_HOURS = 60 * 60 * 1000000;
    1.18 +
    1.19 +do_get_profile();
    1.20 +
    1.21 +var dirSvc = Cc["@mozilla.org/file/directory_service;1"].
    1.22 +             getService(Ci.nsIProperties);
    1.23 +
    1.24 +// Send the profile-after-change notification to the form history component to ensure
    1.25 +// that it has been initialized.
    1.26 +var formHistoryStartup = Cc["@mozilla.org/satchel/form-history-startup;1"].
    1.27 +                         getService(Ci.nsIObserver);
    1.28 +formHistoryStartup.observe(null, "profile-after-change", null);
    1.29 +
    1.30 +function getDBVersion(dbfile) {
    1.31 +    var ss = Cc["@mozilla.org/storage/service;1"].
    1.32 +             getService(Ci.mozIStorageService);
    1.33 +    var dbConnection = ss.openDatabase(dbfile);
    1.34 +    var version = dbConnection.schemaVersion;
    1.35 +    dbConnection.close();
    1.36 +
    1.37 +    return version;
    1.38 +}
    1.39 +
    1.40 +const isGUID = /[A-Za-z0-9\+\/]{16}/;
    1.41 +
    1.42 +// Find form history entries.
    1.43 +function searchEntries(terms, params, iter) {
    1.44 +  let results = [];
    1.45 +  FormHistory.search(terms, params, { handleResult: function (result) results.push(result),
    1.46 +                                      handleError: function (error) {
    1.47 +                                        do_throw("Error occurred searching form history: " + error);
    1.48 +                                      },
    1.49 +                                      handleCompletion: function (reason) { if (!reason) iter.send(results); }
    1.50 +                                    });
    1.51 +}
    1.52 +
    1.53 +// Count the number of entries with the given name and value, and call then(number)
    1.54 +// when done. If name or value is null, then the value of that field does not matter.
    1.55 +function countEntries(name, value, then) {
    1.56 +  var obj = {};
    1.57 +  if (name !== null)
    1.58 +    obj.fieldname = name;
    1.59 +  if (value !== null)
    1.60 +    obj.value = value;
    1.61 +
    1.62 +  let count = 0;
    1.63 +  FormHistory.count(obj, { handleResult: function (result) count = result,
    1.64 +                           handleError: function (error) {
    1.65 +                             do_throw("Error occurred searching form history: " + error);
    1.66 +                           },
    1.67 +                           handleCompletion: function (reason) { if (!reason) then(count); }
    1.68 +                         });
    1.69 +}
    1.70 +
    1.71 +// Perform a single form history update and call then() when done.
    1.72 +function updateEntry(op, name, value, then) {
    1.73 +  var obj = { op: op };
    1.74 +  if (name !== null)
    1.75 +    obj.fieldname = name;
    1.76 +  if (value !== null)
    1.77 +    obj.value = value;
    1.78 +  updateFormHistory(obj, then);
    1.79 +}
    1.80 +
    1.81 +// Add a single form history entry with the current time and call then() when done.
    1.82 +function addEntry(name, value, then) {
    1.83 +  let now = Date.now() * 1000;
    1.84 +  updateFormHistory({ op: "add", fieldname: name, value: value, timesUsed: 1,
    1.85 +                      firstUsed: now, lastUsed: now }, then);
    1.86 +}
    1.87 +
    1.88 +// Wrapper around FormHistory.update which handles errors. Calls then() when done.
    1.89 +function updateFormHistory(changes, then) {
    1.90 +  FormHistory.update(changes, { handleError: function (error) {
    1.91 +                                  do_throw("Error occurred updating form history: " + error);
    1.92 +                                },
    1.93 +                                handleCompletion: function (reason) { if (!reason) then(); },
    1.94 +                              });
    1.95 +}
    1.96 +
    1.97 +/**
    1.98 + * Logs info to the console in the standard way (includes the filename).
    1.99 + *
   1.100 + * @param aMessage
   1.101 + *        The message to log to the console.
   1.102 + */
   1.103 +function do_log_info(aMessage) {
   1.104 +  print("TEST-INFO | " + _TEST_FILE + " | " + aMessage);
   1.105 +}

mercurial