Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 3 | |
michael@0 | 4 | /* |
michael@0 | 5 | * Test initializing from the search cache. |
michael@0 | 6 | */ |
michael@0 | 7 | |
michael@0 | 8 | "use strict"; |
michael@0 | 9 | |
michael@0 | 10 | const Cc = Components.classes; |
michael@0 | 11 | const Ci = Components.interfaces; |
michael@0 | 12 | |
michael@0 | 13 | // Metadata to write to search-metadata.json for the test. |
michael@0 | 14 | let gMetadata = {"[profile]/test-search-engine.xml":{"used":true}}; |
michael@0 | 15 | |
michael@0 | 16 | /** |
michael@0 | 17 | * Gets a directory from the directory service. |
michael@0 | 18 | * @param aKey |
michael@0 | 19 | * The directory service key indicating the directory to get. |
michael@0 | 20 | */ |
michael@0 | 21 | let _dirSvc = null; |
michael@0 | 22 | function getDir(aKey, aIFace) { |
michael@0 | 23 | if (!aKey) { |
michael@0 | 24 | FAIL("getDir requires a directory key!"); |
michael@0 | 25 | } |
michael@0 | 26 | |
michael@0 | 27 | if (!_dirSvc) { |
michael@0 | 28 | _dirSvc = Cc["@mozilla.org/file/directory_service;1"]. |
michael@0 | 29 | getService(Ci.nsIProperties); |
michael@0 | 30 | } |
michael@0 | 31 | return _dirSvc.get(aKey, aIFace || Ci.nsIFile); |
michael@0 | 32 | } |
michael@0 | 33 | |
michael@0 | 34 | let cacheTemplate, appPluginsPath, profPlugins; |
michael@0 | 35 | |
michael@0 | 36 | /** |
michael@0 | 37 | * Test reading from search.json |
michael@0 | 38 | */ |
michael@0 | 39 | function run_test() { |
michael@0 | 40 | removeMetadata(); |
michael@0 | 41 | removeCacheFile(); |
michael@0 | 42 | |
michael@0 | 43 | updateAppInfo(); |
michael@0 | 44 | |
michael@0 | 45 | let cacheTemplateFile = do_get_file("data/search.json"); |
michael@0 | 46 | cacheTemplate = readJSONFile(cacheTemplateFile); |
michael@0 | 47 | |
michael@0 | 48 | let engineFile = gProfD.clone(); |
michael@0 | 49 | engineFile.append("searchplugins"); |
michael@0 | 50 | engineFile.append("test-search-engine.xml"); |
michael@0 | 51 | engineFile.parent.create(Ci.nsIFile.DIRECTORY_TYPE, FileUtils.PERMS_DIRECTORY); |
michael@0 | 52 | |
michael@0 | 53 | // Copy the test engine to the test profile. |
michael@0 | 54 | let engineTemplateFile = do_get_file("data/engine.xml"); |
michael@0 | 55 | engineTemplateFile.copyTo(engineFile.parent, "test-search-engine.xml"); |
michael@0 | 56 | |
michael@0 | 57 | // Add the app's searchplugins directory to the cache so it won't be ignored. |
michael@0 | 58 | let appSearchPlugins = getDir(NS_APP_SEARCH_DIR); |
michael@0 | 59 | appPluginsPath = appSearchPlugins.path; |
michael@0 | 60 | cacheTemplate.directories[appPluginsPath] = {}; |
michael@0 | 61 | cacheTemplate.directories[appPluginsPath].lastModifiedTime = appSearchPlugins.lastModifiedTime; |
michael@0 | 62 | cacheTemplate.directories[appPluginsPath].engines = []; |
michael@0 | 63 | |
michael@0 | 64 | // Replace the profile placeholder with the correct path. |
michael@0 | 65 | profPlugins = engineFile.parent.path; |
michael@0 | 66 | cacheTemplate.directories[profPlugins] = cacheTemplate.directories["[profile]/searchplugins"]; |
michael@0 | 67 | delete cacheTemplate.directories["[profile]/searchplugins"]; |
michael@0 | 68 | cacheTemplate.directories[profPlugins].engines[0].filePath = engineFile.path; |
michael@0 | 69 | cacheTemplate.directories[profPlugins].lastModifiedTime = engineFile.parent.lastModifiedTime; |
michael@0 | 70 | |
michael@0 | 71 | run_next_test(); |
michael@0 | 72 | } |
michael@0 | 73 | |
michael@0 | 74 | add_test(function prepare_test_data() { |
michael@0 | 75 | |
michael@0 | 76 | let ostream = Cc["@mozilla.org/network/file-output-stream;1"]. |
michael@0 | 77 | createInstance(Ci.nsIFileOutputStream); |
michael@0 | 78 | let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"]. |
michael@0 | 79 | createInstance(Ci.nsIScriptableUnicodeConverter); |
michael@0 | 80 | |
michael@0 | 81 | // Write the modified cache template to the profile directory. |
michael@0 | 82 | let cacheFile = gProfD.clone(); |
michael@0 | 83 | cacheFile.append("search.json"); |
michael@0 | 84 | ostream.init(cacheFile, (MODE_WRONLY | MODE_CREATE | MODE_TRUNCATE), FileUtils.PERMS_FILE, |
michael@0 | 85 | ostream.DEFER_OPEN); |
michael@0 | 86 | converter.charset = "UTF-8"; |
michael@0 | 87 | let data = converter.convertToInputStream(JSON.stringify(cacheTemplate)); |
michael@0 | 88 | |
michael@0 | 89 | // Write to the cache and metadata files asynchronously before starting the search service. |
michael@0 | 90 | NetUtil.asyncCopy(data, ostream, function afterMetadataCopy(aResult) { |
michael@0 | 91 | do_check_true(Components.isSuccessCode(aResult)); |
michael@0 | 92 | let metadataFile = gProfD.clone(); |
michael@0 | 93 | metadataFile.append("search-metadata.json"); |
michael@0 | 94 | |
michael@0 | 95 | let ostream = Cc["@mozilla.org/network/file-output-stream;1"]. |
michael@0 | 96 | createInstance(Ci.nsIFileOutputStream); |
michael@0 | 97 | let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"]. |
michael@0 | 98 | createInstance(Ci.nsIScriptableUnicodeConverter); |
michael@0 | 99 | |
michael@0 | 100 | ostream.init(metadataFile, (MODE_WRONLY | MODE_CREATE | MODE_TRUNCATE), FileUtils.PERMS_FILE, |
michael@0 | 101 | ostream.DEFER_OPEN); |
michael@0 | 102 | converter.charset = "UTF-8"; |
michael@0 | 103 | let data = converter.convertToInputStream(JSON.stringify(gMetadata)); |
michael@0 | 104 | |
michael@0 | 105 | NetUtil.asyncCopy(data, ostream, function afterCacheCopy(aResult) { |
michael@0 | 106 | do_check_true(Components.isSuccessCode(aResult)); |
michael@0 | 107 | run_next_test(); |
michael@0 | 108 | }); |
michael@0 | 109 | }); |
michael@0 | 110 | }); |
michael@0 | 111 | |
michael@0 | 112 | /** |
michael@0 | 113 | * Start the search service and confirm the engine properties match the expected values. |
michael@0 | 114 | */ |
michael@0 | 115 | add_test(function test_cached_engine_properties() { |
michael@0 | 116 | do_print("init search service"); |
michael@0 | 117 | |
michael@0 | 118 | let search = Services.search.init(function initComplete(aResult) { |
michael@0 | 119 | do_print("init'd search service"); |
michael@0 | 120 | do_check_true(Components.isSuccessCode(aResult)); |
michael@0 | 121 | |
michael@0 | 122 | let engines = Services.search.getEngines({}); |
michael@0 | 123 | let engine = engines[0]; |
michael@0 | 124 | |
michael@0 | 125 | do_check_true(engine instanceof Ci.nsISearchEngine); |
michael@0 | 126 | isSubObjectOf(EXPECTED_ENGINE.engine, engine); |
michael@0 | 127 | |
michael@0 | 128 | let engineFromSS = Services.search.getEngineByName(EXPECTED_ENGINE.engine.name); |
michael@0 | 129 | do_check_true(!!engineFromSS); |
michael@0 | 130 | isSubObjectOf(EXPECTED_ENGINE.engine, engineFromSS); |
michael@0 | 131 | |
michael@0 | 132 | removeMetadata(); |
michael@0 | 133 | removeCacheFile(); |
michael@0 | 134 | run_next_test(); |
michael@0 | 135 | }); |
michael@0 | 136 | }); |
michael@0 | 137 | |
michael@0 | 138 | /** |
michael@0 | 139 | * Test that the JSON cache written in the profile is correct. |
michael@0 | 140 | */ |
michael@0 | 141 | add_test(function test_cache_write() { |
michael@0 | 142 | do_print("test cache writing"); |
michael@0 | 143 | |
michael@0 | 144 | let cache = gProfD.clone(); |
michael@0 | 145 | cache.append("search.json"); |
michael@0 | 146 | do_check_false(cache.exists()); |
michael@0 | 147 | |
michael@0 | 148 | do_print("Next step is forcing flush"); |
michael@0 | 149 | do_timeout(0, function forceFlush() { |
michael@0 | 150 | do_print("Forcing flush"); |
michael@0 | 151 | // Force flush |
michael@0 | 152 | // Note: the timeout is needed, to avoid some reentrency |
michael@0 | 153 | // issues in nsSearchService. |
michael@0 | 154 | |
michael@0 | 155 | let cacheWriteObserver = { |
michael@0 | 156 | observe: function cacheWriteObserver_observe(aEngine, aTopic, aVerb) { |
michael@0 | 157 | if (aTopic != "browser-search-service" || aVerb != "write-cache-to-disk-complete") { |
michael@0 | 158 | return; |
michael@0 | 159 | } |
michael@0 | 160 | Services.obs.removeObserver(cacheWriteObserver, "browser-search-service"); |
michael@0 | 161 | do_print("Cache write complete"); |
michael@0 | 162 | do_check_true(cache.exists()); |
michael@0 | 163 | // Check that the search.json cache matches the template |
michael@0 | 164 | |
michael@0 | 165 | let cacheWritten = readJSONFile(cache); |
michael@0 | 166 | // Delete the app search plugins directory from the template since it's not currently written out. |
michael@0 | 167 | delete cacheTemplate.directories[appPluginsPath]; |
michael@0 | 168 | |
michael@0 | 169 | do_print("Check search.json"); |
michael@0 | 170 | isSubObjectOf(cacheTemplate, cacheWritten); |
michael@0 | 171 | |
michael@0 | 172 | run_next_test(); |
michael@0 | 173 | } |
michael@0 | 174 | }; |
michael@0 | 175 | Services.obs.addObserver(cacheWriteObserver, "browser-search-service", false); |
michael@0 | 176 | |
michael@0 | 177 | Services.search.QueryInterface(Ci.nsIObserver).observe(null, "browser-search-engine-modified" , "engine-removed"); |
michael@0 | 178 | }); |
michael@0 | 179 | }); |
michael@0 | 180 | |
michael@0 | 181 | let EXPECTED_ENGINE = { |
michael@0 | 182 | engine: { |
michael@0 | 183 | name: "Test search engine", |
michael@0 | 184 | alias: null, |
michael@0 | 185 | description: "A test search engine (based on Google search)", |
michael@0 | 186 | searchForm: "http://www.google.com/", |
michael@0 | 187 | type: Ci.nsISearchEngine.TYPE_MOZSEARCH, |
michael@0 | 188 | wrappedJSObject: { |
michael@0 | 189 | "_iconURL": "data:image/png;base64,AAABAAEAEBAAAAEAGABoAwAAFgAAACgAAAAQAAAAIAAAAAEAGAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADs9Pt8xetPtu9FsfFNtu%2BTzvb2%2B%2Fne4dFJeBw0egA%2FfAJAfAA8ewBBegAAAAD%2B%2FPtft98Mp%2BwWsfAVsvE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2F%2F64g1C5VwDMYwK8Yg7y5tz8%2Fv%2FV1PYKDOcAAP0DAf4AAf0AAfYEAOwAAuAAAAD%2F%2FPvi28ymXyChTATRrIb8%2F%2F3v8fk6P8MAAdUCAvoAAP0CAP0AAfYAAO4AAACAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACAAQAA", |
michael@0 | 190 | _urls : [ |
michael@0 | 191 | { |
michael@0 | 192 | type: "application/x-suggestions+json", |
michael@0 | 193 | method: "GET", |
michael@0 | 194 | template: "http://suggestqueries.google.com/complete/search?output=firefox&client=firefox" + |
michael@0 | 195 | "&hl={moz:locale}&q={searchTerms}", |
michael@0 | 196 | params: "", |
michael@0 | 197 | }, |
michael@0 | 198 | { |
michael@0 | 199 | type: "text/html", |
michael@0 | 200 | method: "GET", |
michael@0 | 201 | template: "http://www.google.com/search", |
michael@0 | 202 | resultDomain: "google.com", |
michael@0 | 203 | params: [ |
michael@0 | 204 | { |
michael@0 | 205 | "name": "q", |
michael@0 | 206 | "value": "{searchTerms}", |
michael@0 | 207 | "purpose": undefined, |
michael@0 | 208 | }, |
michael@0 | 209 | { |
michael@0 | 210 | "name": "ie", |
michael@0 | 211 | "value": "utf-8", |
michael@0 | 212 | "purpose": undefined, |
michael@0 | 213 | }, |
michael@0 | 214 | { |
michael@0 | 215 | "name": "oe", |
michael@0 | 216 | "value": "utf-8", |
michael@0 | 217 | "purpose": undefined, |
michael@0 | 218 | }, |
michael@0 | 219 | { |
michael@0 | 220 | "name": "aq", |
michael@0 | 221 | "value": "t", |
michael@0 | 222 | "purpose": undefined, |
michael@0 | 223 | }, |
michael@0 | 224 | { |
michael@0 | 225 | "name": "client", |
michael@0 | 226 | "value": "firefox", |
michael@0 | 227 | "purpose": undefined, |
michael@0 | 228 | }, |
michael@0 | 229 | { |
michael@0 | 230 | "name": "channel", |
michael@0 | 231 | "value": "fflb", |
michael@0 | 232 | "purpose": "keyword", |
michael@0 | 233 | }, |
michael@0 | 234 | { |
michael@0 | 235 | "name": "channel", |
michael@0 | 236 | "value": "rcs", |
michael@0 | 237 | "purpose": "contextmenu", |
michael@0 | 238 | }, |
michael@0 | 239 | ], |
michael@0 | 240 | mozparams: { |
michael@0 | 241 | "client": { |
michael@0 | 242 | "name": "client", |
michael@0 | 243 | "falseValue": "firefox", |
michael@0 | 244 | "trueValue": "firefox-a", |
michael@0 | 245 | "condition": "defaultEngine", |
michael@0 | 246 | "mozparam": true, |
michael@0 | 247 | }, |
michael@0 | 248 | }, |
michael@0 | 249 | }, |
michael@0 | 250 | { |
michael@0 | 251 | type: "application/x-moz-default-purpose", |
michael@0 | 252 | method: "GET", |
michael@0 | 253 | template: "http://www.google.com/search", |
michael@0 | 254 | resultDomain: "purpose.google.com", |
michael@0 | 255 | params: [ |
michael@0 | 256 | { |
michael@0 | 257 | "name": "q", |
michael@0 | 258 | "value": "{searchTerms}", |
michael@0 | 259 | "purpose": undefined, |
michael@0 | 260 | }, |
michael@0 | 261 | { |
michael@0 | 262 | "name": "client", |
michael@0 | 263 | "value": "firefox", |
michael@0 | 264 | "purpose": undefined, |
michael@0 | 265 | }, |
michael@0 | 266 | { |
michael@0 | 267 | "name": "channel", |
michael@0 | 268 | "value": "none", |
michael@0 | 269 | "purpose": "", |
michael@0 | 270 | }, |
michael@0 | 271 | { |
michael@0 | 272 | "name": "channel", |
michael@0 | 273 | "value": "fflb", |
michael@0 | 274 | "purpose": "keyword", |
michael@0 | 275 | }, |
michael@0 | 276 | { |
michael@0 | 277 | "name": "channel", |
michael@0 | 278 | "value": "rcs", |
michael@0 | 279 | "purpose": "contextmenu", |
michael@0 | 280 | }, |
michael@0 | 281 | ], |
michael@0 | 282 | mozparams: { |
michael@0 | 283 | "client": { |
michael@0 | 284 | "name": "client", |
michael@0 | 285 | "falseValue": "firefox", |
michael@0 | 286 | "trueValue": "firefox-a", |
michael@0 | 287 | "condition": "defaultEngine", |
michael@0 | 288 | "mozparam": true, |
michael@0 | 289 | }, |
michael@0 | 290 | }, |
michael@0 | 291 | }, |
michael@0 | 292 | ], |
michael@0 | 293 | }, |
michael@0 | 294 | }, |
michael@0 | 295 | }; |