-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2192 from CruGlobal/robolectricComposeWorkaround
Add a robolectric Compose workaround
- Loading branch information
Showing
4 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
...ava/org/ccci/gto/android/common/androidx/compose/ui/platform/AndroidUiDispatcherUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
.../ccci/gto/android/common/androidx/compose/ui/platform/AndroidUiDispatcher+TestFixtures.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |