addon-sdk/source/test/addons/l10n-properties/main.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/test/addons/l10n-properties/main.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,188 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +"use strict";
     1.8 +
     1.9 +const prefs = require("sdk/preferences/service");
    1.10 +const { Loader } = require('sdk/test/loader');
    1.11 +const { resolveURI } = require('toolkit/loader');
    1.12 +const { rootURI } = require("@loader/options");
    1.13 +const { usingJSON } = require('sdk/l10n/json/core');
    1.14 +
    1.15 +const PREF_MATCH_OS_LOCALE  = "intl.locale.matchOS";
    1.16 +const PREF_SELECTED_LOCALE  = "general.useragent.locale";
    1.17 +
    1.18 +function setLocale(locale) {
    1.19 +  prefs.set(PREF_MATCH_OS_LOCALE, false);
    1.20 +  prefs.set(PREF_SELECTED_LOCALE, locale);
    1.21 +}
    1.22 +
    1.23 +function resetLocale() {
    1.24 +  prefs.reset(PREF_MATCH_OS_LOCALE);
    1.25 +  prefs.reset(PREF_SELECTED_LOCALE);
    1.26 +}
    1.27 +
    1.28 +function definePseudo(loader, id, exports) {
    1.29 +  let uri = resolveURI(id, loader.mapping);
    1.30 +  loader.modules[uri] = { exports: exports };
    1.31 +}
    1.32 +
    1.33 +function createTest(locale, testFunction) {
    1.34 +  return function (assert, done) {
    1.35 +    let loader = Loader(module);
    1.36 +    // Change the locale before loading new l10n modules in order to load
    1.37 +    // the right .json file
    1.38 +    setLocale(locale);
    1.39 +    // Initialize main l10n module in order to load new locale files
    1.40 +    loader.require("sdk/l10n/loader").
    1.41 +      load(rootURI).
    1.42 +      then(function success(data) {
    1.43 +             definePseudo(loader, '@l10n/data', data);
    1.44 +             // Execute the given test function
    1.45 +             try {
    1.46 +               testFunction(assert, loader, function onDone() {
    1.47 +                 loader.unload();
    1.48 +                 resetLocale();
    1.49 +                 done();
    1.50 +               });
    1.51 +             }
    1.52 +             catch(e) {
    1.53 +              console.exception(e);
    1.54 +             }
    1.55 +           },
    1.56 +           function failure(error) {
    1.57 +             assert.fail("Unable to load locales: " + error);
    1.58 +           });
    1.59 +  };
    1.60 +}
    1.61 +
    1.62 +exports.testExactMatching = createTest("fr-FR", function(assert, loader, done) {
    1.63 +  let _ = loader.require("sdk/l10n").get;
    1.64 +  assert.equal(_("Not translated"), "Not translated",
    1.65 +                   "Key not translated");
    1.66 +  assert.equal(_("Translated"), "Oui",
    1.67 +                   "Simple key translated");
    1.68 +
    1.69 +  // Placeholders
    1.70 +  assert.equal(_("placeholderString", "works"), "Placeholder works",
    1.71 +                   "Value with placeholder");
    1.72 +  assert.equal(_("Placeholder %s", "works"), "Placeholder works",
    1.73 +                   "Key without value but with placeholder");
    1.74 +  assert.equal(_("Placeholders %2s %1s %s.", "working", "are", "correctly"),
    1.75 +                   "Placeholders are working correctly.",
    1.76 +                   "Multiple placeholders");
    1.77 +
    1.78 +  // Plurals
    1.79 +   assert.equal(_("downloadsCount", 0),
    1.80 +                   "0 téléchargement",
    1.81 +                   "PluralForm form 'one' for 0 in french");
    1.82 +  assert.equal(_("downloadsCount", 1),
    1.83 +                   "1 téléchargement",
    1.84 +                   "PluralForm form 'one' for 1 in french");
    1.85 +  assert.equal(_("downloadsCount", 2),
    1.86 +                   "2 téléchargements",
    1.87 +                   "PluralForm form 'other' for n > 1 in french");
    1.88 +
    1.89 +  done();
    1.90 +});
    1.91 +
    1.92 +exports.testHtmlLocalization = createTest("en-GB", function(assert, loader, done) {
    1.93 +  // Ensure initing html component that watch document creations
    1.94 +  // Note that this module is automatically initialized in
    1.95 +  // cuddlefish.js:Loader.main in regular addons. But it isn't for unit tests.
    1.96 +  let loaderHtmlL10n = loader.require("sdk/l10n/html");
    1.97 +  loaderHtmlL10n.enable();
    1.98 +
    1.99 +  let uri = require("sdk/self").data.url("test-localization.html");
   1.100 +  let worker = loader.require("sdk/page-worker").Page({
   1.101 +    contentURL: uri,
   1.102 +    contentScript: "new " + function ContentScriptScope() {
   1.103 +      let nodes = document.body.querySelectorAll("*[data-l10n-id]");
   1.104 +      self.postMessage([nodes[0].innerHTML,
   1.105 +                        nodes[1].innerHTML,
   1.106 +                        nodes[2].innerHTML,
   1.107 +                        nodes[3].innerHTML]);
   1.108 +    },
   1.109 +    onMessage: function (data) {
   1.110 +      assert.equal(
   1.111 +        data[0],
   1.112 +        "Kept as-is",
   1.113 +        "Nodes with unknown id in .properties are kept 'as-is'"
   1.114 +      );
   1.115 +      assert.equal(data[1], "Yes", "HTML is translated");
   1.116 +      assert.equal(
   1.117 +        data[2],
   1.118 +        "no <b>HTML</b> injection",
   1.119 +        "Content from .properties is text content; HTML can't be injected."
   1.120 +      );
   1.121 +      assert.equal(data[3], "Yes", "Multiple elements with same data-l10n-id are accepted.");
   1.122 +
   1.123 +      done();
   1.124 +    }
   1.125 +  });
   1.126 +});
   1.127 +
   1.128 +exports.testEnUsLocaleName = createTest("en-GB", function(assert, loader, done) {
   1.129 +  let _ = loader.require("sdk/l10n").get;
   1.130 +
   1.131 +  assert.equal(_("Not translated"), "Not translated",
   1.132 +               "String w/o translation is kept as-is");
   1.133 +  assert.equal(_("Translated"), "Yes",
   1.134 +               "String with translation is correctly translated");
   1.135 +
   1.136 +  // Check Unicode char escaping sequences
   1.137 +  assert.equal(_("unicodeEscape"), " @ ",
   1.138 +               "Unicode escaped sequances are correctly converted");
   1.139 +
   1.140 +  // Check plural forms regular matching
   1.141 +  assert.equal(_("downloadsCount", 0),
   1.142 +                   "0 downloads",
   1.143 +                   "PluralForm form 'other' for 0 in english");
   1.144 +  assert.equal(_("downloadsCount", 1),
   1.145 +                   "one download",
   1.146 +                   "PluralForm form 'one' for 1 in english");
   1.147 +  assert.equal(_("downloadsCount", 2),
   1.148 +                   "2 downloads",
   1.149 +                   "PluralForm form 'other' for n != 1 in english");
   1.150 +
   1.151 +  // Check optional plural forms
   1.152 +  assert.equal(_("pluralTest", 0),
   1.153 +                   "optional zero form",
   1.154 +                   "PluralForm form 'zero' can be optionaly specified. (Isn't mandatory in english)");
   1.155 +  assert.equal(_("pluralTest", 1),
   1.156 +                   "fallback to other",
   1.157 +                   "If the specific plural form is missing, we fallback to 'other'");
   1.158 +
   1.159 +  // Ensure that we can omit specifying the generic key without [other]
   1.160 +  // key[one] = ...
   1.161 +  // key[other] = ...  # Instead of `key = ...`
   1.162 +  assert.equal(_("explicitPlural", 1),
   1.163 +                   "one",
   1.164 +                   "PluralForm form can be omitting generic key [i.e. without ...[other] at end of key)");
   1.165 +  assert.equal(_("explicitPlural", 10),
   1.166 +                   "other",
   1.167 +                   "PluralForm form can be omitting generic key [i.e. without ...[other] at end of key)");
   1.168 +
   1.169 +  done();
   1.170 +});
   1.171 +
   1.172 +exports.testUsingJSON = function(assert) {
   1.173 +  assert.equal(usingJSON, false, 'not using json');
   1.174 +}
   1.175 +
   1.176 +exports.testShortLocaleName = createTest("eo", function(assert, loader, done) {
   1.177 +  let _ = loader.require("sdk/l10n").get;
   1.178 +  assert.equal(_("Not translated"), "Not translated",
   1.179 +               "String w/o translation is kept as-is");
   1.180 +  assert.equal(_("Translated"), "jes",
   1.181 +               "String with translation is correctly translated");
   1.182 +
   1.183 +  done();
   1.184 +});
   1.185 +
   1.186 +
   1.187 +// Before running tests, disable HTML service which is automatially enabled
   1.188 +// in api-utils/addon/runner.js
   1.189 +require('sdk/l10n/html').disable();
   1.190 +
   1.191 +require("sdk/test/runner").runTestsFromModule(module);

mercurial