Skip to content

Commit

Permalink
Issue mozilla-mobile#11890: Add a new action container in the EditToo…
Browse files Browse the repository at this point in the history
…lbar
  • Loading branch information
gabrielluong committed Mar 18, 2022
1 parent b8bdd6f commit a826978
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -87,9 +88,10 @@ class EditToolbar internal constructor(

@VisibleForTesting(otherwise = PRIVATE)
internal val views = EditToolbarViews(
background = rootView.findViewById<ImageView>(R.id.mozac_browser_toolbar_background),
icon = rootView.findViewById<ImageView>(R.id.mozac_browser_toolbar_edit_icon),
editActions = rootView.findViewById<ActionContainer>(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<ImageView>(R.id.mozac_browser_toolbar_clear_view).apply {
setOnClickListener {
onClear()
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@
tools:ignore="ContentDescription"
android:layout_marginTop="8dp" />

<mozilla.components.browser.toolbar.internal.ActionContainer
android:id="@+id/mozac_browser_toolbar_search_actions"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginTop="8dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toEndOf="@id/mozac_browser_toolbar_edit_icon"
mozac:actionContainerItemSize="56dp"
tools:layout_width="56dp" />

<mozilla.components.ui.autocomplete.InlineAutocompleteEditText
android:id="@+id/mozac_browser_toolbar_edit_url_view"
android:layout_width="0dp"
Expand All @@ -50,7 +60,7 @@
android:background="#00000000"
android:textSize="15sp"
app:layout_goneMarginStart="8dp"
app:layout_constraintStart_toEndOf="@id/mozac_browser_toolbar_edit_icon"
app:layout_constraintStart_toEndOf="@id/mozac_browser_toolbar_search_actions"
app:layout_constraintEnd_toStartOf="@id/mozac_browser_toolbar_edit_actions"
app:layout_constraintTop_toTopOf="parent" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down

0 comments on commit a826978

Please sign in to comment.