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

Commit 0a6de0a0 authored by Ale Nijamkin's avatar Ale Nijamkin Committed by Android (Google) Code Review
Browse files

Merge "Kosmos utility for making Snapshot state unconfined." into main

parents 4dbe8762 98006509
Loading
Loading
Loading
Loading
+18 −0
Original line number Diff line number Diff line
package com.android.systemui.kosmos

import androidx.compose.runtime.snapshots.Snapshot
import com.android.systemui.SysuiTestCase
import com.android.systemui.coroutines.FlowValue
import com.android.systemui.coroutines.collectLastValue
@@ -57,6 +58,23 @@ var Kosmos.brightnessWarningToast: BrightnessWarningToast by
fun Kosmos.runTest(testBody: suspend Kosmos.() -> Unit) =
    testScope.runTest testBody@{ this@runTest.testBody() }

/**
 * Runs the given [Kosmos]-scoped test [block] in an environment where compose snapshot state is
 * settled eagerly. This is the compose equivalent to using an [UnconfinedTestDispatcher] or using
 * [runCurrent] a lot.
 *
 * Note that this shouldn't be needed or used in a compose test environment.
 */
fun Kosmos.runTestWithSnapshots(block: suspend Kosmos.() -> Unit) {
    val handle = Snapshot.registerGlobalWriteObserver { Snapshot.sendApplyNotifications() }

    try {
        testScope.runTest { block() }
    } finally {
        handle.dispose()
    }
}

fun Kosmos.runCurrent() = testScope.runCurrent()

fun <T> Kosmos.collectLastValue(flow: Flow<T>) = testScope.collectLastValue(flow)