Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0f06ee2d authored by omarmt's avatar omarmt
Browse files

Improve SceneGestureHandlerTest tests to support suspended methods calls

We need to refactor the SceneGestureHandler class (see ag/25757212) to
prevent multiple jobs from running concurrently and to enabling the
onDrag() and onDragStopped() methods to return the amount of
offset/velocity consumed.

This CL updates the tests to support both implementations.

Test: atest SceneGestureHandlerTest
Bug: 317063114
Flag: NA
Change-Id: I1662e59f94925199b4f157fc34e4b7060f11874b
parent 72e885eb
Loading
Loading
Loading
Loading
+194 −125

File changed.

Preview size limit exceeded, changes collapsed.

+31 −7
Original line number Diff line number Diff line
@@ -3,7 +3,9 @@ package com.android.compose.test
import androidx.compose.ui.test.ExperimentalTestApi
import androidx.compose.ui.test.TestMonotonicFrameClock
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.TestCoroutineScheduler
import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.withContext

@@ -12,16 +14,38 @@ import kotlinx.coroutines.withContext
 * function.
 *
 * The [TestCoroutineScheduler] is passed to provide the functionality to wait for idle.
 *
 * Note: Please refer to the documentation for [runTest], as this feature utilizes it. This will
 * provide a comprehensive understanding of all its behaviors.
 */
@OptIn(ExperimentalTestApi::class)
@OptIn(ExperimentalTestApi::class, ExperimentalCoroutinesApi::class)
fun runMonotonicClockTest(block: suspend MonotonicClockTestScope.() -> Unit) = runTest {
    // We need a CoroutineScope (like a TestScope) to create a TestMonotonicFrameClock.
    withContext(TestMonotonicFrameClock(this)) {
        MonotonicClockTestScope(coroutineScope = this, testScheduler = testScheduler).block()
    val testScope: TestScope = this

    withContext(TestMonotonicFrameClock(coroutineScope = testScope)) {
        val testScopeWithMonotonicFrameClock: CoroutineScope = this

        val scope =
            MonotonicClockTestScope(
                testScope = testScopeWithMonotonicFrameClock,
                testScheduler = testScope.testScheduler,
                backgroundScope = backgroundScope,
            )

        // Run the test
        scope.block()
    }
}

/**
 * A coroutine scope that for launching test coroutines for Compose.
 *
 * @param testScheduler The delay-skipping scheduler used by the test dispatchers running the code
 *   in this scope (see [TestScope.testScheduler]).
 * @param backgroundScope A scope for background work (see [TestScope.backgroundScope]).
 */
class MonotonicClockTestScope(
    coroutineScope: CoroutineScope,
    val testScheduler: TestCoroutineScheduler
) : CoroutineScope by coroutineScope
    testScope: CoroutineScope,
    val testScheduler: TestCoroutineScheduler,
    val backgroundScope: CoroutineScope,
) : CoroutineScope by testScope