Created
November 2, 2021 06:29
-
-
Save mrmike/3c4f4785dfb7477228d2dc9b872715ae to your computer and use it in GitHub Desktop.
Create Android Compose Rule using Intent
This file contains hidden or 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
| import android.content.Intent | |
| import androidx.activity.ComponentActivity | |
| import androidx.compose.ui.test.junit4.AndroidComposeTestRule | |
| import androidx.test.ext.junit.rules.ActivityScenarioRule | |
| inline fun <reified A : ComponentActivity> createAndroidComposeRule(intent: Intent): AndroidComposeTestRule<ActivityScenarioRule<A>, A> { | |
| return AndroidComposeTestRule( | |
| activityRule = ActivityScenarioRule<A>(intent), | |
| activityProvider = { scenario -> scenario.getActivity() } | |
| ) | |
| } | |
| fun <A : ComponentActivity> ActivityScenarioRule<A>.getActivity(): A { | |
| var activity: A? = null | |
| scenario.onActivity { activity = it } | |
| if (activity == null) { | |
| throw IllegalStateException("Activity was not set in the ActivityScenarioRule!") | |
| } | |
| return activity!! | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment