Bug 1657414 - Support search mode in the Touch Bar. r=adw

Differential Revision: https://phabricator.services.mozilla.com/D91077
This commit is contained in:
Harry Twyford 2020-09-25 20:45:48 +00:00
parent 4515b74439
commit 27650ea6ff
8 changed files with 296 additions and 64 deletions

View File

@ -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) {

View File

@ -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 =

View File

@ -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 });
}
});

View File

@ -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."
);
}
});

View File

@ -208,6 +208,7 @@ var UrlbarUtils = {
"tabmenu",
"topsites_newtab",
"topsites_urlbar",
"touchbar",
"typed",
]),

View File

@ -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``

View File

@ -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);
});

View File

@ -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