fune/browser/components/tests/unit/test_distribution_cachedexistence.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

118 lines
4.1 KiB
JavaScript

/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
/**
* Tests that DistributionCustomizer correctly caches the existence
* of the distribution.ini file and just rechecks it after a version
* update.
*/
const PREF_CACHED_FILE_EXISTENCE = "distribution.iniFile.exists.value";
const PREF_CACHED_FILE_APPVERSION = "distribution.iniFile.exists.appversion";
const PREF_LOAD_FROM_PROFILE = "distribution.testing.loadFromProfile";
const gTestDir = do_get_cwd();
const {AppConstants} = ChromeUtils.import("resource://gre/modules/AppConstants.jsm");
add_task(async function() {
// Start with a clean slate of the prefs that control this feature.
Services.prefs.clearUserPref(PREF_CACHED_FILE_APPVERSION);
Services.prefs.clearUserPref(PREF_CACHED_FILE_EXISTENCE);
setupTest();
let {DistributionCustomizer} = ChromeUtils.import("resource:///modules/distribution.js");
let distribution = new DistributionCustomizer();
copyDistributionToProfile();
// Check that checking for distribution.ini returns the right value and sets up
// the cached prefs.
let exists = distribution._hasDistributionIni;
Assert.ok(exists);
Assert.equal(Services.prefs.getBoolPref(PREF_CACHED_FILE_EXISTENCE, undefined),
true);
Assert.equal(Services.prefs.getStringPref(PREF_CACHED_FILE_APPVERSION, "unknown"),
AppConstants.MOZ_APP_VERSION);
// Check that calling _hasDistributionIni again will use the cached value. We do
// this by deleting the file and expecting it to still return true instead of false.
// Also, we need to delete _hasDistributionIni from the object because the getter
// was replaced with a stored value.
deleteDistribution();
delete distribution._hasDistributionIni;
exists = distribution._hasDistributionIni;
Assert.ok(exists);
// Now let's invalidate the PREF_CACHED_FILE_EXISTENCE pref to make sure the
// value gets recomputed correctly.
Services.prefs.clearUserPref(PREF_CACHED_FILE_EXISTENCE);
delete distribution._hasDistributionIni;
exists = distribution._hasDistributionIni;
// It now should return false, as well as storing false in the pref.
Assert.ok(!exists);
Assert.equal(Services.prefs.getBoolPref(PREF_CACHED_FILE_EXISTENCE, undefined),
false);
// Check now that it will use the new cached value instead of returning true in
// the presence of the file.
copyDistributionToProfile();
delete distribution._hasDistributionIni;
exists = distribution._hasDistributionIni;
Assert.ok(!exists);
// Now let's do the same, but invalidating the App Version, as if a version
// update occurred.
Services.prefs.setStringPref(PREF_CACHED_FILE_APPVERSION, "older version");
delete distribution._hasDistributionIni;
exists = distribution._hasDistributionIni;
Assert.ok(exists);
Assert.equal(Services.prefs.getBoolPref(PREF_CACHED_FILE_EXISTENCE, undefined),
true);
Assert.equal(Services.prefs.getStringPref(PREF_CACHED_FILE_APPVERSION, "unknown"),
AppConstants.MOZ_APP_VERSION);
});
/*
* Helper functions
*/
function copyDistributionToProfile() {
// Copy distribution.ini file to the profile dir.
let distroDir = gProfD.clone();
distroDir.leafName = "distribution";
let iniFile = distroDir.clone();
iniFile.append("distribution.ini");
if (iniFile.exists()) {
iniFile.remove(false);
print("distribution.ini already exists, did some test forget to cleanup?");
}
let testDistributionFile = gTestDir.clone();
testDistributionFile.append("distribution.ini");
testDistributionFile.copyTo(distroDir, "distribution.ini");
Assert.ok(testDistributionFile.exists());
}
function deleteDistribution() {
let distroDir = gProfD.clone();
distroDir.leafName = "distribution";
let iniFile = distroDir.clone();
iniFile.append("distribution.ini");
iniFile.remove(false);
}
function setupTest() {
// Set special pref to load distribution.ini from the profile folder.
Services.prefs.setBoolPref(PREF_LOAD_FROM_PROFILE, true);
}
registerCleanupFunction(function() {
deleteDistribution();
Services.prefs.clearUserPref(PREF_LOAD_FROM_PROFILE);
});