Bug 1592658 - Policy should not override user's locale choice. r=jaws,zbraniecki

Differential Revision: https://phabricator.services.mozilla.com/D79913
This commit is contained in:
Michael Kaply 2020-06-24 23:52:38 +00:00
Родитель 7463bc52c9
Коммит 77ef4b13b8
2 изменённых файлов: 60 добавлений и 3 удалений

Просмотреть файл

@ -1456,13 +1456,21 @@ var Policies = {
RequestedLocales: {
onBeforeAddons(manager, param) {
let requestedLocales;
if (Array.isArray(param)) {
Services.locale.requestedLocales = param;
requestedLocales = param;
} else if (param) {
Services.locale.requestedLocales = param.split(",");
requestedLocales = param.split(",");
} else {
Services.locale.requestedLocales = [];
requestedLocales = [];
}
runOncePerModification(
"requestedLocales",
JSON.stringify(requestedLocales),
() => {
Services.locale.requestedLocales = requestedLocales;
}
);
},
},

Просмотреть файл

@ -3,6 +3,8 @@
"use strict";
var { setTimeout } = ChromeUtils.import("resource://gre/modules/Timer.jsm");
const REQ_LOC_CHANGE_EVENT = "intl:requested-locales-changed";
function promiseLocaleChanged(requestedLocale) {
@ -22,6 +24,27 @@ function promiseLocaleChanged(requestedLocale) {
});
}
function promiseLocaleNotChanged(requestedLocale) {
return new Promise(resolve => {
let localeObserver = {
observe(aSubject, aTopic, aData) {
switch (aTopic) {
case REQ_LOC_CHANGE_EVENT:
ok(false, "Locale should not change.");
Services.obs.removeObserver(localeObserver, REQ_LOC_CHANGE_EVENT);
resolve();
}
},
};
Services.obs.addObserver(localeObserver, REQ_LOC_CHANGE_EVENT);
/* eslint-disable mozilla/no-arbitrary-setTimeout */
setTimeout(function() {
Services.obs.removeObserver(localeObserver, REQ_LOC_CHANGE_EVENT);
resolve();
}, 100);
});
}
add_task(async function test_requested_locale_array() {
let originalLocales = Services.locale.requestedLocales;
let localePromise = promiseLocaleChanged("de");
@ -66,3 +89,29 @@ add_task(async function test_system_locale_string() {
await localePromise;
Services.locale.requestedLocales = originalLocales;
});
add_task(async function test_user_requested_locale_change() {
let originalLocales = Services.locale.requestedLocales;
let localePromise = promiseLocaleChanged("fr");
await setupPolicyEngineWithJson({
policies: {
RequestedLocales: "fr",
},
});
await localePromise;
// Simulate user change of locale
localePromise = promiseLocaleChanged("de");
Services.locale.requestedLocales = ["de"];
await localePromise;
localePromise = promiseLocaleNotChanged("fr");
await setupPolicyEngineWithJson({
policies: {
RequestedLocales: "fr",
},
});
await localePromise;
Services.locale.requestedLocales = originalLocales;
});