diff options
Diffstat (limited to 'browser/components/tests/unit')
7 files changed, 487 insertions, 0 deletions
diff --git a/browser/components/tests/unit/distribution.ini b/browser/components/tests/unit/distribution.ini new file mode 100644 index 0000000000..d7d2988083 --- /dev/null +++ b/browser/components/tests/unit/distribution.ini @@ -0,0 +1,58 @@ +# Distribution Configuration File +# Test of distribution preferences + +[Global] +id=disttest +version=1.0 +about=Test distribution file +about.en-US=Tèƨƭ δïƨƭřïβúƭïôñ ƒïℓè + +[Preferences] +distribution.test.string="Test String" +distribution.test.string.noquotes=Test String +distribution.test.int=777 +distribution.test.bool.true=true +distribution.test.bool.false=false +distribution.test.empty= + +distribution.test.pref.locale="%LOCALE%" +distribution.test.pref.language.reset="Preference Set" +distribution.test.pref.locale.reset="Preference Set" +distribution.test.pref.locale.set="Preference Set" +distribution.test.pref.language.set="Preference Set" + +[Preferences-en] +distribution.test.pref.language.en="en" +distribution.test.pref.language.reset= +distribution.test.pref.language.set="Language Set" +distribution.test.pref.locale.set="Language Set" + +[Preferences-en-US] +distribution.test.pref.locale.en-US="en-US" +distribution.test.pref.locale.reset= +distribution.test.pref.locale.set="Locale Set" + + +[Preferences-de] +distribution.test.pref.language.de="de" + +[LocalizablePreferences] +distribution.test.locale="%LOCALE%" +distribution.test.language.reset="Preference Set" +distribution.test.locale.reset="Preference Set" +distribution.test.locale.set="Preference Set" +distribution.test.language.set="Preference Set" + +[LocalizablePreferences-en] +distribution.test.language.en="en" +distribution.test.language.reset= +distribution.test.language.set="Language Set" +distribution.test.locale.set="Language Set" + +[LocalizablePreferences-en-US] +distribution.test.locale.en-US="en-US" +distribution.test.locale.reset= +distribution.test.locale.set="Locale Set" + +[LocalizablePreferences-de] +distribution.test.language.de="de" diff --git a/browser/components/tests/unit/head.js b/browser/components/tests/unit/head.js new file mode 100644 index 0000000000..5ad9d5e7b6 --- /dev/null +++ b/browser/components/tests/unit/head.js @@ -0,0 +1,7 @@ +/* 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/. */ + +var { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm"); +var { sinon } = ChromeUtils.import("resource://testing-common/Sinon.jsm"); +var gProfD = do_get_profile().QueryInterface(Ci.nsIFile); diff --git a/browser/components/tests/unit/test_browserGlue_migration_no_errors.js b/browser/components/tests/unit/test_browserGlue_migration_no_errors.js new file mode 100644 index 0000000000..aed88c95fe --- /dev/null +++ b/browser/components/tests/unit/test_browserGlue_migration_no_errors.js @@ -0,0 +1,34 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +/** + * Since various migrations in BrowserGlue are sometimes trivial, e.g. just + * clearing a pref, it does not feel necessary to write tests for each of those. + * + * However, ensuring we have at least some coverage to check for errors, e.g. + * typos, is a good idea, hence this test. + * + * If your migration is more complex that clearing a couple of prefs, you + * should consider adding your own BrowserGlue migration test. + */ +const TOPIC_BROWSERGLUE_TEST = "browser-glue-test"; +const TOPICDATA_BROWSERGLUE_TEST = "force-ui-migration"; + +const gBrowserGlue = Cc["@mozilla.org/browser/browserglue;1"].getService( + Ci.nsIObserver +); + +// Set the migration value to 1, to ensure that the migration code is called, +// and so that this doesn't need updating every time we obsolete old tests. +Services.prefs.setIntPref("browser.migration.version", 1); + +add_task(async function test_no_errors() { + // Simulate a migration. + gBrowserGlue.observe( + null, + TOPIC_BROWSERGLUE_TEST, + TOPICDATA_BROWSERGLUE_TEST + ); + + Assert.ok(true, "should have run the migration with no errors"); +}); diff --git a/browser/components/tests/unit/test_browserGlue_migration_social_cleanup.js b/browser/components/tests/unit/test_browserGlue_migration_social_cleanup.js new file mode 100644 index 0000000000..00b59202a4 --- /dev/null +++ b/browser/components/tests/unit/test_browserGlue_migration_social_cleanup.js @@ -0,0 +1,30 @@ +const UI_VERSION = 69; +const TOPIC_BROWSERGLUE_TEST = "browser-glue-test"; +const TOPICDATA_BROWSERGLUE_TEST = "force-ui-migration"; + +var gBrowserGlue = Cc["@mozilla.org/browser/browserglue;1"].getService( + Ci.nsIObserver +); + +Services.prefs.setIntPref("browser.migration.version", UI_VERSION - 1); + +add_task(async function test_check_cleanup_social_prefs() { + Services.prefs.setStringPref("social.manifest.example-com", "example.com"); + + // Simulate a migration. + gBrowserGlue.observe( + null, + TOPIC_BROWSERGLUE_TEST, + TOPICDATA_BROWSERGLUE_TEST + ); + + Assert.ok( + !Services.prefs.prefHasUserValue("social.manifest.example-com"), + "should have cleared old social preference 'social.manifest.example-com'" + ); +}); + +registerCleanupFunction(() => { + Services.prefs.clearUserPref("browser.migration.version"); + Services.prefs.clearUserPref("social.manifest.example-com"); +}); diff --git a/browser/components/tests/unit/test_distribution.js b/browser/components/tests/unit/test_distribution.js new file mode 100644 index 0000000000..a0a046bd7d --- /dev/null +++ b/browser/components/tests/unit/test_distribution.js @@ -0,0 +1,216 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +/** + * Tests that preferences are properly set by distribution.ini + */ + +const { OS } = ChromeUtils.import("resource://gre/modules/osfile.jsm"); + +const { PromiseTestUtils } = ChromeUtils.import( + "resource://testing-common/PromiseTestUtils.jsm" +); + +// This test causes BrowserGlue to start but not fully initialise, when the +// AddonManager shuts down BrowserGlue will then try to uninit which will +// cause AutoComplete.jsm to throw an error. +// TODO: Fix in https://bugzilla.mozilla.org/show_bug.cgi?id=1543112. +PromiseTestUtils.allowMatchingRejectionsGlobally(/A request was aborted/); +PromiseTestUtils.allowMatchingRejectionsGlobally( + /The operation failed for reasons unrelated/ +); + +const TOPICDATA_DISTRIBUTION_CUSTOMIZATION = "force-distribution-customization"; +const TOPIC_BROWSERGLUE_TEST = "browser-glue-test"; + +registerCleanupFunction(async function() { + // Remove the distribution dir, even if the test failed, otherwise all + // next tests will use it. + let folderPath = OS.Path.join(OS.Constants.Path.profileDir, "distribution"); + await OS.File.removeDir(folderPath, { ignoreAbsent: true }); + Assert.ok(!(await OS.File.exists(folderPath))); + Services.prefs.clearUserPref("distribution.testing.loadFromProfile"); +}); + +add_task(async function() { + // Set special pref to load distribution.ini from the profile folder. + Services.prefs.setBoolPref("distribution.testing.loadFromProfile", true); + + // 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 = do_get_cwd().clone(); + testDistributionFile.append("distribution.ini"); + testDistributionFile.copyTo(distroDir, "distribution.ini"); + Assert.ok(testDistributionFile.exists()); +}); + +add_task(async function() { + // Force distribution. + let glue = Cc["@mozilla.org/browser/browserglue;1"].getService( + Ci.nsIObserver + ); + glue.observe( + null, + TOPIC_BROWSERGLUE_TEST, + TOPICDATA_DISTRIBUTION_CUSTOMIZATION + ); + + var defaultBranch = Services.prefs.getDefaultBranch(null); + + Assert.equal(defaultBranch.getCharPref("distribution.id"), "disttest"); + Assert.equal(defaultBranch.getCharPref("distribution.version"), "1.0"); + Assert.equal( + defaultBranch.getStringPref("distribution.about"), + "Tèƨƭ δïƨƭřïβúƭïôñ ƒïℓè" + ); + + Assert.equal( + defaultBranch.getCharPref("distribution.test.string"), + "Test String" + ); + Assert.equal( + defaultBranch.getCharPref("distribution.test.string.noquotes"), + "Test String" + ); + Assert.equal(defaultBranch.getIntPref("distribution.test.int"), 777); + Assert.equal(defaultBranch.getBoolPref("distribution.test.bool.true"), true); + Assert.equal( + defaultBranch.getBoolPref("distribution.test.bool.false"), + false + ); + + Assert.throws( + () => defaultBranch.getCharPref("distribution.test.empty"), + /NS_ERROR_UNEXPECTED/ + ); + Assert.throws( + () => defaultBranch.getIntPref("distribution.test.empty"), + /NS_ERROR_UNEXPECTED/ + ); + Assert.throws( + () => defaultBranch.getBoolPref("distribution.test.empty"), + /NS_ERROR_UNEXPECTED/ + ); + + Assert.equal( + defaultBranch.getCharPref("distribution.test.pref.locale"), + "en-US" + ); + Assert.equal( + defaultBranch.getCharPref("distribution.test.pref.language.en"), + "en" + ); + Assert.equal( + defaultBranch.getCharPref("distribution.test.pref.locale.en-US"), + "en-US" + ); + Assert.throws( + () => defaultBranch.getCharPref("distribution.test.pref.language.de"), + /NS_ERROR_UNEXPECTED/ + ); + // This value was never set because of the empty language specific pref + Assert.throws( + () => defaultBranch.getCharPref("distribution.test.pref.language.reset"), + /NS_ERROR_UNEXPECTED/ + ); + // This value was never set because of the empty locale specific pref + Assert.throws( + () => defaultBranch.getCharPref("distribution.test.pref.locale.reset"), + /NS_ERROR_UNEXPECTED/ + ); + // This value was overridden by a locale specific setting + Assert.equal( + defaultBranch.getCharPref("distribution.test.pref.locale.set"), + "Locale Set" + ); + // This value was overridden by a language specific setting + Assert.equal( + defaultBranch.getCharPref("distribution.test.pref.language.set"), + "Language Set" + ); + // Language should not override locale + Assert.notEqual( + defaultBranch.getCharPref("distribution.test.pref.locale.set"), + "Language Set" + ); + + Assert.equal( + defaultBranch.getComplexValue( + "distribution.test.locale", + Ci.nsIPrefLocalizedString + ).data, + "en-US" + ); + Assert.equal( + defaultBranch.getComplexValue( + "distribution.test.language.en", + Ci.nsIPrefLocalizedString + ).data, + "en" + ); + Assert.equal( + defaultBranch.getComplexValue( + "distribution.test.locale.en-US", + Ci.nsIPrefLocalizedString + ).data, + "en-US" + ); + Assert.throws( + () => + defaultBranch.getComplexValue( + "distribution.test.language.de", + Ci.nsIPrefLocalizedString + ), + /NS_ERROR_UNEXPECTED/ + ); + // This value was never set because of the empty language specific pref + Assert.throws( + () => + defaultBranch.getComplexValue( + "distribution.test.language.reset", + Ci.nsIPrefLocalizedString + ), + /NS_ERROR_UNEXPECTED/ + ); + // This value was never set because of the empty locale specific pref + Assert.throws( + () => + defaultBranch.getComplexValue( + "distribution.test.locale.reset", + Ci.nsIPrefLocalizedString + ), + /NS_ERROR_UNEXPECTED/ + ); + // This value was overridden by a locale specific setting + Assert.equal( + defaultBranch.getComplexValue( + "distribution.test.locale.set", + Ci.nsIPrefLocalizedString + ).data, + "Locale Set" + ); + // This value was overridden by a language specific setting + Assert.equal( + defaultBranch.getComplexValue( + "distribution.test.language.set", + Ci.nsIPrefLocalizedString + ).data, + "Language Set" + ); + // Language should not override locale + Assert.notEqual( + defaultBranch.getComplexValue( + "distribution.test.locale.set", + Ci.nsIPrefLocalizedString + ).data, + "Language Set" + ); +}); diff --git a/browser/components/tests/unit/test_distribution_cachedexistence.js b/browser/components/tests/unit/test_distribution_cachedexistence.js new file mode 100644 index 0000000000..f08a4ae592 --- /dev/null +++ b/browser/components/tests/unit/test_distribution_cachedexistence.js @@ -0,0 +1,131 @@ +/* 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); +}); diff --git a/browser/components/tests/unit/xpcshell.ini b/browser/components/tests/unit/xpcshell.ini new file mode 100644 index 0000000000..b341664db6 --- /dev/null +++ b/browser/components/tests/unit/xpcshell.ini @@ -0,0 +1,11 @@ +[DEFAULT] +head = head.js +firefox-appdir = browser +skip-if = toolkit == 'android' +support-files = + distribution.ini + +[test_distribution.js] +[test_distribution_cachedexistence.js] +[test_browserGlue_migration_no_errors.js] +[test_browserGlue_migration_social_cleanup.js] |