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

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:9e83fdd7e858
1 /* Any copyright is dedicated to the Public Domain.
2 http://creativecommons.org/publicdomain/zero/1.0/ */
3
4 /*
5 * Test initializing from the search cache.
6 */
7
8 "use strict";
9
10 const Cc = Components.classes;
11 const Ci = Components.interfaces;
12
13 // Metadata to write to search-metadata.json for the test.
14 let gMetadata = {"[profile]/test-search-engine.xml":{"used":true}};
15
16 /**
17 * Gets a directory from the directory service.
18 * @param aKey
19 * The directory service key indicating the directory to get.
20 */
21 let _dirSvc = null;
22 function getDir(aKey, aIFace) {
23 if (!aKey) {
24 FAIL("getDir requires a directory key!");
25 }
26
27 if (!_dirSvc) {
28 _dirSvc = Cc["@mozilla.org/file/directory_service;1"].
29 getService(Ci.nsIProperties);
30 }
31 return _dirSvc.get(aKey, aIFace || Ci.nsIFile);
32 }
33
34 let cacheTemplate, appPluginsPath, profPlugins;
35
36 /**
37 * Test reading from search.json
38 */
39 function run_test() {
40 removeMetadata();
41 removeCacheFile();
42
43 updateAppInfo();
44
45 let cacheTemplateFile = do_get_file("data/search.json");
46 cacheTemplate = readJSONFile(cacheTemplateFile);
47
48 let engineFile = gProfD.clone();
49 engineFile.append("searchplugins");
50 engineFile.append("test-search-engine.xml");
51 engineFile.parent.create(Ci.nsIFile.DIRECTORY_TYPE, FileUtils.PERMS_DIRECTORY);
52
53 // Copy the test engine to the test profile.
54 let engineTemplateFile = do_get_file("data/engine.xml");
55 engineTemplateFile.copyTo(engineFile.parent, "test-search-engine.xml");
56
57 // Add the app's searchplugins directory to the cache so it won't be ignored.
58 let appSearchPlugins = getDir(NS_APP_SEARCH_DIR);
59 appPluginsPath = appSearchPlugins.path;
60 cacheTemplate.directories[appPluginsPath] = {};
61 cacheTemplate.directories[appPluginsPath].lastModifiedTime = appSearchPlugins.lastModifiedTime;
62 cacheTemplate.directories[appPluginsPath].engines = [];
63
64 // Replace the profile placeholder with the correct path.
65 profPlugins = engineFile.parent.path;
66 cacheTemplate.directories[profPlugins] = cacheTemplate.directories["[profile]/searchplugins"];
67 delete cacheTemplate.directories["[profile]/searchplugins"];
68 cacheTemplate.directories[profPlugins].engines[0].filePath = engineFile.path;
69 cacheTemplate.directories[profPlugins].lastModifiedTime = engineFile.parent.lastModifiedTime;
70
71 run_next_test();
72 }
73
74 add_test(function prepare_test_data() {
75
76 let ostream = Cc["@mozilla.org/network/file-output-stream;1"].
77 createInstance(Ci.nsIFileOutputStream);
78 let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"].
79 createInstance(Ci.nsIScriptableUnicodeConverter);
80
81 // Write the modified cache template to the profile directory.
82 let cacheFile = gProfD.clone();
83 cacheFile.append("search.json");
84 ostream.init(cacheFile, (MODE_WRONLY | MODE_CREATE | MODE_TRUNCATE), FileUtils.PERMS_FILE,
85 ostream.DEFER_OPEN);
86 converter.charset = "UTF-8";
87 let data = converter.convertToInputStream(JSON.stringify(cacheTemplate));
88
89 // Write to the cache and metadata files asynchronously before starting the search service.
90 NetUtil.asyncCopy(data, ostream, function afterMetadataCopy(aResult) {
91 do_check_true(Components.isSuccessCode(aResult));
92 let metadataFile = gProfD.clone();
93 metadataFile.append("search-metadata.json");
94
95 let ostream = Cc["@mozilla.org/network/file-output-stream;1"].
96 createInstance(Ci.nsIFileOutputStream);
97 let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"].
98 createInstance(Ci.nsIScriptableUnicodeConverter);
99
100 ostream.init(metadataFile, (MODE_WRONLY | MODE_CREATE | MODE_TRUNCATE), FileUtils.PERMS_FILE,
101 ostream.DEFER_OPEN);
102 converter.charset = "UTF-8";
103 let data = converter.convertToInputStream(JSON.stringify(gMetadata));
104
105 NetUtil.asyncCopy(data, ostream, function afterCacheCopy(aResult) {
106 do_check_true(Components.isSuccessCode(aResult));
107 run_next_test();
108 });
109 });
110 });
111
112 /**
113 * Start the search service and confirm the engine properties match the expected values.
114 */
115 add_test(function test_cached_engine_properties() {
116 do_print("init search service");
117
118 let search = Services.search.init(function initComplete(aResult) {
119 do_print("init'd search service");
120 do_check_true(Components.isSuccessCode(aResult));
121
122 let engines = Services.search.getEngines({});
123 let engine = engines[0];
124
125 do_check_true(engine instanceof Ci.nsISearchEngine);
126 isSubObjectOf(EXPECTED_ENGINE.engine, engine);
127
128 let engineFromSS = Services.search.getEngineByName(EXPECTED_ENGINE.engine.name);
129 do_check_true(!!engineFromSS);
130 isSubObjectOf(EXPECTED_ENGINE.engine, engineFromSS);
131
132 removeMetadata();
133 removeCacheFile();
134 run_next_test();
135 });
136 });
137
138 /**
139 * Test that the JSON cache written in the profile is correct.
140 */
141 add_test(function test_cache_write() {
142 do_print("test cache writing");
143
144 let cache = gProfD.clone();
145 cache.append("search.json");
146 do_check_false(cache.exists());
147
148 do_print("Next step is forcing flush");
149 do_timeout(0, function forceFlush() {
150 do_print("Forcing flush");
151 // Force flush
152 // Note: the timeout is needed, to avoid some reentrency
153 // issues in nsSearchService.
154
155 let cacheWriteObserver = {
156 observe: function cacheWriteObserver_observe(aEngine, aTopic, aVerb) {
157 if (aTopic != "browser-search-service" || aVerb != "write-cache-to-disk-complete") {
158 return;
159 }
160 Services.obs.removeObserver(cacheWriteObserver, "browser-search-service");
161 do_print("Cache write complete");
162 do_check_true(cache.exists());
163 // Check that the search.json cache matches the template
164
165 let cacheWritten = readJSONFile(cache);
166 // Delete the app search plugins directory from the template since it's not currently written out.
167 delete cacheTemplate.directories[appPluginsPath];
168
169 do_print("Check search.json");
170 isSubObjectOf(cacheTemplate, cacheWritten);
171
172 run_next_test();
173 }
174 };
175 Services.obs.addObserver(cacheWriteObserver, "browser-search-service", false);
176
177 Services.search.QueryInterface(Ci.nsIObserver).observe(null, "browser-search-engine-modified" , "engine-removed");
178 });
179 });
180
181 let EXPECTED_ENGINE = {
182 engine: {
183 name: "Test search engine",
184 alias: null,
185 description: "A test search engine (based on Google search)",
186 searchForm: "http://www.google.com/",
187 type: Ci.nsISearchEngine.TYPE_M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GGcIRJrERILYFEMwAAuEAAdX%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",
190 _urls : [
191 {
192 type: "application/x-suggestions+json",
193 method: "GET",
194 template: "http://suggestqueries.google.com/complete/search?output=firefox&client=firefox" +
195 "&hl={moz:locale}&q={searchTerms}",
196 params: "",
197 },
198 {
199 type: "text/html",
200 method: "GET",
201 template: "http://www.google.com/search",
202 resultDomain: "google.com",
203 params: [
204 {
205 "name": "q",
206 "value": "{searchTerms}",
207 "purpose": undefined,
208 },
209 {
210 "name": "ie",
211 "value": "utf-8",
212 "purpose": undefined,
213 },
214 {
215 "name": "oe",
216 "value": "utf-8",
217 "purpose": undefined,
218 },
219 {
220 "name": "aq",
221 "value": "t",
222 "purpose": undefined,
223 },
224 {
225 "name": "client",
226 "value": "firefox",
227 "purpose": undefined,
228 },
229 {
230 "name": "channel",
231 "value": "fflb",
232 "purpose": "keyword",
233 },
234 {
235 "name": "channel",
236 "value": "rcs",
237 "purpose": "contextmenu",
238 },
239 ],
240 mozparams: {
241 "client": {
242 "name": "client",
243 "falseValue": "firefox",
244 "trueValue": "firefox-a",
245 "condition": "defaultEngine",
246 "mozparam": true,
247 },
248 },
249 },
250 {
251 type: "application/x-moz-default-purpose",
252 method: "GET",
253 template: "http://www.google.com/search",
254 resultDomain: "purpose.google.com",
255 params: [
256 {
257 "name": "q",
258 "value": "{searchTerms}",
259 "purpose": undefined,
260 },
261 {
262 "name": "client",
263 "value": "firefox",
264 "purpose": undefined,
265 },
266 {
267 "name": "channel",
268 "value": "none",
269 "purpose": "",
270 },
271 {
272 "name": "channel",
273 "value": "fflb",
274 "purpose": "keyword",
275 },
276 {
277 "name": "channel",
278 "value": "rcs",
279 "purpose": "contextmenu",
280 },
281 ],
282 mozparams: {
283 "client": {
284 "name": "client",
285 "falseValue": "firefox",
286 "trueValue": "firefox-a",
287 "condition": "defaultEngine",
288 "mozparam": true,
289 },
290 },
291 },
292 ],
293 },
294 },
295 };

mercurial