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

时间:2015-07-10 00:21:45

标签: android-testing

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

2 个答案:

答案 0 :(得分:2)

你可以找到它 https://android.googlesource.com/platform/frameworks/testing/+/android-support-test/rules/src/main/java/android/support/test/rule/ActivityTestRule.java

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

答案 1 :(得分:0)

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

如果您无法导入此课程,则需要将其添加到dependencies

中的build.gradle
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();
            }

        }
    }
}