chrome/test/unit/test_bug519468.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
     4  */
     6 var MANIFESTS = [
     7   do_get_file("data/test_bug519468.manifest")
     8 ];
    10 // Stub in the locale service so we can control what gets returned as the OS locale setting
    11 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
    13 let stubOSLocale = null;
    15 stubID = Components.ID("9d09d686-d913-414c-a1e6-4be8652d7d93");
    16 localeContractID = "@mozilla.org/intl/nslocaleservice;1";
    18 StubLocaleService = {
    19   classDescription: "Stub version of Locale service for testing",
    20   classID:          stubID,
    21   contractID:       localeContractID,
    22   QueryInterface:   XPCOMUtils.generateQI([Ci.nsILocaleService, Ci.nsISupports, Ci.nsIFactory]),
    24   createInstance: function (outer, iid) {
    25     if (outer)
    26       throw Components.results.NS_ERROR_NO_AGGREGATION;
    27     return this.QueryInterface(iid);
    28   },
    29   lockFactory: function (lock) {
    30     throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
    31   },
    33   getLocaleComponentForUserAgent: function SLS_getLocaleComponentForUserAgent()
    34   {
    35     return stubOSLocale;
    36   }
    37 }
    39 let registrar = Components.manager.nsIComponentRegistrar;
    40 // Save original factory.
    41 let localeCID = registrar.contractIDToCID(localeContractID)
    42 let originalFactory =
    43       Components.manager.getClassObject(Components.classes[localeContractID],
    44                                         Components.interfaces.nsIFactory);
    46 registrar.registerFactory(stubID, "Unit test Locale Service", localeContractID, StubLocaleService);
    48 // Now fire up the test
    49 do_test_pending()
    50 registerManifests(MANIFESTS);
    52 var chromeReg = Cc["@mozilla.org/chrome/chrome-registry;1"]
    53                 .getService(Ci.nsIXULChromeRegistry)
    54                 .QueryInterface(Ci.nsIToolkitChromeRegistry);
    55 chromeReg.checkForNewChrome();
    57 var prefService = Cc["@mozilla.org/preferences-service;1"]
    58                   .getService(Ci.nsIPrefService)
    59                   .QueryInterface(Ci.nsIPrefBranch);
    60 var os = Cc["@mozilla.org/observer-service;1"]
    61          .getService(Ci.nsIObserverService);
    63 var testsLocale = [
    64   // These tests cover when the OS local is included in our manifest
    65   {matchOS: false, selected: "en-US", osLocale: "xx-AA", locale: "en-US"},
    66   {matchOS: true, selected: "en-US", osLocale: "xx-AA", locale: "xx-AA"},
    67   {matchOS: false, selected: "fr-FR", osLocale: "xx-AA", locale: "fr-FR"},
    68   {matchOS: true, selected: "fr-FR", osLocale: "xx-AA", locale: "xx-AA"},
    69   {matchOS: false, selected: "de-DE", osLocale: "xx-AA", locale: "de-DE"},
    70   {matchOS: true, selected: "de-DE", osLocale: "xx-AA", locale: "xx-AA"},
    71   // these tests cover the case where the OS locale is not available in our manifest, but the
    72   // base language is (ie, substitute xx-AA which we have for xx-BB which we don't)
    73   {matchOS: false, selected: "en-US", osLocale: "xx-BB", locale: "en-US"},
    74   {matchOS: true, selected: "en-US", osLocale: "xx-BB", locale: "xx-AA"},
    75   {matchOS: false, selected: "fr-FR", osLocale: "xx-BB", locale: "fr-FR"},
    76   {matchOS: true, selected: "fr-FR", osLocale: "xx-BB", locale: "xx-AA"},
    77   // These tests cover where the language is not available
    78   {matchOS: false, selected: "en-US", osLocale: "xy-BB", locale: "en-US"},
    79   {matchOS: true, selected: "en-US", osLocale: "xy-BB", locale: "en-US"},
    80   {matchOS: false, selected: "fr-FR", osLocale: "xy-BB", locale: "fr-FR"},
    81   {matchOS: true, selected: "fr-FR", osLocale: "xy-BB", locale: "en-US"},
    82 ];
    84 var observedLocale = null;
    86 function test_locale(aTest) {
    87   observedLocale = null;
    89   stubOSLocale = aTest.osLocale;
    90   prefService.setBoolPref("intl.locale.matchOS", aTest.matchOS);
    91   prefService.setCharPref("general.useragent.locale", aTest.selected);
    93   chromeReg.reloadChrome();
    95   do_check_eq(observedLocale, aTest.locale);
    96 }
    98 // Callback function for observing locale change. May be called more than once
    99 // per test iteration.
   100 function checkValidity() {
   101   observedLocale = chromeReg.getSelectedLocale("testmatchos");
   102   dump("checkValidity called back with locale = " + observedLocale + "\n");
   103 }
   105 function run_test() {
   106   os.addObserver(checkValidity, "selected-locale-has-changed", false);
   108   for (let count = 0 ; count < testsLocale.length ; count++) {
   109     dump("count = " + count + " " + testsLocale[count].toSource() + "\n");
   110     test_locale(testsLocale[count]);
   111   }
   113   os.removeObserver(checkValidity, "selected-locale-has-changed");
   114   do_test_finished();
   115 }

mercurial