1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/search/tests/xpcshell/test_purpose.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,74 @@ 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 + * Test that a search purpose can be specified and that query parameters for 1.9 + * that purpose are included in the search URL. 1.10 + */ 1.11 + 1.12 +"use strict"; 1.13 + 1.14 +const Ci = Components.interfaces; 1.15 + 1.16 +Components.utils.import("resource://testing-common/httpd.js"); 1.17 + 1.18 +function search_observer(aSubject, aTopic, aData) { 1.19 + let engine = aSubject.QueryInterface(Ci.nsISearchEngine); 1.20 + do_print("Observer: " + aData + " for " + engine.name); 1.21 + 1.22 + if (aData != "engine-added") 1.23 + return; 1.24 + 1.25 + if (engine.name != "Test search engine") 1.26 + return; 1.27 + 1.28 + function check_submission(aExpected, aSearchTerm, aType, aPurpose) { 1.29 + do_check_eq(engine.getSubmission(aSearchTerm, aType, aPurpose).uri.spec, 1.30 + base + aExpected); 1.31 + } 1.32 + 1.33 + let base = "http://www.google.com/search?q=foo&ie=utf-8&oe=utf-8&aq=t&client=firefox"; 1.34 + check_submission("", "foo"); 1.35 + check_submission("", "foo", null); 1.36 + check_submission("", "foo", "text/html"); 1.37 + check_submission("&channel=rcs", "foo", null, "contextmenu"); 1.38 + check_submission("&channel=rcs", "foo", "text/html", "contextmenu"); 1.39 + check_submission("&channel=fflb", "foo", null, "keyword"); 1.40 + check_submission("&channel=fflb", "foo", "text/html", "keyword"); 1.41 + check_submission("", "foo", "text/html", "invalid"); 1.42 + 1.43 + // Tests for a param that varies with a purpose but has a default value. 1.44 + base = "http://www.google.com/search?q=foo&client=firefox"; 1.45 + check_submission("&channel=none", "foo", "application/x-moz-default-purpose"); 1.46 + check_submission("&channel=none", "foo", "application/x-moz-default-purpose", null); 1.47 + check_submission("&channel=none", "foo", "application/x-moz-default-purpose", ""); 1.48 + check_submission("&channel=rcs", "foo", "application/x-moz-default-purpose", "contextmenu"); 1.49 + check_submission("&channel=fflb", "foo", "application/x-moz-default-purpose", "keyword"); 1.50 + check_submission("", "foo", "application/x-moz-default-purpose", "invalid"); 1.51 + 1.52 + do_test_finished(); 1.53 +}; 1.54 + 1.55 +function run_test() { 1.56 + removeMetadata(); 1.57 + updateAppInfo(); 1.58 + do_load_manifest("data/chrome.manifest"); 1.59 + 1.60 + let httpServer = new HttpServer(); 1.61 + httpServer.start(-1); 1.62 + httpServer.registerDirectory("/", do_get_cwd()); 1.63 + 1.64 + do_register_cleanup(function cleanup() { 1.65 + httpServer.stop(function() {}); 1.66 + Services.obs.removeObserver(search_observer, "browser-search-engine-modified"); 1.67 + }); 1.68 + 1.69 + do_test_pending(); 1.70 + Services.obs.addObserver(search_observer, "browser-search-engine-modified", false); 1.71 + 1.72 + Services.search.addEngine("http://localhost:" + 1.73 + httpServer.identity.primaryPort + 1.74 + "/data/engine.xml", 1.75 + Ci.nsISearchEngine.DATA_XML, 1.76 + null, false); 1.77 +}