Permalink
Browse files

Bug 557024: Mock out the locale service to make test reliable on non …

…en-US systems. r=bsmedberg
  • Loading branch information...
1 parent 89aef9f commit e6ad8ce14d08d17eddd741f9fd6c4e3beb9cd25b Irving Reid committed Feb 15, 2012
Showing with 78 additions and 24 deletions.
  1. +1 −0 chrome/test/unit/data/test_bug519468.manifest
  2. +77 −24 chrome/test/unit/test_bug519468.js
@@ -1,3 +1,4 @@
locale testmatchos en-US jar:en-US.jar!/locale/en-US/global/
locale testmatchos fr-FR jar:en-US.jar!/locale/en-US/global/
locale testmatchos de-DE jar:en-US.jar!/locale/en-US/global/
+locale testmatchos xx-AA jar:en-US.jar!/locale/en-US/global/
@@ -7,6 +7,45 @@ var MANIFESTS = [
do_get_file("data/test_bug519468.manifest")
];
+// 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
do_test_pending()
registerManifests(MANIFESTS);
@@ -15,48 +54,62 @@ var chromeReg = Cc["@mozilla.org/chrome/chrome-registry;1"]
.QueryInterface(Ci.nsIToolkitChromeRegistry);
chromeReg.checkForNewChrome();
-var localeService = Cc["@mozilla.org/intl/nslocaleservice;1"]
- .getService(Ci.nsILocaleService);
-
var prefService = Cc["@mozilla.org/preferences-service;1"]
.getService(Ci.nsIPrefService)
.QueryInterface(Ci.nsIPrefBranch);
var os = Cc["@mozilla.org/observer-service;1"]
.getService(Ci.nsIObserverService);
-var systemLocale = localeService.getLocaleComponentForUserAgent();
-var count = 0;
var testsLocale = [
- {matchOS: false, selected: null, locale: "en-US"},
- {matchOS: true, selected: null, locale: systemLocale},
- {matchOS: true, selected: "fr-FR", locale: systemLocale},
- {matchOS: false, selected: "fr-FR", locale: "fr-FR"},
- {matchOS: false, selected: "de-DE", locale: "de-DE"},
- {matchOS: true, selected: null, locale: systemLocale}
+ // 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"},
];
+var observedLocale = null;
+
function test_locale(aTest) {
+ observedLocale = null;
+
+ stubOSLocale = aTest.osLocale;
prefService.setBoolPref("intl.locale.matchOS", aTest.matchOS);
- prefService.setCharPref("general.useragent.locale", aTest.selected || "en-US");
+ prefService.setCharPref("general.useragent.locale", aTest.selected);
chromeReg.reloadChrome();
+
+ do_check_eq(observedLocale, aTest.locale);
}
+// Callback function for observing locale change. May be called more than once
+// per test iteration.
function checkValidity() {
- var selectedLocale = chromeReg.getSelectedLocale("testmatchos");
- do_check_eq(selectedLocale, testsLocale[count].locale);
-
- count++;
- if (count >= testsLocale.length) {
- os.removeObserver(checkValidity, "selected-locale-has-changed");
- do_test_finished();
- }
- else {
- test_locale(testsLocale[count]);
- }
+ observedLocale = chromeReg.getSelectedLocale("testmatchos");
+ dump("checkValidity called back with locale = " + observedLocale + "\n");
}
function run_test() {
os.addObserver(checkValidity, "selected-locale-has-changed", false);
- test_locale(testsLocale[count]);
+
+ 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();
}

0 comments on commit e6ad8ce

Please sign in to comment.