toolkit/components/search/tests/xpcshell/test_addEngine_callback.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

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 * Tests covering nsIBrowserSearchService::addEngine's optional callback.
michael@0 6 */
michael@0 7
michael@0 8 "use strict";
michael@0 9
michael@0 10 const Ci = Components.interfaces;
michael@0 11 let gHttpServer;
michael@0 12 let gBaseUrl;
michael@0 13
michael@0 14 Components.utils.import("resource://testing-common/httpd.js");
michael@0 15
michael@0 16 // Override the prompt service and nsIPrompt, since the search service currently
michael@0 17 // prompts in response to certain installation failures we test here
michael@0 18 // XXX this should disappear once bug 863474 is fixed
michael@0 19 function replaceService(contractID, component) {
michael@0 20 let registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
michael@0 21 let cid = registrar.contractIDToCID(contractID);
michael@0 22
michael@0 23 let oldFactory = Components.manager.getClassObject(Components.classes[contractID],
michael@0 24 Ci.nsIFactory);
michael@0 25 registrar.unregisterFactory(cid, oldFactory);
michael@0 26
michael@0 27 let factory = {
michael@0 28 createInstance: function(aOuter, aIid) {
michael@0 29 if (aOuter != null)
michael@0 30 throw Components.results.NS_ERROR_NO_AGGREGATION;
michael@0 31 return component.QueryInterface(aIid);
michael@0 32 }
michael@0 33 };
michael@0 34
michael@0 35 registrar.registerFactory(cid, "", contractID, factory);
michael@0 36 }
michael@0 37 // Only need to stub the methods actually called by nsSearchService
michael@0 38 let promptService = {
michael@0 39 QueryInterface: XPCOMUtils.generateQI([Ci.nsIPromptService]),
michael@0 40 confirmEx: function() {}
michael@0 41 };
michael@0 42 let prompt = {
michael@0 43 QueryInterface: XPCOMUtils.generateQI([Ci.nsIPrompt]),
michael@0 44 alert: function() {}
michael@0 45 };
michael@0 46 replaceService("@mozilla.org/embedcomp/prompt-service;1", promptService);
michael@0 47 replaceService("@mozilla.org/prompter;1", prompt);
michael@0 48
michael@0 49
michael@0 50 // First test inits the search service
michael@0 51 add_test(function init_search_service() {
michael@0 52 Services.search.init(function (status) {
michael@0 53 if (!Components.isSuccessCode(status))
michael@0 54 do_throw("Failed to initialize search service");
michael@0 55
michael@0 56 run_next_test();
michael@0 57 });
michael@0 58 });
michael@0 59
michael@0 60 // Simple test of the search callback
michael@0 61 add_test(function simple_callback_test() {
michael@0 62 let searchCallback = {
michael@0 63 onSuccess: function (engine) {
michael@0 64 do_check_true(!!engine);
michael@0 65 do_check_neq(engine.name, Services.search.defaultEngine.name);
michael@0 66 run_next_test();
michael@0 67 },
michael@0 68 onError: function (errorCode) {
michael@0 69 do_throw("search callback returned error: " + errorCode);
michael@0 70 }
michael@0 71 }
michael@0 72 Services.search.addEngine(gBaseUrl + "/data/engine.xml",
michael@0 73 Ci.nsISearchEngine.DATA_XML,
michael@0 74 null, false, searchCallback);
michael@0 75 });
michael@0 76
michael@0 77 // Test of the search callback on duplicate engine failures
michael@0 78 add_test(function duplicate_failure_test() {
michael@0 79 let searchCallback = {
michael@0 80 onSuccess: function (engine) {
michael@0 81 do_throw("this addition should not have succeeded");
michael@0 82 },
michael@0 83 onError: function (errorCode) {
michael@0 84 do_check_true(!!errorCode);
michael@0 85 do_check_eq(errorCode, Ci.nsISearchInstallCallback.ERROR_DUPLICATE_ENGINE);
michael@0 86 run_next_test();
michael@0 87 }
michael@0 88 }
michael@0 89 // Re-add the same engine added in the previous test
michael@0 90 Services.search.addEngine(gBaseUrl + "/data/engine.xml",
michael@0 91 Ci.nsISearchEngine.DATA_XML,
michael@0 92 null, false, searchCallback);
michael@0 93 });
michael@0 94
michael@0 95 // Test of the search callback on failure to load the engine failures
michael@0 96 add_test(function load_failure_test() {
michael@0 97 let searchCallback = {
michael@0 98 onSuccess: function (engine) {
michael@0 99 do_throw("this addition should not have succeeded");
michael@0 100 },
michael@0 101 onError: function (errorCode) {
michael@0 102 do_check_true(!!errorCode);
michael@0 103 do_check_eq(errorCode, Ci.nsISearchInstallCallback.ERROR_UNKNOWN_FAILURE);
michael@0 104 run_next_test();
michael@0 105 }
michael@0 106 }
michael@0 107 // Try adding an engine that doesn't exist
michael@0 108 Services.search.addEngine("http://invalid/data/engine.xml",
michael@0 109 Ci.nsISearchEngine.DATA_XML,
michael@0 110 null, false, searchCallback);
michael@0 111 });
michael@0 112
michael@0 113 function run_test() {
michael@0 114 updateAppInfo();
michael@0 115
michael@0 116 gHttpServer = new HttpServer();
michael@0 117 gHttpServer.start(-1);
michael@0 118 gHttpServer.registerDirectory("/", do_get_cwd());
michael@0 119 gBaseUrl = "http://localhost:" + gHttpServer.identity.primaryPort;
michael@0 120
michael@0 121 do_register_cleanup(function cleanup() {
michael@0 122 gHttpServer.stop(function() {});
michael@0 123 });
michael@0 124
michael@0 125 run_next_test();
michael@0 126 }

mercurial