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 | |
michael@0 | 5 | const { getPreferedLocales, findClosestLocale } = require("sdk/l10n/locale"); |
michael@0 | 6 | const prefs = require("sdk/preferences/service"); |
michael@0 | 7 | const { Cc, Ci, Cu } = require("chrome"); |
michael@0 | 8 | const { Services } = Cu.import("resource://gre/modules/Services.jsm"); |
michael@0 | 9 | const BundleService = Cc["@mozilla.org/intl/stringbundle;1"].getService(Ci.nsIStringBundleService); |
michael@0 | 10 | |
michael@0 | 11 | const PREF_MATCH_OS_LOCALE = "intl.locale.matchOS"; |
michael@0 | 12 | const PREF_SELECTED_LOCALE = "general.useragent.locale"; |
michael@0 | 13 | const PREF_ACCEPT_LANGUAGES = "intl.accept_languages"; |
michael@0 | 14 | |
michael@0 | 15 | function assertPrefered(assert, expected, msg) { |
michael@0 | 16 | assert.equal(JSON.stringify(getPreferedLocales()), JSON.stringify(expected), |
michael@0 | 17 | msg); |
michael@0 | 18 | } |
michael@0 | 19 | |
michael@0 | 20 | exports.testGetPreferedLocales = function(assert) { |
michael@0 | 21 | prefs.set(PREF_MATCH_OS_LOCALE, false); |
michael@0 | 22 | prefs.set(PREF_SELECTED_LOCALE, ""); |
michael@0 | 23 | prefs.set(PREF_ACCEPT_LANGUAGES, ""); |
michael@0 | 24 | assertPrefered(assert, ["en-us"], |
michael@0 | 25 | "When all preferences are empty, we only have en-us"); |
michael@0 | 26 | |
michael@0 | 27 | prefs.set(PREF_SELECTED_LOCALE, "fr"); |
michael@0 | 28 | prefs.set(PREF_ACCEPT_LANGUAGES, "jp"); |
michael@0 | 29 | assertPrefered(assert, ["fr", "jp", "en-us"], |
michael@0 | 30 | "We first have useragent locale, then web one and finally en-US"); |
michael@0 | 31 | |
michael@0 | 32 | prefs.set(PREF_SELECTED_LOCALE, "en-US"); |
michael@0 | 33 | prefs.set(PREF_ACCEPT_LANGUAGES, "en-US"); |
michael@0 | 34 | assertPrefered(assert, ["en-us"], |
michael@0 | 35 | "We do not have duplicates"); |
michael@0 | 36 | |
michael@0 | 37 | prefs.set(PREF_SELECTED_LOCALE, "en-US"); |
michael@0 | 38 | prefs.set(PREF_ACCEPT_LANGUAGES, "fr"); |
michael@0 | 39 | assertPrefered(assert, ["en-us", "fr"], |
michael@0 | 40 | "en-US can be first if specified by higher priority preference"); |
michael@0 | 41 | |
michael@0 | 42 | // Reset what we changed |
michael@0 | 43 | prefs.reset(PREF_MATCH_OS_LOCALE); |
michael@0 | 44 | prefs.reset(PREF_SELECTED_LOCALE); |
michael@0 | 45 | prefs.reset(PREF_ACCEPT_LANGUAGES); |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | // In some cases, mainly on Fennec and on Linux version, |
michael@0 | 49 | // `general.useragent.locale` is a special 'localized' value, like: |
michael@0 | 50 | // "chrome://global/locale/intl.properties" |
michael@0 | 51 | exports.testPreferedLocalizedLocale = function(assert) { |
michael@0 | 52 | prefs.set(PREF_MATCH_OS_LOCALE, false); |
michael@0 | 53 | let bundleURL = "chrome://global/locale/intl.properties"; |
michael@0 | 54 | prefs.setLocalized(PREF_SELECTED_LOCALE, bundleURL); |
michael@0 | 55 | let contentLocale = "ja"; |
michael@0 | 56 | prefs.set(PREF_ACCEPT_LANGUAGES, contentLocale); |
michael@0 | 57 | |
michael@0 | 58 | // Read manually the expected locale value from the property file |
michael@0 | 59 | let expectedLocale = BundleService.createBundle(bundleURL). |
michael@0 | 60 | GetStringFromName(PREF_SELECTED_LOCALE). |
michael@0 | 61 | toLowerCase(); |
michael@0 | 62 | |
michael@0 | 63 | // First add the useragent locale |
michael@0 | 64 | let expectedLocaleList = [expectedLocale]; |
michael@0 | 65 | |
michael@0 | 66 | // Then the content locale |
michael@0 | 67 | if (expectedLocaleList.indexOf(contentLocale) == -1) |
michael@0 | 68 | expectedLocaleList.push(contentLocale); |
michael@0 | 69 | |
michael@0 | 70 | // Add default "en-us" fallback if the main language is not already en-us |
michael@0 | 71 | if (expectedLocaleList.indexOf("en-us") == -1) |
michael@0 | 72 | expectedLocaleList.push("en-us"); |
michael@0 | 73 | |
michael@0 | 74 | assertPrefered(assert, expectedLocaleList, "test localized pref value"); |
michael@0 | 75 | |
michael@0 | 76 | // Reset what we have changed |
michael@0 | 77 | prefs.reset(PREF_MATCH_OS_LOCALE); |
michael@0 | 78 | prefs.reset(PREF_SELECTED_LOCALE); |
michael@0 | 79 | prefs.reset(PREF_ACCEPT_LANGUAGES); |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | exports.testPreferedOsLocale = function(assert) { |
michael@0 | 83 | prefs.set(PREF_MATCH_OS_LOCALE, true); |
michael@0 | 84 | prefs.set(PREF_SELECTED_LOCALE, ""); |
michael@0 | 85 | prefs.set(PREF_ACCEPT_LANGUAGES, ""); |
michael@0 | 86 | |
michael@0 | 87 | let expectedLocale = Services.locale.getLocaleComponentForUserAgent(). |
michael@0 | 88 | toLowerCase(); |
michael@0 | 89 | let expectedLocaleList = [expectedLocale]; |
michael@0 | 90 | |
michael@0 | 91 | // Add default "en-us" fallback if the main language is not already en-us |
michael@0 | 92 | if (expectedLocale != "en-us") |
michael@0 | 93 | expectedLocaleList.push("en-us"); |
michael@0 | 94 | |
michael@0 | 95 | assertPrefered(assert, expectedLocaleList, "Ensure that we select OS locale when related preference is set"); |
michael@0 | 96 | |
michael@0 | 97 | // Reset what we have changed |
michael@0 | 98 | prefs.reset(PREF_MATCH_OS_LOCALE); |
michael@0 | 99 | prefs.reset(PREF_SELECTED_LOCALE); |
michael@0 | 100 | prefs.reset(PREF_ACCEPT_LANGUAGES); |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | exports.testFindClosestLocale = function(assert) { |
michael@0 | 104 | // Second param of findClosestLocale (aMatchLocales) have to be in lowercase |
michael@0 | 105 | assert.equal(findClosestLocale([], []), null, |
michael@0 | 106 | "When everything is empty we get null"); |
michael@0 | 107 | |
michael@0 | 108 | assert.equal(findClosestLocale(["en", "en-US"], ["en"]), |
michael@0 | 109 | "en", "We always accept exact match first 1/5"); |
michael@0 | 110 | assert.equal(findClosestLocale(["en-US", "en"], ["en"]), |
michael@0 | 111 | "en", "We always accept exact match first 2/5"); |
michael@0 | 112 | assert.equal(findClosestLocale(["en", "en-US"], ["en-us"]), |
michael@0 | 113 | "en-US", "We always accept exact match first 3/5"); |
michael@0 | 114 | assert.equal(findClosestLocale(["ja-JP-mac", "ja", "ja-JP"], ["ja-jp"]), |
michael@0 | 115 | "ja-JP", "We always accept exact match first 4/5"); |
michael@0 | 116 | assert.equal(findClosestLocale(["ja-JP-mac", "ja", "ja-JP"], ["ja-jp-mac"]), |
michael@0 | 117 | "ja-JP-mac", "We always accept exact match first 5/5"); |
michael@0 | 118 | |
michael@0 | 119 | assert.equal(findClosestLocale(["en", "en-GB"], ["en-us"]), |
michael@0 | 120 | "en", "We accept more generic locale, when there is no exact match 1/2"); |
michael@0 | 121 | assert.equal(findClosestLocale(["en-ZA", "en"], ["en-gb"]), |
michael@0 | 122 | "en", "We accept more generic locale, when there is no exact match 2/2"); |
michael@0 | 123 | |
michael@0 | 124 | assert.equal(findClosestLocale(["ja-JP"], ["ja"]), |
michael@0 | 125 | "ja-JP", "We accept more specialized locale, when there is no exact match 1/2"); |
michael@0 | 126 | // Better to select "ja" in this case but behave same as current AddonManager |
michael@0 | 127 | assert.equal(findClosestLocale(["ja-JP-mac", "ja"], ["ja-jp"]), |
michael@0 | 128 | "ja-JP-mac", "We accept more specialized locale, when there is no exact match 2/2"); |
michael@0 | 129 | |
michael@0 | 130 | assert.equal(findClosestLocale(["en-US"], ["en-us"]), |
michael@0 | 131 | "en-US", "We keep the original one as result 1/2"); |
michael@0 | 132 | assert.equal(findClosestLocale(["en-us"], ["en-us"]), |
michael@0 | 133 | "en-us", "We keep the original one as result 2/2"); |
michael@0 | 134 | |
michael@0 | 135 | assert.equal(findClosestLocale(["ja-JP-mac"], ["ja-jp-mac"]), |
michael@0 | 136 | "ja-JP-mac", "We accept locale with 3 parts"); |
michael@0 | 137 | assert.equal(findClosestLocale(["ja-JP"], ["ja-jp-mac"]), |
michael@0 | 138 | "ja-JP", "We accept locale with 2 parts from locale with 3 parts"); |
michael@0 | 139 | assert.equal(findClosestLocale(["ja"], ["ja-jp-mac"]), |
michael@0 | 140 | "ja", "We accept locale with 1 part from locale with 3 parts"); |
michael@0 | 141 | }; |
michael@0 | 142 | |
michael@0 | 143 | require('sdk/test').run(exports); |