From a37958496cc8b419626b62b1883c85d0830c6c04 Mon Sep 17 00:00:00 2001 From: Gabriel Luong Date: Fri, 18 Mar 2022 11:12:57 -0400 Subject: [PATCH] Issue #11890: Add a new action container in the EditToolbar --- .../browser/toolbar/BrowserToolbar.kt | 7 +++++ .../browser/toolbar/edit/EditToolbar.kt | 22 +++++++++----- .../mozac_browser_toolbar_edittoolbar.xml | 12 +++++++- .../browser/toolbar/BrowserToolbarTest.kt | 29 +++++++++++++++++++ .../components/concept/toolbar/Toolbar.kt | 5 ++++ .../CustomTabSessionTitleObserverTest.kt | 1 + .../toolbar/ToolbarAutocompleteFeatureTest.kt | 4 +++ .../feature/toolbar/ToolbarInteractorTest.kt | 4 +++ docs/changelog.md | 1 + 9 files changed, 77 insertions(+), 8 deletions(-) diff --git a/components/browser/toolbar/src/main/java/mozilla/components/browser/toolbar/BrowserToolbar.kt b/components/browser/toolbar/src/main/java/mozilla/components/browser/toolbar/BrowserToolbar.kt index 81fe60c9e7a..a6ca8ef0c10 100644 --- a/components/browser/toolbar/src/main/java/mozilla/components/browser/toolbar/BrowserToolbar.kt +++ b/components/browser/toolbar/src/main/java/mozilla/components/browser/toolbar/BrowserToolbar.kt @@ -309,6 +309,13 @@ class BrowserToolbar @JvmOverloads constructor( display.removeNavigationAction(action) } + /** + * Adds an action to be displayed on the left of the URL in edit mode. + */ + override fun addSearchAction(action: Toolbar.Action) { + edit.addSearchAction(action) + } + /** * Adds an action to be displayed on the right of the URL in edit mode. */ diff --git a/components/browser/toolbar/src/main/java/mozilla/components/browser/toolbar/edit/EditToolbar.kt b/components/browser/toolbar/src/main/java/mozilla/components/browser/toolbar/edit/EditToolbar.kt index 0a0b33b9777..ce68d3a164e 100644 --- a/components/browser/toolbar/src/main/java/mozilla/components/browser/toolbar/edit/EditToolbar.kt +++ b/components/browser/toolbar/src/main/java/mozilla/components/browser/toolbar/edit/EditToolbar.kt @@ -40,13 +40,14 @@ private const val AUTOCOMPLETE_QUERY_THREADS = 3 * Sub-component of the browser toolbar responsible for allowing the user to edit the URL ("edit mode"). * * Structure: - * +------+---------------------------+---------+------+ - * | icon | url | actions | exit | - * +------+---------------------------+---------+------+ + * +------+----------------+---------------------------+--------------+------+ + * | icon | search actions | url | edit actions | exit | + * +------+----------------+---------------------------+--------------+------+ * * - icon: Optional icon that will be shown in front of the URL. + * - search actions: Optional action icons injected by other components (e.g. search engines) * - url: Editable URL of the currently displayed website - * - actions: Optional action icons injected by other components (e.g. barcode scanner) + * - edit actions: Optional action icons injected by other components (e.g. barcode scanner) * - exit: Button that switches back to display mode or invoke an app-defined callback. */ @Suppress("LargeClass") @@ -87,9 +88,10 @@ class EditToolbar internal constructor( @VisibleForTesting(otherwise = PRIVATE) internal val views = EditToolbarViews( - background = rootView.findViewById(R.id.mozac_browser_toolbar_background), - icon = rootView.findViewById(R.id.mozac_browser_toolbar_edit_icon), - editActions = rootView.findViewById(R.id.mozac_browser_toolbar_edit_actions), + background = rootView.findViewById(R.id.mozac_browser_toolbar_background), + icon = rootView.findViewById(R.id.mozac_browser_toolbar_edit_icon), + searchActions = rootView.findViewById(R.id.mozac_browser_toolbar_search_actions), + editActions = rootView.findViewById(R.id.mozac_browser_toolbar_edit_actions), clear = rootView.findViewById(R.id.mozac_browser_toolbar_clear_view).apply { setOnClickListener { onClear() @@ -232,9 +234,14 @@ class EditToolbar internal constructor( } internal fun invalidateActions() { + views.searchActions.invalidateActions() views.editActions.invalidateActions() } + internal fun addSearchAction(action: Toolbar.Action) { + views.searchActions.addAction(action) + } + internal fun addEditAction(action: Toolbar.Action) { views.editActions.addAction(action) } @@ -327,6 +334,7 @@ class EditToolbar internal constructor( internal class EditToolbarViews( val background: ImageView, val icon: ImageView, + val searchActions: ActionContainer, val editActions: ActionContainer, val clear: ImageView, val url: InlineAutocompleteEditText diff --git a/components/browser/toolbar/src/main/res/layout/mozac_browser_toolbar_edittoolbar.xml b/components/browser/toolbar/src/main/res/layout/mozac_browser_toolbar_edittoolbar.xml index 6b269b9820f..c5ff4cd53ff 100644 --- a/components/browser/toolbar/src/main/res/layout/mozac_browser_toolbar_edittoolbar.xml +++ b/components/browser/toolbar/src/main/res/layout/mozac_browser_toolbar_edittoolbar.xml @@ -36,6 +36,16 @@ tools:ignore="ContentDescription" android:layout_marginTop="8dp" /> + + diff --git a/components/browser/toolbar/src/test/java/mozilla/components/browser/toolbar/BrowserToolbarTest.kt b/components/browser/toolbar/src/test/java/mozilla/components/browser/toolbar/BrowserToolbarTest.kt index 3be27db4f2b..69e12082206 100644 --- a/components/browser/toolbar/src/test/java/mozilla/components/browser/toolbar/BrowserToolbarTest.kt +++ b/components/browser/toolbar/src/test/java/mozilla/components/browser/toolbar/BrowserToolbarTest.kt @@ -433,6 +433,22 @@ class BrowserToolbarTest { verify(display).addPageAction(action) } + @Test + fun `add search action will be forwarded to edit toolbar`() { + val toolbar = BrowserToolbar(testContext) + + val edit: EditToolbar = mock() + toolbar.edit = edit + + val action = BrowserToolbar.Button(mock(), "QR code scanner") { + // Do nothing + } + + toolbar.addSearchAction(action) + + verify(edit).addSearchAction(action) + } + @Test fun `add edit action will be forwarded to edit toolbar`() { val toolbar = BrowserToolbar(testContext) @@ -511,6 +527,19 @@ class BrowserToolbarTest { verify(display).invalidateActions() } + @Test + fun `invalidate actions is forwarded to edit toolbar`() { + val toolbar = BrowserToolbar(testContext) + val edit: EditToolbar = mock() + toolbar.edit = edit + + verify(edit, never()).invalidateActions() + + toolbar.invalidateActions() + + verify(edit).invalidateActions() + } + @Test fun `search terms (if set) are forwarded to edit toolbar instead of URL`() { val toolbar = BrowserToolbar(testContext) diff --git a/components/concept/toolbar/src/main/java/mozilla/components/concept/toolbar/Toolbar.kt b/components/concept/toolbar/src/main/java/mozilla/components/concept/toolbar/Toolbar.kt index 7556ec99b6e..f98402a6d4f 100644 --- a/components/concept/toolbar/src/main/java/mozilla/components/concept/toolbar/Toolbar.kt +++ b/components/concept/toolbar/src/main/java/mozilla/components/concept/toolbar/Toolbar.kt @@ -152,6 +152,11 @@ interface Toolbar { */ fun addNavigationAction(action: Action) + /** + * Adds an action to be displayed in edit mode. + */ + fun addSearchAction(action: Action) + /** * Adds an action to be displayed in edit mode. */ diff --git a/components/feature/customtabs/src/test/java/mozilla/components/feature/customtabs/feature/CustomTabSessionTitleObserverTest.kt b/components/feature/customtabs/src/test/java/mozilla/components/feature/customtabs/feature/CustomTabSessionTitleObserverTest.kt index 072347eb7d9..3eff488ea45 100644 --- a/components/feature/customtabs/src/test/java/mozilla/components/feature/customtabs/feature/CustomTabSessionTitleObserverTest.kt +++ b/components/feature/customtabs/src/test/java/mozilla/components/feature/customtabs/feature/CustomTabSessionTitleObserverTest.kt @@ -89,6 +89,7 @@ class CustomTabSessionTitleObserverTest { override fun addNavigationAction(action: Toolbar.Action) = Unit override fun removeNavigationAction(action: Toolbar.Action) = Unit override fun addEditAction(action: Toolbar.Action) = Unit + override fun addSearchAction(action: Toolbar.Action) = Unit override fun setOnEditListener(listener: Toolbar.OnEditListener) = Unit override fun displayMode() = Unit override fun editMode() = Unit diff --git a/components/feature/toolbar/src/test/java/mozilla/components/feature/toolbar/ToolbarAutocompleteFeatureTest.kt b/components/feature/toolbar/src/test/java/mozilla/components/feature/toolbar/ToolbarAutocompleteFeatureTest.kt index b85b939be2a..ab399034eb9 100644 --- a/components/feature/toolbar/src/test/java/mozilla/components/feature/toolbar/ToolbarAutocompleteFeatureTest.kt +++ b/components/feature/toolbar/src/test/java/mozilla/components/feature/toolbar/ToolbarAutocompleteFeatureTest.kt @@ -92,6 +92,10 @@ class ToolbarAutocompleteFeatureTest { fail() } + override fun addSearchAction(action: Toolbar.Action) { + fail() + } + override fun setOnEditListener(listener: Toolbar.OnEditListener) { fail() } diff --git a/components/feature/toolbar/src/test/java/mozilla/components/feature/toolbar/ToolbarInteractorTest.kt b/components/feature/toolbar/src/test/java/mozilla/components/feature/toolbar/ToolbarInteractorTest.kt index e81a7cecd7b..c1266e09bf4 100644 --- a/components/feature/toolbar/src/test/java/mozilla/components/feature/toolbar/ToolbarInteractorTest.kt +++ b/components/feature/toolbar/src/test/java/mozilla/components/feature/toolbar/ToolbarInteractorTest.kt @@ -77,6 +77,10 @@ class ToolbarInteractorTest { fail() } + override fun addSearchAction(action: Toolbar.Action) { + fail() + } + override fun setOnEditListener(listener: Toolbar.OnEditListener) { fail() } diff --git a/docs/changelog.md b/docs/changelog.md index ce7fa0e889a..fb1f6954b72 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -19,6 +19,7 @@ permalink: /changelog/ * **browser-toolbar** * Removed reflective access to non-public SDK APIs controlling the sensitivity of the gesture detector following which sparingly and for very short time a pinch/spread to zoom gesture might be identified first as a scroll gesture and move the toolbar a little before snapping to it's original position. + * Added option to add "search actions" that will show up before the URL in edit mode. [#11890](https://github.com/mozilla-mobile/android-components/issues/11890) * **feature-session** * 🆕 New `ScreenOrientationFeature` to enable support for setting a requested screen orientation as part of supporting the ScreenOrientation web APIs.