From 27650ea6ff7ac1bbb927e6e3b4477c03edf20ca8 Mon Sep 17 00:00:00 2001 From: Harry Twyford Date: Fri, 25 Sep 2020 20:45:48 +0000 Subject: [PATCH] Bug 1657414 - Support search mode in the Touch Bar. r=adw Differential Revision: https://phabricator.services.mozilla.com/D91077 --- browser/components/touchbar/MacTouchBar.js | 21 ++- .../touchbar/tests/browser/browser.ini | 2 + .../browser_touchbar_searchrestrictions.js | 113 ++++++------ ...wser_touchbar_searchrestrictions_legacy.js | 174 ++++++++++++++++++ browser/components/urlbar/UrlbarUtils.jsm | 1 + browser/components/urlbar/docs/telemetry.rst | 3 + ...rowser_UsageTelemetry_urlbar_searchmode.js | 29 +++ toolkit/components/telemetry/Scalars.yaml | 17 ++ 8 files changed, 296 insertions(+), 64 deletions(-) create mode 100644 browser/components/touchbar/tests/browser/browser_touchbar_searchrestrictions_legacy.js diff --git a/browser/components/touchbar/MacTouchBar.js b/browser/components/touchbar/MacTouchBar.js index 57a61d7a66c5..ec761da74b3c 100644 --- a/browser/components/touchbar/MacTouchBar.js +++ b/browser/components/touchbar/MacTouchBar.js @@ -183,14 +183,6 @@ var gBuiltInInputs = { UrlbarTokenizer.RESTRICT.BOOKMARK ), }, - History: { - title: "search-history", - type: kInputTypes.BUTTON, - callback: () => - gTouchBarHelper.insertRestrictionInUrlbar( - UrlbarTokenizer.RESTRICT.HISTORY - ), - }, OpenTabs: { title: "search-opentabs", type: kInputTypes.BUTTON, @@ -199,6 +191,14 @@ var gBuiltInInputs = { UrlbarTokenizer.RESTRICT.OPENPAGE ), }, + History: { + title: "search-history", + type: kInputTypes.BUTTON, + callback: () => + gTouchBarHelper.insertRestrictionInUrlbar( + UrlbarTokenizer.RESTRICT.HISTORY + ), + }, Tags: { title: "search-tags", type: kInputTypes.BUTTON, @@ -415,7 +415,10 @@ class TouchBarHelper { } } - TouchBarHelper.window.gURLBar.search(`${restrictionToken} ${searchString}`); + TouchBarHelper.window.gURLBar.search( + `${restrictionToken} ${searchString}`, + { searchModeEntry: "touchbar" } + ); } observe(subject, topic, data) { diff --git a/browser/components/touchbar/tests/browser/browser.ini b/browser/components/touchbar/tests/browser/browser.ini index 4377968a2a06..e00c3134a48a 100644 --- a/browser/components/touchbar/tests/browser/browser.ini +++ b/browser/components/touchbar/tests/browser/browser.ini @@ -2,6 +2,8 @@ [browser_touchbar_searchrestrictions.js] skip-if = os != "mac" +[browser_touchbar_searchrestrictions_legacy.js] +skip-if = os != "mac" [browser_touchbar_tests.js] skip-if = os != "mac" support-files = diff --git a/browser/components/touchbar/tests/browser/browser_touchbar_searchrestrictions.js b/browser/components/touchbar/tests/browser/browser_touchbar_searchrestrictions.js index 4a7ce2482353..ecc803d854d4 100644 --- a/browser/components/touchbar/tests/browser/browser_touchbar_searchrestrictions.js +++ b/browser/components/touchbar/tests/browser/browser_touchbar_searchrestrictions.js @@ -7,6 +7,7 @@ const { XPCOMUtils } = ChromeUtils.import( ); XPCOMUtils.defineLazyModuleGetters(this, { + UrlbarUtils: "resource:///modules/UrlbarUtils.jsm", UrlbarTestUtils: "resource://testing-common/UrlbarTestUtils.jsm", UrlbarTokenizer: "resource:///modules/UrlbarTokenizer.jsm", }); @@ -18,45 +19,80 @@ XPCOMUtils.defineLazyServiceGetter( "nsITouchBarHelper" ); +/** + * Tests the search restriction buttons in the Touch Bar. + */ + +add_task(async function init() { + await SpecialPowers.pushPrefEnv({ + set: [["browser.urlbar.update2", true]], + }); +}); + +/** + * @param {string} input + * The value to be inserted in the Urlbar. + * @param {UrlbarTokenizer.RESTRICT} token + * A restriction token corresponding to a Touch Bar button. + */ +async function searchAndCheckState({ input, token }) { + await UrlbarTestUtils.promiseAutocompleteResultPopup({ + window, + value: input, + }); + input = input.trimStart(); + if (Object.values(UrlbarTokenizer.RESTRICT).includes(input[0])) { + input = input.slice(1).trimStart(); + } + let searchMode = UrlbarUtils.searchModeForToken(token); + let expectedValue = searchMode ? input : `${token} ${input}`; + TouchBarHelper.insertRestrictionInUrlbar(token); + + if (searchMode) { + searchMode.entry = "touchbar"; + await UrlbarTestUtils.assertSearchMode(window, searchMode); + } + Assert.equal( + gURLBar.value, + expectedValue, + "The search restriction token should have been entered." + ); + + await UrlbarTestUtils.promisePopupClose(window); +} + +add_task(async function init() { + UrlbarTestUtils.init(this); + registerCleanupFunction(() => { + UrlbarTestUtils.uninit(); + }); +}); + add_task(async function insertTokens() { const tests = [ { input: "mozilla", token: UrlbarTokenizer.RESTRICT.HISTORY, - expected: "^ mozilla", }, { input: "mozilla", token: UrlbarTokenizer.RESTRICT.BOOKMARK, - expected: "* mozilla", }, { input: "mozilla", token: UrlbarTokenizer.RESTRICT.TAG, - expected: "+ mozilla", }, { input: "mozilla", token: UrlbarTokenizer.RESTRICT.OPENPAGE, - expected: "% mozilla", }, { input: "mozilla", token: UrlbarTokenizer.RESTRICT.TITLE, - expected: "# mozilla", }, ]; - let win = BrowserWindowTracker.getTopWindow(); - for (let { input, token, expected } of tests) { - win.gURLBar.search(input); - await UrlbarTestUtils.promiseSearchComplete(win); - TouchBarHelper.insertRestrictionInUrlbar(token); - Assert.equal( - win.gURLBar.value, - expected, - "The search restriction token should have been entered." - ); - await UrlbarTestUtils.promisePopupClose(win); + for (let test of tests) { + await searchAndCheckState(test); } }); @@ -65,35 +101,22 @@ add_task(async function existingTokens() { { input: "* mozilla", token: UrlbarTokenizer.RESTRICT.HISTORY, - expected: "^ mozilla", }, { input: "+ mozilla", token: UrlbarTokenizer.RESTRICT.BOOKMARK, - expected: "* mozilla", }, { input: "( $ ^ mozilla", token: UrlbarTokenizer.RESTRICT.TAG, - expected: "+ ( $ ^ mozilla", }, { input: "^*+%?#$ mozilla", token: UrlbarTokenizer.RESTRICT.TAG, - expected: "+ *+%?#$ mozilla", }, ]; - let win = BrowserWindowTracker.getTopWindow(); - for (let { input, token, expected } of tests) { - win.gURLBar.search(input); - await UrlbarTestUtils.promiseSearchComplete(win); - TouchBarHelper.insertRestrictionInUrlbar(token); - Assert.equal( - win.gURLBar.value, - expected, - "The search restriction token should have been replaced." - ); - await UrlbarTestUtils.promisePopupClose(win); + for (let test of tests) { + await searchAndCheckState(test); } }); @@ -102,31 +125,18 @@ add_task(async function stripSpaces() { { input: " ^ mozilla", token: UrlbarTokenizer.RESTRICT.HISTORY, - expected: "^ mozilla", }, { input: " + mozilla ", token: UrlbarTokenizer.RESTRICT.BOOKMARK, - expected: "* mozilla ", }, { input: " moz illa ", token: UrlbarTokenizer.RESTRICT.TAG, - expected: "+ moz illa ", }, ]; - let win = BrowserWindowTracker.getTopWindow(); - for (let { input, token, expected } of tests) { - win.gURLBar.search(input); - await UrlbarTestUtils.promiseSearchComplete(win); - TouchBarHelper.insertRestrictionInUrlbar(token); - Assert.equal( - win.gURLBar.value, - expected, - "The search restriction token should have been entered " + - "with stripped whitespace." - ); - await UrlbarTestUtils.promisePopupClose(win); + for (let test of tests) { + await searchAndCheckState(test); } }); @@ -135,17 +145,15 @@ add_task(async function clearURLs() { { loadUrl: "http://example.com/", token: UrlbarTokenizer.RESTRICT.HISTORY, - expected: "^ ", }, { loadUrl: "about:mozilla", token: UrlbarTokenizer.RESTRICT.BOOKMARK, - expected: "* ", }, ]; let win = BrowserWindowTracker.getTopWindow(); await UrlbarTestUtils.promisePopupClose(win); - for (let { loadUrl, token, expected } of tests) { + for (let { loadUrl, token } of tests) { let browser = win.gBrowser.selectedBrowser; let loadedPromise = BrowserTestUtils.browserLoaded(browser, false, loadUrl); BrowserTestUtils.loadURI(browser, loadUrl); @@ -153,11 +161,6 @@ add_task(async function clearURLs() { await TestUtils.waitForCondition( () => win.gURLBar.getAttribute("pageproxystate") == "valid" ); - TouchBarHelper.insertRestrictionInUrlbar(token); - Assert.equal( - win.gURLBar.value, - expected, - "The search restriction token should have cleared out the URL." - ); + await searchAndCheckState({ input: "", token }); } }); diff --git a/browser/components/touchbar/tests/browser/browser_touchbar_searchrestrictions_legacy.js b/browser/components/touchbar/tests/browser/browser_touchbar_searchrestrictions_legacy.js new file mode 100644 index 000000000000..a14270f6b17e --- /dev/null +++ b/browser/components/touchbar/tests/browser/browser_touchbar_searchrestrictions_legacy.js @@ -0,0 +1,174 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +const { XPCOMUtils } = ChromeUtils.import( + "resource://gre/modules/XPCOMUtils.jsm" +); + +XPCOMUtils.defineLazyModuleGetters(this, { + UrlbarTestUtils: "resource://testing-common/UrlbarTestUtils.jsm", + UrlbarTokenizer: "resource:///modules/UrlbarTokenizer.jsm", +}); + +XPCOMUtils.defineLazyServiceGetter( + this, + "TouchBarHelper", + "@mozilla.org/widget/touchbarhelper;1", + "nsITouchBarHelper" +); + +/** + * Tests the search restriction buttons in the Touch Bar. + * This test can be removed after the browser.urlbar.update2 pref is removed. + */ + +add_task(async function init() { + await SpecialPowers.pushPrefEnv({ + set: [["browser.urlbar.update2", false]], + }); +}); + +add_task(async function insertTokens() { + const tests = [ + { + input: "mozilla", + token: UrlbarTokenizer.RESTRICT.HISTORY, + expected: "^ mozilla", + }, + { + input: "mozilla", + token: UrlbarTokenizer.RESTRICT.BOOKMARK, + expected: "* mozilla", + }, + { + input: "mozilla", + token: UrlbarTokenizer.RESTRICT.TAG, + expected: "+ mozilla", + }, + { + input: "mozilla", + token: UrlbarTokenizer.RESTRICT.OPENPAGE, + expected: "% mozilla", + }, + { + input: "mozilla", + token: UrlbarTokenizer.RESTRICT.TITLE, + expected: "# mozilla", + }, + ]; + let win = BrowserWindowTracker.getTopWindow(); + for (let { input, token, expected } of tests) { + win.gURLBar.search(input); + await UrlbarTestUtils.promiseSearchComplete(win); + TouchBarHelper.insertRestrictionInUrlbar(token); + Assert.equal( + win.gURLBar.value, + expected, + "The search restriction token should have been entered." + ); + await UrlbarTestUtils.promisePopupClose(win); + } +}); + +add_task(async function existingTokens() { + const tests = [ + { + input: "* mozilla", + token: UrlbarTokenizer.RESTRICT.HISTORY, + expected: "^ mozilla", + }, + { + input: "+ mozilla", + token: UrlbarTokenizer.RESTRICT.BOOKMARK, + expected: "* mozilla", + }, + { + input: "( $ ^ mozilla", + token: UrlbarTokenizer.RESTRICT.TAG, + expected: "+ ( $ ^ mozilla", + }, + { + input: "^*+%?#$ mozilla", + token: UrlbarTokenizer.RESTRICT.TAG, + expected: "+ *+%?#$ mozilla", + }, + ]; + let win = BrowserWindowTracker.getTopWindow(); + for (let { input, token, expected } of tests) { + win.gURLBar.search(input); + await UrlbarTestUtils.promiseSearchComplete(win); + TouchBarHelper.insertRestrictionInUrlbar(token); + Assert.equal( + win.gURLBar.value, + expected, + "The search restriction token should have been replaced." + ); + await UrlbarTestUtils.promisePopupClose(win); + } +}); + +add_task(async function stripSpaces() { + const tests = [ + { + input: " ^ mozilla", + token: UrlbarTokenizer.RESTRICT.HISTORY, + expected: "^ mozilla", + }, + { + input: " + mozilla ", + token: UrlbarTokenizer.RESTRICT.BOOKMARK, + expected: "* mozilla ", + }, + { + input: " moz illa ", + token: UrlbarTokenizer.RESTRICT.TAG, + expected: "+ moz illa ", + }, + ]; + let win = BrowserWindowTracker.getTopWindow(); + for (let { input, token, expected } of tests) { + win.gURLBar.search(input); + await UrlbarTestUtils.promiseSearchComplete(win); + TouchBarHelper.insertRestrictionInUrlbar(token); + Assert.equal( + win.gURLBar.value, + expected, + "The search restriction token should have been entered " + + "with stripped whitespace." + ); + await UrlbarTestUtils.promisePopupClose(win); + } +}); + +add_task(async function clearURLs() { + const tests = [ + { + loadUrl: "http://example.com/", + token: UrlbarTokenizer.RESTRICT.HISTORY, + expected: "^ ", + }, + { + loadUrl: "about:mozilla", + token: UrlbarTokenizer.RESTRICT.BOOKMARK, + expected: "* ", + }, + ]; + let win = BrowserWindowTracker.getTopWindow(); + await UrlbarTestUtils.promisePopupClose(win); + for (let { loadUrl, token, expected } of tests) { + let browser = win.gBrowser.selectedBrowser; + let loadedPromise = BrowserTestUtils.browserLoaded(browser, false, loadUrl); + BrowserTestUtils.loadURI(browser, loadUrl); + await loadedPromise; + await TestUtils.waitForCondition( + () => win.gURLBar.getAttribute("pageproxystate") == "valid" + ); + TouchBarHelper.insertRestrictionInUrlbar(token); + Assert.equal( + win.gURLBar.value, + expected, + "The search restriction token should have cleared out the URL." + ); + } +}); diff --git a/browser/components/urlbar/UrlbarUtils.jsm b/browser/components/urlbar/UrlbarUtils.jsm index 80c261d75a8b..792e0a040721 100644 --- a/browser/components/urlbar/UrlbarUtils.jsm +++ b/browser/components/urlbar/UrlbarUtils.jsm @@ -208,6 +208,7 @@ var UrlbarUtils = { "tabmenu", "topsites_newtab", "topsites_urlbar", + "touchbar", "typed", ]), diff --git a/browser/components/urlbar/docs/telemetry.rst b/browser/components/urlbar/docs/telemetry.rst index 33e0f5fd088b..e2eb6b2ed51f 100644 --- a/browser/components/urlbar/docs/telemetry.rst +++ b/browser/components/urlbar/docs/telemetry.rst @@ -170,6 +170,9 @@ urlbar.searchmode.* Used when the user selects a search shortcut Top Site from the New Tab Page. - ``topsites_urlbar`` Used when the user selects a search shortcut Top Site from the Urlbar. + - ``touchbar`` + Used when the user taps a search shortct on the Touch Bar, available on some + Macs. - ``typed`` Used when the user types an engine alias in the Urlbar. - ``other`` diff --git a/browser/modules/test/browser/browser_UsageTelemetry_urlbar_searchmode.js b/browser/modules/test/browser/browser_UsageTelemetry_urlbar_searchmode.js index 4b879cac8d24..18a2be747492 100644 --- a/browser/modules/test/browser/browser_UsageTelemetry_urlbar_searchmode.js +++ b/browser/modules/test/browser/browser_UsageTelemetry_urlbar_searchmode.js @@ -18,10 +18,18 @@ const TEST_QUERY = "test"; const SUGGEST_PREF = "browser.search.suggest.enabled"; XPCOMUtils.defineLazyModuleGetters(this, { + AppConstants: "resource://gre/modules/AppConstants.jsm", SearchTelemetry: "resource:///modules/SearchTelemetry.jsm", UrlbarTestUtils: "resource://testing-common/UrlbarTestUtils.jsm", }); +XPCOMUtils.defineLazyServiceGetter( + this, + "TouchBarHelper", + "@mozilla.org/widget/touchbarhelper;1", + "nsITouchBarHelper" +); + /** * Asserts that search mode telemetry was recorded correctly. * @param {string} entry @@ -398,3 +406,24 @@ add_task(async function test_handoff_pbm() { await UrlbarTestUtils.promisePopupClose(win); await BrowserTestUtils.closeWindow(win); }); + +// Enters search mode by tapping a search shortcut on the Touch Bar. +add_task(async function test_touchbar() { + if (AppConstants.platform != "macosx") { + return; + } + + await UrlbarTestUtils.promiseAutocompleteResultPopup({ + window, + value: TEST_QUERY, + }); + // We have to fake the tap on the Touch Bar since mochitests have no way of + // interacting with the Touch Bar. + TouchBarHelper.insertRestrictionInUrlbar(UrlbarTokenizer.RESTRICT.HISTORY); + await UrlbarTestUtils.assertSearchMode(window, { + source: UrlbarUtils.RESULT_SOURCE.HISTORY, + entry: "touchbar", + }); + assertSearchModeScalar("touchbar", "history"); + await UrlbarTestUtils.exitSearchMode(window); +}); diff --git a/toolkit/components/telemetry/Scalars.yaml b/toolkit/components/telemetry/Scalars.yaml index 8d1090a046bb..c8753cec1129 100644 --- a/toolkit/components/telemetry/Scalars.yaml +++ b/toolkit/components/telemetry/Scalars.yaml @@ -5473,6 +5473,23 @@ urlbar.searchmode: - 'firefox' record_in_processes: - main + touchbar: + bug_numbers: + - 1657414 + description: > + A keyed uint recording how many times the user entered a particular search + mode after selecting a search shortcut on the macOS Touch Bar. + expires: never + kind: uint + keyed: true + notification_emails: + - fx-search@mozilla.com + - tbrooks@mozilla.com + release_channel_collection: opt-out + products: + - 'firefox' + record_in_processes: + - main typed: bug_numbers: - 1654680