0

Where can I find the source code for ActivityTestRule?

2 Answers 2

2

You can find it at https://android.googlesource.com/platform/frameworks/testing/+/android-support-test/rules/src/main/java/android/support/test/rule/ActivityTestRule.java

You have to use branch android-support-test instead of master on platform/frameworks/testing project in AOSP.

0

You can add ActivityTestRule in code, import library and CTRL+Left Click to decompile.

If you can't import this class, you need to add it to dependencies in your build.gradle

androidTestCompile 'com.android.support.test:rules:0.3'

EDIT:

My result of decompiling:

package android.support.test.rule;

import android.app.Activity;
import android.app.Instrumentation;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.test.InstrumentationRegistry;
import android.support.test.annotation.Beta;
import android.support.test.internal.util.Checks;
import android.support.test.rule.UiThreadTestRule;
import android.util.Log;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;

@Beta
public class ActivityTestRule<T extends Activity> extends UiThreadTestRule {
    private static final String TAG = "ActivityInstrumentationRule";
    private final Class<T> mActivityClass;
    private Instrumentation mInstrumentation;
    private boolean mInitialTouchMode;
    private boolean mLaunchActivity;
    private T mActivity;

    public ActivityTestRule(Class<T> activityClass) {
        this(activityClass, false);
    }

    public ActivityTestRule(Class<T> activityClass, boolean initialTouchMode) {
        this(activityClass, initialTouchMode, true);
    }

    public ActivityTestRule(Class<T> activityClass, boolean initialTouchMode, boolean launchActivity) {
        this.mInitialTouchMode = false;
        this.mLaunchActivity = false;
        this.mActivityClass = activityClass;
        this.mInitialTouchMode = initialTouchMode;
        this.mLaunchActivity = launchActivity;
        this.mInstrumentation = InstrumentationRegistry.getInstrumentation();
    }

    protected Intent getActivityIntent() {
        return new Intent("android.intent.action.MAIN");
    }

    protected void beforeActivityLaunched() {
    }

    protected void afterActivityLaunched() {
    }

    protected void afterActivityFinished() {
    }

    public T getActivity() {
        if(this.mActivity == null) {
            Log.w("ActivityInstrumentationRule", "Activity wasn\'t created yet");
        }

        return this.mActivity;
    }

    public Statement apply(Statement base, Description description) {
        return new ActivityTestRule.ActivityStatement(super.apply(base, description));
    }

    public T launchActivity(@Nullable Intent startIntent) {
        this.mInstrumentation.setInTouchMode(this.mInitialTouchMode);
        String targetPackage = this.mInstrumentation.getTargetContext().getPackageName();
        if(null == startIntent) {
            startIntent = this.getActivityIntent();
            if(null == startIntent) {
                Log.w("ActivityInstrumentationRule", "getActivityIntent() returned null using default: Intent(Intent.ACTION_MAIN)");
                startIntent = new Intent("android.intent.action.MAIN");
            }
        }

        startIntent.setClassName(targetPackage, this.mActivityClass.getName());
        startIntent.addFlags(268435456);
        Log.d("ActivityInstrumentationRule", String.format("Launching activity %s", new Object[]{this.mActivityClass.getName()}));
        this.beforeActivityLaunched();
        this.mActivity = (Activity)this.mActivityClass.cast(this.mInstrumentation.startActivitySync(startIntent));
        this.mInstrumentation.waitForIdleSync();
        this.afterActivityLaunched();
        return this.mActivity;
    }

    void setInstrumentation(Instrumentation instrumentation) {
        this.mInstrumentation = (Instrumentation)Checks.checkNotNull(instrumentation, "instrumentation cannot be null!");
    }

    void finishActivity() {
        if(this.mActivity != null) {
            this.mActivity.finish();
            this.mActivity = null;
        }

    }

    private class ActivityStatement extends Statement {
        private final Statement mBase;

        public ActivityStatement(Statement base) {
            this.mBase = base;
        }

        public void evaluate() throws Throwable {
            try {
                if(ActivityTestRule.this.mLaunchActivity) {
                    ActivityTestRule.this.mActivity = ActivityTestRule.this.launchActivity(ActivityTestRule.this.getActivityIntent());
                }

                this.mBase.evaluate();
            } finally {
                ActivityTestRule.this.finishActivity();
                ActivityTestRule.this.afterActivityFinished();
            }

        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.