gecko-dev/toolkit/components/satchel/test/unit/head_satchel.js
Kris Maglione e930b89c34 Bug 1514594: Part 3 - Change ChromeUtils.import API.
***
Bug 1514594: Part 3a - Change ChromeUtils.import to return an exports object; not pollute global. r=mccr8

This changes the behavior of ChromeUtils.import() to return an exports object,
rather than a module global, in all cases except when `null` is passed as a
second argument, and changes the default behavior not to pollute the global
scope with the module's exports. Thus, the following code written for the old
model:

  ChromeUtils.import("resource://gre/modules/Services.jsm");

is approximately the same as the following, in the new model:

  var {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");

Since the two behaviors are mutually incompatible, this patch will land with a
scripted rewrite to update all existing callers to use the new model rather
than the old.
***
Bug 1514594: Part 3b - Mass rewrite all JS code to use the new ChromeUtils.import API. rs=Gijs

This was done using the followng script:

https://bitbucket.org/kmaglione/m-c-rewrites/src/tip/processors/cu-import-exports.jsm
***
Bug 1514594: Part 3c - Update ESLint plugin for ChromeUtils.import API changes. r=Standard8

Differential Revision: https://phabricator.services.mozilla.com/D16747
***
Bug 1514594: Part 3d - Remove/fix hundreds of duplicate imports from sync tests. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D16748
***
Bug 1514594: Part 3e - Remove no-op ChromeUtils.import() calls. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D16749
***
Bug 1514594: Part 3f.1 - Cleanup various test corner cases after mass rewrite. r=Gijs
***
Bug 1514594: Part 3f.2 - Cleanup various non-test corner cases after mass rewrite. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D16750

--HG--
extra : rebase_source : 359574ee3064c90f33bf36c2ebe3159a24cc8895
extra : histedit_source : b93c8f42808b1599f9122d7842d2c0b3e656a594%2C64a3a4e3359dc889e2ab2b49461bab9e27fc10a7
2019-01-17 10:18:31 -08:00

169 lines
4.2 KiB
JavaScript

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/* eslint
"no-unused-vars": ["error", {
vars: "local",
args: "none",
}],
*/
const CURRENT_SCHEMA = 4;
const PR_HOURS = 60 * 60 * 1000000;
var {XPCOMUtils} = ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
var {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
var {FormHistory} = ChromeUtils.import("resource://gre/modules/FormHistory.jsm");
do_get_profile();
// Send the profile-after-change notification to the form history component to ensure
// that it has been initialized.
var formHistoryStartup = Cc["@mozilla.org/satchel/form-history-startup;1"]
.getService(Ci.nsIObserver);
formHistoryStartup.observe(null, "profile-after-change", null);
function getDBVersion(dbfile) {
let dbConnection = Services.storage.openDatabase(dbfile);
let version = dbConnection.schemaVersion;
dbConnection.close();
return version;
}
function getFormHistoryDBVersion() {
let profileDir = do_get_profile();
// Cleanup from any previous tests or failures.
let dbFile = profileDir.clone();
dbFile.append("formhistory.sqlite");
return getDBVersion(dbFile);
}
const isGUID = /[A-Za-z0-9\+\/]{16}/;
// Find form history entries.
function searchEntries(terms, params, iter) {
let results = [];
FormHistory.search(terms, params, {
handleResult: result => results.push(result),
handleError(error) {
do_throw("Error occurred searching form history: " + error);
},
handleCompletion(reason) {
if (!reason) {
iter.next(results);
}
},
});
}
// Count the number of entries with the given name and value, and call then(number)
// when done. If name or value is null, then the value of that field does not matter.
function countEntries(name, value, then) {
let obj = {};
if (name !== null) {
obj.fieldname = name;
}
if (value !== null) {
obj.value = value;
}
let count = 0;
FormHistory.count(obj, {
handleResult: result => count = result,
handleError(error) {
do_throw("Error occurred searching form history: " + error);
},
handleCompletion(reason) {
if (!reason) {
then(count);
}
},
});
}
// Perform a single form history update and call then() when done.
function updateEntry(op, name, value, then) {
let obj = { op };
if (name !== null) {
obj.fieldname = name;
}
if (value !== null) {
obj.value = value;
}
updateFormHistory(obj, then);
}
// Add a single form history entry with the current time and call then() when done.
function addEntry(name, value, then) {
let now = Date.now() * 1000;
updateFormHistory({
op: "add",
fieldname: name,
value,
timesUsed: 1,
firstUsed: now,
lastUsed: now,
}, then);
}
function promiseCountEntries(name, value, checkFn = () => {}) {
return new Promise(resolve => {
countEntries(name, value, function(result) { checkFn(result); resolve(result); });
});
}
function promiseUpdateEntry(op, name, value) {
return new Promise(res => {
updateEntry(op, name, value, res);
});
}
function promiseAddEntry(name, value) {
return new Promise(res => {
addEntry(name, value, res);
});
}
// Wrapper around FormHistory.update which handles errors. Calls then() when done.
function updateFormHistory(changes, then) {
FormHistory.update(changes, {
handleError(error) {
do_throw("Error occurred updating form history: " + error);
},
handleCompletion(reason) {
if (!reason) {
then();
}
},
});
}
function promiseUpdate(change) {
return new Promise((resolve, reject) => {
FormHistory.update(change, {
handleError(error) {
this._error = error;
},
handleCompletion(reason) {
if (reason) {
reject(this._error);
} else {
resolve();
}
},
});
});
}
/**
* Logs info to the console in the standard way (includes the filename).
*
* @param {string} aMessage
* The message to log to the console.
*/
function do_log_info(aMessage) {
print("TEST-INFO | " + _TEST_FILE + " | " + aMessage);
}