Skip to content

Instantly share code, notes, and snippets.

@tiwiz
Created February 25, 2019 13:56
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tiwiz/989c74f5d63e8340fee67df7a5fac100 to your computer and use it in GitHub Desktop.
Save tiwiz/989c74f5d63e8340fee67df7a5fac100 to your computer and use it in GitHub Desktop.
This test verifies that the SplashScreenManager works as expected
class SplashScreenManagerTest {
@Test
fun when_user_is_not_logged_login_activity_should_be_invoked() {
Intents.init()
val activityScenario: ActivityScenario<TestActivity> =
ActivityScenario.launch(TestActivity::class.java)
activityScenario.onActivity {
val storage = PreferencesStorage(it)
val tokenManager = TokenManager(storage)
val intentFactory = SplashIntentFactory(it, tokenManager)
it.clearToken()
SplashTimingManager(it, intentFactory, 0)
}
activityScenario.moveToState(Lifecycle.State.STARTED)
intended(hasComponent(LoginActivity::class.java.name))
Intents.release()
activityScenario.close()
}
@Test
fun when_user_is_logged_home_activity_should_be_invoked() {
Intents.init()
val activityScenario: ActivityScenario<TestActivity> = ActivityScenario.launch(TestActivity::class.java)
activityScenario.onActivity {
val storage = PreferencesStorage(it)
val tokenManager = TokenManager(storage)
val intentFactory = SplashIntentFactory(it, tokenManager)
it returnToken "AnyToken123!"
SplashTimingManager(it, intentFactory, 0)
}
activityScenario.moveToState(Lifecycle.State.STARTED)
intended(hasComponent(HomeActivity::class.java.name))
Intents.release()
activityScenario.close()
}
@Test
fun when_user_exits_the_app_before_time_is_elapsed_no_intents_should_be_fired() {
Intents.init()
val activityScenario: ActivityScenario<TestActivity> = ActivityScenario.launch(TestActivity::class.java)
activityScenario.onActivity {
val storage = PreferencesStorage(it)
val tokenManager = TokenManager(storage)
val intentFactory = SplashIntentFactory(it, tokenManager)
it returnToken "AnyToken123!"
SplashTimingManager(it, intentFactory, 10000)
}
activityScenario.moveToState(Lifecycle.State.STARTED)
activityScenario.moveToState(Lifecycle.State.DESTROYED)
getIntents().map { it.component?.className }.forEach {
assertThat(it).doesNotContain("Login")
assertThat(it).doesNotContain("Home")
}
Intents.release()
activityScenario.close()
}
@After
fun tearDown() {
ApplicationProvider.getApplicationContext<Application>().clearToken()
}
}
@danilodequeiroz
Copy link

Is there a way to get the called activity (activity B for example), using a scenario from the caller activity (activity A) using androidX apis?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment