michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: const { getPreferedLocales, findClosestLocale } = require("sdk/l10n/locale"); michael@0: const prefs = require("sdk/preferences/service"); michael@0: const { Cc, Ci, Cu } = require("chrome"); michael@0: const { Services } = Cu.import("resource://gre/modules/Services.jsm"); michael@0: const BundleService = Cc["@mozilla.org/intl/stringbundle;1"].getService(Ci.nsIStringBundleService); michael@0: michael@0: const PREF_MATCH_OS_LOCALE = "intl.locale.matchOS"; michael@0: const PREF_SELECTED_LOCALE = "general.useragent.locale"; michael@0: const PREF_ACCEPT_LANGUAGES = "intl.accept_languages"; michael@0: michael@0: function assertPrefered(assert, expected, msg) { michael@0: assert.equal(JSON.stringify(getPreferedLocales()), JSON.stringify(expected), michael@0: msg); michael@0: } michael@0: michael@0: exports.testGetPreferedLocales = function(assert) { michael@0: prefs.set(PREF_MATCH_OS_LOCALE, false); michael@0: prefs.set(PREF_SELECTED_LOCALE, ""); michael@0: prefs.set(PREF_ACCEPT_LANGUAGES, ""); michael@0: assertPrefered(assert, ["en-us"], michael@0: "When all preferences are empty, we only have en-us"); michael@0: michael@0: prefs.set(PREF_SELECTED_LOCALE, "fr"); michael@0: prefs.set(PREF_ACCEPT_LANGUAGES, "jp"); michael@0: assertPrefered(assert, ["fr", "jp", "en-us"], michael@0: "We first have useragent locale, then web one and finally en-US"); michael@0: michael@0: prefs.set(PREF_SELECTED_LOCALE, "en-US"); michael@0: prefs.set(PREF_ACCEPT_LANGUAGES, "en-US"); michael@0: assertPrefered(assert, ["en-us"], michael@0: "We do not have duplicates"); michael@0: michael@0: prefs.set(PREF_SELECTED_LOCALE, "en-US"); michael@0: prefs.set(PREF_ACCEPT_LANGUAGES, "fr"); michael@0: assertPrefered(assert, ["en-us", "fr"], michael@0: "en-US can be first if specified by higher priority preference"); michael@0: michael@0: // Reset what we changed michael@0: prefs.reset(PREF_MATCH_OS_LOCALE); michael@0: prefs.reset(PREF_SELECTED_LOCALE); michael@0: prefs.reset(PREF_ACCEPT_LANGUAGES); michael@0: } michael@0: michael@0: // In some cases, mainly on Fennec and on Linux version, michael@0: // `general.useragent.locale` is a special 'localized' value, like: michael@0: // "chrome://global/locale/intl.properties" michael@0: exports.testPreferedLocalizedLocale = function(assert) { michael@0: prefs.set(PREF_MATCH_OS_LOCALE, false); michael@0: let bundleURL = "chrome://global/locale/intl.properties"; michael@0: prefs.setLocalized(PREF_SELECTED_LOCALE, bundleURL); michael@0: let contentLocale = "ja"; michael@0: prefs.set(PREF_ACCEPT_LANGUAGES, contentLocale); michael@0: michael@0: // Read manually the expected locale value from the property file michael@0: let expectedLocale = BundleService.createBundle(bundleURL). michael@0: GetStringFromName(PREF_SELECTED_LOCALE). michael@0: toLowerCase(); michael@0: michael@0: // First add the useragent locale michael@0: let expectedLocaleList = [expectedLocale]; michael@0: michael@0: // Then the content locale michael@0: if (expectedLocaleList.indexOf(contentLocale) == -1) michael@0: expectedLocaleList.push(contentLocale); michael@0: michael@0: // Add default "en-us" fallback if the main language is not already en-us michael@0: if (expectedLocaleList.indexOf("en-us") == -1) michael@0: expectedLocaleList.push("en-us"); michael@0: michael@0: assertPrefered(assert, expectedLocaleList, "test localized pref value"); michael@0: michael@0: // Reset what we have changed michael@0: prefs.reset(PREF_MATCH_OS_LOCALE); michael@0: prefs.reset(PREF_SELECTED_LOCALE); michael@0: prefs.reset(PREF_ACCEPT_LANGUAGES); michael@0: } michael@0: michael@0: exports.testPreferedOsLocale = function(assert) { michael@0: prefs.set(PREF_MATCH_OS_LOCALE, true); michael@0: prefs.set(PREF_SELECTED_LOCALE, ""); michael@0: prefs.set(PREF_ACCEPT_LANGUAGES, ""); michael@0: michael@0: let expectedLocale = Services.locale.getLocaleComponentForUserAgent(). michael@0: toLowerCase(); michael@0: let expectedLocaleList = [expectedLocale]; michael@0: michael@0: // Add default "en-us" fallback if the main language is not already en-us michael@0: if (expectedLocale != "en-us") michael@0: expectedLocaleList.push("en-us"); michael@0: michael@0: assertPrefered(assert, expectedLocaleList, "Ensure that we select OS locale when related preference is set"); michael@0: michael@0: // Reset what we have changed michael@0: prefs.reset(PREF_MATCH_OS_LOCALE); michael@0: prefs.reset(PREF_SELECTED_LOCALE); michael@0: prefs.reset(PREF_ACCEPT_LANGUAGES); michael@0: } michael@0: michael@0: exports.testFindClosestLocale = function(assert) { michael@0: // Second param of findClosestLocale (aMatchLocales) have to be in lowercase michael@0: assert.equal(findClosestLocale([], []), null, michael@0: "When everything is empty we get null"); michael@0: michael@0: assert.equal(findClosestLocale(["en", "en-US"], ["en"]), michael@0: "en", "We always accept exact match first 1/5"); michael@0: assert.equal(findClosestLocale(["en-US", "en"], ["en"]), michael@0: "en", "We always accept exact match first 2/5"); michael@0: assert.equal(findClosestLocale(["en", "en-US"], ["en-us"]), michael@0: "en-US", "We always accept exact match first 3/5"); michael@0: assert.equal(findClosestLocale(["ja-JP-mac", "ja", "ja-JP"], ["ja-jp"]), michael@0: "ja-JP", "We always accept exact match first 4/5"); michael@0: assert.equal(findClosestLocale(["ja-JP-mac", "ja", "ja-JP"], ["ja-jp-mac"]), michael@0: "ja-JP-mac", "We always accept exact match first 5/5"); michael@0: michael@0: assert.equal(findClosestLocale(["en", "en-GB"], ["en-us"]), michael@0: "en", "We accept more generic locale, when there is no exact match 1/2"); michael@0: assert.equal(findClosestLocale(["en-ZA", "en"], ["en-gb"]), michael@0: "en", "We accept more generic locale, when there is no exact match 2/2"); michael@0: michael@0: assert.equal(findClosestLocale(["ja-JP"], ["ja"]), michael@0: "ja-JP", "We accept more specialized locale, when there is no exact match 1/2"); michael@0: // Better to select "ja" in this case but behave same as current AddonManager michael@0: assert.equal(findClosestLocale(["ja-JP-mac", "ja"], ["ja-jp"]), michael@0: "ja-JP-mac", "We accept more specialized locale, when there is no exact match 2/2"); michael@0: michael@0: assert.equal(findClosestLocale(["en-US"], ["en-us"]), michael@0: "en-US", "We keep the original one as result 1/2"); michael@0: assert.equal(findClosestLocale(["en-us"], ["en-us"]), michael@0: "en-us", "We keep the original one as result 2/2"); michael@0: michael@0: assert.equal(findClosestLocale(["ja-JP-mac"], ["ja-jp-mac"]), michael@0: "ja-JP-mac", "We accept locale with 3 parts"); michael@0: assert.equal(findClosestLocale(["ja-JP"], ["ja-jp-mac"]), michael@0: "ja-JP", "We accept locale with 2 parts from locale with 3 parts"); michael@0: assert.equal(findClosestLocale(["ja"], ["ja-jp-mac"]), michael@0: "ja", "We accept locale with 1 part from locale with 3 parts"); michael@0: }; michael@0: michael@0: require('sdk/test').run(exports);