Skip to content

Instantly share code, notes, and snippets.

@mrmike
Created November 2, 2021 06:29
Show Gist options
  • Select an option

  • Save mrmike/3c4f4785dfb7477228d2dc9b872715ae to your computer and use it in GitHub Desktop.

Select an option

Save mrmike/3c4f4785dfb7477228d2dc9b872715ae to your computer and use it in GitHub Desktop.
Create Android Compose Rule using Intent
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