main.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. "use strict";
  5. const prefs = require("sdk/preferences/service");
  6. const { Loader } = require('sdk/test/loader');
  7. const { resolveURI } = require('toolkit/loader');
  8. const { rootURI } = require("@loader/options");
  9. const PREF_MATCH_OS_LOCALE = "intl.locale.matchOS";
  10. const PREF_SELECTED_LOCALE = "general.useragent.locale";
  11. function setLocale(locale) {
  12. prefs.set(PREF_MATCH_OS_LOCALE, false);
  13. prefs.set(PREF_SELECTED_LOCALE, locale);
  14. }
  15. function resetLocale() {
  16. prefs.reset(PREF_MATCH_OS_LOCALE);
  17. prefs.reset(PREF_SELECTED_LOCALE);
  18. }
  19. function definePseudo(loader, id, exports) {
  20. let uri = resolveURI(id, loader.mapping);
  21. loader.modules[uri] = { exports: exports };
  22. }
  23. function createTest(locale, testFunction) {
  24. return function (assert, done) {
  25. let loader = Loader(module);
  26. // Change the locale before loading new l10n modules in order to load
  27. // the right .json file
  28. setLocale(locale);
  29. // Initialize main l10n module in order to load new locale files
  30. loader.require("sdk/l10n/loader").
  31. load(rootURI).
  32. then(function success(data) {
  33. definePseudo(loader, '@l10n/data', data);
  34. // Execute the given test function
  35. try {
  36. testFunction(assert, loader, function onDone() {
  37. loader.unload();
  38. resetLocale();
  39. done();
  40. });
  41. }
  42. catch(e) {
  43. console.exception(e);
  44. }
  45. },
  46. function failure(error) {
  47. assert.fail("Unable to load locales: " + error);
  48. });
  49. };
  50. }
  51. exports.testExactMatching = createTest("fr-FR", function(assert, loader, done) {
  52. let _ = loader.require("sdk/l10n").get;
  53. assert.equal(_("Not translated"), "Not translated",
  54. "Key not translated");
  55. assert.equal(_("Translated"), "Oui",
  56. "Simple key translated");
  57. // Placeholders
  58. assert.equal(_("placeholderString", "works"), "Placeholder works",
  59. "Value with placeholder");
  60. assert.equal(_("Placeholder %s", "works"), "Placeholder works",
  61. "Key without value but with placeholder");
  62. assert.equal(_("Placeholders %2s %1s %s.", "working", "are", "correctly"),
  63. "Placeholders are working correctly.",
  64. "Multiple placeholders");
  65. // Plurals
  66. assert.equal(_("downloadsCount", 0),
  67. "0 téléchargement",
  68. "PluralForm form 'one' for 0 in french");
  69. assert.equal(_("downloadsCount", 1),
  70. "1 téléchargement",
  71. "PluralForm form 'one' for 1 in french");
  72. assert.equal(_("downloadsCount", 2),
  73. "2 téléchargements",
  74. "PluralForm form 'other' for n > 1 in french");
  75. done();
  76. });
  77. exports.testHtmlLocalization = createTest("en-GB", function(assert, loader, done) {
  78. // Ensure initing html component that watch document creations
  79. // Note that this module is automatically initialized in
  80. // cuddlefish.js:Loader.main in regular addons. But it isn't for unit tests.
  81. let loaderHtmlL10n = loader.require("sdk/l10n/html");
  82. loaderHtmlL10n.enable();
  83. let uri = require("sdk/self").data.url("test-localization.html");
  84. let worker = loader.require("sdk/page-worker").Page({
  85. contentURL: uri,
  86. contentScript: "new " + function ContentScriptScope() {
  87. let nodes = document.body.querySelectorAll("*[data-l10n-id]");
  88. self.postMessage([nodes[0].innerHTML,
  89. nodes[1].innerHTML,
  90. nodes[2].innerHTML,
  91. nodes[3].innerHTML]);
  92. },
  93. onMessage: function (data) {
  94. assert.equal(
  95. data[0],
  96. "Kept as-is",
  97. "Nodes with unknown id in .properties are kept 'as-is'"
  98. );
  99. assert.equal(data[1], "Yes", "HTML is translated");
  100. assert.equal(
  101. data[2],
  102. "no <b>HTML</b> injection",
  103. "Content from .properties is text content; HTML can't be injected."
  104. );
  105. assert.equal(data[3], "Yes", "Multiple elements with same data-l10n-id are accepted.");
  106. done();
  107. }
  108. });
  109. });
  110. exports.testEnUsLocaleName = createTest("en-US", function(assert, loader, done) {
  111. let _ = loader.require("sdk/l10n").get;
  112. assert.equal(_("Not translated"), "Not translated",
  113. "String w/o translation is kept as-is");
  114. assert.equal(_("Translated"), "Yes",
  115. "String with translation is correctly translated");
  116. // Check Unicode char escaping sequences
  117. assert.equal(_("unicodeEscape"), " @ ",
  118. "Unicode escaped sequances are correctly converted");
  119. // Check plural forms regular matching
  120. assert.equal(_("downloadsCount", 0),
  121. "0 downloads",
  122. "PluralForm form 'other' for 0 in english");
  123. assert.equal(_("downloadsCount", 1),
  124. "one download",
  125. "PluralForm form 'one' for 1 in english");
  126. assert.equal(_("downloadsCount", 2),
  127. "2 downloads",
  128. "PluralForm form 'other' for n != 1 in english");
  129. // Check optional plural forms
  130. assert.equal(_("pluralTest", 0),
  131. "optional zero form",
  132. "PluralForm form 'zero' can be optionaly specified. (Isn't mandatory in english)");
  133. assert.equal(_("pluralTest", 1),
  134. "fallback to other",
  135. "If the specific plural form is missing, we fallback to 'other'");
  136. // Ensure that we can omit specifying the generic key without [other]
  137. // key[one] = ...
  138. // key[other] = ... # Instead of `key = ...`
  139. assert.equal(_("explicitPlural", 1),
  140. "one",
  141. "PluralForm form can be omitting generic key [i.e. without ...[other] at end of key)");
  142. assert.equal(_("explicitPlural", 10),
  143. "other",
  144. "PluralForm form can be omitting generic key [i.e. without ...[other] at end of key)");
  145. done();
  146. });
  147. exports.testShortLocaleName = createTest("eo", function(assert, loader, done) {
  148. let _ = loader.require("sdk/l10n").get;
  149. assert.equal(_("Not translated"), "Not translated",
  150. "String w/o translation is kept as-is");
  151. assert.equal(_("Translated"), "jes",
  152. "String with translation is correctly translated");
  153. done();
  154. });
  155. // Before running tests, disable HTML service which is automatially enabled
  156. // in api-utils/addon/runner.js
  157. require('sdk/l10n/html').disable();
  158. require("sdk/test/runner").runTestsFromModule(module);