head_docshell.js (2735B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 var { XPCOMUtils } = ChromeUtils.importESModule( 6 "resource://gre/modules/XPCOMUtils.sys.mjs" 7 ); 8 9 ChromeUtils.defineESModuleGetters(this, { 10 HttpServer: "resource://testing-common/httpd.sys.mjs", 11 NetUtil: "resource://gre/modules/NetUtil.sys.mjs", 12 SearchTestUtils: "resource://testing-common/SearchTestUtils.sys.mjs", 13 SearchUtils: "moz-src:///toolkit/components/search/SearchUtils.sys.mjs", 14 TestUtils: "resource://testing-common/TestUtils.sys.mjs", 15 }); 16 17 var profileDir = do_get_profile(); 18 19 const kSearchEngineID = "test_urifixup_search_engine"; 20 const kSearchEngineURL = "https://www.example.org/?search={searchTerms}"; 21 const kPrivateSearchEngineID = "test_urifixup_search_engine_private"; 22 const kPrivateSearchEngineURL = 23 "https://www.example.org/?private={searchTerms}"; 24 const kPostSearchEngineID = "test_urifixup_search_engine_post"; 25 const kPostSearchEngineURL = "https://www.example.org/"; 26 const kPostSearchEngineData = "q={searchTerms}"; 27 28 const CONFIG = [{ identifier: "test_urifixup_search_engine_app_provided" }]; 29 30 async function setupSearchService() { 31 SearchTestUtils.init(this); 32 33 await SearchTestUtils.setRemoteSettingsConfig(CONFIG); 34 await Services.search.init(); 35 } 36 37 /** 38 * After useHttpServer() is called, this string contains the URL of the "data" 39 * directory, including the final slash. 40 */ 41 var gDataUrl; 42 43 /** 44 * Initializes the HTTP server and ensures that it is terminated when tests end. 45 * 46 * @param {string} dir 47 * The test sub-directory to use for the engines. 48 * @returns {HttpServer} 49 * The HttpServer object in case further customization is needed. 50 */ 51 function useHttpServer(dir = "data") { 52 let httpServer = new HttpServer(); 53 httpServer.start(-1); 54 httpServer.registerDirectory("/", do_get_cwd()); 55 gDataUrl = `http://localhost:${httpServer.identity.primaryPort}/${dir}/`; 56 registerCleanupFunction(async function cleanup_httpServer() { 57 await new Promise(resolve => { 58 httpServer.stop(resolve); 59 }); 60 }); 61 return httpServer; 62 } 63 64 async function addTestEngines() { 65 useHttpServer(); 66 // This is a hack, ideally we should be setting up a configuration with 67 // built-in engines, but the `chrome_settings_overrides` section that 68 // WebExtensions need is only defined for browser/ 69 await SearchTestUtils.installOpenSearchEngine({ 70 url: `${gDataUrl}/engine.xml`, 71 }); 72 await SearchTestUtils.installOpenSearchEngine({ 73 url: `${gDataUrl}/enginePrivate.xml`, 74 }); 75 await SearchTestUtils.installOpenSearchEngine({ 76 url: `${gDataUrl}/enginePost.xml`, 77 }); 78 }