1 /* Any copyright is dedicated to the Public Domain.
2 http://creativecommons.org/publicdomain/zero/1.0/ */
5 * This test ensures the placeholder is set correctly for different search
11 var originalEngine, extraEngine, extraPrivateEngine, expectedString;
16 add_setup(async function () {
17 originalEngine = await Services.search.getDefault();
18 [noEngineString, expectedString] = (
19 await document.l10n.formatMessages([
20 { id: "urlbar-placeholder" },
22 id: "urlbar-placeholder-with-name",
23 args: { name: originalEngine.name },
26 ).map(msg => msg.attributes[0].value);
28 let rootUrl = getRootDirectory(gTestPath).replace(
29 "chrome://mochitests/content",
30 "https://mochi.test:8888/"
32 await SearchTestUtils.installSearchExtension({
34 search_url: "https://mochi.test:8888/",
35 suggest_url: `${rootUrl}/searchSuggestionEngine.sjs`,
37 extraEngine = Services.search.getEngineByName("extraEngine");
38 await SearchTestUtils.installSearchExtension({
39 name: "extraPrivateEngine",
40 search_url: "https://mochi.test:8888/",
41 suggest_url: `${rootUrl}/searchSuggestionEngine.sjs`,
43 extraPrivateEngine = Services.search.getEngineByName("extraPrivateEngine");
45 // Force display of a tab with a URL bar, to clear out any possible placeholder
46 // initialization listeners that happen on startup.
47 let urlTab = await BrowserTestUtils.openNewForegroundTab(
51 BrowserTestUtils.removeTab(urlTab);
53 await SpecialPowers.pushPrefEnv({
55 ["browser.search.separatePrivateDefault.ui.enabled", true],
56 ["browser.search.separatePrivateDefault", false],
57 ["browser.urlbar.suggest.quickactions", false],
61 registerCleanupFunction(async () => {
62 await Services.search.setDefault(
64 Ci.nsISearchService.CHANGE_REASON_UNKNOWN
66 for (let tab of tabs) {
67 BrowserTestUtils.removeTab(tab);
72 add_task(async function test_change_default_engine_updates_placeholder() {
73 tabs.push(await BrowserTestUtils.openNewForegroundTab(gBrowser));
75 await Services.search.setDefault(
77 Ci.nsISearchService.CHANGE_REASON_UNKNOWN
80 await TestUtils.waitForCondition(
81 () => gURLBar.placeholder == noEngineString,
82 "The placeholder should match the default placeholder for non-built-in engines."
84 Assert.equal(gURLBar.placeholder, noEngineString);
86 await Services.search.setDefault(
88 Ci.nsISearchService.CHANGE_REASON_UNKNOWN
91 await TestUtils.waitForCondition(
92 () => gURLBar.placeholder == expectedString,
93 "The placeholder should include the engine name for built-in engines."
95 Assert.equal(gURLBar.placeholder, expectedString);
98 add_task(async function test_delayed_update_placeholder() {
99 // We remove the change of engine listener here as that is set so that
100 // if the engine is changed by the user then the placeholder is always updated
101 // straight away. As we want to test the delay update here, we remove the
102 // listener and call the placeholder update manually with the delay flag.
103 Services.obs.removeObserver(BrowserSearch, "browser-search-engine-modified");
105 // Since we can't easily test for startup changes, we'll at least test the delay
106 // of update for the placeholder works.
107 let urlTab = await BrowserTestUtils.openNewForegroundTab(
113 // Open a tab with a blank URL bar.
114 let blankTab = await BrowserTestUtils.openNewForegroundTab(gBrowser);
117 await Services.search.setDefault(
119 Ci.nsISearchService.CHANGE_REASON_UNKNOWN
121 // Pretend we've "initialized".
122 BrowserSearch._updateURLBarPlaceholder(extraEngine.name, false, true);
127 "Placeholder should be unchanged."
130 // Now switch to a tab with something in the URL Bar.
131 await BrowserTestUtils.switchTab(gBrowser, urlTab);
133 await TestUtils.waitForCondition(
134 () => gURLBar.placeholder == noEngineString,
135 "The placeholder should have updated in the background."
138 // Do it the other way to check both named engine and fallback code paths.
139 await BrowserTestUtils.switchTab(gBrowser, blankTab);
141 await Services.search.setDefault(
143 Ci.nsISearchService.CHANGE_REASON_UNKNOWN
145 BrowserSearch._updateURLBarPlaceholder(originalEngine.name, false, true);
150 "Placeholder should be unchanged."
153 document.l10n.getAttributes(gURLBar.inputField),
154 { id: "urlbar-placeholder", args: null },
155 "Placeholder data should be unchanged."
158 await BrowserTestUtils.switchTab(gBrowser, urlTab);
160 await TestUtils.waitForCondition(
161 () => gURLBar.placeholder == expectedString,
162 "The placeholder should include the engine name for built-in engines."
165 // Now check when we have a URL displayed, the placeholder is updated straight away.
166 BrowserSearch._updateURLBarPlaceholder(extraEngine.name, false);
168 await TestUtils.waitForCondition(
169 () => gURLBar.placeholder == noEngineString,
170 "The placeholder should go back to the default"
175 "Placeholder should be the default."
178 Services.obs.addObserver(BrowserSearch, "browser-search-engine-modified");
181 add_task(async function test_private_window_no_separate_engine() {
182 const win = await BrowserTestUtils.openNewBrowserWindow({ private: true });
184 await Services.search.setDefault(
186 Ci.nsISearchService.CHANGE_REASON_UNKNOWN
189 await TestUtils.waitForCondition(
190 () => win.gURLBar.placeholder == noEngineString,
191 "The placeholder should match the default placeholder for non-built-in engines."
193 Assert.equal(win.gURLBar.placeholder, noEngineString);
195 await Services.search.setDefault(
197 Ci.nsISearchService.CHANGE_REASON_UNKNOWN
200 await TestUtils.waitForCondition(
201 () => win.gURLBar.placeholder == expectedString,
202 "The placeholder should include the engine name for built-in engines."
204 Assert.equal(win.gURLBar.placeholder, expectedString);
206 await BrowserTestUtils.closeWindow(win);
209 add_task(async function test_private_window_separate_engine() {
210 await SpecialPowers.pushPrefEnv({
211 set: [["browser.search.separatePrivateDefault", true]],
213 const originalPrivateEngine = await Services.search.getDefaultPrivate();
214 registerCleanupFunction(async () => {
215 await Services.search.setDefaultPrivate(
216 originalPrivateEngine,
217 Ci.nsISearchService.CHANGE_REASON_UNKNOWN
221 const win = await BrowserTestUtils.openNewBrowserWindow({ private: true });
223 // Keep the normal default as a different string to the private, so that we
224 // can be sure we're testing the right thing.
225 await Services.search.setDefault(
227 Ci.nsISearchService.CHANGE_REASON_UNKNOWN
229 await Services.search.setDefaultPrivate(
231 Ci.nsISearchService.CHANGE_REASON_UNKNOWN
234 await TestUtils.waitForCondition(
235 () => win.gURLBar.placeholder == noEngineString,
236 "The placeholder should match the default placeholder for non-built-in engines."
238 Assert.equal(win.gURLBar.placeholder, noEngineString);
240 await Services.search.setDefault(
242 Ci.nsISearchService.CHANGE_REASON_UNKNOWN
244 await Services.search.setDefaultPrivate(
246 Ci.nsISearchService.CHANGE_REASON_UNKNOWN
249 await TestUtils.waitForCondition(
250 () => win.gURLBar.placeholder == expectedString,
251 "The placeholder should include the engine name for built-in engines."
253 Assert.equal(win.gURLBar.placeholder, expectedString);
255 await BrowserTestUtils.closeWindow(win);
257 // Verify that the placeholder for private windows is updated even when no
258 // private window is visible (https://bugzilla.mozilla.org/1792816).
259 await Services.search.setDefault(
261 Ci.nsISearchService.CHANGE_REASON_UNKNOWN
263 await Services.search.setDefaultPrivate(
265 Ci.nsISearchService.CHANGE_REASON_UNKNOWN
267 const win2 = await BrowserTestUtils.openNewBrowserWindow({ private: true });
268 Assert.equal(win2.gURLBar.placeholder, noEngineString);
269 await BrowserTestUtils.closeWindow(win2);
271 // And ensure this doesn't affect the placeholder for non private windows.
272 tabs.push(await BrowserTestUtils.openNewForegroundTab(gBrowser));
273 Assert.equal(win.gURLBar.placeholder, expectedString);
276 add_task(async function test_search_mode_engine_web() {
277 // Add our test engine to WEB_ENGINE_NAMES so that it's recognized as a web
279 SearchUtils.GENERAL_SEARCH_ENGINE_IDS.add(
280 extraEngine.wrappedJSObject._extensionID
283 await doSearchModeTest(
285 source: UrlbarUtils.RESULT_SOURCE.SEARCH,
286 engineName: extraEngine.name,
289 id: "urlbar-placeholder-search-mode-web-2",
290 args: { name: extraEngine.name },
294 SearchUtils.GENERAL_SEARCH_ENGINE_IDS.delete(
295 extraEngine.wrappedJSObject._extensionID
299 add_task(async function test_search_mode_engine_other() {
300 await doSearchModeTest(
301 { engineName: extraEngine.name },
303 id: "urlbar-placeholder-search-mode-other-engine",
304 args: { name: extraEngine.name },
309 add_task(async function test_search_mode_bookmarks() {
310 await doSearchModeTest(
311 { source: UrlbarUtils.RESULT_SOURCE.BOOKMARKS },
312 { id: "urlbar-placeholder-search-mode-other-bookmarks", args: null }
316 add_task(async function test_search_mode_tabs() {
317 await doSearchModeTest(
318 { source: UrlbarUtils.RESULT_SOURCE.TABS },
319 { id: "urlbar-placeholder-search-mode-other-tabs", args: null }
323 add_task(async function test_search_mode_history() {
324 await doSearchModeTest(
325 { source: UrlbarUtils.RESULT_SOURCE.HISTORY },
326 { id: "urlbar-placeholder-search-mode-other-history", args: null }
330 add_task(async function test_change_default_engine_updates_placeholder() {
331 tabs.push(await BrowserTestUtils.openNewForegroundTab(gBrowser));
333 info(`Set engine to ${extraEngine.name}`);
334 await Services.search.setDefault(
336 Ci.nsISearchService.CHANGE_REASON_UNKNOWN
338 await TestUtils.waitForCondition(
339 () => gURLBar.placeholder == noEngineString,
340 "The placeholder should match the default placeholder for non-built-in engines."
342 Assert.equal(gURLBar.placeholder, noEngineString);
344 info(`Set engine to ${originalEngine.name}`);
345 await Services.search.setDefault(
347 Ci.nsISearchService.CHANGE_REASON_UNKNOWN
349 await TestUtils.waitForCondition(
350 () => gURLBar.placeholder == expectedString,
351 "The placeholder should include the engine name for built-in engines."
354 // Simulate the placeholder not having changed due to the delayed update
356 BrowserSearch._setURLBarPlaceholder("");
357 await TestUtils.waitForCondition(
358 () => gURLBar.placeholder == noEngineString,
359 "The placeholder should have been reset."
362 info("Show search engine removal info bar");
363 BrowserSearch.removalOfSearchEngineNotificationBox(
367 const notificationBox = gNotificationBox.getNotificationWithValue(
368 "search-engine-removal"
370 Assert.ok(notificationBox, "Search engine removal should be shown.");
372 await TestUtils.waitForCondition(
373 () => gURLBar.placeholder == expectedString,
374 "The placeholder should include the engine name for built-in engines."
377 Assert.equal(gURLBar.placeholder, expectedString);
379 notificationBox.close();
383 * Opens the view, clicks a one-off button to enter search mode, and asserts
384 * that the placeholder is corrrect.
386 * @param {object} expectedSearchMode
387 * The expected search mode object for the one-off.
388 * @param {object} expectedPlaceholderL10n
389 * The expected l10n object for the one-off.
391 async function doSearchModeTest(expectedSearchMode, expectedPlaceholderL10n) {
392 // Click the urlbar to open the top-sites view.
393 if (gURLBar.getAttribute("pageproxystate") == "invalid") {
394 gURLBar.handleRevert();
396 await UrlbarTestUtils.promisePopupOpen(window, () => {
397 EventUtils.synthesizeMouseAtCenter(gURLBar.inputField, {});
400 // Enter search mode.
401 await UrlbarTestUtils.enterSearchMode(window, expectedSearchMode);
403 // Check the placeholder.
405 document.l10n.getAttributes(gURLBar.inputField),
406 expectedPlaceholderL10n,
407 "Placeholder has expected l10n"
410 await UrlbarTestUtils.exitSearchMode(window, { clickClose: true });
411 await UrlbarTestUtils.promisePopupClose(window);