2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2009-11-02 10:04:45 -08:00
|
|
|
*/
|
|
|
|
|
|
|
|
var MANIFESTS = [
|
2010-11-29 17:40:42 -08:00
|
|
|
do_get_file("data/test_bug519468.manifest")
|
2009-11-02 10:04:45 -08:00
|
|
|
];
|
|
|
|
|
2012-02-14 18:33:43 -08:00
|
|
|
// 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");
|
|
|
|
|
|
|
|
let stubOSLocale = null;
|
|
|
|
|
|
|
|
stubID = Components.ID("9d09d686-d913-414c-a1e6-4be8652d7d93");
|
|
|
|
localeContractID = "@mozilla.org/intl/nslocaleservice;1";
|
|
|
|
|
|
|
|
StubLocaleService = {
|
|
|
|
classDescription: "Stub version of Locale service for testing",
|
|
|
|
classID: stubID,
|
|
|
|
contractID: localeContractID,
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsILocaleService, Ci.nsISupports, Ci.nsIFactory]),
|
|
|
|
|
|
|
|
createInstance: function (outer, iid) {
|
|
|
|
if (outer)
|
|
|
|
throw Components.results.NS_ERROR_NO_AGGREGATION;
|
|
|
|
return this.QueryInterface(iid);
|
|
|
|
},
|
|
|
|
lockFactory: function (lock) {
|
|
|
|
throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
},
|
|
|
|
|
|
|
|
getLocaleComponentForUserAgent: function SLS_getLocaleComponentForUserAgent()
|
|
|
|
{
|
|
|
|
return stubOSLocale;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let registrar = Components.manager.nsIComponentRegistrar;
|
|
|
|
// Save original factory.
|
|
|
|
let localeCID = registrar.contractIDToCID(localeContractID)
|
|
|
|
let originalFactory =
|
|
|
|
Components.manager.getClassObject(Components.classes[localeContractID],
|
|
|
|
Components.interfaces.nsIFactory);
|
|
|
|
|
|
|
|
registrar.registerFactory(stubID, "Unit test Locale Service", localeContractID, StubLocaleService);
|
|
|
|
|
|
|
|
// Now fire up the test
|
2010-11-30 06:02:23 -08:00
|
|
|
do_test_pending()
|
2009-11-02 10:04:45 -08:00
|
|
|
registerManifests(MANIFESTS);
|
|
|
|
|
|
|
|
var chromeReg = Cc["@mozilla.org/chrome/chrome-registry;1"]
|
|
|
|
.getService(Ci.nsIXULChromeRegistry)
|
|
|
|
.QueryInterface(Ci.nsIToolkitChromeRegistry);
|
2010-04-02 09:25:13 -07:00
|
|
|
chromeReg.checkForNewChrome();
|
2009-11-02 10:04:45 -08:00
|
|
|
|
|
|
|
var prefService = Cc["@mozilla.org/preferences-service;1"]
|
|
|
|
.getService(Ci.nsIPrefService)
|
|
|
|
.QueryInterface(Ci.nsIPrefBranch);
|
2010-11-30 06:02:23 -08:00
|
|
|
var os = Cc["@mozilla.org/observer-service;1"]
|
|
|
|
.getService(Ci.nsIObserverService);
|
|
|
|
|
|
|
|
var testsLocale = [
|
2012-02-14 18:33:43 -08:00
|
|
|
// These tests cover when the OS local is included in our manifest
|
|
|
|
{matchOS: false, selected: "en-US", osLocale: "xx-AA", locale: "en-US"},
|
|
|
|
{matchOS: true, selected: "en-US", osLocale: "xx-AA", locale: "xx-AA"},
|
|
|
|
{matchOS: false, selected: "fr-FR", osLocale: "xx-AA", locale: "fr-FR"},
|
|
|
|
{matchOS: true, selected: "fr-FR", osLocale: "xx-AA", locale: "xx-AA"},
|
|
|
|
{matchOS: false, selected: "de-DE", osLocale: "xx-AA", locale: "de-DE"},
|
|
|
|
{matchOS: true, selected: "de-DE", osLocale: "xx-AA", locale: "xx-AA"},
|
|
|
|
// these tests cover the case where the OS locale is not available in our manifest, but the
|
|
|
|
// base language is (ie, substitute xx-AA which we have for xx-BB which we don't)
|
|
|
|
{matchOS: false, selected: "en-US", osLocale: "xx-BB", locale: "en-US"},
|
|
|
|
{matchOS: true, selected: "en-US", osLocale: "xx-BB", locale: "xx-AA"},
|
|
|
|
{matchOS: false, selected: "fr-FR", osLocale: "xx-BB", locale: "fr-FR"},
|
|
|
|
{matchOS: true, selected: "fr-FR", osLocale: "xx-BB", locale: "xx-AA"},
|
|
|
|
// These tests cover where the language is not available
|
|
|
|
{matchOS: false, selected: "en-US", osLocale: "xy-BB", locale: "en-US"},
|
|
|
|
{matchOS: true, selected: "en-US", osLocale: "xy-BB", locale: "en-US"},
|
|
|
|
{matchOS: false, selected: "fr-FR", osLocale: "xy-BB", locale: "fr-FR"},
|
|
|
|
{matchOS: true, selected: "fr-FR", osLocale: "xy-BB", locale: "en-US"},
|
2010-11-30 06:02:23 -08:00
|
|
|
];
|
2009-11-02 10:04:45 -08:00
|
|
|
|
2012-02-14 18:33:43 -08:00
|
|
|
var observedLocale = null;
|
|
|
|
|
2009-11-02 10:04:45 -08:00
|
|
|
function test_locale(aTest) {
|
2012-02-14 18:33:43 -08:00
|
|
|
observedLocale = null;
|
|
|
|
|
|
|
|
stubOSLocale = aTest.osLocale;
|
2009-11-02 10:04:45 -08:00
|
|
|
prefService.setBoolPref("intl.locale.matchOS", aTest.matchOS);
|
2012-02-14 18:33:43 -08:00
|
|
|
prefService.setCharPref("general.useragent.locale", aTest.selected);
|
2009-11-02 10:04:45 -08:00
|
|
|
|
2010-11-30 06:02:23 -08:00
|
|
|
chromeReg.reloadChrome();
|
2012-02-14 18:33:43 -08:00
|
|
|
|
|
|
|
do_check_eq(observedLocale, aTest.locale);
|
2009-11-02 10:04:45 -08:00
|
|
|
}
|
|
|
|
|
2012-02-14 18:33:43 -08:00
|
|
|
// Callback function for observing locale change. May be called more than once
|
|
|
|
// per test iteration.
|
2010-11-30 06:02:23 -08:00
|
|
|
function checkValidity() {
|
2012-02-14 18:33:43 -08:00
|
|
|
observedLocale = chromeReg.getSelectedLocale("testmatchos");
|
|
|
|
dump("checkValidity called back with locale = " + observedLocale + "\n");
|
2009-11-02 10:04:45 -08:00
|
|
|
}
|
2010-11-30 06:02:23 -08:00
|
|
|
|
|
|
|
function run_test() {
|
|
|
|
os.addObserver(checkValidity, "selected-locale-has-changed", false);
|
2012-02-14 18:33:43 -08:00
|
|
|
|
|
|
|
for (let count = 0 ; count < testsLocale.length ; count++) {
|
|
|
|
dump("count = " + count + " " + testsLocale[count].toSource() + "\n");
|
|
|
|
test_locale(testsLocale[count]);
|
|
|
|
}
|
|
|
|
|
|
|
|
os.removeObserver(checkValidity, "selected-locale-has-changed");
|
|
|
|
do_test_finished();
|
2010-11-30 06:02:23 -08:00
|
|
|
}
|