From d9ff564f597140bb8a14be3782c02154d82e4c99 Mon Sep 17 00:00:00 2001 From: Daniel Frett Date: Fri, 11 Oct 2024 14:15:34 -0600 Subject: [PATCH] switch to common test util to clear pending dispatches --- app/build.gradle.kts | 1 + .../test/kotlin/org/cru/godtools/TestUtils.kt | 44 ------------------- .../delete/DeleteAccountPresenterTest.kt | 4 +- .../ui/dashboard/tools/ToolsPresenterTest.kt | 4 +- .../ui/drawer/DrawerMenuPresenterTest.kt | 4 +- .../languages/app/AppLanguagePresenterTest.kt | 4 +- .../tooldetails/ToolDetailsPresenterTest.kt | 4 +- .../ui/tools/ToolCardPresenterTest.kt | 4 +- gradle/libs.versions.toml | 2 +- 9 files changed, 14 insertions(+), 57 deletions(-) delete mode 100644 app/src/test/kotlin/org/cru/godtools/TestUtils.kt diff --git a/app/build.gradle.kts b/app/build.gradle.kts index f92a850628..dfbede7ad8 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -206,6 +206,7 @@ dependencies { ksp(libs.dagger.compiler) ksp(libs.hilt.compiler) + testApi(testFixtures(libs.gtoSupport.androidx.compose)) testImplementation(libs.androidx.arch.core.testing) testImplementation(libs.androidx.lifecycle.runtime.testing) testImplementation(libs.androidx.test.espresso.core) diff --git a/app/src/test/kotlin/org/cru/godtools/TestUtils.kt b/app/src/test/kotlin/org/cru/godtools/TestUtils.kt deleted file mode 100644 index 6ad1df8a28..0000000000 --- a/app/src/test/kotlin/org/cru/godtools/TestUtils.kt +++ /dev/null @@ -1,44 +0,0 @@ -package org.cru.godtools - -import kotlin.coroutines.CoroutineContext -import kotlin.test.assertFalse - -object TestUtils { - // HACK: Workaround for https://github.com/robolectric/robolectric/issues/7055#issuecomment-1551119229 - fun clearAndroidUiDispatcher(pkg: String = "androidx.compose.ui.platform") { - val clazz = javaClass.classLoader!!.loadClass("$pkg.AndroidUiDispatcher") - val combinedContextClass = javaClass.classLoader!!.loadClass("kotlin.coroutines.CombinedContext") - val companionClazz = clazz.getDeclaredField("Companion").get(clazz) - val combinedContext = companionClazz.javaClass.getDeclaredMethod("getMain") - .invoke(companionClazz) as CoroutineContext - - val androidUiDispatcher = combinedContextClass.getDeclaredField("element") - .apply { isAccessible = true } - .get(combinedContext) - .let { clazz.cast(it) } - - var scheduledFrameDispatch = clazz.getDeclaredField("scheduledFrameDispatch") - .apply { isAccessible = true } - .getBoolean(androidUiDispatcher) - var scheduledTrampolineDispatch = clazz.getDeclaredField("scheduledTrampolineDispatch") - .apply { isAccessible = true } - .getBoolean(androidUiDispatcher) - - val dispatchCallback = clazz.getDeclaredField("dispatchCallback") - .apply { isAccessible = true } - .get(androidUiDispatcher) as Runnable - - if (scheduledFrameDispatch || scheduledTrampolineDispatch) { - dispatchCallback.run() - scheduledFrameDispatch = clazz.getDeclaredField("scheduledFrameDispatch") - .apply { isAccessible = true } - .getBoolean(androidUiDispatcher) - scheduledTrampolineDispatch = clazz.getDeclaredField("scheduledTrampolineDispatch") - .apply { isAccessible = true } - .getBoolean(androidUiDispatcher) - } - - assertFalse(scheduledFrameDispatch) - assertFalse(scheduledTrampolineDispatch) - } -} diff --git a/app/src/testDebug/kotlin/org/cru/godtools/ui/account/delete/DeleteAccountPresenterTest.kt b/app/src/testDebug/kotlin/org/cru/godtools/ui/account/delete/DeleteAccountPresenterTest.kt index 366912ff75..8a23a08c59 100644 --- a/app/src/testDebug/kotlin/org/cru/godtools/ui/account/delete/DeleteAccountPresenterTest.kt +++ b/app/src/testDebug/kotlin/org/cru/godtools/ui/account/delete/DeleteAccountPresenterTest.kt @@ -14,7 +14,7 @@ import kotlin.test.AfterTest import kotlin.test.Test import kotlin.test.assertIs import kotlinx.coroutines.test.runTest -import org.cru.godtools.TestUtils.clearAndroidUiDispatcher +import org.ccci.gto.android.common.androidx.compose.ui.platform.AndroidUiDispatcherUtil import org.cru.godtools.account.GodToolsAccountManager import org.cru.godtools.ui.account.delete.DeleteAccountScreen.Event import org.cru.godtools.ui.account.delete.DeleteAccountScreen.State @@ -34,7 +34,7 @@ class DeleteAccountPresenterTest { private val presenter = DeleteAccountPresenter(navigator, accountManager) @AfterTest - fun cleanup() = clearAndroidUiDispatcher() + fun cleanup() = AndroidUiDispatcherUtil.runScheduledDispatches() @Test fun `Delete Account - succeeds`() = runTest { diff --git a/app/src/testDebug/kotlin/org/cru/godtools/ui/dashboard/tools/ToolsPresenterTest.kt b/app/src/testDebug/kotlin/org/cru/godtools/ui/dashboard/tools/ToolsPresenterTest.kt index 49400b623d..a58867d371 100644 --- a/app/src/testDebug/kotlin/org/cru/godtools/ui/dashboard/tools/ToolsPresenterTest.kt +++ b/app/src/testDebug/kotlin/org/cru/godtools/ui/dashboard/tools/ToolsPresenterTest.kt @@ -23,7 +23,7 @@ import kotlin.test.assertTrue import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.flowOf import kotlinx.coroutines.test.runTest -import org.cru.godtools.TestUtils.clearAndroidUiDispatcher +import org.ccci.gto.android.common.androidx.compose.ui.platform.AndroidUiDispatcherUtil import org.cru.godtools.base.Settings import org.cru.godtools.db.repository.LanguagesRepository import org.cru.godtools.db.repository.ToolsRepository @@ -101,7 +101,7 @@ class ToolsPresenterTest { } @AfterTest - fun cleanup() = clearAndroidUiDispatcher() + fun cleanup() = AndroidUiDispatcherUtil.runScheduledDispatches() // region State.banner @Test diff --git a/app/src/testDebug/kotlin/org/cru/godtools/ui/drawer/DrawerMenuPresenterTest.kt b/app/src/testDebug/kotlin/org/cru/godtools/ui/drawer/DrawerMenuPresenterTest.kt index 5529968d10..dfeefc3855 100644 --- a/app/src/testDebug/kotlin/org/cru/godtools/ui/drawer/DrawerMenuPresenterTest.kt +++ b/app/src/testDebug/kotlin/org/cru/godtools/ui/drawer/DrawerMenuPresenterTest.kt @@ -15,7 +15,7 @@ import kotlin.test.assertFalse import kotlin.test.assertTrue import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.test.runTest -import org.cru.godtools.TestUtils.clearAndroidUiDispatcher +import org.ccci.gto.android.common.androidx.compose.ui.platform.AndroidUiDispatcherUtil import org.cru.godtools.account.GodToolsAccountManager import org.junit.runner.RunWith import org.robolectric.annotation.Config @@ -34,7 +34,7 @@ class DrawerMenuPresenterTest { ) @AfterTest - fun cleanup() = clearAndroidUiDispatcher() + fun cleanup() = AndroidUiDispatcherUtil.runScheduledDispatches() @Test fun `State - isLoggedIn`() = runTest { diff --git a/app/src/testDebug/kotlin/org/cru/godtools/ui/languages/app/AppLanguagePresenterTest.kt b/app/src/testDebug/kotlin/org/cru/godtools/ui/languages/app/AppLanguagePresenterTest.kt index e99c5b5d90..9237b2e4cd 100644 --- a/app/src/testDebug/kotlin/org/cru/godtools/ui/languages/app/AppLanguagePresenterTest.kt +++ b/app/src/testDebug/kotlin/org/cru/godtools/ui/languages/app/AppLanguagePresenterTest.kt @@ -22,8 +22,8 @@ import kotlin.test.assertNotEquals import kotlin.test.assertNotNull import kotlin.test.assertNull import kotlinx.coroutines.test.runTest +import org.ccci.gto.android.common.androidx.compose.ui.platform.AndroidUiDispatcherUtil import org.ccci.gto.android.common.androidx.core.app.LocaleConfigCompat -import org.cru.godtools.TestUtils.clearAndroidUiDispatcher import org.cru.godtools.base.Settings import org.junit.runner.RunWith import org.robolectric.annotation.Config @@ -55,7 +55,7 @@ class AppLanguagePresenterTest { @AfterTest fun cleanup() { - clearAndroidUiDispatcher() + AndroidUiDispatcherUtil.runScheduledDispatches() unmockkObject(LocaleConfigCompat) } diff --git a/app/src/testDebug/kotlin/org/cru/godtools/ui/tooldetails/ToolDetailsPresenterTest.kt b/app/src/testDebug/kotlin/org/cru/godtools/ui/tooldetails/ToolDetailsPresenterTest.kt index e1b2416ed2..ad5f97df92 100644 --- a/app/src/testDebug/kotlin/org/cru/godtools/ui/tooldetails/ToolDetailsPresenterTest.kt +++ b/app/src/testDebug/kotlin/org/cru/godtools/ui/tooldetails/ToolDetailsPresenterTest.kt @@ -38,8 +38,8 @@ import kotlin.test.assertTrue import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.flowOf import kotlinx.coroutines.test.runTest +import org.ccci.gto.android.common.androidx.compose.ui.platform.AndroidUiDispatcherUtil import org.ccci.gto.android.common.util.content.equalsIntent -import org.cru.godtools.TestUtils.clearAndroidUiDispatcher import org.cru.godtools.analytics.model.OpenAnalyticsActionEvent import org.cru.godtools.analytics.model.OpenAnalyticsActionEvent.Companion.ACTION_OPEN_TOOL import org.cru.godtools.analytics.model.OpenAnalyticsActionEvent.Companion.SOURCE_TOOL_DETAILS @@ -163,7 +163,7 @@ class ToolDetailsPresenterTest { @AfterTest fun cleanup() { unmockkStatic("org.cru.godtools.downloadmanager.compose.DownloadLatestTranslationKt") - clearAndroidUiDispatcher() + AndroidUiDispatcherUtil.runScheduledDispatches() } // region State.tool diff --git a/app/src/testDebug/kotlin/org/cru/godtools/ui/tools/ToolCardPresenterTest.kt b/app/src/testDebug/kotlin/org/cru/godtools/ui/tools/ToolCardPresenterTest.kt index 6cdf9f7447..722be89bbc 100644 --- a/app/src/testDebug/kotlin/org/cru/godtools/ui/tools/ToolCardPresenterTest.kt +++ b/app/src/testDebug/kotlin/org/cru/godtools/ui/tools/ToolCardPresenterTest.kt @@ -27,7 +27,7 @@ import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.flowOf import kotlinx.coroutines.test.runTest -import org.cru.godtools.TestUtils.clearAndroidUiDispatcher +import org.ccci.gto.android.common.androidx.compose.ui.platform.AndroidUiDispatcherUtil import org.cru.godtools.base.Settings import org.cru.godtools.base.ToolFileSystem import org.cru.godtools.db.repository.AttachmentsRepository @@ -86,7 +86,7 @@ class ToolCardPresenterTest { ) @AfterTest - fun cleanup() = clearAndroidUiDispatcher() + fun cleanup() = AndroidUiDispatcherUtil.runScheduledDispatches() // region ToolCard.State.tool @Test diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index afd8bdffd0..387efb9926 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -21,7 +21,7 @@ firebase-crashlytics = "19.2.0" firebase-perf = "21.0.1" godtoolsShared = "1.0.1" google-auto-value = "1.11.0" -gtoSupport = "4.2.2" +gtoSupport = "4.2.3-SNAPSHOT" kotlin = "2.0.21" kotlinCoroutines = "1.9.0" kotlinKover = "0.7.6"