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

Thu, 15 Jan 2015 15:59:08 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:59:08 +0100
branch
TOR_BUG_9701
changeset 10
ac0c01689b40
permissions
-rw-r--r--

Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

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

mercurial