tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

commit 894075a8526492de8a6035d100b70eceb2e49ad4
parent da3ca9041cbcbdf88915e1b5a1cf2b0fc390199c
Author: Marco Bonardo <mbonardo@mozilla.com>
Date:   Mon, 22 Dec 2025 15:55:37 +0000

Bug 2003061 - Make Results Groups generation a bit more flexible. r=adw,urlbar-reviewers

Make urlbar groups building more flexible, so that it can be defined
per SAP.
The "smartbar" path is mostly a placeholder at this stage, it will be
used soon.
To simplify some of the handling, this removes the old migration code
that ran from Firefox 87 as it's not worth updating it; there's a visible
preference in Settings to customize the behavior, in the worst case.

Differential Revision: https://phabricator.services.mozilla.com/D276464

Diffstat:
Mbrowser/components/ProfileDataUpgrader.sys.mjs | 6------
Mbrowser/components/urlbar/UrlbarMuxerStandard.sys.mjs | 14++------------
Mbrowser/components/urlbar/UrlbarPrefs.sys.mjs | 116+++++++++++++++++++++++++++++++++++++++----------------------------------------
Mbrowser/components/urlbar/UrlbarProviderPlaces.sys.mjs | 7++++++-
Mbrowser/components/urlbar/tests/unit/test_UrlbarPrefs.js | 394++++++++++++++++++++++++++++++++-----------------------------------------------
Mbrowser/components/urlbar/tests/unit/test_muxer.js | 6+++---
Mbrowser/components/urlbar/tests/unit/test_resultGroups.js | 2+-
Mbrowser/components/urlbar/tests/unit/test_search_suggestions.js | 44+++++++++++++++++++++-----------------------
Mbrowser/components/urlbar/tests/unit/test_suggestedIndexRelativeToGroup.js | 2+-
9 files changed, 248 insertions(+), 343 deletions(-)

diff --git a/browser/components/ProfileDataUpgrader.sys.mjs b/browser/components/ProfileDataUpgrader.sys.mjs @@ -13,7 +13,6 @@ ChromeUtils.defineESModuleGetters(lazy, { "resource:///modules/FirefoxBridgeExtensionUtils.sys.mjs", LoginHelper: "resource://gre/modules/LoginHelper.sys.mjs", PlacesUIUtils: "moz-src:///browser/components/places/PlacesUIUtils.sys.mjs", - UrlbarPrefs: "moz-src:///browser/components/urlbar/UrlbarPrefs.sys.mjs", UsageReporting: "resource://gre/modules/UsageReporting.sys.mjs", }); @@ -318,11 +317,6 @@ export let ProfileDataUpgrader = { Services.prefs.clearUserPref(oldPrefName); } - // Initialize the new browser.urlbar.showSuggestionsBeforeGeneral pref. - if (existingDataVersion < 106) { - lazy.UrlbarPrefs.initializeShowSearchSuggestionsFirstPref(); - } - if (existingDataVersion < 107) { // Migrate old http URIs for mailto handlers to their https equivalents. // The handler service will do this. We need to wait with migrating diff --git a/browser/components/urlbar/UrlbarMuxerStandard.sys.mjs b/browser/components/urlbar/UrlbarMuxerStandard.sys.mjs @@ -117,18 +117,8 @@ class MuxerUnifiedComplete extends UrlbarMuxer { // When you add state, update _copyState() as necessary. }; - // Show Top Sites above trending results. - let showSearchSuggestionsFirst = - context.searchString || - (!lazy.UrlbarPrefs.get("suggest.trending") && - !lazy.UrlbarPrefs.get("suggest.recentsearches")); - - // Determine the result groups to use for this sort. In search mode with - // an engine, show search suggestions first. - let rootGroup = - context.searchMode?.engineName || !showSearchSuggestionsFirst - ? lazy.UrlbarPrefs.makeResultGroups({ showSearchSuggestionsFirst }) - : lazy.UrlbarPrefs.resultGroups; + // Determine the result groups to use for this sort. + let rootGroup = lazy.UrlbarPrefs.getResultGroups({ context }); lazy.logger.debug("Root groups", rootGroup); // We must do a first pass over the result to reorder some groups. diff --git a/browser/components/urlbar/UrlbarPrefs.sys.mjs b/browser/components/urlbar/UrlbarPrefs.sys.mjs @@ -716,7 +716,7 @@ const PREF_TYPES = new Map([ ]); /** - * Builds the standard result groups and returns the root group. Result + * Builds the default result groups and returns the root group. Result * groups determine the composition of results in the muxer, i.e., how they're * grouped and sorted. Each group is an object that looks like this: * @@ -753,7 +753,7 @@ const PREF_TYPES = new Map([ * @param {boolean} options.showSearchSuggestionsFirst * If true, the suggestions group will come before the general group. */ -function makeResultGroups({ showSearchSuggestionsFirst }) { +function makeDefaultResultGroups({ showSearchSuggestionsFirst }) { /** * @type {ResultGroup} */ @@ -982,16 +982,6 @@ class Preferences { } /** - * Builds the standard result groups. See makeResultGroups. - * - * @param {object} options - * See makeResultGroups. - */ - makeResultGroups(options) { - return makeResultGroups(options); - } - - /** * Gets a pref but allows the `scotchBonnet.enableOverride` pref to * short circuit them so one pref can be used to enable multiple * features. @@ -1004,13 +994,47 @@ class Preferences { return this.get("scotchBonnet.enableOverride") || this.get(pref); } - get resultGroups() { - if (!this.#resultGroups) { - this.#resultGroups = makeResultGroups({ - showSearchSuggestionsFirst: this.get("showSearchSuggestionsFirst"), - }); + getResultGroups({ context }) { + // We try to cache result groups so we don't have to rebuild them each time. + // This key may be modified per each SAP, and will track the cached groups, + // any additionally used condition must be added to the key. + let key = context.sapName; + switch (context.sapName) { + case "urlbar": { + let showSearchSuggestionsFirst = + context.searchString || + (!this.get("suggest.trending") && + !this.get("suggest.recentsearches")); + + let inSearchEngineMode = !!context.searchMode?.engineName; + + // If we're in a case were search suggestions would be shown first, but not + // in search engine mode, then just use the user preference. + if (!inSearchEngineMode && showSearchSuggestionsFirst) { + showSearchSuggestionsFirst = this.get("showSearchSuggestionsFirst"); + } + + key += showSearchSuggestionsFirst; + return this.#getOrCacheResultGroups(key, () => + makeDefaultResultGroups({ showSearchSuggestionsFirst }) + ); + } + case "searchbar": { + // This is a temporary placeholder until searchbar gets its own config. + return this.#getOrCacheResultGroups(key, () => + makeDefaultResultGroups({ showSearchSuggestionsFirst: true }) + ); + } + case "smartbar": { + // This is a temporary placeholder until smartbar gets its own config. + return this.#getOrCacheResultGroups(key, () => + makeDefaultResultGroups({ showSearchSuggestionsFirst: false }) + ); + } + default: { + throw new Error(`Unknown SAP name: ${context.sapName}`); + } } - return this.#resultGroups; } /** @@ -1079,7 +1103,7 @@ class Preferences { this._map.delete("autoFillAdaptiveHistoryUseCountThreshold"); return; case "showSearchSuggestionsFirst": - this.#resultGroups = null; + this.#cachedResultGroups.clear(); return; } @@ -1288,44 +1312,6 @@ class Preferences { } /** - * Initializes the showSearchSuggestionsFirst pref based on the matchGroups - * pref. This function can be removed when the corresponding UI migration in - * BrowserGlue.sys.mjs is no longer needed. - */ - initializeShowSearchSuggestionsFirstPref() { - let matchGroups = []; - let pref = Services.prefs.getCharPref("browser.urlbar.matchGroups", ""); - try { - matchGroups = pref.split(",").map(v => { - let group = v.split(":"); - return [group[0].trim().toLowerCase(), Number(group[1])]; - }); - } catch (ex) {} - let groupNames = matchGroups.map(group => group[0]); - let suggestionIndex = groupNames.indexOf("suggestion"); - let generalIndex = groupNames.indexOf("general"); - let showSearchSuggestionsFirst = - generalIndex < 0 || - (suggestionIndex >= 0 && suggestionIndex < generalIndex); - let oldValue = Services.prefs.getBoolPref( - "browser.urlbar.showSearchSuggestionsFirst" - ); - Services.prefs.setBoolPref( - "browser.urlbar.showSearchSuggestionsFirst", - showSearchSuggestionsFirst - ); - - // Pref observers aren't called when a pref is set to its current value, but - // we always want to set matchGroups to the appropriate default value via - // onPrefChanged, so call it now if necessary. This is really only - // necessary for tests since the only time this function is called outside - // of tests is by a UI migration in BrowserGlue. - if (oldValue == showSearchSuggestionsFirst) { - this.onPrefChanged("showSearchSuggestionsFirst"); - } - } - - /** * Return whether or not persisted search terms is enabled. * * @returns {boolean} true: if enabled. @@ -1338,6 +1324,20 @@ class Preferences { ); } + /** + * @type {Map<string, ResultGroup>} + * Result groups cached by search access point and params used to build them. + */ + #cachedResultGroups = new Map(); + #getOrCacheResultGroups(key, builder) { + let groups = this.#cachedResultGroups.get(key); + if (!groups) { + groups = builder(); + this.#cachedResultGroups.set(key, groups); + } + return groups; + } + #notifyObservers(method, changed, ...rest) { for (let i = 0; i < this._observerWeakRefs.length; ) { let observer = this._observerWeakRefs[i].get(); @@ -1356,8 +1356,6 @@ class Preferences { ++i; } } - - #resultGroups = null; } export var UrlbarPrefs = new Preferences(); diff --git a/browser/components/urlbar/UrlbarProviderPlaces.sys.mjs b/browser/components/urlbar/UrlbarProviderPlaces.sys.mjs @@ -573,6 +573,7 @@ class Search { this.#listener = listener; this.#provider = provider; + this.#queryContext = queryContext; } /** @@ -797,6 +798,7 @@ class Search { #searchModeEngine; #searchTokens; #userContextId; + #queryContext; /** * Used to avoid adding duplicate entries to the results. @@ -1084,7 +1086,10 @@ class Search { let index = 0; if (!this.#groups) { this.#groups = []; - this.#makeGroups(lazy.UrlbarPrefs.resultGroups, this.#maxResults); + this.#makeGroups( + lazy.UrlbarPrefs.getResultGroups({ context: this.#queryContext }), + this.#maxResults + ); } let replace = false; diff --git a/browser/components/urlbar/tests/unit/test_UrlbarPrefs.js b/browser/components/urlbar/tests/unit/test_UrlbarPrefs.js @@ -39,212 +39,197 @@ add_task(function test() { ); }); -// Tests UrlbarPrefs.makeResultGroups({ showSearchSuggestionsFirst: true }). -add_task(function makeResultGroups_true() { - Assert.deepEqual( - UrlbarPrefs.makeResultGroups({ showSearchSuggestionsFirst: true }), +const EXPECTED_SUGGESTIONS_FIRST_GROUPS = { + children: [ + // heuristic { + maxResultCount: 1, children: [ - // heuristic - { - maxResultCount: 1, - children: [ - { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_TEST }, - { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_EXTENSION }, - { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_SEARCH_TIP }, - { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_OMNIBOX }, - { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_ENGINE_ALIAS }, - { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_BOOKMARK_KEYWORD }, - { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_AUTOFILL }, - { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_TOKEN_ALIAS_ENGINE }, - { - group: - UrlbarUtils.RESULT_GROUP.HEURISTIC_RESTRICT_KEYWORD_AUTOFILL, - }, - { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_HISTORY_URL }, - { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_FALLBACK }, - ], - }, - // extensions using the omnibox API + { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_TEST }, + { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_EXTENSION }, + { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_SEARCH_TIP }, + { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_OMNIBOX }, + { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_ENGINE_ALIAS }, + { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_BOOKMARK_KEYWORD }, + { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_AUTOFILL }, + { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_TOKEN_ALIAS_ENGINE }, { - group: UrlbarUtils.RESULT_GROUP.OMNIBOX, + group: UrlbarUtils.RESULT_GROUP.HEURISTIC_RESTRICT_KEYWORD_AUTOFILL, }, - // main group + { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_HISTORY_URL }, + { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_FALLBACK }, + ], + }, + // extensions using the omnibox API + { + group: UrlbarUtils.RESULT_GROUP.OMNIBOX, + }, + // main group + { + flexChildren: true, + children: [ + // suggestions { - flexChildren: true, + flex: 2, children: [ - // suggestions { - flex: 2, + flexChildren: true, children: [ { - flexChildren: true, - children: [ - { - flex: 2, - group: UrlbarUtils.RESULT_GROUP.FORM_HISTORY, - }, - { - flex: 99, - group: UrlbarUtils.RESULT_GROUP.RECENT_SEARCH, - }, - { - flex: 4, - group: UrlbarUtils.RESULT_GROUP.REMOTE_SUGGESTION, - }, - ], + flex: 2, + group: UrlbarUtils.RESULT_GROUP.FORM_HISTORY, + }, + { + flex: 99, + group: UrlbarUtils.RESULT_GROUP.RECENT_SEARCH, }, { - group: UrlbarUtils.RESULT_GROUP.TAIL_SUGGESTION, + flex: 4, + group: UrlbarUtils.RESULT_GROUP.REMOTE_SUGGESTION, }, ], }, - // general { - group: UrlbarUtils.RESULT_GROUP.GENERAL_PARENT, - flex: 1, + group: UrlbarUtils.RESULT_GROUP.TAIL_SUGGESTION, + }, + ], + }, + // general + { + group: UrlbarUtils.RESULT_GROUP.GENERAL_PARENT, + flex: 1, + children: [ + { + availableSpan: 3, + group: UrlbarUtils.RESULT_GROUP.INPUT_HISTORY, + }, + { + flexChildren: true, children: [ { - availableSpan: 3, - group: UrlbarUtils.RESULT_GROUP.INPUT_HISTORY, + flex: 1, + group: UrlbarUtils.RESULT_GROUP.REMOTE_TAB, }, { - flexChildren: true, - children: [ - { - flex: 1, - group: UrlbarUtils.RESULT_GROUP.REMOTE_TAB, - }, - { - flex: 2, - group: UrlbarUtils.RESULT_GROUP.GENERAL, - orderBy: "frecency", - }, - { - flex: 2, - group: UrlbarUtils.RESULT_GROUP.ABOUT_PAGES, - }, - { - flex: 99, - group: UrlbarUtils.RESULT_GROUP.RESTRICT_SEARCH_KEYWORD, - }, - ], + flex: 2, + group: UrlbarUtils.RESULT_GROUP.GENERAL, + orderBy: "frecency", }, { - group: UrlbarUtils.RESULT_GROUP.INPUT_HISTORY, + flex: 2, + group: UrlbarUtils.RESULT_GROUP.ABOUT_PAGES, + }, + { + flex: 99, + group: UrlbarUtils.RESULT_GROUP.RESTRICT_SEARCH_KEYWORD, }, ], }, + { + group: UrlbarUtils.RESULT_GROUP.INPUT_HISTORY, + }, ], }, ], - } - ); -}); - -// Tests UrlbarPrefs.makeResultGroups({ showSearchSuggestionsFirst: false }). -add_task(function makeResultGroups_false() { - Assert.deepEqual( - UrlbarPrefs.makeResultGroups({ showSearchSuggestionsFirst: false }), + }, + ], +}; +const EXPECTED_NOT_SUGGESTIONS_FIRST_GROUPS = { + children: [ + // heuristic { + maxResultCount: 1, children: [ - // heuristic - { - maxResultCount: 1, - children: [ - { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_TEST }, - { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_EXTENSION }, - { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_SEARCH_TIP }, - { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_OMNIBOX }, - { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_ENGINE_ALIAS }, - { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_BOOKMARK_KEYWORD }, - { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_AUTOFILL }, - { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_TOKEN_ALIAS_ENGINE }, - { - group: - UrlbarUtils.RESULT_GROUP.HEURISTIC_RESTRICT_KEYWORD_AUTOFILL, - }, - { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_HISTORY_URL }, - { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_FALLBACK }, - ], - }, - // extensions using the omnibox API + { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_TEST }, + { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_EXTENSION }, + { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_SEARCH_TIP }, + { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_OMNIBOX }, + { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_ENGINE_ALIAS }, + { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_BOOKMARK_KEYWORD }, + { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_AUTOFILL }, + { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_TOKEN_ALIAS_ENGINE }, { - group: UrlbarUtils.RESULT_GROUP.OMNIBOX, + group: UrlbarUtils.RESULT_GROUP.HEURISTIC_RESTRICT_KEYWORD_AUTOFILL, }, - // main group + { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_HISTORY_URL }, + { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_FALLBACK }, + ], + }, + // extensions using the omnibox API + { + group: UrlbarUtils.RESULT_GROUP.OMNIBOX, + }, + // main group + { + flexChildren: true, + children: [ + // general { - flexChildren: true, + group: UrlbarUtils.RESULT_GROUP.GENERAL_PARENT, + flex: 2, children: [ - // general { - group: UrlbarUtils.RESULT_GROUP.GENERAL_PARENT, - flex: 2, + availableSpan: 3, + group: UrlbarUtils.RESULT_GROUP.INPUT_HISTORY, + }, + { + flexChildren: true, children: [ { - availableSpan: 3, - group: UrlbarUtils.RESULT_GROUP.INPUT_HISTORY, + flex: 1, + group: UrlbarUtils.RESULT_GROUP.REMOTE_TAB, }, { - flexChildren: true, - children: [ - { - flex: 1, - group: UrlbarUtils.RESULT_GROUP.REMOTE_TAB, - }, - { - flex: 2, - group: UrlbarUtils.RESULT_GROUP.GENERAL, - orderBy: "frecency", - }, - { - flex: 2, - group: UrlbarUtils.RESULT_GROUP.ABOUT_PAGES, - }, - { - flex: 99, - group: UrlbarUtils.RESULT_GROUP.RESTRICT_SEARCH_KEYWORD, - }, - ], + flex: 2, + group: UrlbarUtils.RESULT_GROUP.GENERAL, + orderBy: "frecency", }, { - group: UrlbarUtils.RESULT_GROUP.INPUT_HISTORY, + flex: 2, + group: UrlbarUtils.RESULT_GROUP.ABOUT_PAGES, + }, + { + flex: 99, + group: UrlbarUtils.RESULT_GROUP.RESTRICT_SEARCH_KEYWORD, }, ], }, - // suggestions { - flex: 1, + group: UrlbarUtils.RESULT_GROUP.INPUT_HISTORY, + }, + ], + }, + // suggestions + { + flex: 1, + children: [ + { + flexChildren: true, children: [ { - flexChildren: true, - children: [ - { - flex: 2, - group: UrlbarUtils.RESULT_GROUP.FORM_HISTORY, - }, - { - flex: 99, - group: UrlbarUtils.RESULT_GROUP.RECENT_SEARCH, - }, - { - flex: 4, - group: UrlbarUtils.RESULT_GROUP.REMOTE_SUGGESTION, - }, - ], + flex: 2, + group: UrlbarUtils.RESULT_GROUP.FORM_HISTORY, + }, + { + flex: 99, + group: UrlbarUtils.RESULT_GROUP.RECENT_SEARCH, }, { - group: UrlbarUtils.RESULT_GROUP.TAIL_SUGGESTION, + flex: 4, + group: UrlbarUtils.RESULT_GROUP.REMOTE_SUGGESTION, }, ], }, + { + group: UrlbarUtils.RESULT_GROUP.TAIL_SUGGESTION, + }, ], }, ], - } - ); -}); + }, + ], +}; // Tests interaction between showSearchSuggestionsFirst and resultGroups. add_task(function showSearchSuggestionsFirst_resultGroups() { @@ -255,40 +240,48 @@ add_task(function showSearchSuggestionsFirst_resultGroups() { "showSearchSuggestionsFirst is true initially" ); Assert.deepEqual( - UrlbarPrefs.resultGroups, - UrlbarPrefs.makeResultGroups({ showSearchSuggestionsFirst: true }), - "resultGroups is the same as the groups for which howSearchSuggestionsFirst is true" + UrlbarPrefs.getResultGroups({ + context: { sapName: "urlbar", searchString: "test" }, + }), + EXPECTED_SUGGESTIONS_FIRST_GROUPS, + "resultGroups is the same as the groups for which showSearchSuggestionsFirst is true" ); - // Set showSearchSuggestionsFirst = false. UrlbarPrefs.set("showSearchSuggestionsFirst", false); Assert.deepEqual( - UrlbarPrefs.resultGroups, - UrlbarPrefs.makeResultGroups({ showSearchSuggestionsFirst: false }), + UrlbarPrefs.getResultGroups({ + context: { sapName: "urlbar", searchString: "test" }, + }), + EXPECTED_NOT_SUGGESTIONS_FIRST_GROUPS, "resultGroups is updated after setting showSearchSuggestionsFirst = false" ); - // Set showSearchSuggestionsFirst = true. UrlbarPrefs.set("showSearchSuggestionsFirst", true); Assert.deepEqual( - UrlbarPrefs.resultGroups, - UrlbarPrefs.makeResultGroups({ showSearchSuggestionsFirst: true }), + UrlbarPrefs.getResultGroups({ + context: { sapName: "urlbar", searchString: "test" }, + }), + EXPECTED_SUGGESTIONS_FIRST_GROUPS, "resultGroups is updated after setting showSearchSuggestionsFirst = true" ); // Set showSearchSuggestionsFirst = false again so we can clear it next. UrlbarPrefs.set("showSearchSuggestionsFirst", false); Assert.deepEqual( - UrlbarPrefs.resultGroups, - UrlbarPrefs.makeResultGroups({ showSearchSuggestionsFirst: false }), + UrlbarPrefs.getResultGroups({ + context: { sapName: "urlbar", searchString: "test" }, + }), + EXPECTED_NOT_SUGGESTIONS_FIRST_GROUPS, "resultGroups is updated after setting showSearchSuggestionsFirst = false" ); // Clear showSearchSuggestionsFirst. Services.prefs.clearUserPref("browser.urlbar.showSearchSuggestionsFirst"); Assert.deepEqual( - UrlbarPrefs.resultGroups, - UrlbarPrefs.makeResultGroups({ showSearchSuggestionsFirst: true }), + UrlbarPrefs.getResultGroups({ + context: { sapName: "urlbar", searchString: "test" }, + }), + EXPECTED_SUGGESTIONS_FIRST_GROUPS, "resultGroups is updated immediately after clearing showSearchSuggestionsFirst" ); Assert.equal( @@ -296,79 +289,6 @@ add_task(function showSearchSuggestionsFirst_resultGroups() { true, "showSearchSuggestionsFirst defaults to true after clearing it" ); - Assert.deepEqual( - UrlbarPrefs.resultGroups, - UrlbarPrefs.makeResultGroups({ showSearchSuggestionsFirst: true }), - "resultGroups remains correct after getting showSearchSuggestionsFirst" - ); -}); - -// Tests UrlbarPrefs.initializeShowSearchSuggestionsFirstPref() and the -// interaction between matchGroups, showSearchSuggestionsFirst, and -// resultGroups. It's a little complex, but the flow is: -// -// 1. The old matchGroups pref has some value -// 2. UrlbarPrefs.initializeShowSearchSuggestionsFirstPref() is called to -// translate matchGroups into the newer showSearchSuggestionsFirst pref -// 3. The update to showSearchSuggestionsFirst causes the new resultGroups -// pref to be set -add_task(function initializeShowSearchSuggestionsFirstPref() { - // Each value in `tests`: [matchGroups, expectedShowSearchSuggestionsFirst] - let tests = [ - ["suggestion:4,general:Infinity", true], - ["suggestion:4,general:5", true], - ["suggestion:1,general:5,suggestion:Infinity", true], - ["suggestion:Infinity", true], - ["suggestion:4", true], - - ["foo:1,suggestion:4,general:Infinity", true], - ["foo:2,suggestion:4,general:5", true], - ["foo:3,suggestion:1,general:5,suggestion:Infinity", true], - ["foo:4,suggestion:Infinity", true], - ["foo:5,suggestion:4", true], - - ["general:5,suggestion:Infinity", false], - ["general:5,suggestion:4", false], - ["general:1,suggestion:4,general:Infinity", false], - ["general:Infinity", false], - ["general:5", false], - - ["foo:1,general:5,suggestion:Infinity", false], - ["foo:2,general:5,suggestion:4", false], - ["foo:3,general:1,suggestion:4,general:Infinity", false], - ["foo:4,general:Infinity", false], - ["foo:5,general:5", false], - - ["", true], - ["bogus groups", true], - ]; - - for (let [matchGroups, expectedValue] of tests) { - info("Running test: " + JSON.stringify({ matchGroups, expectedValue })); - Services.prefs.clearUserPref("browser.urlbar.showSearchSuggestionsFirst"); - - // Set matchGroups. - Services.prefs.setCharPref("browser.urlbar.matchGroups", matchGroups); - - // Call initializeShowSearchSuggestionsFirstPref. - UrlbarPrefs.initializeShowSearchSuggestionsFirstPref(); - - // Both showSearchSuggestionsFirst and resultGroups should be updated. - Assert.equal( - Services.prefs.getBoolPref("browser.urlbar.showSearchSuggestionsFirst"), - expectedValue, - "showSearchSuggestionsFirst has the expected value" - ); - Assert.deepEqual( - UrlbarPrefs.resultGroups, - UrlbarPrefs.makeResultGroups({ - showSearchSuggestionsFirst: expectedValue, - }), - "resultGroups should be updated with the appropriate default" - ); - } - - Services.prefs.clearUserPref("browser.urlbar.matchGroups"); }); // Tests whether observer.onNimbusChanged works. diff --git a/browser/components/urlbar/tests/unit/test_muxer.js b/browser/components/urlbar/tests/unit/test_muxer.js @@ -588,9 +588,9 @@ add_task(async function test_badHeuristicsGroups_notFirst_4() { * The expected results. */ async function doBadHeuristicGroupsTest(resultGroups, expectedResults) { - sandbox.stub(UrlbarPrefs, "resultGroups").get(() => { - return { children: resultGroups }; - }); + sandbox + .stub(UrlbarPrefs, "getResultGroups") + .returns({ children: resultGroups }); let provider = registerBasicTestProvider(BAD_HEURISTIC_RESULTS); let context = createContext("foo", { providers: [provider.name] }); diff --git a/browser/components/urlbar/tests/unit/test_resultGroups.js b/browser/components/urlbar/tests/unit/test_resultGroups.js @@ -1572,6 +1572,6 @@ function makeIndexRange(startIndex, count) { function setResultGroups(resultGroups) { sandbox.restore(); if (resultGroups) { - sandbox.stub(UrlbarPrefs, "resultGroups").get(() => resultGroups); + sandbox.stub(UrlbarPrefs, "getResultGroups").returns(resultGroups); } } diff --git a/browser/components/urlbar/tests/unit/test_search_suggestions.js b/browser/components/urlbar/tests/unit/test_search_suggestions.js @@ -102,29 +102,27 @@ function makeRemoteSuggestionResults( function setResultGroups(groups) { sandbox.restore(); - sandbox.stub(UrlbarPrefs, "resultGroups").get(() => { - return { - children: [ - // heuristic - { - maxResultCount: 1, - children: [ - { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_TEST }, - { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_EXTENSION }, - { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_SEARCH_TIP }, - { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_OMNIBOX }, - { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_AUTOFILL }, - { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_TOKEN_ALIAS_ENGINE }, - { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_FALLBACK }, - ], - }, - // extensions using the omnibox API - { - group: UrlbarUtils.RESULT_GROUP.OMNIBOX, - }, - ...groups, - ], - }; + sandbox.stub(UrlbarPrefs, "getResultGroups").returns({ + children: [ + // heuristic + { + maxResultCount: 1, + children: [ + { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_TEST }, + { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_EXTENSION }, + { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_SEARCH_TIP }, + { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_OMNIBOX }, + { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_AUTOFILL }, + { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_TOKEN_ALIAS_ENGINE }, + { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_FALLBACK }, + ], + }, + // extensions using the omnibox API + { + group: UrlbarUtils.RESULT_GROUP.OMNIBOX, + }, + ...groups, + ], }); } diff --git a/browser/components/urlbar/tests/unit/test_suggestedIndexRelativeToGroup.js b/browser/components/urlbar/tests/unit/test_suggestedIndexRelativeToGroup.js @@ -630,6 +630,6 @@ function makeSuggestedIndexResults(objects) { function setResultGroups(resultGroups) { sandbox.restore(); if (resultGroups) { - sandbox.stub(UrlbarPrefs, "resultGroups").get(() => resultGroups); + sandbox.stub(UrlbarPrefs, "getResultGroups").returns(resultGroups); } }