michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: /* michael@0: * Test initializing from the search cache. michael@0: */ michael@0: michael@0: "use strict"; michael@0: michael@0: const Cc = Components.classes; michael@0: const Ci = Components.interfaces; michael@0: michael@0: // Metadata to write to search-metadata.json for the test. michael@0: let gMetadata = {"[profile]/test-search-engine.xml":{"used":true}}; michael@0: michael@0: /** michael@0: * Gets a directory from the directory service. michael@0: * @param aKey michael@0: * The directory service key indicating the directory to get. michael@0: */ michael@0: let _dirSvc = null; michael@0: function getDir(aKey, aIFace) { michael@0: if (!aKey) { michael@0: FAIL("getDir requires a directory key!"); michael@0: } michael@0: michael@0: if (!_dirSvc) { michael@0: _dirSvc = Cc["@mozilla.org/file/directory_service;1"]. michael@0: getService(Ci.nsIProperties); michael@0: } michael@0: return _dirSvc.get(aKey, aIFace || Ci.nsIFile); michael@0: } michael@0: michael@0: let cacheTemplate, appPluginsPath, profPlugins; michael@0: michael@0: /** michael@0: * Test reading from search.json michael@0: */ michael@0: function run_test() { michael@0: removeMetadata(); michael@0: removeCacheFile(); michael@0: michael@0: updateAppInfo(); michael@0: michael@0: let cacheTemplateFile = do_get_file("data/search.json"); michael@0: cacheTemplate = readJSONFile(cacheTemplateFile); michael@0: michael@0: let engineFile = gProfD.clone(); michael@0: engineFile.append("searchplugins"); michael@0: engineFile.append("test-search-engine.xml"); michael@0: engineFile.parent.create(Ci.nsIFile.DIRECTORY_TYPE, FileUtils.PERMS_DIRECTORY); michael@0: michael@0: // Copy the test engine to the test profile. michael@0: let engineTemplateFile = do_get_file("data/engine.xml"); michael@0: engineTemplateFile.copyTo(engineFile.parent, "test-search-engine.xml"); michael@0: michael@0: // Add the app's searchplugins directory to the cache so it won't be ignored. michael@0: let appSearchPlugins = getDir(NS_APP_SEARCH_DIR); michael@0: appPluginsPath = appSearchPlugins.path; michael@0: cacheTemplate.directories[appPluginsPath] = {}; michael@0: cacheTemplate.directories[appPluginsPath].lastModifiedTime = appSearchPlugins.lastModifiedTime; michael@0: cacheTemplate.directories[appPluginsPath].engines = []; michael@0: michael@0: // Replace the profile placeholder with the correct path. michael@0: profPlugins = engineFile.parent.path; michael@0: cacheTemplate.directories[profPlugins] = cacheTemplate.directories["[profile]/searchplugins"]; michael@0: delete cacheTemplate.directories["[profile]/searchplugins"]; michael@0: cacheTemplate.directories[profPlugins].engines[0].filePath = engineFile.path; michael@0: cacheTemplate.directories[profPlugins].lastModifiedTime = engineFile.parent.lastModifiedTime; michael@0: michael@0: run_next_test(); michael@0: } michael@0: michael@0: add_test(function prepare_test_data() { michael@0: michael@0: let ostream = Cc["@mozilla.org/network/file-output-stream;1"]. michael@0: createInstance(Ci.nsIFileOutputStream); michael@0: let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"]. michael@0: createInstance(Ci.nsIScriptableUnicodeConverter); michael@0: michael@0: // Write the modified cache template to the profile directory. michael@0: let cacheFile = gProfD.clone(); michael@0: cacheFile.append("search.json"); michael@0: ostream.init(cacheFile, (MODE_WRONLY | MODE_CREATE | MODE_TRUNCATE), FileUtils.PERMS_FILE, michael@0: ostream.DEFER_OPEN); michael@0: converter.charset = "UTF-8"; michael@0: let data = converter.convertToInputStream(JSON.stringify(cacheTemplate)); michael@0: michael@0: // Write to the cache and metadata files asynchronously before starting the search service. michael@0: NetUtil.asyncCopy(data, ostream, function afterMetadataCopy(aResult) { michael@0: do_check_true(Components.isSuccessCode(aResult)); michael@0: let metadataFile = gProfD.clone(); michael@0: metadataFile.append("search-metadata.json"); michael@0: michael@0: let ostream = Cc["@mozilla.org/network/file-output-stream;1"]. michael@0: createInstance(Ci.nsIFileOutputStream); michael@0: let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"]. michael@0: createInstance(Ci.nsIScriptableUnicodeConverter); michael@0: michael@0: ostream.init(metadataFile, (MODE_WRONLY | MODE_CREATE | MODE_TRUNCATE), FileUtils.PERMS_FILE, michael@0: ostream.DEFER_OPEN); michael@0: converter.charset = "UTF-8"; michael@0: let data = converter.convertToInputStream(JSON.stringify(gMetadata)); michael@0: michael@0: NetUtil.asyncCopy(data, ostream, function afterCacheCopy(aResult) { michael@0: do_check_true(Components.isSuccessCode(aResult)); michael@0: run_next_test(); michael@0: }); michael@0: }); michael@0: }); michael@0: michael@0: /** michael@0: * Start the search service and confirm the engine properties match the expected values. michael@0: */ michael@0: add_test(function test_cached_engine_properties() { michael@0: do_print("init search service"); michael@0: michael@0: let search = Services.search.init(function initComplete(aResult) { michael@0: do_print("init'd search service"); michael@0: do_check_true(Components.isSuccessCode(aResult)); michael@0: michael@0: let engines = Services.search.getEngines({}); michael@0: let engine = engines[0]; michael@0: michael@0: do_check_true(engine instanceof Ci.nsISearchEngine); michael@0: isSubObjectOf(EXPECTED_ENGINE.engine, engine); michael@0: michael@0: let engineFromSS = Services.search.getEngineByName(EXPECTED_ENGINE.engine.name); michael@0: do_check_true(!!engineFromSS); michael@0: isSubObjectOf(EXPECTED_ENGINE.engine, engineFromSS); michael@0: michael@0: removeMetadata(); michael@0: removeCacheFile(); michael@0: run_next_test(); michael@0: }); michael@0: }); michael@0: michael@0: /** michael@0: * Test that the JSON cache written in the profile is correct. michael@0: */ michael@0: add_test(function test_cache_write() { michael@0: do_print("test cache writing"); michael@0: michael@0: let cache = gProfD.clone(); michael@0: cache.append("search.json"); michael@0: do_check_false(cache.exists()); michael@0: michael@0: do_print("Next step is forcing flush"); michael@0: do_timeout(0, function forceFlush() { michael@0: do_print("Forcing flush"); michael@0: // Force flush michael@0: // Note: the timeout is needed, to avoid some reentrency michael@0: // issues in nsSearchService. michael@0: michael@0: let cacheWriteObserver = { michael@0: observe: function cacheWriteObserver_observe(aEngine, aTopic, aVerb) { michael@0: if (aTopic != "browser-search-service" || aVerb != "write-cache-to-disk-complete") { michael@0: return; michael@0: } michael@0: Services.obs.removeObserver(cacheWriteObserver, "browser-search-service"); michael@0: do_print("Cache write complete"); michael@0: do_check_true(cache.exists()); michael@0: // Check that the search.json cache matches the template michael@0: michael@0: let cacheWritten = readJSONFile(cache); michael@0: // Delete the app search plugins directory from the template since it's not currently written out. michael@0: delete cacheTemplate.directories[appPluginsPath]; michael@0: michael@0: do_print("Check search.json"); michael@0: isSubObjectOf(cacheTemplate, cacheWritten); michael@0: michael@0: run_next_test(); michael@0: } michael@0: }; michael@0: Services.obs.addObserver(cacheWriteObserver, "browser-search-service", false); michael@0: michael@0: Services.search.QueryInterface(Ci.nsIObserver).observe(null, "browser-search-engine-modified" , "engine-removed"); michael@0: }); michael@0: }); michael@0: michael@0: let EXPECTED_ENGINE = { michael@0: engine: { michael@0: name: "Test search engine", michael@0: alias: null, michael@0: description: "A test search engine (based on Google search)", michael@0: searchForm: "http://www.google.com/", michael@0: type: Ci.nsISearchEngine.TYPE_MOZSEARCH, michael@0: wrappedJSObject: { michael@0: "_iconURL": "data:image/png;base64,AAABAAEAEBAAAAEAGABoAwAAFgAAACgAAAAQAAAAIAAAAAEAGAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADs9Pt8xetPtu9FsfFNtu%2BTzvb2%2B%2Fne4dFJeBw0egA%2FfAJAfAA8ewBBegAAAAD%2B%2FPtft98Mp%2BwWsfAVsvEbs%2FQeqvF8xO7%2F%2F%2F63yqkxdgM7gwE%2FggM%2BfQA%2BegBDeQDe7PIbotgQufcMufEPtfIPsvAbs%2FQvq%2Bfz%2Bf%2F%2B%2B%2FZKhR05hgBBhQI8hgBAgAI9ewD0%2B%2Fg3pswAtO8Cxf4Kw%2FsJvvYAqupKsNv%2B%2Fv7%2F%2FP5VkSU0iQA7jQA9hgBDgQU%2BfQH%2F%2Ff%2FQ6fM4sM4KsN8AteMCruIqqdbZ7PH8%2Fv%2Fg6Nc%2Fhg05kAA8jAM9iQI%2BhQA%2BgQDQu6b97uv%2F%2F%2F7V8Pqw3eiWz97q8%2Ff%2F%2F%2F%2F7%2FPptpkkqjQE4kwA7kAA5iwI8iAA8hQCOSSKdXjiyflbAkG7u2s%2F%2B%2F%2F39%2F%2F7r8utrqEYtjQE8lgA7kwA7kwA9jwA9igA9hACiWSekVRyeSgiYSBHx6N%2F%2B%2Fv7k7OFRmiYtlAA5lwI7lwI4lAA7kgI9jwE9iwI4iQCoVhWcTxCmb0K%2BooT8%2Fv%2F7%2F%2F%2FJ2r8fdwI1mwA3mQA3mgA8lAE8lAE4jwA9iwE%2BhwGfXifWvqz%2B%2Ff%2F58u%2Fev6Dt4tr%2B%2F%2F2ZuIUsggA7mgM6mAM3lgA5lgA6kQE%2FkwBChwHt4dv%2F%2F%2F728ei1bCi7VAC5XQ7kz7n%2F%2F%2F6bsZkgcB03lQA9lgM7kwA2iQktZToPK4r9%2F%2F%2F9%2F%2F%2FSqYK5UwDKZAS9WALIkFn%2B%2F%2F3%2F%2BP8oKccGGcIRJrERILYFEMwAAuEAAdX%2F%2Ff7%2F%2FP%2B%2BfDvGXQLIZgLEWgLOjlf7%2F%2F%2F%2F%2F%2F9QU90EAPQAAf8DAP0AAfMAAOUDAtr%2F%2F%2F%2F7%2B%2Fu2bCTIYwDPZgDBWQDSr4P%2F%2Fv%2F%2F%2FP5GRuABAPkAA%2FwBAfkDAPAAAesAAN%2F%2F%2B%2Fz%2F%2F%2F64g1C5VwDMYwK8Yg7y5tz8%2Fv%2FV1PYKDOcAAP0DAf4AAf0AAfYEAOwAAuAAAAD%2F%2FPvi28ymXyChTATRrIb8%2F%2F3v8fk6P8MAAdUCAvoAAP0CAP0AAfYAAO4AAACAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACAAQAA", michael@0: _urls : [ michael@0: { michael@0: type: "application/x-suggestions+json", michael@0: method: "GET", michael@0: template: "http://suggestqueries.google.com/complete/search?output=firefox&client=firefox" + michael@0: "&hl={moz:locale}&q={searchTerms}", michael@0: params: "", michael@0: }, michael@0: { michael@0: type: "text/html", michael@0: method: "GET", michael@0: template: "http://www.google.com/search", michael@0: resultDomain: "google.com", michael@0: params: [ michael@0: { michael@0: "name": "q", michael@0: "value": "{searchTerms}", michael@0: "purpose": undefined, michael@0: }, michael@0: { michael@0: "name": "ie", michael@0: "value": "utf-8", michael@0: "purpose": undefined, michael@0: }, michael@0: { michael@0: "name": "oe", michael@0: "value": "utf-8", michael@0: "purpose": undefined, michael@0: }, michael@0: { michael@0: "name": "aq", michael@0: "value": "t", michael@0: "purpose": undefined, michael@0: }, michael@0: { michael@0: "name": "client", michael@0: "value": "firefox", michael@0: "purpose": undefined, michael@0: }, michael@0: { michael@0: "name": "channel", michael@0: "value": "fflb", michael@0: "purpose": "keyword", michael@0: }, michael@0: { michael@0: "name": "channel", michael@0: "value": "rcs", michael@0: "purpose": "contextmenu", michael@0: }, michael@0: ], michael@0: mozparams: { michael@0: "client": { michael@0: "name": "client", michael@0: "falseValue": "firefox", michael@0: "trueValue": "firefox-a", michael@0: "condition": "defaultEngine", michael@0: "mozparam": true, michael@0: }, michael@0: }, michael@0: }, michael@0: { michael@0: type: "application/x-moz-default-purpose", michael@0: method: "GET", michael@0: template: "http://www.google.com/search", michael@0: resultDomain: "purpose.google.com", michael@0: params: [ michael@0: { michael@0: "name": "q", michael@0: "value": "{searchTerms}", michael@0: "purpose": undefined, michael@0: }, michael@0: { michael@0: "name": "client", michael@0: "value": "firefox", michael@0: "purpose": undefined, michael@0: }, michael@0: { michael@0: "name": "channel", michael@0: "value": "none", michael@0: "purpose": "", michael@0: }, michael@0: { michael@0: "name": "channel", michael@0: "value": "fflb", michael@0: "purpose": "keyword", michael@0: }, michael@0: { michael@0: "name": "channel", michael@0: "value": "rcs", michael@0: "purpose": "contextmenu", michael@0: }, michael@0: ], michael@0: mozparams: { michael@0: "client": { michael@0: "name": "client", michael@0: "falseValue": "firefox", michael@0: "trueValue": "firefox-a", michael@0: "condition": "defaultEngine", michael@0: "mozparam": true, michael@0: }, michael@0: }, michael@0: }, michael@0: ], michael@0: }, michael@0: }, michael@0: };