Bug 1252627 - distribution defaultLocale shoudld take precedence over user agent locale; r=mixedpuppy
authorMichael Kaply <mozilla@kaply.com>
Mon, 07 Mar 2016 08:48:26 -0600
changeset 287025 99e6ed34499483d39ada0c501ffd736116f35828
parent 287024 1dc3cfa74e2b8a69e7ed81ed95fb72089f6e28a7
child 287026 6f19b1a57959a2e918096c0d66c325ec39583e91
push id18041
push usermozilla@kaply.com
push dateMon, 07 Mar 2016 14:48:34 +0000
treeherderfx-team@99e6ed344994 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmixedpuppy
bugs1252627
milestone47.0a1
Bug 1252627 - distribution defaultLocale shoudld take precedence over user agent locale; r=mixedpuppy
browser/components/dirprovider/DirectoryProvider.cpp
browser/components/tests/unit/data/engine-de-DE.xml
browser/components/tests/unit/test_distribution.js
browser/components/tests/unit/xpcshell.ini
--- a/browser/components/dirprovider/DirectoryProvider.cpp
+++ b/browser/components/dirprovider/DirectoryProvider.cpp
@@ -101,16 +101,34 @@ AppendDistroSearchDirs(nsIProperties* aD
 
     nsCOMPtr<nsIFile> localePlugins;
     rv = searchPlugins->Clone(getter_AddRefs(localePlugins));
     if (NS_FAILED(rv))
       return;
 
     localePlugins->AppendNative(NS_LITERAL_CSTRING("locale"));
 
+    nsCString defLocale;
+    rv = prefs->GetCharPref("distribution.searchplugins.defaultLocale",
+                            getter_Copies(defLocale));
+    if (NS_SUCCEEDED(rv)) {
+
+      nsCOMPtr<nsIFile> defLocalePlugins;
+      rv = localePlugins->Clone(getter_AddRefs(defLocalePlugins));
+      if (NS_SUCCEEDED(rv)) {
+
+        defLocalePlugins->AppendNative(defLocale);
+        rv = defLocalePlugins->Exists(&exists);
+        if (NS_SUCCEEDED(rv) && exists)
+          array.AppendObject(defLocalePlugins);
+          return; // all done
+      }
+    }
+
+    // we didn't have a defaultLocale, use the user agent locale
     nsCString locale;
     nsCOMPtr<nsIPrefLocalizedString> prefString;
     rv = prefs->GetComplexValue("general.useragent.locale",
                                 NS_GET_IID(nsIPrefLocalizedString),
                                 getter_AddRefs(prefString));
     if (NS_SUCCEEDED(rv)) {
       nsAutoString wLocale;
       prefString->GetData(getter_Copies(wLocale));
@@ -128,33 +146,16 @@ AppendDistroSearchDirs(nsIProperties* aD
         curLocalePlugins->AppendNative(locale);
         rv = curLocalePlugins->Exists(&exists);
         if (NS_SUCCEEDED(rv) && exists) {
           array.AppendObject(curLocalePlugins);
           return; // all done
         }
       }
     }
-
-    // we didn't append the locale dir - try the default one
-    nsCString defLocale;
-    rv = prefs->GetCharPref("distribution.searchplugins.defaultLocale",
-                            getter_Copies(defLocale));
-    if (NS_SUCCEEDED(rv)) {
-
-      nsCOMPtr<nsIFile> defLocalePlugins;
-      rv = localePlugins->Clone(getter_AddRefs(defLocalePlugins));
-      if (NS_SUCCEEDED(rv)) {
-
-        defLocalePlugins->AppendNative(defLocale);
-        rv = defLocalePlugins->Exists(&exists);
-        if (NS_SUCCEEDED(rv) && exists)
-          array.AppendObject(defLocalePlugins);
-      }
-    }
   }
 }
 
 NS_IMETHODIMP
 DirectoryProvider::GetFiles(const char *aKey, nsISimpleEnumerator* *aResult)
 {
   /**
    * We want to preserve the following order, since the search service loads
new file mode 100644
--- /dev/null
+++ b/browser/components/tests/unit/data/engine-de-DE.xml
@@ -0,0 +1,8 @@
+<SearchPlugin xmlns="http://www.mozilla.org/2006/browser/search/">
+<ShortName>Google</ShortName>
+<Description>override-de-DE</Description>
+<InputEncoding>UTF-8</InputEncoding>
+<Url type="text/html" method="GET" template="http://searchtest.local">
+  <Param name="search" value="{searchTerms}"/>
+</Url>
+</SearchPlugin>
--- a/browser/components/tests/unit/test_distribution.js
+++ b/browser/components/tests/unit/test_distribution.js
@@ -18,16 +18,55 @@ var commonFile = do_get_file("../../../.
 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";
 
+/**
+ * Copy the engine-distribution.xml engine to a fake distribution
+ * created in the profile, and registered with the directory service.
+ * Create an empty en-US directory to make sure it isn't used.
+ */
+function installDistributionEngine() {
+  const XRE_APP_DISTRIBUTION_DIR = "XREAppDist";
+
+  const gProfD = do_get_profile().QueryInterface(Ci.nsILocalFile);
+
+  let dir = gProfD.clone();
+  dir.append("distribution");
+  let distDir = dir.clone();
+
+  dir.append("searchplugins");
+  dir.create(dir.DIRECTORY_TYPE, FileUtils.PERMS_DIRECTORY);
+
+  dir.append("locale");
+  dir.create(dir.DIRECTORY_TYPE, FileUtils.PERMS_DIRECTORY);
+  let localeDir = dir.clone();
+
+  dir.append("en-US");
+  dir.create(dir.DIRECTORY_TYPE, FileUtils.PERMS_DIRECTORY);
+
+  localeDir.append("de-DE");
+  localeDir.create(dir.DIRECTORY_TYPE, FileUtils.PERMS_DIRECTORY);
+
+  do_get_file("data/engine-de-DE.xml").copyTo(localeDir, "engine-de-DE.xml");
+
+  Services.dirsvc.registerProvider({
+    getFile: function(aProp, aPersistent) {
+      aPersistent.value = true;
+      if (aProp == XRE_APP_DISTRIBUTION_DIR)
+        return distDir.clone();
+      return null;
+    }
+  });
+}
+
 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();
@@ -37,29 +76,28 @@ function run_test() {
     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());
 
+  installDistributionEngine();
+  
   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());
+  // Remove the distribution dir, even if the test failed, otherwise all
+  // next tests will use it.
+  let distDir = gProfD.clone();
+  distDir.append("distribution");
+  distDir.remove(true);
+  Assert.ok(!distDir.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.id"), "disttest");
@@ -100,9 +138,20 @@ add_task(function* () {
   // This value was never set because of the empty locale specific pref
   Assert.throws(() => Services.prefs.getComplexValue("distribution.test.locale.reset", Ci.nsIPrefLocalizedString));
   // This value was overridden by a locale specific setting
   Assert.equal(Services.prefs.getComplexValue("distribution.test.locale.set", Ci.nsIPrefLocalizedString).data, "Locale Set");
   // This value was overridden by a language specific setting
   Assert.equal(Services.prefs.getComplexValue("distribution.test.language.set", Ci.nsIPrefLocalizedString).data, "Language Set");
   // Language should not override locale
   Assert.notEqual(Services.prefs.getComplexValue("distribution.test.locale.set", Ci.nsIPrefLocalizedString).data, "Language Set");
+
+  do_test_pending();
+
+  Services.prefs.setCharPref("distribution.searchplugins.defaultLocale", "de-DE");
+
+  Services.search.init(function() {
+    Assert.equal(Services.search.isInitialized, true);
+    var engine = Services.search.getEngineByName("Google");
+    Assert.equal(engine.description, "override-de-DE");
+    do_test_finished();
+  });
 });
--- a/browser/components/tests/unit/xpcshell.ini
+++ b/browser/components/tests/unit/xpcshell.ini
@@ -1,7 +1,8 @@
 [DEFAULT]
 firefox-appdir = browser
 skip-if = toolkit == 'android' || toolkit == 'gonk'
 support-files =
   distribution.ini
+  data/engine-de-DE.xml
 
 [test_distribution.js]