gecko-dev/toolkit/modules/tests/xpcshell/test_ProfileAge.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

79 lines
2.8 KiB
JavaScript

const {ProfileAge} = ChromeUtils.import("resource://gre/modules/ProfileAge.jsm");
const {OS} = ChromeUtils.import("resource://gre/modules/osfile.jsm");
const {CommonUtils} = ChromeUtils.import("resource://services-common/utils.js");
const gProfD = do_get_profile();
let ID = 0;
// Creates a unique profile directory to use for a test.
function withDummyProfile(task) {
return async () => {
let profile = OS.Path.join(gProfD.path, "" + ID++);
await OS.File.makeDir(profile);
await task(profile);
await OS.File.removeDir(profile);
};
}
add_task(withDummyProfile(async (profile) => {
let times = await ProfileAge(profile);
Assert.ok((await times.created) > 0, "We can't really say what this will be, just assume if it is a number it's ok.");
Assert.equal(await times.reset, undefined, "Reset time is undefined in a new profile");
Assert.ok((await times.firstUse) <= Date.now(), "Should have initialised a first use time.");
}));
add_task(withDummyProfile(async (profile) => {
const CREATED_TIME = Date.now() - 2000;
const RESET_TIME = Date.now() - 1000;
await CommonUtils.writeJSON({
created: CREATED_TIME,
}, OS.Path.join(profile, "times.json"));
let times = await ProfileAge(profile);
Assert.equal((await times.created), CREATED_TIME, "Should have seen the right profile time.");
Assert.equal((await times.firstUse), undefined, "Should be no first use time.");
let times2 = await ProfileAge(profile);
Assert.equal(times, times2, "Should have got the same instance.");
let promise = times.recordProfileReset(RESET_TIME);
Assert.equal((await times2.reset), RESET_TIME, "Should have seen the right reset time in the second instance immediately.");
await promise;
let results = await CommonUtils.readJSON(OS.Path.join(profile, "times.json"));
Assert.deepEqual(results, {
created: CREATED_TIME,
reset: RESET_TIME,
}, "Should have seen the right results.");
}));
add_task(withDummyProfile(async (profile) => {
const RESET_TIME = Date.now() - 1000;
const RESET_TIME2 = Date.now() - 2000;
// The last call to recordProfileReset should always win.
let times = await ProfileAge(profile);
await Promise.all([
times.recordProfileReset(RESET_TIME),
times.recordProfileReset(RESET_TIME2),
]);
let results = await CommonUtils.readJSON(OS.Path.join(profile, "times.json"));
delete results.firstUse;
Assert.deepEqual(results, {
reset: RESET_TIME2,
}, "Should have seen the right results.");
}));
add_task(withDummyProfile(async (profile) => {
const CREATED_TIME = Date.now() - 1000;
await CommonUtils.writeJSON({
created: CREATED_TIME,
firstUse: null,
}, OS.Path.join(profile, "times.json"));
let times = await ProfileAge(profile);
Assert.ok((await times.firstUse) <= Date.now(), "Should have initialised a first use time.");
}));