Backed out 7 changesets (bug 1845150) for causing dt failures on browser_screenshot_b...
[gecko.git] / browser / components / urlbar / tests / browser / browser_placeholder.js
blobfbf0a5007c62a9230d69fa9ed1f7aed3c4c795fd
1 /* Any copyright is dedicated to the Public Domain.
2    http://creativecommons.org/publicdomain/zero/1.0/ */
4 /**
5  * This test ensures the placeholder is set correctly for different search
6  * engines.
7  */
9 "use strict";
11 var originalEngine, extraEngine, extraPrivateEngine, expectedString;
12 var tabs = [];
14 var noEngineString;
16 add_setup(async function () {
17   originalEngine = await Services.search.getDefault();
18   [noEngineString, expectedString] = (
19     await document.l10n.formatMessages([
20       { id: "urlbar-placeholder" },
21       {
22         id: "urlbar-placeholder-with-name",
23         args: { name: originalEngine.name },
24       },
25     ])
26   ).map(msg => msg.attributes[0].value);
28   let rootUrl = getRootDirectory(gTestPath).replace(
29     "chrome://mochitests/content",
30     "https://mochi.test:8888/"
31   );
32   await SearchTestUtils.installSearchExtension({
33     name: "extraEngine",
34     search_url: "https://mochi.test:8888/",
35     suggest_url: `${rootUrl}/searchSuggestionEngine.sjs`,
36   });
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`,
42   });
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(
48     gBrowser,
49     "about:mozilla"
50   );
51   BrowserTestUtils.removeTab(urlTab);
53   await SpecialPowers.pushPrefEnv({
54     set: [
55       ["browser.search.separatePrivateDefault.ui.enabled", true],
56       ["browser.search.separatePrivateDefault", false],
57       ["browser.urlbar.suggest.quickactions", false],
58     ],
59   });
61   registerCleanupFunction(async () => {
62     await Services.search.setDefault(
63       originalEngine,
64       Ci.nsISearchService.CHANGE_REASON_UNKNOWN
65     );
66     for (let tab of tabs) {
67       BrowserTestUtils.removeTab(tab);
68     }
69   });
70 });
72 add_task(async function test_change_default_engine_updates_placeholder() {
73   tabs.push(await BrowserTestUtils.openNewForegroundTab(gBrowser));
75   await Services.search.setDefault(
76     extraEngine,
77     Ci.nsISearchService.CHANGE_REASON_UNKNOWN
78   );
80   await TestUtils.waitForCondition(
81     () => gURLBar.placeholder == noEngineString,
82     "The placeholder should match the default placeholder for non-built-in engines."
83   );
84   Assert.equal(gURLBar.placeholder, noEngineString);
86   await Services.search.setDefault(
87     originalEngine,
88     Ci.nsISearchService.CHANGE_REASON_UNKNOWN
89   );
91   await TestUtils.waitForCondition(
92     () => gURLBar.placeholder == expectedString,
93     "The placeholder should include the engine name for built-in engines."
94   );
95   Assert.equal(gURLBar.placeholder, expectedString);
96 });
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(
108     gBrowser,
109     "about:mozilla"
110   );
111   tabs.push(urlTab);
113   // Open a tab with a blank URL bar.
114   let blankTab = await BrowserTestUtils.openNewForegroundTab(gBrowser);
115   tabs.push(blankTab);
117   await Services.search.setDefault(
118     extraEngine,
119     Ci.nsISearchService.CHANGE_REASON_UNKNOWN
120   );
121   // Pretend we've "initialized".
122   BrowserSearch._updateURLBarPlaceholder(extraEngine.name, false, true);
124   Assert.equal(
125     gURLBar.placeholder,
126     expectedString,
127     "Placeholder should be unchanged."
128   );
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."
136   );
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(
142     originalEngine,
143     Ci.nsISearchService.CHANGE_REASON_UNKNOWN
144   );
145   BrowserSearch._updateURLBarPlaceholder(originalEngine.name, false, true);
147   Assert.equal(
148     gURLBar.placeholder,
149     noEngineString,
150     "Placeholder should be unchanged."
151   );
152   Assert.deepEqual(
153     document.l10n.getAttributes(gURLBar.inputField),
154     { id: "urlbar-placeholder", args: null },
155     "Placeholder data should be unchanged."
156   );
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."
163   );
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"
171   );
172   Assert.equal(
173     gURLBar.placeholder,
174     noEngineString,
175     "Placeholder should be the default."
176   );
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(
185     extraEngine,
186     Ci.nsISearchService.CHANGE_REASON_UNKNOWN
187   );
189   await TestUtils.waitForCondition(
190     () => win.gURLBar.placeholder == noEngineString,
191     "The placeholder should match the default placeholder for non-built-in engines."
192   );
193   Assert.equal(win.gURLBar.placeholder, noEngineString);
195   await Services.search.setDefault(
196     originalEngine,
197     Ci.nsISearchService.CHANGE_REASON_UNKNOWN
198   );
200   await TestUtils.waitForCondition(
201     () => win.gURLBar.placeholder == expectedString,
202     "The placeholder should include the engine name for built-in engines."
203   );
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]],
212   });
213   const originalPrivateEngine = await Services.search.getDefaultPrivate();
214   registerCleanupFunction(async () => {
215     await Services.search.setDefaultPrivate(
216       originalPrivateEngine,
217       Ci.nsISearchService.CHANGE_REASON_UNKNOWN
218     );
219   });
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(
226     originalEngine,
227     Ci.nsISearchService.CHANGE_REASON_UNKNOWN
228   );
229   await Services.search.setDefaultPrivate(
230     extraPrivateEngine,
231     Ci.nsISearchService.CHANGE_REASON_UNKNOWN
232   );
234   await TestUtils.waitForCondition(
235     () => win.gURLBar.placeholder == noEngineString,
236     "The placeholder should match the default placeholder for non-built-in engines."
237   );
238   Assert.equal(win.gURLBar.placeholder, noEngineString);
240   await Services.search.setDefault(
241     extraEngine,
242     Ci.nsISearchService.CHANGE_REASON_UNKNOWN
243   );
244   await Services.search.setDefaultPrivate(
245     originalEngine,
246     Ci.nsISearchService.CHANGE_REASON_UNKNOWN
247   );
249   await TestUtils.waitForCondition(
250     () => win.gURLBar.placeholder == expectedString,
251     "The placeholder should include the engine name for built-in engines."
252   );
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(
260     originalEngine,
261     Ci.nsISearchService.CHANGE_REASON_UNKNOWN
262   );
263   await Services.search.setDefaultPrivate(
264     extraPrivateEngine,
265     Ci.nsISearchService.CHANGE_REASON_UNKNOWN
266   );
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
278   // engine.
279   SearchUtils.GENERAL_SEARCH_ENGINE_IDS.add(
280     extraEngine.wrappedJSObject._extensionID
281   );
283   await doSearchModeTest(
284     {
285       source: UrlbarUtils.RESULT_SOURCE.SEARCH,
286       engineName: extraEngine.name,
287     },
288     {
289       id: "urlbar-placeholder-search-mode-web-2",
290       args: { name: extraEngine.name },
291     }
292   );
294   SearchUtils.GENERAL_SEARCH_ENGINE_IDS.delete(
295     extraEngine.wrappedJSObject._extensionID
296   );
299 add_task(async function test_search_mode_engine_other() {
300   await doSearchModeTest(
301     { engineName: extraEngine.name },
302     {
303       id: "urlbar-placeholder-search-mode-other-engine",
304       args: { name: extraEngine.name },
305     }
306   );
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 }
313   );
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 }
320   );
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 }
327   );
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(
335     extraEngine,
336     Ci.nsISearchService.CHANGE_REASON_UNKNOWN
337   );
338   await TestUtils.waitForCondition(
339     () => gURLBar.placeholder == noEngineString,
340     "The placeholder should match the default placeholder for non-built-in engines."
341   );
342   Assert.equal(gURLBar.placeholder, noEngineString);
344   info(`Set engine to ${originalEngine.name}`);
345   await Services.search.setDefault(
346     originalEngine,
347     Ci.nsISearchService.CHANGE_REASON_UNKNOWN
348   );
349   await TestUtils.waitForCondition(
350     () => gURLBar.placeholder == expectedString,
351     "The placeholder should include the engine name for built-in engines."
352   );
354   // Simulate the placeholder not having changed due to the delayed update
355   // on startup.
356   BrowserSearch._setURLBarPlaceholder("");
357   await TestUtils.waitForCondition(
358     () => gURLBar.placeholder == noEngineString,
359     "The placeholder should have been reset."
360   );
362   info("Show search engine removal info bar");
363   BrowserSearch.removalOfSearchEngineNotificationBox(
364     extraEngine.name,
365     originalEngine.name
366   );
367   const notificationBox = gNotificationBox.getNotificationWithValue(
368     "search-engine-removal"
369   );
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."
375   );
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.
390  */
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();
395   }
396   await UrlbarTestUtils.promisePopupOpen(window, () => {
397     EventUtils.synthesizeMouseAtCenter(gURLBar.inputField, {});
398   });
400   // Enter search mode.
401   await UrlbarTestUtils.enterSearchMode(window, expectedSearchMode);
403   // Check the placeholder.
404   Assert.deepEqual(
405     document.l10n.getAttributes(gURLBar.inputField),
406     expectedPlaceholderL10n,
407     "Placeholder has expected l10n"
408   );
410   await UrlbarTestUtils.exitSearchMode(window, { clickClose: true });
411   await UrlbarTestUtils.promisePopupClose(window);