1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/search/tests/xpcshell/test_addEngine_callback.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,126 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + * http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +/* 1.8 + * Tests covering nsIBrowserSearchService::addEngine's optional callback. 1.9 + */ 1.10 + 1.11 +"use strict"; 1.12 + 1.13 +const Ci = Components.interfaces; 1.14 +let gHttpServer; 1.15 +let gBaseUrl; 1.16 + 1.17 +Components.utils.import("resource://testing-common/httpd.js"); 1.18 + 1.19 +// Override the prompt service and nsIPrompt, since the search service currently 1.20 +// prompts in response to certain installation failures we test here 1.21 +// XXX this should disappear once bug 863474 is fixed 1.22 +function replaceService(contractID, component) { 1.23 + let registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar); 1.24 + let cid = registrar.contractIDToCID(contractID); 1.25 + 1.26 + let oldFactory = Components.manager.getClassObject(Components.classes[contractID], 1.27 + Ci.nsIFactory); 1.28 + registrar.unregisterFactory(cid, oldFactory); 1.29 + 1.30 + let factory = { 1.31 + createInstance: function(aOuter, aIid) { 1.32 + if (aOuter != null) 1.33 + throw Components.results.NS_ERROR_NO_AGGREGATION; 1.34 + return component.QueryInterface(aIid); 1.35 + } 1.36 + }; 1.37 + 1.38 + registrar.registerFactory(cid, "", contractID, factory); 1.39 +} 1.40 +// Only need to stub the methods actually called by nsSearchService 1.41 +let promptService = { 1.42 + QueryInterface: XPCOMUtils.generateQI([Ci.nsIPromptService]), 1.43 + confirmEx: function() {} 1.44 +}; 1.45 +let prompt = { 1.46 + QueryInterface: XPCOMUtils.generateQI([Ci.nsIPrompt]), 1.47 + alert: function() {} 1.48 +}; 1.49 +replaceService("@mozilla.org/embedcomp/prompt-service;1", promptService); 1.50 +replaceService("@mozilla.org/prompter;1", prompt); 1.51 + 1.52 + 1.53 +// First test inits the search service 1.54 +add_test(function init_search_service() { 1.55 + Services.search.init(function (status) { 1.56 + if (!Components.isSuccessCode(status)) 1.57 + do_throw("Failed to initialize search service"); 1.58 + 1.59 + run_next_test(); 1.60 + }); 1.61 +}); 1.62 + 1.63 +// Simple test of the search callback 1.64 +add_test(function simple_callback_test() { 1.65 + let searchCallback = { 1.66 + onSuccess: function (engine) { 1.67 + do_check_true(!!engine); 1.68 + do_check_neq(engine.name, Services.search.defaultEngine.name); 1.69 + run_next_test(); 1.70 + }, 1.71 + onError: function (errorCode) { 1.72 + do_throw("search callback returned error: " + errorCode); 1.73 + } 1.74 + } 1.75 + Services.search.addEngine(gBaseUrl + "/data/engine.xml", 1.76 + Ci.nsISearchEngine.DATA_XML, 1.77 + null, false, searchCallback); 1.78 +}); 1.79 + 1.80 +// Test of the search callback on duplicate engine failures 1.81 +add_test(function duplicate_failure_test() { 1.82 + let searchCallback = { 1.83 + onSuccess: function (engine) { 1.84 + do_throw("this addition should not have succeeded"); 1.85 + }, 1.86 + onError: function (errorCode) { 1.87 + do_check_true(!!errorCode); 1.88 + do_check_eq(errorCode, Ci.nsISearchInstallCallback.ERROR_DUPLICATE_ENGINE); 1.89 + run_next_test(); 1.90 + } 1.91 + } 1.92 + // Re-add the same engine added in the previous test 1.93 + Services.search.addEngine(gBaseUrl + "/data/engine.xml", 1.94 + Ci.nsISearchEngine.DATA_XML, 1.95 + null, false, searchCallback); 1.96 +}); 1.97 + 1.98 +// Test of the search callback on failure to load the engine failures 1.99 +add_test(function load_failure_test() { 1.100 + let searchCallback = { 1.101 + onSuccess: function (engine) { 1.102 + do_throw("this addition should not have succeeded"); 1.103 + }, 1.104 + onError: function (errorCode) { 1.105 + do_check_true(!!errorCode); 1.106 + do_check_eq(errorCode, Ci.nsISearchInstallCallback.ERROR_UNKNOWN_FAILURE); 1.107 + run_next_test(); 1.108 + } 1.109 + } 1.110 + // Try adding an engine that doesn't exist 1.111 + Services.search.addEngine("http://invalid/data/engine.xml", 1.112 + Ci.nsISearchEngine.DATA_XML, 1.113 + null, false, searchCallback); 1.114 +}); 1.115 + 1.116 +function run_test() { 1.117 + updateAppInfo(); 1.118 + 1.119 + gHttpServer = new HttpServer(); 1.120 + gHttpServer.start(-1); 1.121 + gHttpServer.registerDirectory("/", do_get_cwd()); 1.122 + gBaseUrl = "http://localhost:" + gHttpServer.identity.primaryPort; 1.123 + 1.124 + do_register_cleanup(function cleanup() { 1.125 + gHttpServer.stop(function() {}); 1.126 + }); 1.127 + 1.128 + run_next_test(); 1.129 +}