Bug 1449317 - Update the default string in the address bar. r=florian

MozReview-Commit-ID: C00zxCTJmHY

--HG--
extra : rebase_source : 459e7e14370a0c884bb394bde4ec5bf3edf1aadc
This commit is contained in:
Mark Banner 2018-04-09 15:32:19 +01:00
Родитель bf5ac484c7
Коммит 1f4fe4c185
5 изменённых файлов: 203 добавлений и 1 удалений

Просмотреть файл

@ -1267,6 +1267,8 @@ var gBrowserInit = {
remoteType, sameProcessAsFrameLoader remoteType, sameProcessAsFrameLoader
}); });
BrowserSearch.initPlaceHolder();
// Hack to ensure that the about:home favicon is loaded // Hack to ensure that the about:home favicon is loaded
// instantaneously, to avoid flickering and improve perceived performance. // instantaneously, to avoid flickering and improve perceived performance.
this._callWithURIToLoad(uriToLoad => { this._callWithURIToLoad(uriToLoad => {
@ -1462,6 +1464,7 @@ var gBrowserInit = {
UpdateUrlbarSearchSplitterState(); UpdateUrlbarSearchSplitterState();
BookmarkingUI.init(); BookmarkingUI.init();
BrowserSearch.delayedStartupInit();
AutoShowBookmarksToolbar.init(); AutoShowBookmarksToolbar.init();
Services.prefs.addObserver(gHomeButton.prefDomain, gHomeButton); Services.prefs.addObserver(gHomeButton.prefDomain, gHomeButton);
@ -3767,10 +3770,25 @@ const DOMEventHandler = {
}; };
const BrowserSearch = { const BrowserSearch = {
_searchInitComplete: false,
init() { init() {
Services.obs.addObserver(this, "browser-search-engine-modified"); Services.obs.addObserver(this, "browser-search-engine-modified");
}, },
delayedStartupInit() {
// Asynchronously initialize the search service if necessary, to get the
// current engine for working out the placeholder.
Services.search.init(rv => {
if (Components.isSuccessCode(rv)) {
// Delay the update for this until so that we don't change it while
// the user is looking at it / isn't expecting it.
this._updateURLBarPlaceholder(Services.search.currentEngine, true);
this._searchInitComplete = true;
}
});
},
uninit() { uninit() {
Services.obs.removeObserver(this, "browser-search-engine-modified"); Services.obs.removeObserver(this, "browser-search-engine-modified");
}, },
@ -3797,6 +3815,11 @@ const BrowserSearch = {
// browser's offered engines. // browser's offered engines.
this._removeMaybeOfferedEngine(engineName); this._removeMaybeOfferedEngine(engineName);
break; break;
case "engine-current":
if (this._searchInitComplete) {
this._updateURLBarPlaceholder(engine);
}
break;
} }
}, },
@ -3844,6 +3867,88 @@ const BrowserSearch = {
} }
}, },
/**
* Initializes the urlbar placeholder to the pre-saved engine name. We do this
* via a preference, to avoid needing to synchronously init the search service.
*
* This should be called around the time of DOMContentLoaded, so that it is
* initialized quickly before the user sees anything.
*
* Note: If the preference doesn't exist, we don't do anything as the default
* placeholder is a string which doesn't have the engine name.
*/
initPlaceHolder() {
let engineName = Services.prefs.getStringPref("browser.urlbar.placeholderName", "");
if (engineName) {
// We can do this directly, since we know we're at DOMContentLoaded.
this._setURLBarPlaceholder(engineName);
}
},
/**
* Updates the URLBar placeholder for the specified engine, delaying the
* update if required. This also saves the current engine name in preferences
* for the next restart.
*
* Note: The engine name will only be displayed for built-in engines, as we
* know they should have short names.
*
* @param {nsISearchEngine} engine The search engine to use for the update.
* @param {Boolean} delayUpdate Set to true, to delay update until the
* placeholder is not displayed.
*/
_updateURLBarPlaceholder(engine, delayUpdate = false) {
if (!engine) {
throw new Error("Expected an engine to be specified");
}
let engineName = "";
if (Services.search.getDefaultEngines().includes(engine)) {
engineName = engine.name;
Services.prefs.setStringPref("browser.urlbar.placeholderName", engineName);
} else {
Services.prefs.clearUserPref("browser.urlbar.placeholderName");
}
// Only delay if requested, and we're not displaying text in the URL bar
// currently.
if (delayUpdate && !gURLBar.value) {
// Delays changing the URL Bar placeholder until the user is not going to be
// seeing it, e.g. when there is a value entered in the bar, or if there is
// a tab switch to a tab which has a url loaded.
let placeholderUpdateListener = () => {
if (gURLBar.value) {
this._setURLBarPlaceholder(engineName);
gURLBar.removeEventListener("input", placeholderUpdateListener);
gBrowser.tabContainer.removeEventListener("TabSelect", placeholderUpdateListener);
}
};
gURLBar.addEventListener("input", placeholderUpdateListener);
gBrowser.tabContainer.addEventListener("TabSelect", placeholderUpdateListener);
} else {
this._setURLBarPlaceholder(engineName);
}
},
/**
* Sets the URLBar placeholder to either something based on the engine name,
* or the default placeholder.
*
* @param {String} name The name of the engine to use, an empty string if to
* use the default placeholder.
*/
_setURLBarPlaceholder(name) {
let placeholder;
if (name) {
placeholder = gBrowserBundle.formatStringFromName("urlbar.placeholder",
[name], 1);
} else {
placeholder = gURLBar.getAttribute("defaultPlaceholder");
}
gURLBar.setAttribute("placeholder", placeholder);
},
addEngine(browser, engine, uri) { addEngine(browser, engine, uri) {
// Check to see whether we've already added an engine with this title // Check to see whether we've already added an engine with this title
if (browser.engines) { if (browser.engines) {

Просмотреть файл

@ -777,6 +777,7 @@
class="chromeclass-location" overflows="false"> class="chromeclass-location" overflows="false">
<textbox id="urlbar" flex="1" <textbox id="urlbar" flex="1"
placeholder="&urlbar.placeholder2;" placeholder="&urlbar.placeholder2;"
defaultPlaceholder="&urlbar.placeholder2;"
focused="true" focused="true"
type="autocomplete" type="autocomplete"
autocompletesearch="unifiedcomplete" autocompletesearch="unifiedcomplete"

Просмотреть файл

@ -92,6 +92,10 @@ support-files =
searchSuggestionEngine.xml searchSuggestionEngine.xml
searchSuggestionEngine.sjs searchSuggestionEngine.sjs
[browser_urlbarOneOffs_searchSuggestions.js] [browser_urlbarOneOffs_searchSuggestions.js]
support-files =
searchSuggestionEngine.xml
searchSuggestionEngine.sjs
[browser_urlbarPlaceholder.js]
support-files = support-files =
searchSuggestionEngine.xml searchSuggestionEngine.xml
searchSuggestionEngine.sjs searchSuggestionEngine.sjs

Просмотреть файл

@ -0,0 +1,93 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
/**
* This test ensures the placeholder is set correctly for different search
* engines.
*/
"use strict";
const TEST_ENGINE_BASENAME = "searchSuggestionEngine.xml";
const originalEngine = Services.search.currentEngine;
const expectedString = gBrowserBundle.formatStringFromName("urlbar.placeholder",
[originalEngine.name], 1);
var extraEngine;
var tabs = [];
add_task(async function setup() {
extraEngine = await promiseNewSearchEngine(TEST_ENGINE_BASENAME);
// Force display of a tab with a URL bar, to clear out any possible placeholder
// initialization listeners that happen on startup.
let urlTab = await BrowserTestUtils.openNewForegroundTab(gBrowser, "about:mozilla");
BrowserTestUtils.removeTab(urlTab);
registerCleanupFunction(() => {
Services.search.currentEngine = originalEngine;
for (let tab of tabs) {
BrowserTestUtils.removeTab(tab);
}
});
});
add_task(async function test_change_default_engine_updates_placeholder() {
tabs.push(await BrowserTestUtils.openNewForegroundTab(gBrowser));
Services.search.currentEngine = extraEngine;
await TestUtils.waitForCondition(
() => gURLBar.getAttribute("placeholder") == gURLBar.getAttribute("defaultPlaceholder"),
"The placeholder should match the default placeholder for non-built-in engines.");
Services.search.currentEngine = originalEngine;
await TestUtils.waitForCondition(
() => gURLBar.getAttribute("placeholder") == expectedString,
"The placeholder should include the engine name for built-in engines.");
});
add_task(async function test_delayed_update_placeholder() {
// Since we can't easily test for startup changes, we'll at least test the delay
// of update for the placeholder works.
let urlTab = await BrowserTestUtils.openNewForegroundTab(gBrowser, "about:mozilla");
tabs.push(urlTab);
// Open a tab with a blank URL bar.
let blankTab = await BrowserTestUtils.openNewForegroundTab(gBrowser);
tabs.push(blankTab);
// Pretend we've "initialized".
BrowserSearch._updateURLBarPlaceholder(extraEngine, true);
Assert.equal(gURLBar.getAttribute("placeholder"), expectedString,
"Placeholder should be unchanged.");
// Now switch to a tab with something in the URL Bar.
await BrowserTestUtils.switchTab(gBrowser, urlTab);
await TestUtils.waitForCondition(
() => gURLBar.getAttribute("placeholder") == gURLBar.getAttribute("defaultPlaceholder"),
"The placeholder should have updated in the background.");
// Do it the other way to check both named engine and fallback code paths.
await BrowserTestUtils.switchTab(gBrowser, blankTab);
BrowserSearch._updateURLBarPlaceholder(originalEngine, true);
Assert.equal(gURLBar.getAttribute("placeholder"), gURLBar.getAttribute("defaultPlaceholder"),
"Placeholder should be unchanged.");
await BrowserTestUtils.switchTab(gBrowser, urlTab);
await TestUtils.waitForCondition(
() => gURLBar.getAttribute("placeholder") == expectedString,
"The placeholder should include the engine name for built-in engines.");
// Now check when we have a URL displayed, the placeholder is updated straight away.
BrowserSearch._updateURLBarPlaceholder(extraEngine);
Assert.equal(gURLBar.getAttribute("placeholder"), gURLBar.getAttribute("defaultPlaceholder"),
"Placeholder should be the default.");
});

Просмотреть файл

@ -434,7 +434,6 @@ These should match what Safari and other Apple applications use on OS X Lion. --
<!ENTITY openCmd.commandkey "l"> <!ENTITY openCmd.commandkey "l">
<!ENTITY urlbar.placeholder2 "Search or enter address"> <!ENTITY urlbar.placeholder2 "Search or enter address">
<!ENTITY urlbar.placeholder3 "Enter search terms and addresses here">
<!ENTITY urlbar.accesskey "d"> <!ENTITY urlbar.accesskey "d">
<!-- LOCALIZATION NOTE (urlbar.extension.label): Used to indicate that a selected autocomplete entry is provided by an extension. --> <!-- LOCALIZATION NOTE (urlbar.extension.label): Used to indicate that a selected autocomplete entry is provided by an extension. -->
<!ENTITY urlbar.extension.label "Extension:"> <!ENTITY urlbar.extension.label "Extension:">