|
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 */ |
|
5 |
|
6 var MANIFESTS = [ |
|
7 do_get_file("data/test_bug519468.manifest") |
|
8 ]; |
|
9 |
|
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"); |
|
12 |
|
13 let stubOSLocale = null; |
|
14 |
|
15 stubID = Components.ID("9d09d686-d913-414c-a1e6-4be8652d7d93"); |
|
16 localeContractID = "@mozilla.org/intl/nslocaleservice;1"; |
|
17 |
|
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]), |
|
23 |
|
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 }, |
|
32 |
|
33 getLocaleComponentForUserAgent: function SLS_getLocaleComponentForUserAgent() |
|
34 { |
|
35 return stubOSLocale; |
|
36 } |
|
37 } |
|
38 |
|
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); |
|
45 |
|
46 registrar.registerFactory(stubID, "Unit test Locale Service", localeContractID, StubLocaleService); |
|
47 |
|
48 // Now fire up the test |
|
49 do_test_pending() |
|
50 registerManifests(MANIFESTS); |
|
51 |
|
52 var chromeReg = Cc["@mozilla.org/chrome/chrome-registry;1"] |
|
53 .getService(Ci.nsIXULChromeRegistry) |
|
54 .QueryInterface(Ci.nsIToolkitChromeRegistry); |
|
55 chromeReg.checkForNewChrome(); |
|
56 |
|
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); |
|
62 |
|
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 ]; |
|
83 |
|
84 var observedLocale = null; |
|
85 |
|
86 function test_locale(aTest) { |
|
87 observedLocale = null; |
|
88 |
|
89 stubOSLocale = aTest.osLocale; |
|
90 prefService.setBoolPref("intl.locale.matchOS", aTest.matchOS); |
|
91 prefService.setCharPref("general.useragent.locale", aTest.selected); |
|
92 |
|
93 chromeReg.reloadChrome(); |
|
94 |
|
95 do_check_eq(observedLocale, aTest.locale); |
|
96 } |
|
97 |
|
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 } |
|
104 |
|
105 function run_test() { |
|
106 os.addObserver(checkValidity, "selected-locale-has-changed", false); |
|
107 |
|
108 for (let count = 0 ; count < testsLocale.length ; count++) { |
|
109 dump("count = " + count + " " + testsLocale[count].toSource() + "\n"); |
|
110 test_locale(testsLocale[count]); |
|
111 } |
|
112 |
|
113 os.removeObserver(checkValidity, "selected-locale-has-changed"); |
|
114 do_test_finished(); |
|
115 } |