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

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:90436d06f948
1 /* Any copyright is dedicated to the Public Domain.
2 * http://creativecommons.org/publicdomain/zero/1.0/ */
3
4 /*
5 * Test that a search purpose can be specified and that query parameters for
6 * that purpose are included in the search URL.
7 */
8
9 "use strict";
10
11 const Ci = Components.interfaces;
12
13 Components.utils.import("resource://testing-common/httpd.js");
14
15 function search_observer(aSubject, aTopic, aData) {
16 let engine = aSubject.QueryInterface(Ci.nsISearchEngine);
17 do_print("Observer: " + aData + " for " + engine.name);
18
19 if (aData != "engine-added")
20 return;
21
22 if (engine.name != "Test search engine")
23 return;
24
25 function check_submission(aExpected, aSearchTerm, aType, aPurpose) {
26 do_check_eq(engine.getSubmission(aSearchTerm, aType, aPurpose).uri.spec,
27 base + aExpected);
28 }
29
30 let base = "http://www.google.com/search?q=foo&ie=utf-8&oe=utf-8&aq=t&client=firefox";
31 check_submission("", "foo");
32 check_submission("", "foo", null);
33 check_submission("", "foo", "text/html");
34 check_submission("&channel=rcs", "foo", null, "contextmenu");
35 check_submission("&channel=rcs", "foo", "text/html", "contextmenu");
36 check_submission("&channel=fflb", "foo", null, "keyword");
37 check_submission("&channel=fflb", "foo", "text/html", "keyword");
38 check_submission("", "foo", "text/html", "invalid");
39
40 // Tests for a param that varies with a purpose but has a default value.
41 base = "http://www.google.com/search?q=foo&client=firefox";
42 check_submission("&channel=none", "foo", "application/x-moz-default-purpose");
43 check_submission("&channel=none", "foo", "application/x-moz-default-purpose", null);
44 check_submission("&channel=none", "foo", "application/x-moz-default-purpose", "");
45 check_submission("&channel=rcs", "foo", "application/x-moz-default-purpose", "contextmenu");
46 check_submission("&channel=fflb", "foo", "application/x-moz-default-purpose", "keyword");
47 check_submission("", "foo", "application/x-moz-default-purpose", "invalid");
48
49 do_test_finished();
50 };
51
52 function run_test() {
53 removeMetadata();
54 updateAppInfo();
55 do_load_manifest("data/chrome.manifest");
56
57 let httpServer = new HttpServer();
58 httpServer.start(-1);
59 httpServer.registerDirectory("/", do_get_cwd());
60
61 do_register_cleanup(function cleanup() {
62 httpServer.stop(function() {});
63 Services.obs.removeObserver(search_observer, "browser-search-engine-modified");
64 });
65
66 do_test_pending();
67 Services.obs.addObserver(search_observer, "browser-search-engine-modified", false);
68
69 Services.search.addEngine("http://localhost:" +
70 httpServer.identity.primaryPort +
71 "/data/engine.xml",
72 Ci.nsISearchEngine.DATA_XML,
73 null, false);
74 }

mercurial