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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
michael@0 6 Components.utils.import("resource://gre/modules/Services.jsm");
michael@0 7 Components.utils.import("resource://gre/modules/FormHistory.jsm");
michael@0 8
michael@0 9 const Ci = Components.interfaces;
michael@0 10 const Cc = Components.classes;
michael@0 11 const Cu = Components.utils;
michael@0 12
michael@0 13 const CURRENT_SCHEMA = 4;
michael@0 14 const PR_HOURS = 60 * 60 * 1000000;
michael@0 15
michael@0 16 do_get_profile();
michael@0 17
michael@0 18 var dirSvc = Cc["@mozilla.org/file/directory_service;1"].
michael@0 19 getService(Ci.nsIProperties);
michael@0 20
michael@0 21 // Send the profile-after-change notification to the form history component to ensure
michael@0 22 // that it has been initialized.
michael@0 23 var formHistoryStartup = Cc["@mozilla.org/satchel/form-history-startup;1"].
michael@0 24 getService(Ci.nsIObserver);
michael@0 25 formHistoryStartup.observe(null, "profile-after-change", null);
michael@0 26
michael@0 27 function getDBVersion(dbfile) {
michael@0 28 var ss = Cc["@mozilla.org/storage/service;1"].
michael@0 29 getService(Ci.mozIStorageService);
michael@0 30 var dbConnection = ss.openDatabase(dbfile);
michael@0 31 var version = dbConnection.schemaVersion;
michael@0 32 dbConnection.close();
michael@0 33
michael@0 34 return version;
michael@0 35 }
michael@0 36
michael@0 37 const isGUID = /[A-Za-z0-9\+\/]{16}/;
michael@0 38
michael@0 39 // Find form history entries.
michael@0 40 function searchEntries(terms, params, iter) {
michael@0 41 let results = [];
michael@0 42 FormHistory.search(terms, params, { handleResult: function (result) results.push(result),
michael@0 43 handleError: function (error) {
michael@0 44 do_throw("Error occurred searching form history: " + error);
michael@0 45 },
michael@0 46 handleCompletion: function (reason) { if (!reason) iter.send(results); }
michael@0 47 });
michael@0 48 }
michael@0 49
michael@0 50 // Count the number of entries with the given name and value, and call then(number)
michael@0 51 // when done. If name or value is null, then the value of that field does not matter.
michael@0 52 function countEntries(name, value, then) {
michael@0 53 var obj = {};
michael@0 54 if (name !== null)
michael@0 55 obj.fieldname = name;
michael@0 56 if (value !== null)
michael@0 57 obj.value = value;
michael@0 58
michael@0 59 let count = 0;
michael@0 60 FormHistory.count(obj, { handleResult: function (result) count = result,
michael@0 61 handleError: function (error) {
michael@0 62 do_throw("Error occurred searching form history: " + error);
michael@0 63 },
michael@0 64 handleCompletion: function (reason) { if (!reason) then(count); }
michael@0 65 });
michael@0 66 }
michael@0 67
michael@0 68 // Perform a single form history update and call then() when done.
michael@0 69 function updateEntry(op, name, value, then) {
michael@0 70 var obj = { op: op };
michael@0 71 if (name !== null)
michael@0 72 obj.fieldname = name;
michael@0 73 if (value !== null)
michael@0 74 obj.value = value;
michael@0 75 updateFormHistory(obj, then);
michael@0 76 }
michael@0 77
michael@0 78 // Add a single form history entry with the current time and call then() when done.
michael@0 79 function addEntry(name, value, then) {
michael@0 80 let now = Date.now() * 1000;
michael@0 81 updateFormHistory({ op: "add", fieldname: name, value: value, timesUsed: 1,
michael@0 82 firstUsed: now, lastUsed: now }, then);
michael@0 83 }
michael@0 84
michael@0 85 // Wrapper around FormHistory.update which handles errors. Calls then() when done.
michael@0 86 function updateFormHistory(changes, then) {
michael@0 87 FormHistory.update(changes, { handleError: function (error) {
michael@0 88 do_throw("Error occurred updating form history: " + error);
michael@0 89 },
michael@0 90 handleCompletion: function (reason) { if (!reason) then(); },
michael@0 91 });
michael@0 92 }
michael@0 93
michael@0 94 /**
michael@0 95 * Logs info to the console in the standard way (includes the filename).
michael@0 96 *
michael@0 97 * @param aMessage
michael@0 98 * The message to log to the console.
michael@0 99 */
michael@0 100 function do_log_info(aMessage) {
michael@0 101 print("TEST-INFO | " + _TEST_FILE + " | " + aMessage);
michael@0 102 }

mercurial