1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/addon-sdk/source/test/test-l10n-locale.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,143 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +const { getPreferedLocales, findClosestLocale } = require("sdk/l10n/locale"); 1.9 +const prefs = require("sdk/preferences/service"); 1.10 +const { Cc, Ci, Cu } = require("chrome"); 1.11 +const { Services } = Cu.import("resource://gre/modules/Services.jsm"); 1.12 +const BundleService = Cc["@mozilla.org/intl/stringbundle;1"].getService(Ci.nsIStringBundleService); 1.13 + 1.14 +const PREF_MATCH_OS_LOCALE = "intl.locale.matchOS"; 1.15 +const PREF_SELECTED_LOCALE = "general.useragent.locale"; 1.16 +const PREF_ACCEPT_LANGUAGES = "intl.accept_languages"; 1.17 + 1.18 +function assertPrefered(assert, expected, msg) { 1.19 + assert.equal(JSON.stringify(getPreferedLocales()), JSON.stringify(expected), 1.20 + msg); 1.21 +} 1.22 + 1.23 +exports.testGetPreferedLocales = function(assert) { 1.24 + prefs.set(PREF_MATCH_OS_LOCALE, false); 1.25 + prefs.set(PREF_SELECTED_LOCALE, ""); 1.26 + prefs.set(PREF_ACCEPT_LANGUAGES, ""); 1.27 + assertPrefered(assert, ["en-us"], 1.28 + "When all preferences are empty, we only have en-us"); 1.29 + 1.30 + prefs.set(PREF_SELECTED_LOCALE, "fr"); 1.31 + prefs.set(PREF_ACCEPT_LANGUAGES, "jp"); 1.32 + assertPrefered(assert, ["fr", "jp", "en-us"], 1.33 + "We first have useragent locale, then web one and finally en-US"); 1.34 + 1.35 + prefs.set(PREF_SELECTED_LOCALE, "en-US"); 1.36 + prefs.set(PREF_ACCEPT_LANGUAGES, "en-US"); 1.37 + assertPrefered(assert, ["en-us"], 1.38 + "We do not have duplicates"); 1.39 + 1.40 + prefs.set(PREF_SELECTED_LOCALE, "en-US"); 1.41 + prefs.set(PREF_ACCEPT_LANGUAGES, "fr"); 1.42 + assertPrefered(assert, ["en-us", "fr"], 1.43 + "en-US can be first if specified by higher priority preference"); 1.44 + 1.45 + // Reset what we changed 1.46 + prefs.reset(PREF_MATCH_OS_LOCALE); 1.47 + prefs.reset(PREF_SELECTED_LOCALE); 1.48 + prefs.reset(PREF_ACCEPT_LANGUAGES); 1.49 +} 1.50 + 1.51 +// In some cases, mainly on Fennec and on Linux version, 1.52 +// `general.useragent.locale` is a special 'localized' value, like: 1.53 +// "chrome://global/locale/intl.properties" 1.54 +exports.testPreferedLocalizedLocale = function(assert) { 1.55 + prefs.set(PREF_MATCH_OS_LOCALE, false); 1.56 + let bundleURL = "chrome://global/locale/intl.properties"; 1.57 + prefs.setLocalized(PREF_SELECTED_LOCALE, bundleURL); 1.58 + let contentLocale = "ja"; 1.59 + prefs.set(PREF_ACCEPT_LANGUAGES, contentLocale); 1.60 + 1.61 + // Read manually the expected locale value from the property file 1.62 + let expectedLocale = BundleService.createBundle(bundleURL). 1.63 + GetStringFromName(PREF_SELECTED_LOCALE). 1.64 + toLowerCase(); 1.65 + 1.66 + // First add the useragent locale 1.67 + let expectedLocaleList = [expectedLocale]; 1.68 + 1.69 + // Then the content locale 1.70 + if (expectedLocaleList.indexOf(contentLocale) == -1) 1.71 + expectedLocaleList.push(contentLocale); 1.72 + 1.73 + // Add default "en-us" fallback if the main language is not already en-us 1.74 + if (expectedLocaleList.indexOf("en-us") == -1) 1.75 + expectedLocaleList.push("en-us"); 1.76 + 1.77 + assertPrefered(assert, expectedLocaleList, "test localized pref value"); 1.78 + 1.79 + // Reset what we have changed 1.80 + prefs.reset(PREF_MATCH_OS_LOCALE); 1.81 + prefs.reset(PREF_SELECTED_LOCALE); 1.82 + prefs.reset(PREF_ACCEPT_LANGUAGES); 1.83 +} 1.84 + 1.85 +exports.testPreferedOsLocale = function(assert) { 1.86 + prefs.set(PREF_MATCH_OS_LOCALE, true); 1.87 + prefs.set(PREF_SELECTED_LOCALE, ""); 1.88 + prefs.set(PREF_ACCEPT_LANGUAGES, ""); 1.89 + 1.90 + let expectedLocale = Services.locale.getLocaleComponentForUserAgent(). 1.91 + toLowerCase(); 1.92 + let expectedLocaleList = [expectedLocale]; 1.93 + 1.94 + // Add default "en-us" fallback if the main language is not already en-us 1.95 + if (expectedLocale != "en-us") 1.96 + expectedLocaleList.push("en-us"); 1.97 + 1.98 + assertPrefered(assert, expectedLocaleList, "Ensure that we select OS locale when related preference is set"); 1.99 + 1.100 + // Reset what we have changed 1.101 + prefs.reset(PREF_MATCH_OS_LOCALE); 1.102 + prefs.reset(PREF_SELECTED_LOCALE); 1.103 + prefs.reset(PREF_ACCEPT_LANGUAGES); 1.104 +} 1.105 + 1.106 +exports.testFindClosestLocale = function(assert) { 1.107 + // Second param of findClosestLocale (aMatchLocales) have to be in lowercase 1.108 + assert.equal(findClosestLocale([], []), null, 1.109 + "When everything is empty we get null"); 1.110 + 1.111 + assert.equal(findClosestLocale(["en", "en-US"], ["en"]), 1.112 + "en", "We always accept exact match first 1/5"); 1.113 + assert.equal(findClosestLocale(["en-US", "en"], ["en"]), 1.114 + "en", "We always accept exact match first 2/5"); 1.115 + assert.equal(findClosestLocale(["en", "en-US"], ["en-us"]), 1.116 + "en-US", "We always accept exact match first 3/5"); 1.117 + assert.equal(findClosestLocale(["ja-JP-mac", "ja", "ja-JP"], ["ja-jp"]), 1.118 + "ja-JP", "We always accept exact match first 4/5"); 1.119 + assert.equal(findClosestLocale(["ja-JP-mac", "ja", "ja-JP"], ["ja-jp-mac"]), 1.120 + "ja-JP-mac", "We always accept exact match first 5/5"); 1.121 + 1.122 + assert.equal(findClosestLocale(["en", "en-GB"], ["en-us"]), 1.123 + "en", "We accept more generic locale, when there is no exact match 1/2"); 1.124 + assert.equal(findClosestLocale(["en-ZA", "en"], ["en-gb"]), 1.125 + "en", "We accept more generic locale, when there is no exact match 2/2"); 1.126 + 1.127 + assert.equal(findClosestLocale(["ja-JP"], ["ja"]), 1.128 + "ja-JP", "We accept more specialized locale, when there is no exact match 1/2"); 1.129 + // Better to select "ja" in this case but behave same as current AddonManager 1.130 + assert.equal(findClosestLocale(["ja-JP-mac", "ja"], ["ja-jp"]), 1.131 + "ja-JP-mac", "We accept more specialized locale, when there is no exact match 2/2"); 1.132 + 1.133 + assert.equal(findClosestLocale(["en-US"], ["en-us"]), 1.134 + "en-US", "We keep the original one as result 1/2"); 1.135 + assert.equal(findClosestLocale(["en-us"], ["en-us"]), 1.136 + "en-us", "We keep the original one as result 2/2"); 1.137 + 1.138 + assert.equal(findClosestLocale(["ja-JP-mac"], ["ja-jp-mac"]), 1.139 + "ja-JP-mac", "We accept locale with 3 parts"); 1.140 + assert.equal(findClosestLocale(["ja-JP"], ["ja-jp-mac"]), 1.141 + "ja-JP", "We accept locale with 2 parts from locale with 3 parts"); 1.142 + assert.equal(findClosestLocale(["ja"], ["ja-jp-mac"]), 1.143 + "ja", "We accept locale with 1 part from locale with 3 parts"); 1.144 +}; 1.145 + 1.146 +require('sdk/test').run(exports);