package com.vayunmathur.testsupport import android.os.Looper import androidx.compose.ui.test.junit4.ComposeContentTestRule import kotlin.time.Duration.Companion.seconds import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.first import kotlinx.coroutines.runBlocking import org.robolectric.Shadows /** * Shared deterministic waits for JVM/Robolectric tests. * * This is the repo's one shared test-await helper. It lives in * `build-logic/shared-test/java`, which both convention plugins * (`common-conventions-app` and `common-conventions-library`) add to every * module's unit-test source set — the same mechanism that already shares * `build-logic/shared-test-resources/robolectric.properties`. There is no * shared test-util Gradle module in this repo, and none is invented here. * * Every wait below is a wait *on a signal*, never on a wall clock: * - [awaitValue] suspends until a [Flow] emits a value matching a predicate * (a StateFlow terminal state, a Room/DataStore flow, a completion flow). * - [awaitWithMainIdle] pumps Robolectric's paused main looper until a * terminal condition holds. Idling the looper is what lets a coroutine * resumed on Main actually run; sleeping would only bet on load. * - [awaitSemantics] is a Compose semantics wait on a terminal node state, * pumping the looper *and* the composition's test scheduler on every * check for the same reason (see its doc). * - [pumpUntil] is the step-bounded pumped wait the per-class copies used * for ViewModel/DataStore terminal states, now shared verbatim. * * None of these sleep, burn CPU, or read `System.currentTimeMillis`. */ /** Runs every task currently queued on Robolectric's paused main looper. */ fun idleMainLooper() { // At @BeforeClass time (before the Robolectric sandbox installs its // main looper) there is nothing to idle yet; class-level error drains // call this from there, so a missing looper is a no-op, not a crash. val looper = Looper.getMainLooper() ?: return Shadows.shadowOf(looper).idle() } /** * Suspends until [this] flow emits a value matching [predicate], and returns * that value. For a [kotlinx.coroutines.flow.StateFlow] this is a wait for a * terminal state: the caller is resumed by the emission itself, not by a poll. */ suspend fun Flow.awaitValue(predicate: (T) -> Boolean): T = first(predicate) /** * Blocking form of [awaitValue] for plain (non-coroutine) JUnit tests. * Safe when the flow's producers run on IO/Default dispatchers or on an * unconfined Main dispatcher; do not use it for a value whose only producer * is queued on Robolectric's paused main looper — use [awaitWithMainIdle]. */ fun awaitValueBlocking(flow: Flow, predicate: (T) -> Boolean): T = runBlocking { flow.awaitValue(predicate) } /** * Idles the main looper until [condition] holds, then returns. * * Step-counted as a failure bound only: each step is a full looper idle, * which is the work that makes progress possible, and the thread parks * briefly between steps so IO/Default producers on other threads can run. * (A bare yield is not enough in the Robolectric sandbox: a tight * idle+yield loop kept the test thread runnable and an IO search behind * it never landed in 10,000 steps, while parking let it finish in ~10.) * This replaces `Thread.sleep` + `System.currentTimeMillis` deadline loops, * which under Robolectric measured a substituted clock and gave up before * real IO had landed. */ fun awaitWithMainIdle(description: String, maxSteps: Int = 10_000, condition: () -> Boolean) { var step = 0 while (step++ < maxSteps) { idleMainLooper() if (condition()) return java.util.concurrent.locks.LockSupport.parkNanos(1_000_000) } // One last idle + check so the failure reports the settled state. idleMainLooper() check(condition()) { "timed out (step-bounded, no wall clock): $description after $maxSteps looper idles" } } /** * Compose semantics wait on a terminal state: [ComposeContentTestRule.waitUntil] * re-checks [condition] after every idle, and each check first pumps the * main looper so a parse/load resumed on Main can land and compose. * The timeout is the framework's failure bound, not the mechanism. * * The pump has three parts, and all are needed. Idling Robolectric's paused * looper runs work posted to the Android main thread. But the composition's * own coroutines — the recomposer and its [androidx.compose.runtime.LaunchedEffect]s * — live on the Compose test environment's scheduler (the one behind * [androidx.compose.ui.test.MainTestClock]), and `waitUntil` only *advances * that scheduler's frame clock* between checks; it never *runs* the tasks * queued on the scheduler. A state write landing from an IO thread therefore * invalidated the tree (the scaffold title flipped to the parsed subject) * while the recomposition that would compose the new branch sat queued past * the whole 15s wait, and a debounced autosave's timer sat in the * scheduler's future. So each check also (a) runs the scheduler's due tasks * and (b) steps its virtual clock forward a bounded window, executing the * timers and delayed resumes queued there. Stepping virtual time is the * mechanism, not a timing bet: it runs the actual queued work. */ fun ComposeContentTestRule.awaitSemantics(description: String, condition: () -> Boolean) { waitUntil(15_000) { idleMainLooper() mainClock.scheduler.runCurrent() mainClock.scheduler.advanceTimeBy(SCHEDULER_ADVANCE_SECONDS.seconds) idleMainLooper() condition() } idleMainLooper() // Bounded settle, never advanceUntilIdle(): a composition can host an // infinitely self-rescheduling scheduler task (a focused text field's // cursor blink, an infinite animation), and advancing until idle then // never returns — the test JVM spins at 100% CPU and its whole // forkEvery batch (and the Gradle task) never completes. Stepping the // same bounded window as the wait above runs every timer that could // still be queued without betting on the scheduler ever going idle. mainClock.scheduler.advanceTimeBy(SCHEDULER_ADVANCE_SECONDS.seconds) idleMainLooper() check(condition()) { "terminal semantics state not reached: $description" } } /** * Virtual seconds [awaitSemantics] steps the composition scheduler per * check: past an 800ms debounce or a sharing timeout (5s), far short of any * point where virtual time itself becomes the thing being tested. */ private const val SCHEDULER_ADVANCE_SECONDS = 60 /** * Step-bounded pumped wait on a terminal [condition]: every step pumps * the paused main looper (running the ViewModel/DataStore work queued * there, including delayed tasks), then parks briefly so * Dispatchers.IO/Default producers get a CPU slice on a contended box. * The bound is pumping steps, never wall-clock seconds that CI contention * can burn before any work runs. This is the shared form of the identical * `pumpUntil` copies that used to live in individual test classes. * * With `stepMs > 0` each step also advances the looper's clock by that * many milliseconds, running delayed work due in that window; classes * whose delayed work must not be driven early pass `stepMs = 0`, which * idles without advancing the clock. Pump first, then check: work that is * already queued still needs that one pump to settle before the condition * behind it can hold. */ fun pumpUntil(description: String, stepMs: Long = 200L, condition: () -> Boolean) { var steps = 0 while (true) { // Pump first, then check: the loops this replaces always idled at // least once before judging the condition, and a condition that is // already true at entry can still need that one pump for the work // behind it to settle. val looper = Looper.getMainLooper() if (looper != null) { val shadow = Shadows.shadowOf(looper) if (stepMs > 0L) { shadow.idleFor(stepMs, java.util.concurrent.TimeUnit.MILLISECONDS) } else { shadow.idle() } } if (condition()) break java.util.concurrent.locks.LockSupport.parkNanos( java.util.concurrent.TimeUnit.MILLISECONDS.toNanos(PUMP_PARK_MS), ) if (++steps >= MAX_PUMP_STEPS) { throw AssertionError( "timed out waiting for: $description", AssertionError("timed out (step-bounded): $description after $MAX_PUMP_STEPS pumps"), ) } } } /** Park between [pumpUntil] steps so IO/Default producers get a CPU slice. */ private const val PUMP_PARK_MS = 50L /** Step bound for [pumpUntil]: steps are looper pumps, never wall-clock time. */ private const val MAX_PUMP_STEPS = 6_000