1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/chrome/test/unit/test_bug519468.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,115 @@ 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 + 1.9 +var MANIFESTS = [ 1.10 + do_get_file("data/test_bug519468.manifest") 1.11 +]; 1.12 + 1.13 +// Stub in the locale service so we can control what gets returned as the OS locale setting 1.14 +Components.utils.import("resource://gre/modules/XPCOMUtils.jsm"); 1.15 + 1.16 +let stubOSLocale = null; 1.17 + 1.18 +stubID = Components.ID("9d09d686-d913-414c-a1e6-4be8652d7d93"); 1.19 +localeContractID = "@mozilla.org/intl/nslocaleservice;1"; 1.20 + 1.21 +StubLocaleService = { 1.22 + classDescription: "Stub version of Locale service for testing", 1.23 + classID: stubID, 1.24 + contractID: localeContractID, 1.25 + QueryInterface: XPCOMUtils.generateQI([Ci.nsILocaleService, Ci.nsISupports, Ci.nsIFactory]), 1.26 + 1.27 + createInstance: function (outer, iid) { 1.28 + if (outer) 1.29 + throw Components.results.NS_ERROR_NO_AGGREGATION; 1.30 + return this.QueryInterface(iid); 1.31 + }, 1.32 + lockFactory: function (lock) { 1.33 + throw Components.results.NS_ERROR_NOT_IMPLEMENTED; 1.34 + }, 1.35 + 1.36 + getLocaleComponentForUserAgent: function SLS_getLocaleComponentForUserAgent() 1.37 + { 1.38 + return stubOSLocale; 1.39 + } 1.40 +} 1.41 + 1.42 +let registrar = Components.manager.nsIComponentRegistrar; 1.43 +// Save original factory. 1.44 +let localeCID = registrar.contractIDToCID(localeContractID) 1.45 +let originalFactory = 1.46 + Components.manager.getClassObject(Components.classes[localeContractID], 1.47 + Components.interfaces.nsIFactory); 1.48 + 1.49 +registrar.registerFactory(stubID, "Unit test Locale Service", localeContractID, StubLocaleService); 1.50 + 1.51 +// Now fire up the test 1.52 +do_test_pending() 1.53 +registerManifests(MANIFESTS); 1.54 + 1.55 +var chromeReg = Cc["@mozilla.org/chrome/chrome-registry;1"] 1.56 + .getService(Ci.nsIXULChromeRegistry) 1.57 + .QueryInterface(Ci.nsIToolkitChromeRegistry); 1.58 +chromeReg.checkForNewChrome(); 1.59 + 1.60 +var prefService = Cc["@mozilla.org/preferences-service;1"] 1.61 + .getService(Ci.nsIPrefService) 1.62 + .QueryInterface(Ci.nsIPrefBranch); 1.63 +var os = Cc["@mozilla.org/observer-service;1"] 1.64 + .getService(Ci.nsIObserverService); 1.65 + 1.66 +var testsLocale = [ 1.67 + // These tests cover when the OS local is included in our manifest 1.68 + {matchOS: false, selected: "en-US", osLocale: "xx-AA", locale: "en-US"}, 1.69 + {matchOS: true, selected: "en-US", osLocale: "xx-AA", locale: "xx-AA"}, 1.70 + {matchOS: false, selected: "fr-FR", osLocale: "xx-AA", locale: "fr-FR"}, 1.71 + {matchOS: true, selected: "fr-FR", osLocale: "xx-AA", locale: "xx-AA"}, 1.72 + {matchOS: false, selected: "de-DE", osLocale: "xx-AA", locale: "de-DE"}, 1.73 + {matchOS: true, selected: "de-DE", osLocale: "xx-AA", locale: "xx-AA"}, 1.74 + // these tests cover the case where the OS locale is not available in our manifest, but the 1.75 + // base language is (ie, substitute xx-AA which we have for xx-BB which we don't) 1.76 + {matchOS: false, selected: "en-US", osLocale: "xx-BB", locale: "en-US"}, 1.77 + {matchOS: true, selected: "en-US", osLocale: "xx-BB", locale: "xx-AA"}, 1.78 + {matchOS: false, selected: "fr-FR", osLocale: "xx-BB", locale: "fr-FR"}, 1.79 + {matchOS: true, selected: "fr-FR", osLocale: "xx-BB", locale: "xx-AA"}, 1.80 + // These tests cover where the language is not available 1.81 + {matchOS: false, selected: "en-US", osLocale: "xy-BB", locale: "en-US"}, 1.82 + {matchOS: true, selected: "en-US", osLocale: "xy-BB", locale: "en-US"}, 1.83 + {matchOS: false, selected: "fr-FR", osLocale: "xy-BB", locale: "fr-FR"}, 1.84 + {matchOS: true, selected: "fr-FR", osLocale: "xy-BB", locale: "en-US"}, 1.85 +]; 1.86 + 1.87 +var observedLocale = null; 1.88 + 1.89 +function test_locale(aTest) { 1.90 + observedLocale = null; 1.91 + 1.92 + stubOSLocale = aTest.osLocale; 1.93 + prefService.setBoolPref("intl.locale.matchOS", aTest.matchOS); 1.94 + prefService.setCharPref("general.useragent.locale", aTest.selected); 1.95 + 1.96 + chromeReg.reloadChrome(); 1.97 + 1.98 + do_check_eq(observedLocale, aTest.locale); 1.99 +} 1.100 + 1.101 +// Callback function for observing locale change. May be called more than once 1.102 +// per test iteration. 1.103 +function checkValidity() { 1.104 + observedLocale = chromeReg.getSelectedLocale("testmatchos"); 1.105 + dump("checkValidity called back with locale = " + observedLocale + "\n"); 1.106 +} 1.107 + 1.108 +function run_test() { 1.109 + os.addObserver(checkValidity, "selected-locale-has-changed", false); 1.110 + 1.111 + for (let count = 0 ; count < testsLocale.length ; count++) { 1.112 + dump("count = " + count + " " + testsLocale[count].toSource() + "\n"); 1.113 + test_locale(testsLocale[count]); 1.114 + } 1.115 + 1.116 + os.removeObserver(checkValidity, "selected-locale-has-changed"); 1.117 + do_test_finished(); 1.118 +}