Created
February 21, 2019 12:17
-
-
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
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
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 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks, works great!