Bug 436564 - Add unit tests for distribution.js; r=mixedpuppy
authorMichael Kaply <mozilla@kaply.com>
Thu, 25 Feb 2016 12:40:39 -0600
changeset 309363 8a390011304e8adb8abe4880792148f6a3a9bf56
parent 309362 8e34b12969bf345b0cd765f612d59f8c0a6de444
child 309364 675f335b6b44b0a85aa8a8d5066cbeaaf3d1bc64
push id5913
push userjlund@mozilla.com
push dateMon, 25 Apr 2016 16:57:49 +0000
treeherdermozilla-esr52@dcaf0a6fa115 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmixedpuppy
bugs436564
milestone47.0a1
Bug 436564 - Add unit tests for distribution.js; r=mixedpuppy MozReview-Commit-ID: 47aRIjly0YZ
browser/components/moz.build
browser/components/test/.eslintrc
browser/components/test/browser.ini
browser/components/test/browser_bug538331.js
browser/components/tests/browser/.eslintrc
browser/components/tests/browser/browser.ini
browser/components/tests/browser/browser_bug538331.js
browser/components/tests/unit/distribution.ini
browser/components/tests/unit/test_distribution.js
browser/components/tests/unit/xpcshell.ini
--- a/browser/components/moz.build
+++ b/browser/components/moz.build
@@ -44,17 +44,21 @@ EXTRA_COMPONENTS += [
     'nsBrowserGlue.js',
 ]
 
 EXTRA_JS_MODULES += [
     'distribution.js',
 ]
 
 BROWSER_CHROME_MANIFESTS += [
-    'test/browser.ini'
+    'tests/browser/browser.ini'
+]
+
+XPCSHELL_TESTS_MANIFESTS += [
+    'tests/unit/xpcshell.ini'
 ]
 
 if CONFIG['MOZ_SAFE_BROWSING']:
     BROWSER_CHROME_MANIFESTS += ['safebrowsing/content/test/browser.ini']
 
 with Files('safebrowsing/*'):
     BUG_COMPONENT = ('Toolkit', 'Phishing Protection')
 
rename from browser/components/test/.eslintrc
rename to browser/components/tests/browser/.eslintrc
--- a/browser/components/test/.eslintrc
+++ b/browser/components/tests/browser/.eslintrc
@@ -1,5 +1,5 @@
 {
   "extends": [
-    "../../../testing/mochitest/browser.eslintrc"
+    "../../../../testing/mochitest/browser.eslintrc"
   ]
 }
rename from browser/components/test/browser.ini
rename to browser/components/tests/browser/browser.ini
rename from browser/components/test/browser_bug538331.js
rename to browser/components/tests/browser/browser_bug538331.js
new file mode 100644
--- /dev/null
+++ b/browser/components/tests/unit/distribution.ini
@@ -0,0 +1,32 @@
+# Distribution Configuration File
+# Test of distribution preferences
+
+[Global]
+id=disttest
+version=1.0
+about=Test distribution file
+
+[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
+
+[LocalizablePreferences]
+distribution.test.locale="%LOCALE%"
+distribution.test.reset="Set"
+distribution.test.locale.set="First Set"
+distribution.test.language.set="First Set"
+
+[LocalizablePreferences-en]
+distribution.test.language.en="en"
+distribution.test.language.set="Second Set"
+
+[LocalizablePreferences-en-US]
+distribution.test.locale.en-US="en-US"
+distribution.test.reset=
+distribution.test.locale.set="Second Set"
+
+[LocalizablePreferences-de]
+distribution.test.locale.de="de"
new file mode 100644
--- /dev/null
+++ b/browser/components/tests/unit/test_distribution.js
@@ -0,0 +1,81 @@
+/* Any copyright is dedicated to the Public Domain.
+   http://creativecommons.org/publicdomain/zero/1.0/ */
+
+/**
+ * Tests that preferences are properly set by distribution.ini
+ */
+
+var Ci = Components.interfaces;
+var Cc = Components.classes;
+var Cr = Components.results;
+var Cu = Components.utils;
+
+Cu.import("resource://gre/modules/Services.jsm");
+Cu.import("resource://gre/modules/LoadContextInfo.jsm");
+
+// Import common head.
+var commonFile = do_get_file("../../../../toolkit/components/places/tests/head_common.js", false);
+if (commonFile) {
+  let uri = Services.io.newFileURI(commonFile);
+  Services.scriptloader.loadSubScript(uri.spec, this);
+}
+
+const TOPICDATA_DISTRIBUTION_CUSTOMIZATION = "force-distribution-customization";
+const TOPIC_BROWSERGLUE_TEST = "browser-glue-test";
+
+function run_test() {
+  // 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 = gTestDir.clone();
+  testDistributionFile.append("distribution.ini");
+  testDistributionFile.copyTo(distroDir, "distribution.ini");
+  Assert.ok(testDistributionFile.exists());
+
+  run_next_test();
+}
+
+do_register_cleanup(function () {
+  // Remove the distribution file, even if the test failed, otherwise all
+  // next tests will import it.
+  let iniFile = gProfD.clone();
+  iniFile.leafName = "distribution";
+  iniFile.append("distribution.ini");
+  if (iniFile.exists()) {
+    iniFile.remove(false);
+  }
+  Assert.ok(!iniFile.exists());
+});
+
+add_task(function* () {
+  // Force distribution.
+  let glue = Cc["@mozilla.org/browser/browserglue;1"].getService(Ci.nsIObserver)
+  glue.observe(null, TOPIC_BROWSERGLUE_TEST, TOPICDATA_DISTRIBUTION_CUSTOMIZATION);
+
+  Assert.equal(Services.prefs.getCharPref("distribution.test.string"), "Test String");
+  Assert.throws(() => Services.prefs.getCharPref("distribution.test.string.noquotes"));
+  Assert.equal(Services.prefs.getIntPref("distribution.test.int"), 777);
+  Assert.equal(Services.prefs.getBoolPref("distribution.test.bool.true"), true);
+  Assert.equal(Services.prefs.getBoolPref("distribution.test.bool.false"), false);
+  Assert.equal(Services.prefs.getComplexValue("distribution.test.locale", Ci.nsIPrefLocalizedString).data, "en-US");
+  Assert.equal(Services.prefs.getComplexValue("distribution.test.language.en", Ci.nsIPrefLocalizedString).data, "en");
+  Assert.equal(Services.prefs.getComplexValue("distribution.test.locale.en-US", Ci.nsIPrefLocalizedString).data, "en-US");
+  Assert.throws(() => Services.prefs.getComplexValue("distribution.test.locale.de", Ci.nsIPrefLocalizedString));
+  // This value was never set because of the empty locale specific pref
+  // This testcase currently fails - the value is set to "undefined" - it should not be set at all (throw)
+  // Assert.throws(() => Services.prefs.getComplexValue("distribution.test.reset", Ci.nsIPrefLocalizedString));
+  // This value was overriden by a locale specific setting
+  Assert.equal(Services.prefs.getComplexValue("distribution.test.locale.set", Ci.nsIPrefLocalizedString).data, "Second Set");
+  // This value was overriden by a language specific setting
+  Assert.equal(Services.prefs.getComplexValue("distribution.test.language.set", Ci.nsIPrefLocalizedString).data, "Second Set");
+});
new file mode 100644
--- /dev/null
+++ b/browser/components/tests/unit/xpcshell.ini
@@ -0,0 +1,7 @@
+[DEFAULT]
+firefox-appdir = browser
+skip-if = toolkit == 'android' || toolkit == 'gonk'
+support-files =
+  distribution.ini
+
+[test_distribution.js]