@@ -698,230 +698,6 @@ public class MainActivityRobotiumTest {
698698
699699[ MainActivityRobotiumTestRule.java] ( https://github.com/ravidsrk/android-testing-guide/blob/master/SampleApp/app/src/androidTest/java/in/ravidsrk/sample/MainActivityRobotiumTestRule.java )
700700
701- ``` java
702- @Beta
703- public class MainActivityRobotiumTestRule <T extends Activity > extends UiThreadTestRule {
704-
705- private static final String TAG = " InstrumentationRule" ;
706- private final Class<T > mActivityClass;
707-
708- public Instrumentation getInstrumentation () {
709- return mInstrumentation;
710- }
711-
712- private Instrumentation mInstrumentation;
713- private boolean mInitialTouchMode = false ;
714- private boolean mLaunchActivity = false ;
715- private T mActivity;
716-
717- /**
718- * Similar to {@link #MainActivityRobotiumTestRule(Class, boolean, boolean) } but with "touch mode" disabled.
719- *
720- * @param activityClass The activity under test. This must be a class in the instrumentation
721- * targetPackage specified in the AndroidManifest.xml
722- * @see MainActivityRobotiumTestRule#MainActivityRobotiumTestRule(Class, boolean, boolean)
723- */
724- public MainActivityRobotiumTestRule (Class<T > activityClass ) {
725- this (activityClass, false );
726- }
727-
728- /**
729- * Similar to {@link #MainActivityRobotiumTestRule(Class, boolean, boolean) } but defaults to launch the
730- * activity under test once per
731- * <a href =" http://junit.org/javadoc/latest/org/junit/Test.html" ><code >Test</code></a> method.
732- * It is launched before the first
733- * <a href =" http://junit.sourceforge.net/javadoc/org/junit/Before.html" ><code >Before</code></a>
734- * method, and terminated after the last
735- * <a href =" http://junit.sourceforge.net/javadoc/org/junit/After.html" ><code >After</code></a>
736- * method.
737- *
738- * @param activityClass The activity under test. This must be a class in the instrumentation
739- * targetPackage specified in the AndroidManifest.xml
740- * @param initialTouchMode true if the Activity should be placed into "touch mode" when started
741- * @see MainActivityRobotiumTestRule#MainActivityRobotiumTestRule(Class, boolean, boolean)
742- */
743- public MainActivityRobotiumTestRule (Class<T > activityClass , boolean initialTouchMode ) {
744- this (activityClass, initialTouchMode, true );
745- }
746-
747- /**
748- * Creates an {@link MainActivityRobotiumTestRule} for the Activity under test.
749- *
750- * @param activityClass The activity under test. This must be a class in the instrumentation
751- * targetPackage specified in the AndroidManifest.xml
752- * @param initialTouchMode true if the Activity should be placed into "touch mode" when started
753- * @param launchActivity true if the Activity should be launched once per
754- * <a href =" http://junit.org/javadoc/latest/org/junit/Test.html" >
755- * <code >Test</code></a> method. It will be launched before the first
756- * <a href =" http://junit.sourceforge.net/javadoc/org/junit/Before.html" >
757- * <code >Before</code></a> method, and terminated after the last
758- * <a href =" http://junit.sourceforge.net/javadoc/org/junit/After.html" >
759- * <code >After</code></a> method.
760- */
761- public MainActivityRobotiumTestRule (Class<T > activityClass , boolean initialTouchMode ,
762- boolean launchActivity ) {
763- mActivityClass = activityClass;
764- mInitialTouchMode = initialTouchMode;
765- mLaunchActivity = launchActivity;
766- mInstrumentation = InstrumentationRegistry . getInstrumentation();
767- }
768-
769- /**
770- * Override this method to set up Intent as if supplied to
771- * {@link android.content.Context#startActivity}.
772- * <p >
773- * The default Intent (if this method returns null or is not overwritten) is:
774- * action = {@link Intent#ACTION_MAIN}
775- * flags = {@link Intent#FLAG_ACTIVITY_NEW_TASK}
776- * All other intent fields are null or empty.
777- *
778- * @return The Intent as if supplied to {@link android.content.Context#startActivity}.
779- */
780- protected Intent getActivityIntent () {
781- return new Intent (Intent . ACTION_MAIN );
782- }
783-
784- /**
785- * Override this method to execute any code that should run before your {@link Activity} is
786- * created and launched.
787- * This method is called before each test method, including any method annotated with
788- * <a href =" http://junit.sourceforge.net/javadoc/org/junit/Before.html" ><code >Before</code></a>.
789- */
790- protected void beforeActivityLaunched () {
791- // empty by default
792- }
793-
794- /**
795- * Override this method to execute any code that should run after your {@link Activity} is
796- * launched, but before any test code is run including any method annotated with
797- * <a href =" http://junit.sourceforge.net/javadoc/org/junit/Before.html" ><code >Before</code></a>.
798- * <p >
799- * Prefer
800- * <a href =" http://junit.sourceforge.net/javadoc/org/junit/Before.html" ><code >Before</code></a>
801- * over this method. This method should usually not be overwritten directly in tests and only be
802- * used by subclasses of MainActivityRobotiumTestRule to get notified when the activity is created and
803- * visible but test runs.
804- */
805- protected void afterActivityLaunched () {
806- // empty by default
807- }
808-
809- /**
810- * Override this method to execute any code that should run after your {@link Activity} is
811- * finished.
812- * This method is called after each test method, including any method annotated with
813- * <a href =" http://junit.sourceforge.net/javadoc/org/junit/After.html" ><code >After</code></a>.
814- */
815- protected void afterActivityFinished () {
816- // empty by default
817- }
818-
819- /**
820- * @return The activity under test.
821- */
822- public T getActivity () {
823- if (mActivity == null ) {
824- Log . w(TAG , " Activity wasn't created yet" );
825- }
826- return mActivity;
827- }
828-
829- @Override
830- public Statement apply (final Statement base , Description description ) {
831- return new ActivityStatement (super . apply(base, description));
832- }
833-
834- /**
835- * Launches the Activity under test.
836- * <p >
837- * Don't call this method directly, unless you explicitly requested not to launch the Activity
838- * manually using the launchActivity flag in
839- * {@link MainActivityRobotiumTestRule#MainActivityRobotiumTestRule(Class, boolean, boolean) }.
840- * <p >
841- * Usage:
842- * <pre >
843- * @ ; Test
844- * public void customIntentToStartActivity() {
845- * Intent intent = new Intent(Intent.ACTION_PICK);
846- * mActivity = mActivityRule.launchActivity(intent);
847- * }
848- * </pre>
849- * @param startIntent The Intent that will be used to start the Activity under test. If
850- * {@code startIntent } is null, the Intent returned by
851- * {@link MainActivityRobotiumTestRule#getActivityIntent()} is used.
852- * @return the Activity launched by this rule.
853- * @see MainActivityRobotiumTestRule#getActivityIntent()
854- */
855- public T launchActivity (@Nullable Intent startIntent ) {
856- // set initial touch mode
857- mInstrumentation. setInTouchMode(mInitialTouchMode);
858-
859- final String targetPackage = mInstrumentation. getTargetContext(). getPackageName();
860- // inject custom intent, if provided
861- if (null == startIntent) {
862- startIntent = getActivityIntent();
863- if (null == startIntent) {
864- Log . w(TAG , " getActivityIntent() returned null using default: " +
865- " Intent(Intent.ACTION_MAIN)" );
866- startIntent = new Intent (Intent . ACTION_MAIN );
867- }
868- }
869- startIntent. setClassName(targetPackage, mActivityClass. getName());
870- startIntent. addFlags(Intent . FLAG_ACTIVITY_NEW_TASK );
871- Log . d(TAG , String . format(" Launching activity %s" ,
872- mActivityClass. getName()));
873-
874- beforeActivityLaunched();
875- // The following cast is correct because the activity we're creating is of the same type as
876- // the one passed in
877- mActivity = mActivityClass. cast(mInstrumentation. startActivitySync(startIntent));
878-
879- mInstrumentation. waitForIdleSync();
880-
881- afterActivityLaunched();
882- return mActivity;
883- }
884-
885- // Visible for testing
886- void setInstrumentation (Instrumentation instrumentation ) {
887- mInstrumentation = checkNotNull(instrumentation, " instrumentation cannot be null!" );
888- }
889-
890- void finishActivity () {
891- if (mActivity != null ) {
892- mActivity. finish();
893- mActivity = null ;
894- }
895- }
896-
897- /**
898- * <a href =" http://junit.org/apidocs/org/junit/runners/model/Statement.html" >
899- * <code >Statement</code></a> that finishes the activity after the test was executed
900- */
901- private class ActivityStatement extends Statement {
902-
903- private final Statement mBase;
904-
905- public ActivityStatement (Statement base ) {
906- mBase = base;
907- }
908-
909- @Override
910- public void evaluate () throws Throwable {
911- try {
912- if (mLaunchActivity) {
913- mActivity = launchActivity(getActivityIntent());
914- }
915- mBase. evaluate();
916- } finally {
917- finishActivity();
918- afterActivityFinished();
919- }
920- }
921- }
922-
923- }
924- ```
925701### UI testing and UI Automator
926702
927703[ MainActivityTest] ( https://github.com/ravidsrk/android-testing-guide/blob/master/SampleApp/app/src/androidTest/java/in/ravidsrk/sample/MainActivityTest.java#L101 )
0 commit comments