Bug 1423247 - Couple Awesome Bar Composition (matchBuckets pref) to user's Address bar state (unified vs. 2 bar). r?mak
MozReview-Commit-ID: LZIWVcDvMrK
--- a/browser/components/nsBrowserGlue.js
+++ b/browser/components/nsBrowserGlue.js
@@ -27,16 +27,17 @@ XPCOMUtils.defineLazyModuleGetters(this,
AsyncShutdown: "resource://gre/modules/AsyncShutdown.jsm",
AutoCompletePopup: "resource://gre/modules/AutoCompletePopup.jsm",
BookmarkHTMLUtils: "resource://gre/modules/BookmarkHTMLUtils.jsm",
BookmarkJSONUtils: "resource://gre/modules/BookmarkJSONUtils.jsm",
BrowserUITelemetry: "resource:///modules/BrowserUITelemetry.jsm",
BrowserUsageTelemetry: "resource:///modules/BrowserUsageTelemetry.jsm",
ContentClick: "resource:///modules/ContentClick.jsm",
ContextualIdentityService: "resource://gre/modules/ContextualIdentityService.jsm",
+ CustomizableUI: "resource:///modules/CustomizableUI.jsm",
DateTimePickerHelper: "resource://gre/modules/DateTimePickerHelper.jsm",
DirectoryLinksProvider: "resource:///modules/DirectoryLinksProvider.jsm",
ExtensionsUI: "resource:///modules/ExtensionsUI.jsm",
Feeds: "resource:///modules/Feeds.jsm",
FileUtils: "resource://gre/modules/FileUtils.jsm",
FileSource: "resource://gre/modules/L10nRegistry.jsm",
FormValidationHandler: "resource:///modules/FormValidationHandler.jsm",
Integration: "resource://gre/modules/Integration.jsm",
@@ -1165,16 +1166,20 @@ BrowserGlue.prototype = {
Services.tm.idleDispatchToMainThread(() => {
JawsScreenReaderVersionCheck.onWindowsRestored();
});
}
Services.tm.idleDispatchToMainThread(() => {
LanguagePrompt.init();
});
+
+ Services.tm.idleDispatchToMainThread(() => {
+ this._urlbarMatchBuckets.init();
+ });
},
/**
* Use this function as an entry point to schedule tasks that need
* to run once per session, at any arbitrary point in time.
* This function will be called from an idle observer. Check the value of
* LATE_TASKS_IDLE_TIME_SEC to see the current value for this idle
* observer.
@@ -2337,16 +2342,59 @@ BrowserGlue.prototype = {
.add(promptCount);
} catch (ex) { /* Don't break the default prompt if telemetry is broken. */ }
if (willPrompt) {
DefaultBrowserCheck.prompt(RecentWindow.getMostRecentBrowserWindow());
}
},
+ // This keeps the browser.urlbar.matchBuckets pref updated depending on the
+ // placement of the searchbar.
+ _urlbarMatchBuckets: {
+ searchbarWidgetID: "search-container",
+ prefName: "browser.urlbar.matchBuckets",
+ searchbarPresentPrefValue: "general:5,suggestion:Infinity",
+
+ init() {
+ this._updatePref();
+ let checkWidget = widgetID => {
+ if (widgetID == this.searchbarWidgetID) {
+ this._updatePref();
+ }
+ };
+ CustomizableUI.addListener({
+ onWidgetAdded: checkWidget,
+ onWidgetRemoved: checkWidget,
+ });
+ },
+
+ _checkWidget(widgetID) {
+ if (widgetID == this.searchbarWidgetID) {
+ this._updatePref();
+ }
+ },
+
+ _updatePref() {
+ let pref = Services.prefs.getCharPref(this.prefName, "");
+ if (pref && pref != this.searchbarPresentPrefValue) {
+ // The user has customized the pref. Don't touch it.
+ return;
+ }
+ let placement =
+ CustomizableUI.getPlacementOfWidget(this.searchbarWidgetID);
+ if (placement) {
+ Services.prefs.setCharPref(this.prefName,
+ this.searchbarPresentPrefValue);
+ } else {
+ Services.prefs.clearUserPref(this.prefName);
+ }
+ },
+ },
+
// ------------------------------
// public nsIBrowserGlue members
// ------------------------------
sanitize: function BG_sanitize(aParentWindow) {
this._sanitizer.sanitize(aParentWindow);
},
--- a/browser/components/tests/browser/browser.ini
+++ b/browser/components/tests/browser/browser.ini
@@ -1,7 +1,8 @@
[DEFAULT]
[browser_bug538331.js]
skip-if = !updater
reason = test depends on update channel
[browser_contentpermissionprompt.js]
[browser_default_bookmark_toolbar_visibility.js]
+[browser_urlbar_matchBuckets.js]
new file mode 100644
--- /dev/null
+++ b/browser/components/tests/browser/browser_urlbar_matchBuckets.js
@@ -0,0 +1,80 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+// Makes sure the browser.urlbar.matchBuckets pref is correct and updated when
+// the searchbar is added to or removed from the UI.
+
+const SEARCHBAR_WIDGET_ID = "search-container";
+const PREF_NAME = "browser.urlbar.matchBuckets";
+const SEARCHBAR_PRESENT_PREF_VALUE = "general:5,suggestion:Infinity";
+
+// nsBrowserGlue initializes the relevant code via idleDispatchToMainThread(),
+// so to make sure this test runs after that happens, first run a dummy task
+// that queues an idle callback.
+add_task(async function waitForIdle() {
+ await TestUtils.waitForIdle();
+});
+
+add_task(async function test() {
+ // Initial checks.
+ Assert.equal(CustomizableUI.getPlacementOfWidget(SEARCHBAR_WIDGET_ID), null,
+ "Sanity check: searchbar should not be placed initially");
+ Assert.equal(Services.prefs.getCharPref(PREF_NAME, ""), "",
+ "Sanity check: pref should be cleared initially");
+
+ // Add the searchbar. The pref should be set.
+ let widgetPromise = promiseWidget("onWidgetAdded");
+ CustomizableUI.addWidgetToArea(SEARCHBAR_WIDGET_ID,
+ CustomizableUI.AREA_NAVBAR);
+ info("Waiting for searchbar to be added");
+ await widgetPromise;
+ Assert.equal(Services.prefs.getCharPref(PREF_NAME),
+ SEARCHBAR_PRESENT_PREF_VALUE,
+ "Pref should be set after adding searchbar");
+
+ // Remove the searchbar. The pref should be cleared.
+ widgetPromise = promiseWidget("onWidgetRemoved");
+ CustomizableUI.removeWidgetFromArea(SEARCHBAR_WIDGET_ID);
+ info("Waiting for searchbar to be removed");
+ await widgetPromise;
+ Assert.equal(Services.prefs.getCharPref(PREF_NAME, ""), "",
+ "Pref should be cleared after removing searchbar");
+
+ // Customize the pref.
+ let customizedPref = "extension:1," + SEARCHBAR_PRESENT_PREF_VALUE;
+ Services.prefs.setCharPref(PREF_NAME, customizedPref);
+
+ // Add the searchbar again. Since the pref is customized, it shouldn't be
+ // changed.
+ widgetPromise = promiseWidget("onWidgetAdded");
+ CustomizableUI.addWidgetToArea(SEARCHBAR_WIDGET_ID,
+ CustomizableUI.AREA_NAVBAR);
+ info("Waiting for searchbar to be added");
+ await widgetPromise;
+ Assert.equal(Services.prefs.getCharPref(PREF_NAME), customizedPref,
+ "Customized pref should remain same after adding searchbar");
+
+ // Remove the searchbar again. Since the pref is customized, it shouldn't be
+ // changed.
+ widgetPromise = promiseWidget("onWidgetRemoved");
+ CustomizableUI.removeWidgetFromArea(SEARCHBAR_WIDGET_ID);
+ info("Waiting for searchbar to be removed");
+ await widgetPromise;
+ Assert.equal(Services.prefs.getCharPref(PREF_NAME), customizedPref,
+ "Customized pref should remain same after removing searchbar");
+
+ Services.prefs.clearUserPref(PREF_NAME);
+});
+
+function promiseWidget(observerName) {
+ return new Promise(resolve => {
+ let listener = {};
+ listener[observerName] = widgetID => {
+ if (widgetID == SEARCHBAR_WIDGET_ID) {
+ CustomizableUI.removeListener(listener);
+ executeSoon(resolve);
+ }
+ };
+ CustomizableUI.addListener(listener);
+ });
+}
--- a/testing/modules/TestUtils.jsm
+++ b/testing/modules/TestUtils.jsm
@@ -32,16 +32,22 @@ this.TestUtils = {
executeSoon(callbackFn) {
Services.tm.dispatchToMainThread(callbackFn);
},
waitForTick() {
return new Promise(resolve => this.executeSoon(resolve));
},
+ waitForIdle(timeout = undefined) {
+ return new Promise(resolve => {
+ Services.tm.idleDispatchToMainThread(resolve);
+ });
+ },
+
/**
* Waits for the specified topic to be observed.
*
* @param {string} topic
* The topic to observe.
* @param {function} checkFn [optional]
* Called with (subject, data) as arguments, should return true if the
* notification is the expected one, or false if it should be ignored
--- a/toolkit/components/places/UnifiedComplete.js
+++ b/toolkit/components/places/UnifiedComplete.js
@@ -45,17 +45,17 @@ const PREF_URLBAR_DEFAULTS = new Map([
["suggest.bookmark", true],
["suggest.openpage", true],
["suggest.history.onlyTyped", false],
["suggest.searches", false],
["maxCharsForSearchSuggestions", 20],
["maxHistoricalSearchSuggestions", 0],
["usepreloadedtopurls.enabled", true],
["usepreloadedtopurls.expire_days", 14],
- ["matchBuckets", "general:5,suggestion:Infinity"],
+ ["matchBuckets", "suggestion:4,general:Infinity"],
["matchBucketsSearch", ""],
["insertMethod", INSERTMETHOD.MERGE_RELATED],
]);
const PREF_OTHER_DEFAULTS = new Map([
["keyword.enabled", true],
]);
// AutoComplete query type constants.