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