browser_packaged_as_locales.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. XPCOMUtils.defineLazyServiceGetter(this, "aboutNewTabService",
  2. "@mozilla.org/browser/aboutnewtab-service;1",
  3. "nsIAboutNewTabService");
  4. // Tests are by default run with non-debug en-US configuration
  5. const DEFAULT_URL = SpecialPowers.getBoolPref("browser.tabs.remote.separatePrivilegedContentProcess") ?
  6. "resource://activity-stream/prerendered/en-US/activity-stream-noscripts.html" :
  7. "resource://activity-stream/prerendered/en-US/activity-stream.html";
  8. /**
  9. * Temporarily change the app locale to get the localized activity stream url
  10. */
  11. async function getUrlForLocale(locale) {
  12. const origAvailable = Services.locale.availableLocales;
  13. const origRequested = Services.locale.requestedLocales;
  14. try {
  15. Services.locale.availableLocales = [locale];
  16. Services.locale.requestedLocales = [locale];
  17. return aboutNewTabService.defaultURL;
  18. } finally {
  19. // Always clean up after returning the url
  20. Services.locale.availableLocales = origAvailable;
  21. Services.locale.requestedLocales = origRequested;
  22. }
  23. }
  24. /**
  25. * Test that an unknown locale defaults to en-US
  26. */
  27. add_task(async function test_unknown_locale() {
  28. const url = await getUrlForLocale("und");
  29. Assert.equal(url, DEFAULT_URL);
  30. });
  31. /**
  32. * Test that we at least have en-US
  33. */
  34. add_task(async function test_default_locale() {
  35. const url = await getUrlForLocale("en-US");
  36. Assert.equal(url, DEFAULT_URL);
  37. });
  38. /**
  39. * Test that we use a shared language before en-US
  40. */
  41. add_task(async function test_default_locale() {
  42. const url = await getUrlForLocale("de-UNKNOWN");
  43. Assert.equal(url, DEFAULT_URL.replace("en-US", "de"));
  44. });
  45. /**
  46. * Tests that all activity stream packaged locales can be referenced / accessed
  47. */
  48. add_task(async function test_all_packaged_locales() {
  49. let gotID = false;
  50. const listing = await (await fetch("resource://activity-stream/prerendered/")).text();
  51. for (const line of listing.split("\n").slice(2)) {
  52. const [file, , , type] = line.split(" ").slice(1);
  53. if (type === "DIRECTORY") {
  54. const locale = file.replace("/", "");
  55. if (locale !== "static") {
  56. const url = await getUrlForLocale(locale);
  57. Assert.equal(url, DEFAULT_URL.replace("en-US", locale), `can reference "${locale}" files`);
  58. // Specially remember if we saw an ID locale packaged as it can be
  59. // easily ignored by source control, e.g., .gitignore
  60. gotID |= locale === "id";
  61. }
  62. }
  63. }
  64. Assert.ok(gotID, `"id" locale packaged and not ignored`);
  65. });