Skip to content

Instantly share code, notes, and snippets.

@zawadz88
Created June 5, 2019 18:24
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zawadz88/f057c70d3061454207ccd56e0add81c6 to your computer and use it in GitHub Desktop.
Save zawadz88/f057c70d3061454207ccd56e0add81c6 to your computer and use it in GitHub Desktop.
package com.stepstone.test.rule
import android.app.Activity
import android.content.Intent
import androidx.test.core.app.ActivityScenario
import org.junit.rules.ExternalResource
class LazyActivityScenarioRule<A : Activity> : ExternalResource {
constructor(launchActivity: Boolean, startActivityIntentSupplier: () -> Intent) {
this.launchActivity = launchActivity
scenarioSupplier = { ActivityScenario.launch<A>(startActivityIntentSupplier()) }
}
constructor(launchActivity: Boolean, startActivityIntent: Intent) {
this.launchActivity = launchActivity
scenarioSupplier = { ActivityScenario.launch<A>(startActivityIntent) }
}
constructor(launchActivity: Boolean, startActivityClass: Class<A>) {
this.launchActivity = launchActivity
scenarioSupplier = { ActivityScenario.launch<A>(startActivityClass) }
}
private var launchActivity: Boolean
private var scenarioSupplier: () -> ActivityScenario<A>
private var scenario: ActivityScenario<A>? = null
private var scenarioLaunched: Boolean = false
override fun before() {
if (launchActivity) {
launch()
}
}
override fun after() {
scenario?.close()
}
fun launch(newIntent: Intent? = null) {
if (scenarioLaunched) throw IllegalStateException("Scenario has already been launched!")
newIntent?.let { scenarioSupplier = { ActivityScenario.launch<A>(it) } }
scenario = scenarioSupplier()
scenarioLaunched = true
}
fun getScenario(): ActivityScenario<A> = checkNotNull(scenario)
}
inline fun <reified A : Activity> lazyActivityScenarioRule(launchActivity: Boolean = true, noinline intentSupplier: () -> Intent): LazyActivityScenarioRule<A> =
LazyActivityScenarioRule(launchActivity, intentSupplier)
inline fun <reified A : Activity> lazyActivityScenarioRule(launchActivity: Boolean = true, intent: Intent? = null): LazyActivityScenarioRule<A> = if (intent == null) {
LazyActivityScenarioRule(launchActivity, A::class.java)
} else {
LazyActivityScenarioRule(launchActivity, intent)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment