diff --git a/app/src/main/java/org/mozilla/fenix/search/toolbar/ToolbarView.kt b/app/src/main/java/org/mozilla/fenix/search/toolbar/ToolbarView.kt index 386512e66b..9ec51c0c1f 100644 --- a/app/src/main/java/org/mozilla/fenix/search/toolbar/ToolbarView.kt +++ b/app/src/main/java/org/mozilla/fenix/search/toolbar/ToolbarView.kt @@ -12,6 +12,7 @@ import androidx.appcompat.content.res.AppCompatResources import androidx.core.content.ContextCompat import mozilla.components.browser.state.search.SearchEngine import mozilla.components.browser.toolbar.BrowserToolbar +import mozilla.components.concept.toolbar.Toolbar import mozilla.components.support.ktx.android.content.getColorFromAttr import mozilla.components.support.ktx.android.content.res.resolveAttribute import mozilla.components.support.ktx.android.view.hideKeyboard @@ -135,7 +136,12 @@ class ToolbarView( // we have the most up to date text interactor.onTextChanged(view.url.toString()) - view.editMode() + // If search terms are displayed, move the cursor to the end instead of selecting all text. + if (settings.showUnifiedSearchFeature && searchState.searchTerms.isNotBlank()) { + view.editMode(cursorPlacement = Toolbar.CursorPlacement.END) + } else { + view.editMode() + } isInitialized = true } diff --git a/app/src/test/java/org/mozilla/fenix/search/toolbar/ToolbarViewTest.kt b/app/src/test/java/org/mozilla/fenix/search/toolbar/ToolbarViewTest.kt index 99e6e55299..84d2cada49 100644 --- a/app/src/test/java/org/mozilla/fenix/search/toolbar/ToolbarViewTest.kt +++ b/app/src/test/java/org/mozilla/fenix/search/toolbar/ToolbarViewTest.kt @@ -11,12 +11,14 @@ import io.mockk.MockKAnnotations import io.mockk.every import io.mockk.impl.annotations.MockK import io.mockk.mockk +import io.mockk.mockkObject import io.mockk.spyk import io.mockk.verify import mozilla.components.browser.state.search.SearchEngine import mozilla.components.browser.toolbar.BrowserToolbar import mozilla.components.browser.toolbar.edit.EditToolbar import mozilla.components.concept.engine.Engine +import mozilla.components.concept.toolbar.Toolbar import mozilla.components.support.test.robolectric.testContext import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse @@ -24,12 +26,13 @@ import org.junit.Assert.assertTrue import org.junit.Before import org.junit.Test import org.junit.runner.RunWith +import org.mozilla.fenix.FeatureFlags import org.mozilla.fenix.R import org.mozilla.fenix.components.metrics.MetricsUtils +import org.mozilla.fenix.ext.settings import org.mozilla.fenix.helpers.FenixRobolectricTestRunner import org.mozilla.fenix.search.SearchEngineSource import org.mozilla.fenix.search.SearchFragmentState -import org.mozilla.fenix.utils.Settings import java.util.UUID @RunWith(FenixRobolectricTestRunner::class) @@ -71,6 +74,7 @@ class ToolbarViewTest { fun setup() { MockKAnnotations.init(this) context = ContextThemeWrapper(testContext, R.style.NormalTheme) + every { context.settings() } returns mockk(relaxed = true) toolbar = spyk(BrowserToolbar(context)) } @@ -115,6 +119,31 @@ class ToolbarViewTest { verify(exactly = 2) { interactor.onTextChanged(any()) } } + @Test + fun `GIVEN search term is set WHEN switching to edit mode THEN the cursor is set at the end of the search term`() { + every { context.settings().showUnifiedSearchFeature } returns true + val view = buildToolbarView(false) + mockkObject(FeatureFlags) + + view.update(defaultState.copy(searchTerms = "search terms")) + + // editMode gets called when the view is initialized. + verify(exactly = 1) { toolbar.editMode(Toolbar.CursorPlacement.ALL) } + verify(exactly = 1) { toolbar.editMode(Toolbar.CursorPlacement.END) } + } + + @Test + fun `GIVEN no search term is set WHEN switching to edit mode THEN the cursor is set at the end of the search term`() { + every { context.settings().showUnifiedSearchFeature } returns true + val view = buildToolbarView(false) + mockkObject(FeatureFlags) + + view.update(defaultState) + + // editMode gets called when the view is initialized. + verify(exactly = 2) { toolbar.editMode(Toolbar.CursorPlacement.ALL) } + } + @Test fun `URL gets set to the states query`() { val toolbarView = buildToolbarView(false) @@ -223,7 +252,7 @@ class ToolbarViewTest { private fun buildToolbarView(isPrivate: Boolean) = ToolbarView( context, - Settings(context), + context.settings(), interactor, isPrivate = isPrivate, view = toolbar,