diff options
| author | 2024-07-23 13:46:47 +0200 | |
|---|---|---|
| committer | 2024-07-23 13:52:13 +0200 | |
| commit | db6ed76fda3714019aa78b6ffb52a8d769f3e75a (patch) | |
| tree | ddb1cbbaa1f01a01703dd9fe4612e947d910d338 | |
| parent | 158f837e06038488f48bc8d2b0b5e2cd36ff8cb8 (diff) | |
Add SysuiTestCase.runOnMainThreadAndWaitForIdleSync()
This CL introduces a runOnMainThreadAndWaitForIdleSync() utility to
easily post work in the main thread and wait for its completion in a
test. This should make it easier to work with code that has to run on
the main thread, like dialogs.
Bug: 346519570
Test: None, this is a test utility
Flag: EXEMPT test utility
Change-Id: I93790fb9a729d273b3806db9637d9a9af94d966f
| -rw-r--r-- | packages/SystemUI/tests/utils/src/com/android/systemui/SysuiTestCaseExt.kt | 8 |
1 files changed, 8 insertions, 0 deletions
diff --git a/packages/SystemUI/tests/utils/src/com/android/systemui/SysuiTestCaseExt.kt b/packages/SystemUI/tests/utils/src/com/android/systemui/SysuiTestCaseExt.kt index 46259a6964cd..d3dccb021ff8 100644 --- a/packages/SystemUI/tests/utils/src/com/android/systemui/SysuiTestCaseExt.kt +++ b/packages/SystemUI/tests/utils/src/com/android/systemui/SysuiTestCaseExt.kt @@ -20,3 +20,11 @@ import com.android.systemui.kosmos.Kosmos import com.android.systemui.kosmos.testCase fun SysuiTestCase.testKosmos(): Kosmos = Kosmos().apply { testCase = this@testKosmos } + +/** Run [f] on the main thread and return its result once completed. */ +fun <T : Any> SysuiTestCase.runOnMainThreadAndWaitForIdleSync(f: () -> T): T { + lateinit var result: T + context.mainExecutor.execute { result = f() } + waitForIdleSync() + return result +} |