Created
April 21, 2015 23:16
-
-
Save krschultz/c455fe58527ea5c60675 to your computer and use it in GitHub Desktop.
Jake Wharton ActivityRule -> Support Lib ActivityTestRule
This file contains 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
import android.app.Activity; | |
import android.content.Intent; | |
import android.support.test.InstrumentationRegistry; | |
import android.support.test.rule.ActivityTestRule; | |
/** | |
* Matches the intent returned by Jake Wharton's ActivityRule but is layered on top of new support lib rules | |
* | |
* @see {https://gist.github.com/JakeWharton/1c2f2cadab2ddd97f9fb} | |
* @see {https://code.google.com/p/android-test-kit/wiki/JUnit4RulesInATSL} | |
*/ | |
public abstract class BaseActivityTestRule<T extends Activity> extends ActivityTestRule<T> { | |
private final Class<T> activityClass; | |
public BaseActivityTestRule(Class<T> activityClass) { | |
super(activityClass); | |
this.activityClass = activityClass; | |
} | |
public BaseActivityTestRule(Class<T> activityClass, boolean initialTouchMode) { | |
super(activityClass, initialTouchMode); | |
this.activityClass = activityClass; | |
} | |
public BaseActivityTestRule(Class<T> activityClass, boolean initialTouchMode, boolean launchActivity) { | |
super(activityClass, initialTouchMode, launchActivity); | |
this.activityClass = activityClass; | |
} | |
@Override | |
protected Intent getActivityIntent() { | |
Intent intent = super.getActivityIntent(); | |
intent.setClassName(getTargetPackage(), getActivityClassName()); | |
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | |
return intent; | |
} | |
private String getTargetPackage() { | |
return InstrumentationRegistry.getTargetContext().getPackageName(); | |
} | |
private String getActivityClassName() { | |
return activityClass.getClass().getName(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment