Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package androidx.compose.ui.interaction
package androidx.compose.ui.interaction.swipeback

import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.gestures.draggable
Expand All @@ -28,17 +28,17 @@ import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.background
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.test.UIKitInstrumentedTest
import androidx.compose.ui.test.findNodeWithTag
import androidx.compose.ui.test.findNodeWithTagOrNull
import androidx.compose.ui.test.runUIKitInstrumentedTest
import androidx.compose.ui.test.utils.hold
import androidx.compose.ui.test.utils.leftCenter
import androidx.compose.ui.test.utils.offsetBy
import androidx.compose.ui.test.utils.rightCenter
import androidx.compose.ui.test.utils.up
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import androidx.compose.ui.window.DialogProperties
import androidx.navigationevent.NavigationEventInfo
import androidx.navigationevent.NavigationEventTransitionState
import androidx.navigationevent.NavigationEventTransitionState.InProgress
Expand All @@ -48,116 +48,159 @@ import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse

internal class SwipeBackInHostingViewTest : SwipeBackTest(
internal class DialogSwipeBackInHostingViewTest : DialogSwipeBackTest(
runUIKitInstrumentedTest = { runUIKitInstrumentedTest(useHostingView = true, it) }
)

internal class SwipeBackInHostingViewControllerTest : SwipeBackTest(
internal class DialogSwipeBackInHostingViewControllerTest : DialogSwipeBackTest(
runUIKitInstrumentedTest = { runUIKitInstrumentedTest(useHostingView = false, it) }
)

internal abstract class SwipeBackTest(
internal abstract class DialogSwipeBackTest(
private val runUIKitInstrumentedTest: (UIKitInstrumentedTest.() -> Unit) -> Unit
) {
@Test
fun edgeBackSwipeDoesNotDispatchHorizontalDragToCompose() = runUIKitInstrumentedTest {
fun testEdgeBackSwipeOverDialogDoesNotDispatchHorizontalDragToCompose() = runUIKitInstrumentedTest {
var dragDistance = Float.NaN
var transitionState: NavigationEventTransitionState = NavigationEventTransitionState.Idle
var backCompletedCount = -1

setContent {
TestContent(
DialogBackGestureContent(
onDragDistanceChanged = { dragDistance = it },
onTransitionStateChanged = { transitionState = it },
onBackCompletedCountChanged = { backCompletedCount = it }
)
}

waitUntil("drag surface should be ready") {
findNodeWithTagOrNull(DRAG_SURFACE) != null &&
!dragDistance.isNaN() &&
backCompletedCount == 0
}
waitUntilReady { !dragDistance.isNaN() && backCompletedCount == 0 }

val backSwipe = swipeRightFromEdge().hold()

waitUntil("back swipe should be in progress") {
transitionState is InProgress
}
waitForIdle()

assertFalse(
transitionState is InProgress,
"Edge swipe over Dialog should not start root back navigation"
)
assertEquals(
expected = 0f,
actual = dragDistance,
absoluteTolerance = 0.01f,
message = "Edge back swipe should not dispatch horizontal drag deltas to Compose"
message = "Edge back swipe over Dialog should not dispatch horizontal drag deltas to Compose"
)
assertEquals(
expected = 0,
actual = backCompletedCount,
message = "Back gesture should not complete before release"
message = "Back gesture over Dialog should not complete before release"
)

backSwipe.up()

waitUntil("back swipe should complete after release") {
backCompletedCount == 1
}
waitForIdle()

assertFalse(
transitionState is InProgress,
"Releasing edge swipe over Dialog should still not start root back navigation"
)
assertEquals(
expected = 0,
actual = backCompletedCount,
message = "Edge swipe over Dialog should not complete root back navigation"
)
}

@Test
fun innerSwipeDispatchesHorizontalDragWithoutStartingBack() = runUIKitInstrumentedTest {
fun testInnerSwipeOverDialogDispatchesHorizontalDragWithoutStartingBack() = runUIKitInstrumentedTest {
var dragDistance = Float.NaN
var transitionState: NavigationEventTransitionState = NavigationEventTransitionState.Idle
var backCompletedCount = -1

setContent {
TestContent(
DialogBackGestureContent(
onDragDistanceChanged = { dragDistance = it },
onTransitionStateChanged = { transitionState = it },
onBackCompletedCountChanged = { backCompletedCount = it }
)
}

waitUntil("drag surface should be ready") {
findNodeWithTagOrNull(DRAG_SURFACE) != null &&
!dragDistance.isNaN() &&
backCompletedCount == 0
}
waitUntilReady { !dragDistance.isNaN() && backCompletedCount == 0 }

findNodeWithTag(DRAG_SURFACE).swipe(
fromPosition = { leftCenter().offsetBy(dx = 16.dp) },
toPosition = { rightCenter().offsetBy(dx = (-16).dp) }
)
findNodeWithTag(OVERLAY_SURFACE).swipeRight()

waitUntil("inner swipe should dispatch drag deltas to Compose") {
waitUntil("Inner swipe should dispatch drag deltas over Dialog") {
dragDistance > 0f
}

assertFalse(
transitionState is InProgress,
"Inner swipe should not start back navigation"
"Inner swipe over Dialog should not start back navigation"
)
assertEquals(
expected = 0,
actual = backCompletedCount,
message = "Inner swipe should not complete back navigation"
message = "Inner swipe over Dialog should not complete back navigation"
)
}
}

@Composable
private fun TestContent(
private fun DialogBackGestureContent(
onDragDistanceChanged: (Float) -> Unit,
onTransitionStateChanged: (NavigationEventTransitionState) -> Unit,
onBackCompletedCountChanged: (Int) -> Unit,
) {
BackGestureHost(
onTransitionStateChanged = onTransitionStateChanged,
onBackCompletedCountChanged = onBackCompletedCountChanged
) {
Dialog(
onDismissRequest = {},
properties = DialogProperties(
dismissOnBackPress = false,
usePlatformDefaultWidth = false,
usePlatformInsets = false
)
) {
DraggableSurface(onDragDistanceChanged = onDragDistanceChanged)
}
}
}

@Composable
private fun DraggableSurface(
onDragDistanceChanged: (Float) -> Unit,
) {
var dragDistance by remember { mutableFloatStateOf(0f) }

onDragDistanceChanged(dragDistance)

Box(
modifier = Modifier
.background(Color.Red)
.fillMaxSize()
.testTag(OVERLAY_SURFACE)
.draggable(
state = rememberDraggableState { delta ->
dragDistance += delta
},
orientation = Orientation.Horizontal,
)
)
}

@Composable
private fun BackGestureHost(
onTransitionStateChanged: (NavigationEventTransitionState) -> Unit,
onBackCompletedCountChanged: (Int) -> Unit,
content: @Composable () -> Unit,
) {
var backCompletedCount by remember { mutableIntStateOf(0) }
val navigationEventState = rememberNavigationEventState<NavigationEventInfo>(
currentInfo = NavigationEventInfo.None,
backInfo = listOf<NavigationEventInfo>(NavigationEventInfo.None)
backInfo = listOf(NavigationEventInfo.None)
)

onDragDistanceChanged(dragDistance)
onTransitionStateChanged(navigationEventState.transitionState)
onBackCompletedCountChanged(backCompletedCount)

Expand All @@ -168,17 +211,17 @@ private fun TestContent(
}
)

Box(
modifier = Modifier
.fillMaxSize()
.testTag(DRAG_SURFACE)
.draggable(
state = rememberDraggableState { delta ->
dragDistance += delta
},
orientation = Orientation.Horizontal,
)
)
Box(modifier = Modifier.fillMaxSize()) {
content()
}
}

private fun UIKitInstrumentedTest.waitUntilReady(
otherConditions: () -> Boolean,
) {
waitUntil("$OVERLAY_SURFACE should be ready") {
findNodeWithTagOrNull(OVERLAY_SURFACE) != null && otherConditions()
}
}

private const val DRAG_SURFACE = "dragSurface"
private const val OVERLAY_SURFACE = "overlaySurface"
Loading
Loading