From ae6bdc265d0290e97e68f60f43a04bfb7abf4ea8 Mon Sep 17 00:00:00 2001 From: Alexandru2909 Date: Mon, 31 Oct 2022 14:13:47 +0200 Subject: [PATCH] [fenix] For https://github.com/mozilla-mobile/fenix/issues/25811 - Add header to unified search engine menu --- .../fenix/search/SearchDialogFragment.kt | 4 +- .../search/toolbar/SearchSelectorMenu.kt | 34 +++++++------- app/src/main/res/values/strings.xml | 2 + .../search/toolbar/SearchSelectorMenuTest.kt | 47 +++++++++++++++++++ 4 files changed, 69 insertions(+), 18 deletions(-) create mode 100644 app/src/test/java/org/mozilla/fenix/search/toolbar/SearchSelectorMenuTest.kt diff --git a/app/src/main/java/org/mozilla/fenix/search/SearchDialogFragment.kt b/app/src/main/java/org/mozilla/fenix/search/SearchDialogFragment.kt index 6562e59423..d4d885a3b3 100644 --- a/app/src/main/java/org/mozilla/fenix/search/SearchDialogFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/search/SearchDialogFragment.kt @@ -730,9 +730,9 @@ class SearchDialogFragment : AppCompatDialogFragment(), UserInteractionHandler { ) { interactor.onMenuItemTapped(SearchSelectorMenu.Item.SearchEngine(it)) } - } + searchSelectorMenu.menuItems() + } - searchSelectorMenu.menuController.submitList(searchEngineList) + searchSelectorMenu.menuController.submitList(searchSelectorMenu.menuItems(searchEngineList)) toolbarView.view.invalidateActions() } diff --git a/app/src/main/java/org/mozilla/fenix/search/toolbar/SearchSelectorMenu.kt b/app/src/main/java/org/mozilla/fenix/search/toolbar/SearchSelectorMenu.kt index 8f3ca13203..3152c05dc0 100644 --- a/app/src/main/java/org/mozilla/fenix/search/toolbar/SearchSelectorMenu.kt +++ b/app/src/main/java/org/mozilla/fenix/search/toolbar/SearchSelectorMenu.kt @@ -5,12 +5,13 @@ package org.mozilla.fenix.search.toolbar import android.content.Context -import androidx.annotation.VisibleForTesting import androidx.appcompat.content.res.AppCompatResources import mozilla.components.browser.menu2.BrowserMenuController import mozilla.components.browser.state.search.SearchEngine import mozilla.components.concept.menu.MenuController +import mozilla.components.concept.menu.candidate.DecorativeTextMenuCandidate import mozilla.components.concept.menu.candidate.DrawableMenuIcon +import mozilla.components.concept.menu.candidate.MenuCandidate import mozilla.components.concept.menu.candidate.TextMenuCandidate import mozilla.components.support.ktx.android.content.getColorFromAttr import org.mozilla.fenix.R @@ -47,21 +48,22 @@ class SearchSelectorMenu( val menuController: MenuController by lazy { BrowserMenuController() } - @VisibleForTesting - internal fun menuItems(): List { - return listOf( - TextMenuCandidate( - text = context.getString(R.string.search_settings_menu_item), - start = DrawableMenuIcon( - drawable = AppCompatResources.getDrawable( - context, - R.drawable.mozac_ic_settings, - ), - tint = context.getColorFromAttr(R.attr.textPrimary), - ), - ) { - interactor.onMenuItemTapped(Item.SearchSettings) - }, + internal fun menuItems(searchEngines: List): List { + val headerCandidate = DecorativeTextMenuCandidate( + text = context.getString(R.string.search_header_menu_item), ) + val settingsCandidate = TextMenuCandidate( + text = context.getString(R.string.search_settings_menu_item), + start = DrawableMenuIcon( + drawable = AppCompatResources.getDrawable( + context, + R.drawable.mozac_ic_settings, + ), + tint = context.getColorFromAttr(R.attr.textPrimary), + ), + ) { + interactor.onMenuItemTapped(Item.SearchSettings) + } + return listOf(headerCandidate) + searchEngines + listOf(settingsCandidate) } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 2ade8fc1dd..3c24676a90 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -239,6 +239,8 @@ Search directly from the address bar Search settings + + This time search: diff --git a/app/src/test/java/org/mozilla/fenix/search/toolbar/SearchSelectorMenuTest.kt b/app/src/test/java/org/mozilla/fenix/search/toolbar/SearchSelectorMenuTest.kt new file mode 100644 index 0000000000..38c49564d6 --- /dev/null +++ b/app/src/test/java/org/mozilla/fenix/search/toolbar/SearchSelectorMenuTest.kt @@ -0,0 +1,47 @@ +package org.mozilla.fenix.search.toolbar + +import io.mockk.Runs +import io.mockk.every +import io.mockk.just +import io.mockk.mockk +import io.mockk.verify +import mozilla.components.concept.menu.candidate.DecorativeTextMenuCandidate +import mozilla.components.concept.menu.candidate.TextMenuCandidate +import mozilla.components.support.test.robolectric.testContext +import org.junit.Assert.assertEquals +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.mozilla.fenix.R +import org.mozilla.fenix.helpers.FenixRobolectricTestRunner + +@RunWith(FenixRobolectricTestRunner::class) +class SearchSelectorMenuTest { + + private lateinit var menu: SearchSelectorMenu + private val interactor = mockk() + + @Before + fun setup() { + menu = SearchSelectorMenu(testContext, interactor) + } + + @Test + fun `WHEN building the menu items THEN the header is the first item AND the search settings is the last item`() { + every { interactor.onMenuItemTapped(any()) } just Runs + + val items = menu.menuItems(listOf()) + val lastItem = (items.last() as TextMenuCandidate) + lastItem.onClick() + + assertEquals( + testContext.getString(R.string.search_header_menu_item), + (items.first() as DecorativeTextMenuCandidate).text, + ) + assertEquals( + testContext.getString(R.string.search_settings_menu_item), + lastItem.text, + ) + verify { interactor.onMenuItemTapped(SearchSelectorMenu.Item.SearchSettings) } + } +}