Skip to content

Commit

Permalink
Bug 1814425 - Don't show a suggestions header for topic specific sear…
Browse files Browse the repository at this point in the history
…ch engines
  • Loading branch information
Mugurell authored and mergify[bot] committed Feb 6, 2023
1 parent 6846dda commit 9a63f2c
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,12 @@ class AwesomeBarView(

if (!searchEngine.isNullOrEmpty()) {
searchEngine = when (searchEngine) {
GOOGLE_SEARCH_ENGINE_NAME -> activity.getString(
GOOGLE_SEARCH_ENGINE_NAME -> getString(
activity,
R.string.google_search_engine_suggestion_header,
)
else -> activity.getString(
else -> getString(
activity,
R.string.other_default_search_engine_suggestion_header,
searchEngine,
)
Expand All @@ -210,6 +212,20 @@ class AwesomeBarView(
return searchEngine
}

/**
* Get a suggestions header if [currentEngineName] is the one of the default search engine.
*
* @param currentEngine The currently selected search engine.
*/
private fun getSearchEngineSuggestionsHeader(currentEngine: SearchEngine?): String? {
val defaultSearchEngine = activity.components.core.store.state.search.selectedOrDefaultSearchEngine

return when (defaultSearchEngine == currentEngine) {
true -> getSearchEngineSuggestionsHeader()
false -> null
}
}

fun update(state: SearchFragmentState) {
// Do not make suggestions based on user's current URL unless it's a search shortcut
if (state.query.isNotEmpty() && state.query == state.url && !state.showSearchShortcuts) {
Expand Down Expand Up @@ -375,7 +391,7 @@ class AwesomeBarView(
searchEngine = validSearchEngine,
icon = getDrawable(activity, R.drawable.ic_history)?.toBitmap(),
engine = engineForSpeculativeConnects,
suggestionsHeader = getSearchEngineSuggestionsHeader(),
suggestionsHeader = getSearchEngineSuggestionsHeader(searchEngineSource.searchEngine),
)
}

Expand Down Expand Up @@ -545,6 +561,11 @@ class AwesomeBarView(
internal fun getDrawable(context: Context, resId: Int): Drawable? {
return AppCompatResources.getDrawable(context, resId)
}

@VisibleForTesting
internal fun getString(context: Context, resId: Int, vararg formatArgs: String?): String {
return context.getString(resId, *formatArgs)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import io.mockk.mockkObject
import io.mockk.mockkStatic
import io.mockk.unmockkObject
import io.mockk.unmockkStatic
import mozilla.components.browser.state.search.SearchEngine
import mozilla.components.browser.state.state.SearchState
import mozilla.components.browser.state.state.selectedOrDefaultSearchEngine
import mozilla.components.feature.awesomebar.provider.BookmarksStorageSuggestionProvider
import mozilla.components.feature.awesomebar.provider.CombinedHistorySuggestionProvider
import mozilla.components.feature.awesomebar.provider.HistoryStorageSuggestionProvider
Expand Down Expand Up @@ -42,7 +45,7 @@ import org.mozilla.fenix.utils.Settings

@RunWith(FenixRobolectricTestRunner::class)
class AwesomeBarViewTest {
private val activity: HomeActivity = mockk(relaxed = true)
private var activity: HomeActivity = mockk(relaxed = true)
private lateinit var awesomeBarView: AwesomeBarView

@Before
Expand Down Expand Up @@ -603,13 +606,77 @@ class AwesomeBarViewTest {

@Test
fun `GIVEN a search engine is available WHEN asking for a search term provider THEN return a valid provider`() {
val searchEngineSource = SearchEngineSource.Default(mockk())
val searchEngineSource = SearchEngineSource.Default(mockk(relaxed = true))

val result = awesomeBarView.getSearchTermSuggestionsProvider(searchEngineSource)

assertTrue(result is SearchTermSuggestionsProvider)
}

@Test
fun `GIVEN the default search engine WHEN asking for a search term provider THEN the provider should have a suggestions header`() {
val engine: SearchEngine = mockk {
every { name } returns "Test"
}
val searchEngineSource = SearchEngineSource.Default(engine)
every { AwesomeBarView.Companion.getString(any(), any(), any()) } answers {
"Search Test"
}

mockkStatic("mozilla.components.browser.state.state.SearchStateKt") {
every { any<SearchState>().selectedOrDefaultSearchEngine } returns engine

val result = awesomeBarView.getSearchTermSuggestionsProvider(searchEngineSource)

assertTrue(result is SearchTermSuggestionsProvider)
assertEquals("Search Test", result?.groupTitle())
}
}

@Test
fun `GIVEN a shortcut search engine selected WHEN asking for a search term provider THEN the provider should not have a suggestions header`() {
val defaultEngine: SearchEngine = mockk {
every { name } returns "Test"
}
val otherEngine: SearchEngine = mockk {
every { name } returns "Other"
}
val searchEngineSource = SearchEngineSource.Shortcut(otherEngine)
every { AwesomeBarView.Companion.getString(any(), any(), any()) } answers {
"Search Test"
}

mockkStatic("mozilla.components.browser.state.state.SearchStateKt") {
every { any<SearchState>().selectedOrDefaultSearchEngine } returns defaultEngine

val result = awesomeBarView.getSearchTermSuggestionsProvider(searchEngineSource)

assertTrue(result is SearchTermSuggestionsProvider)
assertNull(result?.groupTitle())
}
}

@Test
fun `GIVEN the default search engine is unknown WHEN asking for a search term provider THEN the provider should not have a suggestions header`() {
val defaultEngine: SearchEngine? = null
val otherEngine: SearchEngine = mockk {
every { name } returns "Other"
}
val searchEngineSource = SearchEngineSource.Shortcut(otherEngine)
every { AwesomeBarView.Companion.getString(any(), any(), any()) } answers {
"Search Test"
}

mockkStatic("mozilla.components.browser.state.state.SearchStateKt") {
every { any<SearchState>().selectedOrDefaultSearchEngine } returns defaultEngine

val result = awesomeBarView.getSearchTermSuggestionsProvider(searchEngineSource)

assertTrue(result is SearchTermSuggestionsProvider)
assertNull(result?.groupTitle())
}
}

@Test
fun `GIVEN history search term suggestions disabled WHEN getting suggestions providers THEN don't search term provider of past searches`() {
every { activity.settings() } returns mockk(relaxed = true)
Expand Down

0 comments on commit 9a63f2c

Please sign in to comment.