Skip to content

Commit

Permalink
Merge pull request #2192 from CruGlobal/robolectricComposeWorkaround
Browse files Browse the repository at this point in the history
Add a robolectric Compose workaround
  • Loading branch information
frett authored Oct 11, 2024
2 parents 6761ec6 + 70dcda1 commit edd7671
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
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)
}

0 comments on commit edd7671

Please sign in to comment.