Skip to content

Commit

Permalink
Fix awaitDragStartOnSlop to detect slop-passing on both axes. (#534)
Browse files Browse the repository at this point in the history
  • Loading branch information
m-sasha authored and igordmn committed Jun 8, 2023
1 parent cf0837a commit 34dae43
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ private suspend fun AwaitPointerEventScope.awaitDragStartOnSlop(initialDown: Poi
do {
drag = awaitPointerSlopOrCancellation(
initialDown.changes[0].id,
initialDown.changes[0].type
initialDown.changes[0].type,
triggerOnMainAxisSlop = false
) { change, over ->
change.consume()
overSlop = over
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,7 @@ import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.dp
import androidx.compose.ui.use
import kotlin.math.ceil
import kotlin.test.Ignore
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue
import kotlin.test.*
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.delay
import kotlinx.coroutines.test.UnconfinedTestDispatcher
Expand Down Expand Up @@ -180,6 +176,69 @@ class DragGestureTest {
}
}

private fun assertDragSucceeds(
density: Density,
startOffset: Offset,
endOffset: Offset
){
ImageComposeScene(
width = 100,
height = 100,
density = density
).use { scene ->

var dragStarted = false
var dragged = false
var dragEnded = false

scene.setContent {
Box(
modifier = Modifier
.size(40.dp, 40.dp)
.onDrag(
enabled = true,
onDragStart = { dragStarted = true },
onDragEnd = { dragEnded = true },
onDrag = { dragged = true }
)
)
}

scene.sendPointerEvent(PointerEventType.Move, startOffset)
scene.sendPointerEvent(PointerEventType.Press, startOffset, button = PointerButton.Primary)
scene.sendPointerEvent(PointerEventType.Move, endOffset)
scene.sendPointerEvent(PointerEventType.Release,endOffset, button = PointerButton.Primary)

assertTrue(dragStarted)
assertTrue(dragged)
assertTrue(dragEnded)
}
}

@Test
fun vertical_drag_passes_slop() {
val density = Density(1f)
val viewConfiguration = DefaultViewConfiguration(density)
val startOffset = Offset(5f, 5f)
assertDragSucceeds(
density = density,
startOffset = startOffset,
endOffset = startOffset + Offset(0f, viewConfiguration.touchSlop + 1f)
)
}

@Test
fun horizontal_drag_passes_slop() {
val density = Density(1f)
val viewConfiguration = DefaultViewConfiguration(density)
val startOffset = Offset(5f, 5f)
assertDragSucceeds(
density = density,
startOffset = startOffset,
endOffset = startOffset + Offset(viewConfiguration.touchSlop + 1f, 0f)
)
}

@Test
fun draggable_by_touch() {
val density = Density(1f)
Expand Down

0 comments on commit 34dae43

Please sign in to comment.