For #25812 - Set the cursor at the end for search terms when switching to toolbar editMode.

This commit is contained in:
Alexandru2909 2022-12-08 12:34:48 +02:00 committed by mergify[bot]
parent 9565b48b70
commit d88d930b83
2 changed files with 38 additions and 3 deletions

View File

@ -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
}

View File

@ -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,