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