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: * Tests covering nsIBrowserSearchService::addEngine's optional callback. michael@0: */ michael@0: michael@0: "use strict"; michael@0: michael@0: const Ci = Components.interfaces; michael@0: let gHttpServer; michael@0: let gBaseUrl; michael@0: michael@0: Components.utils.import("resource://testing-common/httpd.js"); michael@0: michael@0: // Override the prompt service and nsIPrompt, since the search service currently michael@0: // prompts in response to certain installation failures we test here michael@0: // XXX this should disappear once bug 863474 is fixed michael@0: function replaceService(contractID, component) { michael@0: let registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar); michael@0: let cid = registrar.contractIDToCID(contractID); michael@0: michael@0: let oldFactory = Components.manager.getClassObject(Components.classes[contractID], michael@0: Ci.nsIFactory); michael@0: registrar.unregisterFactory(cid, oldFactory); michael@0: michael@0: let factory = { michael@0: createInstance: function(aOuter, aIid) { michael@0: if (aOuter != null) michael@0: throw Components.results.NS_ERROR_NO_AGGREGATION; michael@0: return component.QueryInterface(aIid); michael@0: } michael@0: }; michael@0: michael@0: registrar.registerFactory(cid, "", contractID, factory); michael@0: } michael@0: // Only need to stub the methods actually called by nsSearchService michael@0: let promptService = { michael@0: QueryInterface: XPCOMUtils.generateQI([Ci.nsIPromptService]), michael@0: confirmEx: function() {} michael@0: }; michael@0: let prompt = { michael@0: QueryInterface: XPCOMUtils.generateQI([Ci.nsIPrompt]), michael@0: alert: function() {} michael@0: }; michael@0: replaceService("@mozilla.org/embedcomp/prompt-service;1", promptService); michael@0: replaceService("@mozilla.org/prompter;1", prompt); michael@0: michael@0: michael@0: // First test inits the search service michael@0: add_test(function init_search_service() { michael@0: Services.search.init(function (status) { michael@0: if (!Components.isSuccessCode(status)) michael@0: do_throw("Failed to initialize search service"); michael@0: michael@0: run_next_test(); michael@0: }); michael@0: }); michael@0: michael@0: // Simple test of the search callback michael@0: add_test(function simple_callback_test() { michael@0: let searchCallback = { michael@0: onSuccess: function (engine) { michael@0: do_check_true(!!engine); michael@0: do_check_neq(engine.name, Services.search.defaultEngine.name); michael@0: run_next_test(); michael@0: }, michael@0: onError: function (errorCode) { michael@0: do_throw("search callback returned error: " + errorCode); michael@0: } michael@0: } michael@0: Services.search.addEngine(gBaseUrl + "/data/engine.xml", michael@0: Ci.nsISearchEngine.DATA_XML, michael@0: null, false, searchCallback); michael@0: }); michael@0: michael@0: // Test of the search callback on duplicate engine failures michael@0: add_test(function duplicate_failure_test() { michael@0: let searchCallback = { michael@0: onSuccess: function (engine) { michael@0: do_throw("this addition should not have succeeded"); michael@0: }, michael@0: onError: function (errorCode) { michael@0: do_check_true(!!errorCode); michael@0: do_check_eq(errorCode, Ci.nsISearchInstallCallback.ERROR_DUPLICATE_ENGINE); michael@0: run_next_test(); michael@0: } michael@0: } michael@0: // Re-add the same engine added in the previous test michael@0: Services.search.addEngine(gBaseUrl + "/data/engine.xml", michael@0: Ci.nsISearchEngine.DATA_XML, michael@0: null, false, searchCallback); michael@0: }); michael@0: michael@0: // Test of the search callback on failure to load the engine failures michael@0: add_test(function load_failure_test() { michael@0: let searchCallback = { michael@0: onSuccess: function (engine) { michael@0: do_throw("this addition should not have succeeded"); michael@0: }, michael@0: onError: function (errorCode) { michael@0: do_check_true(!!errorCode); michael@0: do_check_eq(errorCode, Ci.nsISearchInstallCallback.ERROR_UNKNOWN_FAILURE); michael@0: run_next_test(); michael@0: } michael@0: } michael@0: // Try adding an engine that doesn't exist michael@0: Services.search.addEngine("http://invalid/data/engine.xml", michael@0: Ci.nsISearchEngine.DATA_XML, michael@0: null, false, searchCallback); michael@0: }); michael@0: michael@0: function run_test() { michael@0: updateAppInfo(); michael@0: michael@0: gHttpServer = new HttpServer(); michael@0: gHttpServer.start(-1); michael@0: gHttpServer.registerDirectory("/", do_get_cwd()); michael@0: gBaseUrl = "http://localhost:" + gHttpServer.identity.primaryPort; michael@0: michael@0: do_register_cleanup(function cleanup() { michael@0: gHttpServer.stop(function() {}); michael@0: }); michael@0: michael@0: run_next_test(); michael@0: }