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: michael@0: var MANIFESTS = [ michael@0: do_get_file("data/test_bug519468.manifest") michael@0: ]; michael@0: michael@0: // Stub in the locale service so we can control what gets returned as the OS locale setting michael@0: Components.utils.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: michael@0: let stubOSLocale = null; michael@0: michael@0: stubID = Components.ID("9d09d686-d913-414c-a1e6-4be8652d7d93"); michael@0: localeContractID = "@mozilla.org/intl/nslocaleservice;1"; michael@0: michael@0: StubLocaleService = { michael@0: classDescription: "Stub version of Locale service for testing", michael@0: classID: stubID, michael@0: contractID: localeContractID, michael@0: QueryInterface: XPCOMUtils.generateQI([Ci.nsILocaleService, Ci.nsISupports, Ci.nsIFactory]), michael@0: michael@0: createInstance: function (outer, iid) { michael@0: if (outer) michael@0: throw Components.results.NS_ERROR_NO_AGGREGATION; michael@0: return this.QueryInterface(iid); michael@0: }, michael@0: lockFactory: function (lock) { michael@0: throw Components.results.NS_ERROR_NOT_IMPLEMENTED; michael@0: }, michael@0: michael@0: getLocaleComponentForUserAgent: function SLS_getLocaleComponentForUserAgent() michael@0: { michael@0: return stubOSLocale; michael@0: } michael@0: } michael@0: michael@0: let registrar = Components.manager.nsIComponentRegistrar; michael@0: // Save original factory. michael@0: let localeCID = registrar.contractIDToCID(localeContractID) michael@0: let originalFactory = michael@0: Components.manager.getClassObject(Components.classes[localeContractID], michael@0: Components.interfaces.nsIFactory); michael@0: michael@0: registrar.registerFactory(stubID, "Unit test Locale Service", localeContractID, StubLocaleService); michael@0: michael@0: // Now fire up the test michael@0: do_test_pending() michael@0: registerManifests(MANIFESTS); michael@0: michael@0: var chromeReg = Cc["@mozilla.org/chrome/chrome-registry;1"] michael@0: .getService(Ci.nsIXULChromeRegistry) michael@0: .QueryInterface(Ci.nsIToolkitChromeRegistry); michael@0: chromeReg.checkForNewChrome(); michael@0: michael@0: var prefService = Cc["@mozilla.org/preferences-service;1"] michael@0: .getService(Ci.nsIPrefService) michael@0: .QueryInterface(Ci.nsIPrefBranch); michael@0: var os = Cc["@mozilla.org/observer-service;1"] michael@0: .getService(Ci.nsIObserverService); michael@0: michael@0: var testsLocale = [ michael@0: // These tests cover when the OS local is included in our manifest michael@0: {matchOS: false, selected: "en-US", osLocale: "xx-AA", locale: "en-US"}, michael@0: {matchOS: true, selected: "en-US", osLocale: "xx-AA", locale: "xx-AA"}, michael@0: {matchOS: false, selected: "fr-FR", osLocale: "xx-AA", locale: "fr-FR"}, michael@0: {matchOS: true, selected: "fr-FR", osLocale: "xx-AA", locale: "xx-AA"}, michael@0: {matchOS: false, selected: "de-DE", osLocale: "xx-AA", locale: "de-DE"}, michael@0: {matchOS: true, selected: "de-DE", osLocale: "xx-AA", locale: "xx-AA"}, michael@0: // these tests cover the case where the OS locale is not available in our manifest, but the michael@0: // base language is (ie, substitute xx-AA which we have for xx-BB which we don't) michael@0: {matchOS: false, selected: "en-US", osLocale: "xx-BB", locale: "en-US"}, michael@0: {matchOS: true, selected: "en-US", osLocale: "xx-BB", locale: "xx-AA"}, michael@0: {matchOS: false, selected: "fr-FR", osLocale: "xx-BB", locale: "fr-FR"}, michael@0: {matchOS: true, selected: "fr-FR", osLocale: "xx-BB", locale: "xx-AA"}, michael@0: // These tests cover where the language is not available michael@0: {matchOS: false, selected: "en-US", osLocale: "xy-BB", locale: "en-US"}, michael@0: {matchOS: true, selected: "en-US", osLocale: "xy-BB", locale: "en-US"}, michael@0: {matchOS: false, selected: "fr-FR", osLocale: "xy-BB", locale: "fr-FR"}, michael@0: {matchOS: true, selected: "fr-FR", osLocale: "xy-BB", locale: "en-US"}, michael@0: ]; michael@0: michael@0: var observedLocale = null; michael@0: michael@0: function test_locale(aTest) { michael@0: observedLocale = null; michael@0: michael@0: stubOSLocale = aTest.osLocale; michael@0: prefService.setBoolPref("intl.locale.matchOS", aTest.matchOS); michael@0: prefService.setCharPref("general.useragent.locale", aTest.selected); michael@0: michael@0: chromeReg.reloadChrome(); michael@0: michael@0: do_check_eq(observedLocale, aTest.locale); michael@0: } michael@0: michael@0: // Callback function for observing locale change. May be called more than once michael@0: // per test iteration. michael@0: function checkValidity() { michael@0: observedLocale = chromeReg.getSelectedLocale("testmatchos"); michael@0: dump("checkValidity called back with locale = " + observedLocale + "\n"); michael@0: } michael@0: michael@0: function run_test() { michael@0: os.addObserver(checkValidity, "selected-locale-has-changed", false); michael@0: michael@0: for (let count = 0 ; count < testsLocale.length ; count++) { michael@0: dump("count = " + count + " " + testsLocale[count].toSource() + "\n"); michael@0: test_locale(testsLocale[count]); michael@0: } michael@0: michael@0: os.removeObserver(checkValidity, "selected-locale-has-changed"); michael@0: do_test_finished(); michael@0: }