Skip to content

Commit

Permalink
Fix closing scene during scroll animation (#1096)
Browse files Browse the repository at this point in the history
## Proposed Changes

- A part from #1055 with cancellation fix

## Testing

Test: run a new unit test
  • Loading branch information
MatkovIvan committed Feb 12, 2024
1 parent 0ab187d commit 9267e3a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ private class AnimatedMouseWheelScrollPhysics(
var requiredAnimation = true
var lastValue = 0f
val anim = AnimationState(0f)
while (requiredAnimation) {
while (requiredAnimation && coroutineContext.isActive) {
requiredAnimation = false
val durationMillis = (abs(target - anim.value) / speed)
.roundToInt()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,34 @@
package androidx.compose.foundation.gestures

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.foundation.v2.runBlockingIfPossible
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.runtime.withFrameNanos
import androidx.compose.ui.InternalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.input.pointer.PointerEventType
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.test.ExperimentalTestApi
import androidx.compose.ui.test.ScrollWheel
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.performMouseInput
import androidx.compose.ui.test.performTouchInput
import androidx.compose.ui.test.runComposeUiTest
import androidx.compose.ui.test.runSkikoComposeUiTest
import androidx.compose.ui.test.swipe
import androidx.compose.ui.unit.dp
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.jetbrains.skiko.KotlinBackend
import org.jetbrains.skiko.OS
import org.jetbrains.skiko.hostOs
Expand Down Expand Up @@ -132,4 +133,27 @@ class SkikoScrollableTest {
}
runOnIdle { assertTrue(state.firstVisibleItemIndex > 0) }
}

@OptIn(InternalComposeUiApi::class)
@Test
fun closeSceneDuringScrollAnimation() = runSkikoComposeUiTest(
size = Size(100f, 100f),
) {
setContent {
val state = rememberScrollState()
Column(Modifier.size(10.dp).verticalScroll(state)) {
Box(Modifier.size(10.dp, 210.dp))
}
}
scene.sendPointerEvent(
eventType = PointerEventType.Scroll,
position = Offset(0f, 0f),
scrollDelta = Offset(0f, 10000f)
)
/*
* This test doesn't contain any real assertions - its primary function is to ensure
* it doesn't freeze. This was an issue previously, as inside the scroll animation, there
* was a handler for the CancellationException that used to stop the scene from closing.
*/
}
}

0 comments on commit 9267e3a

Please sign in to comment.