Skip to content

Instantly share code, notes, and snippets.

@pedrovgs
Created February 21, 2019 12:17
Show Gist options
  • Save pedrovgs/6a305ba4c5e3acfac854ce4c36558d9b to your computer and use it in GitHub Desktop.
Save pedrovgs/6a305ba4c5e3acfac854ce4c36558d9b to your computer and use it in GitHub Desktop.
Imroved IntentsTestRule implementation. This class initialized the intents components before starting the activity so we avoid a race condition between our test and the activity we are testing
package com.aplazame.utils
import android.app.Activity
import androidx.test.espresso.intent.Intents
import androidx.test.rule.ActivityTestRule
class ExhaustiveIntentsTestRule<T : Activity> : ActivityTestRule<T> {
private var isInitialized: Boolean = false
constructor(activityClass: Class<T>) : super(activityClass)
constructor(activityClass: Class<T>, initialTouchMode: Boolean) : super(activityClass, initialTouchMode)
constructor(activityClass: Class<T>, initialTouchMode: Boolean, launchActivity: Boolean) : super(
activityClass,
initialTouchMode,
launchActivity
)
override fun beforeActivityLaunched() {
super.beforeActivityLaunched()
Intents.init()
isInitialized = true
}
override fun afterActivityFinished() {
super.afterActivityFinished()
if (isInitialized) {
// Otherwise will throw a NPE if Intents.init() wasn't called.
Intents.release()
isInitialized = false
}
}
}
@TonyNikolov
Copy link

Thanks, works great!

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