Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a robolectric Compose workaround #2192

Merged
merged 2 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ org.gradle.caching=true
org.gradle.jvmargs=-Xmx4g
org.gradle.parallel=true

android.experimental.enableTestFixturesKotlinSupport=true
android.nonTransitiveRClass=true
android.useAndroidX=true

Expand Down
5 changes: 5 additions & 0 deletions gto-support-androidx-compose/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ plugins {
android {
namespace = "org.ccci.gto.android.common.androidx.compose"
configureCompose(project)
testFixtures.enable = true
}

dependencies {
Expand All @@ -14,4 +15,8 @@ dependencies {
// region Linkify support
implementation(libs.androidx.core)
// endregion Linkify support

testFixturesImplementation(kotlin("test"))
testFixturesImplementation(libs.androidx.compose.ui)
testFixturesImplementation(libs.kotlin.coroutines)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.ccci.gto.android.common.androidx.compose.ui.platform;

public class AndroidUiDispatcherUtil {
/**
* This method is provided to workaround a bug with Kotlin Test Fixtures not being accessible via an external aar
* dependency.
*/
public static void runScheduledDispatches() {
AndroidUiDispatcher_TestFixturesKt.clearAndroidUiDispatcher();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.ccci.gto.android.common.androidx.compose.ui.platform

import androidx.compose.ui.platform.AndroidUiDispatcher
import kotlin.test.assertFalse
import kotlinx.coroutines.CoroutineDispatcher

// HACK: Workaround for https://github.com/robolectric/robolectric/issues/7055#issuecomment-1551119229
@OptIn(ExperimentalStdlibApi::class)
fun clearAndroidUiDispatcher() {
val androidUiDispatcher = AndroidUiDispatcher.Main[CoroutineDispatcher.Key] as AndroidUiDispatcher

val dispatchCallback by lazy {
androidUiDispatcher.javaClass.getDeclaredField("dispatchCallback")
.apply { isAccessible = true }
.get(androidUiDispatcher) as Runnable
}

var scheduledFrameDispatch = false
var scheduledTrampolineDispatch = false
for (i in 0 until 5) {
scheduledFrameDispatch = androidUiDispatcher.javaClass.getDeclaredField("scheduledFrameDispatch")
.apply { isAccessible = true }
.getBoolean(androidUiDispatcher)
scheduledTrampolineDispatch = androidUiDispatcher.javaClass.getDeclaredField("scheduledTrampolineDispatch")
.apply { isAccessible = true }
.getBoolean(androidUiDispatcher)
if (!scheduledFrameDispatch && !scheduledTrampolineDispatch) break

dispatchCallback.run()
}

assertFalse(scheduledFrameDispatch)
assertFalse(scheduledTrampolineDispatch)
}