realm-locale.js (1835B)
1 // |reftest| skip-if(!this.hasOwnProperty('Intl')||!xulRuntime.shell) 2 3 // Don't run in browser because `SpecialPowers.Cu.getJSTestingFunctions()` doesn't 4 // appear to be able to change locales in other Realms. 5 6 // Default locale for jstests is en-US. 7 const defaultLocale = "en-US"; 8 9 assertEq(getDefaultLocale(), defaultLocale); 10 11 function timeZoneComment(date) { 12 return date.toString().match(/\((.*?)\)/)[1]; 13 } 14 15 function test(locale, timeZoneName) { 16 // The locale of the initial global is correct. 17 assertEq(getRealmLocale(), defaultLocale); 18 19 // Create a new global with a different locale. 20 var g = newGlobal({locale}); 21 22 var initialLocale = locale ?? defaultLocale; 23 24 // Ensure the new global has the expected locale. 25 assertEq(g.getRealmLocale(), initialLocale); 26 27 // Create a Date object in the new global. 28 var d = new g.Date(0); 29 30 // Get the time zone name from Date.prototype.toString. 31 assertEq(timeZoneComment(d), timeZoneName); 32 33 // Change the locale of the new global. 34 g.setRealmLocale("fr"); 35 36 // Ensure the new global has the expected locale. 37 assertEq(g.getRealmLocale(), "fr"); 38 39 // The locale of the initial global hasn't changed. 40 assertEq(getRealmLocale(), defaultLocale); 41 42 // Ensure the time zone comment uses the new locale. 43 assertEq(timeZoneComment(d), "heure normale du Pacifique nord-américain"); 44 45 // Change the locale of the new global to use the default locale. 46 g.setRealmLocale(undefined); 47 48 // Ensure the new global has the expected locale. 49 assertEq(g.getRealmLocale(), defaultLocale); 50 51 // Ensure the time zone comment uses the default locale. 52 assertEq(timeZoneComment(d), "Pacific Standard Time"); 53 } 54 55 test(undefined, "Pacific Standard Time"); 56 test("de", "Nordamerikanische Westküsten-Normalzeit"); 57 58 if (typeof reportCompare === "function") 59 reportCompare(true, true);