Skip to content

Commit

Permalink
Don't crash, and allow selection in SelectionContainer to start when …
Browse files Browse the repository at this point in the history
…drag starts below the bounds of the visible text (#1230)
  • Loading branch information
m-sasha committed Apr 2, 2024
1 parent d4c13ae commit 28cad83
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ internal fun SelectionLayoutBuilder.appendSelectableInfo(
val bounds = Rect(
0.0f,
0.0f,
textLayoutResult.multiParagraph.width.toFloat(),
textLayoutResult.multiParagraph.height.toFloat()
textLayoutResult.size.width.toFloat(),
textLayoutResult.size.height.toFloat()
)

val currentXDirection = getXDirection(localPosition, bounds)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package androidx.compose.foundation.text.selection

import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.text.BasicText
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.test.ExperimentalTestApi
import androidx.compose.ui.test.dragAndDrop
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.performMouseInput
import androidx.compose.ui.test.runComposeUiTest
import androidx.compose.ui.text.TextRange
import androidx.compose.ui.unit.dp
import kotlin.test.Test
import kotlin.test.assertEquals

@OptIn(ExperimentalTestApi::class)
class SelectionContainerTest {
@Test
fun selectionWorksWhenDraggingFromBelowText() = runComposeUiTest {
var selection by mutableStateOf<Selection?>(null)
val text = "Line 1\nLine2"
setContent {
SelectionContainer(
modifier = Modifier.size(500.dp).testTag("selection_container"),
selection = selection,
onSelectionChange = {
selection = it
}
) {
BasicText(
text = text,
modifier = Modifier.fillMaxSize()
)
}
}

onNodeWithTag("selection_container").performMouseInput {
dragAndDrop(
start = Offset(250f, 499f),
end = Offset.Zero
)
}

assertEquals(
expected = TextRange(text.length, 0),
actual = selection?.toTextRange()
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import kotlin.test.assertEquals
* is that [PointerIconService] is needed for some of the tests, and it is internal in `ui`.
*/
@OptIn(ExperimentalTestApi::class)
class SelectionContainerTest {
class SelectionContainerUiTest {
@Test
fun selectionContainerSetsTextPointerIcon() = runComposeUiTest {
lateinit var pointerIconService: PointerIconService
Expand Down

0 comments on commit 28cad83

Please sign in to comment.