在哪里可以找到 ActivityTestRule 的源代码?

Where can I find the source code for ActivityTestRule?

在哪里可以找到 ActivityTestRule 的源代码?

可以在代码中添加ActivityTestRule,导入库,CTRL+左键反编译

如果您无法导入此 class,您需要将其添加到 build.gradle

中的 dependencies
androidTestCompile 'com.android.support.test:rules:0.3'

编辑:

我的反编译结果:

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();
            }

        }
    }
}

您可以在 https://android.googlesource.com/platform/frameworks/testing/+/android-support-test/rules/src/main/java/android/support/test/rule/ActivityTestRule.java

您必须在 AOSP 的 platform/frameworks/testing 项目上使用分支 android-support-test 而不是 master