diff --git a/browser/base/content/test/about/browser_aboutHome_search_POST.js b/browser/base/content/test/about/browser_aboutHome_search_POST.js index b8d2ef8dd45b..895786a9dc16 100644 --- a/browser/base/content/test/about/browser_aboutHome_search_POST.js +++ b/browser/base/content/test/about/browser_aboutHome_search_POST.js @@ -7,75 +7,83 @@ ignoreAllUncaughtExceptions(); add_task(async function() { info("Check POST search engine support"); - await BrowserTestUtils.withNewTab({ gBrowser, url: "about:home" }, function( - browser - ) { - return new Promise(resolve => { - let searchObserver = async function search_observer( - subject, - topic, - data - ) { - let currEngine = await Services.search.getDefault(); - let engine = subject.QueryInterface(Ci.nsISearchEngine); - info("Observer: " + data + " for " + engine.name); + let currEngine = await Services.search.getDefault(); - if (data != "engine-added") { - return; - } + await BrowserTestUtils.withNewTab( + { gBrowser, url: "about:home" }, + async browser => { + let observerPromise = new Promise(resolve => { + let searchObserver = async function search_observer( + subject, + topic, + data + ) { + let engine = subject.QueryInterface(Ci.nsISearchEngine); + info("Observer: " + data + " for " + engine.name); - if (engine.name != "POST Search") { - return; - } + if (data != "engine-added") { + return; + } - Services.obs.removeObserver( + if (engine.name != "POST Search") { + return; + } + + Services.obs.removeObserver( + searchObserver, + "browser-search-engine-modified" + ); + + resolve(engine); + }; + + Services.obs.addObserver( searchObserver, "browser-search-engine-modified" ); + }); - // Ready to execute the tests! - let needle = "Search for something awesome."; + let engine; + await promiseContentSearchChange(browser, async () => { + Services.search.addEngine( + "http://test:80/browser/browser/base/content/test/about/POSTSearchEngine.xml", + null, + false + ); - await Promise.all([ - promiseContentSearchChange(browser, engine.name), - Services.search.setDefault(engine), - ]); - let promise = BrowserTestUtils.browserLoaded(browser); - await SpecialPowers.spawn(browser, [{ needle }], async function(args) { - let doc = content.document; - let el = doc.querySelector(["#searchText", "#newtab-search-text"]); - el.value = args.needle; - doc.getElementById("searchSubmit").click(); - }); + engine = await observerPromise; + Services.search.setDefault(engine); + return engine.name; + }); - await promise; + // Ready to execute the tests! + let needle = "Search for something awesome."; - // When the search results load, check them for correctness. - await SpecialPowers.spawn(browser, [{ needle }], async function(args) { - let loadedText = content.document.body.textContent; - ok(loadedText, "search page loaded"); - is( - loadedText, - "searchterms=" + escape(args.needle.replace(/\s/g, "+")), - "Search text should arrive correctly" - ); - }); + let promise = BrowserTestUtils.browserLoaded(browser); + await SpecialPowers.spawn(browser, [{ needle }], async function(args) { + let doc = content.document; + let el = doc.querySelector(["#searchText", "#newtab-search-text"]); + el.value = args.needle; + doc.getElementById("searchSubmit").click(); + }); - await Services.search.setDefault(currEngine); - try { - await Services.search.removeEngine(engine); - } catch (ex) {} - resolve(); - }; - Services.obs.addObserver( - searchObserver, - "browser-search-engine-modified" - ); - Services.search.addEngine( - "http://test:80/browser/browser/base/content/test/about/POSTSearchEngine.xml", - null, - false - ); - }); - }); + await promise; + + // When the search results load, check them for correctness. + await SpecialPowers.spawn(browser, [{ needle }], async function(args) { + let loadedText = content.document.body.textContent; + ok(loadedText, "search page loaded"); + is( + loadedText, + "searchterms=" + escape(args.needle.replace(/\s/g, "+")), + "Search text should arrive correctly" + ); + }); + + await Services.search.setDefault(currEngine); + try { + await Services.search.removeEngine(engine); + } catch (ex) {} + } + ); }); diff --git a/browser/base/content/test/about/browser_aboutHome_search_composing.js b/browser/base/content/test/about/browser_aboutHome_search_composing.js index 8d32bf380e11..7668e8a37123 100644 --- a/browser/base/content/test/about/browser_aboutHome_search_composing.js +++ b/browser/base/content/test/about/browser_aboutHome_search_composing.js @@ -12,10 +12,13 @@ add_task(async function() { async function(browser) { // Add a test engine that provides suggestions and switch to it. let currEngine = await Services.search.getDefault(); - let engine = await promiseNewEngine("searchSuggestionEngine.xml"); - let p = promiseContentSearchChange(browser, engine.name); - await Services.search.setDefault(engine); - await p; + + let engine; + await promiseContentSearchChange(browser, async () => { + engine = await promiseNewEngine("searchSuggestionEngine.xml"); + await Services.search.setDefault(engine); + return engine.name; + }); // Clear any search history results await new Promise((resolve, reject) => { diff --git a/browser/base/content/test/about/browser_aboutHome_search_suggestion.js b/browser/base/content/test/about/browser_aboutHome_search_suggestion.js index 03006c11485c..6fe9aa22f00d 100644 --- a/browser/base/content/test/about/browser_aboutHome_search_suggestion.js +++ b/browser/base/content/test/about/browser_aboutHome_search_suggestion.js @@ -13,11 +13,13 @@ add_task(async function() { async function(browser) { // Add a test engine that provides suggestions and switch to it. let currEngine = await Services.search.getDefault(); - let engine = await promiseNewEngine("searchSuggestionEngine.xml"); - await Promise.all([ - promiseContentSearchChange(browser, engine.name), - Services.search.setDefault(engine), - ]); + + let engine; + await promiseContentSearchChange(browser, async () => { + engine = await promiseNewEngine("searchSuggestionEngine.xml"); + await Services.search.setDefault(engine); + return engine.name; + }); await SpecialPowers.spawn(browser, [], async function() { // Type an X in the search input. diff --git a/browser/base/content/test/about/browser_aboutHome_search_telemetry.js b/browser/base/content/test/about/browser_aboutHome_search_telemetry.js index 89c892e511bb..189a33271a3a 100644 --- a/browser/base/content/test/about/browser_aboutHome_search_telemetry.js +++ b/browser/base/content/test/about/browser_aboutHome_search_telemetry.js @@ -13,17 +13,19 @@ add_task(async function() { { gBrowser, url: "about:home" }, async function(browser) { let currEngine = await Services.search.getDefault(); - let engine = await promiseNewEngine("searchSuggestionEngine.xml"); + + let engine; + await promiseContentSearchChange(browser, async () => { + engine = await promiseNewEngine("searchSuggestionEngine.xml"); + await Services.search.setDefault(engine); + return engine.name; + }); + // Make this actually work in healthreport by giving it an ID: Object.defineProperty(engine.wrappedJSObject, "identifier", { value: "org.mozilla.testsearchsuggestions", }); - await Promise.all([ - promiseContentSearchChange(browser, engine.name), - Services.search.setDefault(engine), - ]); - await SpecialPowers.spawn( browser, [{ expectedName: engine.name }], diff --git a/browser/base/content/test/about/head.js b/browser/base/content/test/about/head.js index 9ff328afb2c4..55d79b556ad5 100644 --- a/browser/base/content/test/about/head.js +++ b/browser/base/content/test/about/head.js @@ -158,28 +158,56 @@ function promiseTabLoadEvent(tab, url) { } /** - * Wait for the search engine to change. + * Wait for the search engine to change. searchEngineChangeFn is a function + * that will be called to change the search engine. */ -function promiseContentSearchChange(browser, newEngineName) { - // Callers of this depend on very specific, very racy timing, and fail - // if we introduce the trip through SpecialPowersParent that - // SpecialPowers.spawn requires. - return ContentTask.spawn(browser, { newEngineName }, async function(args) { - return new Promise(resolve => { - content.addEventListener("ContentSearchService", function listener( - aEvent - ) { - if ( - aEvent.detail.type == "CurrentState" && - content.wrappedJSObject.gContentSearchController.defaultEngine.name == - args.newEngineName - ) { - content.removeEventListener("ContentSearchService", listener); - resolve(); +async function promiseContentSearchChange(browser, searchEngineChangeFn) { + // Add an event listener manually then perform the action, rather than using + // BrowserTestUtils.addContentEventListener as that doesn't add the listener + // early enough. + await SpecialPowers.spawn(browser, [], async () => { + // Store the results in a temporary place. + content._searchDetails = { + defaultEnginesList: [], + listener: event => { + if (event.detail.type == "CurrentState") { + content._searchDetails.defaultEnginesList.push( + content.wrappedJSObject.gContentSearchController.defaultEngine.name + ); } - }); - }); + }, + }; + + // Listen using the system group to ensure that it fires after + // the default behaviour. + content.addEventListener( + "ContentSearchService", + content._searchDetails.listener, + { mozSystemGroup: true } + ); }); + + let expectedEngineName = await searchEngineChangeFn(); + + await SpecialPowers.spawn( + browser, + [expectedEngineName], + async expectedEngineNameChild => { + await ContentTaskUtils.waitForCondition( + () => + content._searchDetails.defaultEnginesList && + content._searchDetails.defaultEnginesList[ + content._searchDetails.defaultEnginesList.length - 1 + ] == expectedEngineNameChild + ); + content.removeEventListener( + "ContentSearchService", + content._searchDetails.listener, + { mozSystemGroup: true } + ); + delete content._searchDetails; + } + ); } /**