Bug 1410736 - Replace remaining uses of general.useragent.locale with LocaleService API. r=jfkthame

MozReview-Commit-ID: F0Ofe2lHHEo

--HG--
extra : rebase_source : 420c65111dc5066eb00ef86ddf375db533008bbf
This commit is contained in:
Zibi Braniecki 2017-11-03 17:08:34 -07:00
parent dad9d3748b
commit 4c3a3ac1fb
18 changed files with 52 additions and 83 deletions

View File

@ -55,14 +55,12 @@ function asyncInit() {
}
function asyncReInit() {
const kLocalePref = "general.useragent.locale";
let promise = new Promise(resolve => {
waitForSearchNotification("reinit-complete", resolve);
});
Services.search.QueryInterface(Ci.nsIObserver)
.observe(null, "nsPref:changed", kLocalePref);
.observe(null, "intl:requested-locales-changed", null);
return promise;
}

View File

@ -55,14 +55,12 @@ function asyncInit() {
}
function asyncReInit() {
const kLocalePref = "general.useragent.locale";
let promise = new Promise(resolve => {
waitForSearchNotification("reinit-complete", resolve);
});
Services.search.QueryInterface(Ci.nsIObserver)
.observe(null, "nsPref:changed", kLocalePref);
.observe(null, "intl:requested-locales-changed", null);
return promise;
}

View File

@ -9,6 +9,7 @@ var MANIFESTS = [
// Stub in the locale service so we can control what gets returned as the OS locale setting
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
Components.utils.import("resource://gre/modules/Services.jsm");
registerManifests(MANIFESTS);
@ -33,7 +34,7 @@ function enum_to_array(strings) {
function run_test() {
// without override
prefService.setCharPref("general.useragent.locale", "de");
Services.locale.setRequestedLocales(["de"]);
do_check_eq(chromeReg.getSelectedLocale("basepack"), "en-US");
do_check_eq(chromeReg.getSelectedLocale("overpack"), "de");
do_check_matches(enum_to_array(chromeReg.getLocalesForPackage("basepack")),

View File

@ -31,8 +31,10 @@ var gStringBundle;
W3CTest.runner.requestLongerTimeout(2);
const { Services } = SpecialPowers.Cu.import("resource://gre/modules/Services.jsm");
Services.locale.setRequestedLocales(["en-US"]);
SpecialPowers.pushPrefEnv({ "set": [
["general.useragent.locale", "en-US"],
// Need to set devPixelsPerPx explicitly to gain
// consistent pixel values in warning messages
// regardless of platform DPIs.

View File

@ -11,10 +11,11 @@ server.registerDirectory("/data/", do_get_file("data"));
const BASE_URL = `http://localhost:${server.identity.primaryPort}/data`;
var originalReqLocales = Services.locale.getRequestedLocales();
do_register_cleanup(() => {
Preferences.reset("intl.accept_languages");
Preferences.reset("intl.locale.matchOS");
Preferences.reset("general.useragent.locale");
Services.locale.setRequestedLocales(originalReqLocales);
});
@ -218,9 +219,8 @@ add_task(async function test_i18n_negotiation() {
let contentPage = await ExtensionTestUtils.loadContentPage(`${BASE_URL}/file_sample.html`);
Preferences.set("intl.locale.matchOS", false);
for (let [lang, msg] of [["en-US", "English."], ["jp", "\u65e5\u672c\u8a9e"]]) {
Preferences.set("general.useragent.locale", lang);
Services.locale.setRequestedLocales([lang]);
let extension = ExtensionTestUtils.loadExtension(extensionData);
await extension.startup();
@ -232,7 +232,7 @@ add_task(async function test_i18n_negotiation() {
await extension.unload();
}
Preferences.reset("general.useragent.locale");
Services.locale.setRequestedLocales(originalReqLocales);
await contentPage.close();
});
@ -377,7 +377,7 @@ add_task(async function test_get_ui_language() {
// We don't currently have a good way to mock this.
if (false) {
Preferences.set("general.useragent.locale", "he");
Services.locale.setRequestedLocales(["he"]);
extension.sendMessage(["expect-results", "he"]);

View File

@ -122,23 +122,20 @@ async function test_i18n_css(options = {}) {
// We don't currently have a good way to mock this.
if (false) {
const LOCALE = "general.useragent.locale";
const DIR = "intl.uidirection";
const DIR_LEGACY = "intl.uidirection.en"; // Needed for Android until bug 1215247 is resolved
// We don't wind up actually switching the chrome registry locale, since we
// don't have a chrome package for Hebrew. So just override it, and force
// RTL directionality.
Preferences.set(LOCALE, "he");
const origReqLocales = Services.locale.getRequestedLocales();
Services.locale.setRequestedLocales(["he"]);
Preferences.set(DIR, 1);
Preferences.set(DIR_LEGACY, "rtl");
css = await fetch(cssURL);
equal(css, '* { content: "he rtl ltr right left" }', "CSS file localized in mochitest scope");
Preferences.reset(LOCALE);
Services.locale.setRequestedLocales(origReqLocales);
Preferences.reset(DIR);
Preferences.reset(DIR_LEGACY);
}
await extension.awaitFinish("i18n-css");

View File

@ -80,8 +80,7 @@ add_task(async function() {
do_print("Change locale to 'fr' and restart");
Preferences.set("intl.locale.matchOS", false);
Preferences.set("general.useragent.locale", "fr");
Services.locale.setRequestedLocales(["fr"]);
await AddonTestUtils.promiseRestartManager();
await extension.awaitStartup();
@ -99,7 +98,7 @@ add_task(async function() {
do_print("Change locale to 'en-US' and restart");
Preferences.set("general.useragent.locale", "en-US");
Services.locale.setRequestedLocales(["en-US"]);
await AddonTestUtils.promiseRestartManager();
await extension.awaitStartup();

View File

@ -74,14 +74,14 @@ add_task(function test_setup() {
Services.prefs.setCharPref("urlclassifier.downloadBlockTable",
"goog-badbinurl-shavar");
// SendRemoteQueryInternal needs locale preference.
let locale = Services.prefs.getCharPref("general.useragent.locale");
Services.prefs.setCharPref("general.useragent.locale", "en-US");
let originalReqLocales = Services.locale.getRequestedLocales();
Services.locale.setRequestedLocales(["en-US"]);
do_register_cleanup(function() {
Services.prefs.clearUserPref("browser.safebrowsing.malware.enabled");
Services.prefs.clearUserPref("browser.safebrowsing.downloads.enabled");
Services.prefs.clearUserPref("urlclassifier.downloadBlockTable");
Services.prefs.setCharPref("general.useragent.locale", locale);
Services.locale.setRequestedLocales(originalReqLocales);
});
gHttpServer = new HttpServer();

View File

@ -163,15 +163,15 @@ add_task(async function test_setup() {
Services.prefs.setCharPref("urlclassifier.downloadAllowTable",
"goog-downloadwhite-digest256");
// SendRemoteQueryInternal needs locale preference.
let locale = Services.prefs.getCharPref("general.useragent.locale");
Services.prefs.setCharPref("general.useragent.locale", "en-US");
let originalReqLocales = Services.locale.getRequestedLocales();
Services.locale.setRequestedLocales(["en-US"]);
do_register_cleanup(function() {
Services.prefs.clearUserPref("browser.safebrowsing.malware.enabled");
Services.prefs.clearUserPref("browser.safebrowsing.downloads.enabled");
Services.prefs.clearUserPref("urlclassifier.downloadBlockTable");
Services.prefs.clearUserPref("urlclassifier.downloadAllowTable");
Services.prefs.setCharPref("general.useragent.locale", locale);
Services.locale.setRequestedLocales(originalReqLocales);
});
gHttpServer = new HttpServer();

View File

@ -67,6 +67,7 @@ const APP_SEARCH_PREFIX = "resource://search-plugins/";
// See documentation in nsIBrowserSearchService.idl.
const SEARCH_ENGINE_TOPIC = "browser-search-engine-modified";
const REQ_LOCALES_CHANGED_TOPIC = "intl:requested-locales-changed";
const QUIT_APPLICATION_TOPIC = "quit-application";
const SEARCH_ENGINE_REMOVED = "engine-removed";
@ -137,7 +138,6 @@ const URLTYPE_SEARCH_HTML = "text/html";
const URLTYPE_OPENSEARCH = "application/opensearchdescription+xml";
const BROWSER_SEARCH_PREF = "browser.search.";
const LOCALE_PREF = "general.useragent.locale";
const USER_DEFINED = "searchTerms";
@ -2290,7 +2290,7 @@ Engine.prototype = {
// we'll accept as a 'default' engine anything that has been registered at
// resource://search-plugins/ even if the file doesn't come from the
// application folder. If not, skip costly additional checks.
if (!Services.prefs.prefHasUserValue(LOCALE_PREF) &&
if (Services.locale.defaultLocale !== Services.locale.getRequestedLocale() &&
!gEnvironment.get("XPCSHELL_TEST_PROFILE_DIR"))
return false;
@ -4666,13 +4666,11 @@ SearchService.prototype = {
this._removeObservers();
break;
case "nsPref:changed":
if (aVerb == LOCALE_PREF) {
// Locale changed. Re-init. We rely on observers, because we can't
// return this promise to anyone.
this._asyncReInit();
break;
}
case REQ_LOCALES_CHANGED_TOPIC:
// Locale changed. Re-init. We rely on observers, because we can't
// return this promise to anyone.
this._asyncReInit();
break;
}
},
@ -4727,7 +4725,7 @@ SearchService.prototype = {
Services.obs.addObserver(this, QUIT_APPLICATION_TOPIC);
if (AppConstants.MOZ_BUILD_APP == "mobile/android") {
Services.prefs.addObserver(LOCALE_PREF, this);
Services.obs.addObserver(this, REQ_LOCALES_CHANGED_TOPIC);
}
// The current stage of shutdown. Used to help analyze crash
@ -4772,7 +4770,7 @@ SearchService.prototype = {
Services.obs.removeObserver(this, QUIT_APPLICATION_TOPIC);
if (AppConstants.MOZ_BUILD_APP == "mobile/android") {
Services.prefs.removeObserver(LOCALE_PREF, this);
Services.obs.removeObserver(this, REQ_LOCALES_CHANGED_TOPIC);
}
},

View File

@ -254,14 +254,14 @@ function isUSTimezone() {
const kDefaultenginenamePref = "browser.search.defaultenginename";
const kTestEngineName = "Test search engine";
const kLocalePref = "general.useragent.locale";
const REQ_LOCALES_CHANGED_TOPIC = "intl:requested-locales-changed";
function getDefaultEngineName(isUS) {
const nsIPLS = Ci.nsIPrefLocalizedString;
// Copy the logic from nsSearchService
let pref = kDefaultenginenamePref;
if (isUS === undefined)
isUS = Services.prefs.getCharPref(kLocalePref) == "en-US" && isUSTimezone();
isUS = Services.locale.getRequestedLocale() == "en-US" && isUSTimezone();
if (isUS) {
pref += ".US";
}
@ -509,7 +509,7 @@ function asyncReInit() {
let promise = waitForSearchNotification("reinit-complete");
Services.search.QueryInterface(Ci.nsIObserver)
.observe(null, "nsPref:changed", kLocalePref);
.observe(null, REQ_LOCALES_CHANGED_TOPIC, null);
return promise;
}

View File

@ -3,9 +3,6 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
const PREF_MATCH_OS_LOCALE = "intl.locale.matchOS";
const PREF_SELECTED_LOCALE = "general.useragent.locale";
// Disables security checking our updates which haven't been signed
Services.prefs.setBoolPref("extensions.checkUpdateSecurity", false);
@ -160,8 +157,7 @@ function run_test() {
server.registerPathHandler("/3", requestHandler);
server.start(4444);
Services.prefs.setBoolPref(PREF_MATCH_OS_LOCALE, false);
Services.prefs.setCharPref(PREF_SELECTED_LOCALE, "en-US");
Services.locale.setRequestedLocales(["en-US"]);
startupManager();
installAllFiles(ADDONS.map(a => do_get_addon(a.addon)), function() {

View File

@ -3,9 +3,6 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
const PREF_MATCH_OS_LOCALE = "intl.locale.matchOS";
const PREF_SELECTED_LOCALE = "general.useragent.locale";
const ADDON = "test_bug397778";
const ID = "bug397778@tests.mozilla.org";
@ -13,8 +10,7 @@ function run_test() {
// Setup for test
do_test_pending();
createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1");
Services.prefs.setBoolPref(PREF_MATCH_OS_LOCALE, false);
Services.prefs.setCharPref(PREF_SELECTED_LOCALE, "fr-FR");
Services.locale.setRequestedLocales(["fr-FR"]);
// Install test add-on
startupManager();
@ -46,7 +42,7 @@ function run_test_1() {
function run_test_2() {
// Change locale. The more specific de-DE is the best match
Services.prefs.setCharPref(PREF_SELECTED_LOCALE, "de");
Services.locale.setRequestedLocales(["de"]);
restartManager();
AddonManager.getAddonByID(ID, function(addon) {
@ -60,7 +56,7 @@ function run_test_2() {
function run_test_3() {
// Change locale. Locale case should have no effect
Services.prefs.setCharPref(PREF_SELECTED_LOCALE, "DE-de");
Services.locale.setRequestedLocales(["DE-de"]);
restartManager();
AddonManager.getAddonByID(ID, function(addon) {
@ -74,7 +70,7 @@ function run_test_3() {
function run_test_4() {
// Change locale. es-ES should closely match
Services.prefs.setCharPref(PREF_SELECTED_LOCALE, "es-AR");
Services.locale.setRequestedLocales(["es-AR"]);
restartManager();
AddonManager.getAddonByID(ID, function(addon) {
@ -88,7 +84,7 @@ function run_test_4() {
function run_test_5() {
// Change locale. Either zh-CN or zh-TW could match
Services.prefs.setCharPref(PREF_SELECTED_LOCALE, "zh");
Services.locale.setRequestedLocales(["zh"]);
restartManager();
AddonManager.getAddonByID(ID, function(addon) {
@ -103,7 +99,7 @@ function run_test_5() {
function run_test_6() {
// Unknown locale should try to match against en-US as well. Of en,en-GB
// en should match as being less specific
Services.prefs.setCharPref(PREF_SELECTED_LOCALE, "nl-NL");
Services.locale.setRequestedLocales(["nl-NL"]);
restartManager();
AddonManager.getAddonByID(ID, function(addon) {

View File

@ -9,7 +9,6 @@ const PREF_BLOCKLIST_ENABLED = "extensions.blocklist.enabled";
const PREF_APP_DISTRIBUTION = "distribution.id";
const PREF_APP_DISTRIBUTION_VERSION = "distribution.version";
const PREF_APP_UPDATE_CHANNEL = "app.update.channel";
const PREF_GENERAL_USERAGENT_LOCALE = "general.useragent.locale";
const CATEGORY_UPDATE_TIMER = "update-timer";
// Get the HTTP server.
@ -121,7 +120,7 @@ function run_test() {
defaults.setCharPref(PREF_APP_UPDATE_CHANNEL, "updatechannel");
defaults.setCharPref(PREF_APP_DISTRIBUTION, "distribution");
defaults.setCharPref(PREF_APP_DISTRIBUTION_VERSION, "distribution-version");
defaults.setCharPref(PREF_GENERAL_USERAGENT_LOCALE, "locale");
Services.locale.setRequestedLocales(["locale"]);
// This should correctly escape everything
Services.prefs.setCharPref(PREF_BLOCKLIST_URL, "http://localhost:" + gPort + "/2?" +

View File

@ -2,19 +2,12 @@
* http://creativecommons.org/publicdomain/zero/1.0/
*/
// This verifies that localized properties work as expected
const PREF_MATCH_OS_LOCALE = "intl.locale.matchOS";
const PREF_SELECTED_LOCALE = "general.useragent.locale";
function run_test() {
do_test_pending();
// Setup for test
createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9.2");
Services.prefs.setBoolPref(PREF_MATCH_OS_LOCALE, false);
Services.prefs.setCharPref(PREF_SELECTED_LOCALE, "fr-FR");
Services.locale.setRequestedLocales(["fr-FR"]);
startupManager();
@ -88,7 +81,7 @@ function run_test_4() {
// Test that changing locale works
function run_test_5() {
Services.prefs.setCharPref(PREF_SELECTED_LOCALE, "de-DE");
Services.locale.setRequestedLocales(["de-DE"]);
restartManager();
AddonManager.getAddonByID("addon1@tests.mozilla.org", function(addon) {
@ -103,7 +96,7 @@ function run_test_5() {
// Test that missing locales use the fallbacks
function run_test_6() {
Services.prefs.setCharPref(PREF_SELECTED_LOCALE, "nl-NL");
Services.locale.setRequestedLocales(["nl-NL"]);
restartManager();
AddonManager.getAddonByID("addon1@tests.mozilla.org", callback_soon(function(addon) {
@ -132,7 +125,7 @@ function run_test_7() {
// Test that the prefs will override localized values from the manifest
function run_test_8() {
Services.prefs.setCharPref(PREF_SELECTED_LOCALE, "fr-FR");
Services.locale.setRequestedLocales(["fr-FR"]);
restartManager();
AddonManager.getAddonByID("addon1@tests.mozilla.org", function(addon) {

View File

@ -4,8 +4,6 @@
// This verifies that add-on update checks work
const PREF_MATCH_OS_LOCALE = "intl.locale.matchOS";
const PREF_SELECTED_LOCALE = "general.useragent.locale";
const PREF_GETADDONS_CACHE_ENABLED = "extensions.getAddons.cache.enabled";
// The test extension uses an insecure update url.
@ -38,8 +36,7 @@ var originalSyncGUID;
function run_test() {
createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1");
Services.prefs.setBoolPref(PREF_MATCH_OS_LOCALE, false);
Services.prefs.setCharPref(PREF_SELECTED_LOCALE, "fr-FR");
Services.locale.setRequestedLocales(["fr-FR"]);
run_next_test();
}

View File

@ -4,8 +4,6 @@
// This verifies that add-on update checks work
const PREF_MATCH_OS_LOCALE = "intl.locale.matchOS";
const PREF_SELECTED_LOCALE = "general.useragent.locale";
const PREF_GETADDONS_CACHE_ENABLED = "extensions.getAddons.cache.enabled";
// The test extension uses an insecure update url.
@ -36,8 +34,7 @@ profileDir.append("extensions");
function run_test() {
createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1");
Services.prefs.setBoolPref(PREF_MATCH_OS_LOCALE, false);
Services.prefs.setCharPref(PREF_SELECTED_LOCALE, "fr-FR");
Services.locale.setRequestedLocales(["fr-FR"]);
run_next_test();
}

View File

@ -6,8 +6,6 @@ Components.utils.import("resource://gre/modules/AppConstants.jsm");
const ID = "webextension1@tests.mozilla.org";
const PREF_SELECTED_LOCALE = "general.useragent.locale";
const profileDir = gProfD.clone();
profileDir.append("extensions");
@ -148,7 +146,7 @@ add_task(async function test_manifest_localization() {
equal(addon.name, "Web Extensiøn foo ☹");
equal(addon.description, "Descriptïon bar ☹ of add-on");
Services.prefs.setCharPref(PREF_SELECTED_LOCALE, "fr-FR");
Services.locale.setRequestedLocales(["fr-FR"]);
await promiseRestartManager();
addon = await promiseAddonByID(extensionId);
@ -156,7 +154,7 @@ add_task(async function test_manifest_localization() {
equal(addon.name, "Web Extensiøn le foo ☺");
equal(addon.description, "Descriptïon le bar ☺ of add-on");
Services.prefs.setCharPref(PREF_SELECTED_LOCALE, "de");
Services.locale.setRequestedLocales(["de"]);
await promiseRestartManager();
addon = await promiseAddonByID(extensionId);