自动化测试AndroidJUnitRunner

2018-06-07  本文已影响0人  JuneLeo
背景
目的
执行顺序图
image
收益
源码分析
public T launchActivity(@Nullable Intent startIntent) {
  ...
  if (null == startIntent) {

    startIntent = getActivityIntent();

    if (null == startIntent) {

        ...

      startIntent = new Intent(Intent.ACTION_MAIN);

    }

  }
    ...
  beforeActivityLaunched();  //子线程

  mActivity = mActivityClass.cast(mInstrumentation.startActivitySync(startIntent));//异步启动activity

  mInstrumentation.waitForIdleSync();  //子线程阻塞

  if (mActivity != null) {

    afterActivityLaunched();  //子线程

  } else {

    ...

  }

  return mActivity;

}

public void waitForIdleSync() {

    validateNotAppThread();  //判断不是UI线程

    Idler idler = new Idler(null);

    mMessageQueue.addIdleHandler(idler); //添加IdlerHandler

    mThread.getHandler().post(new EmptyRunnable());

    idler.waitForIdle(); //阻塞

}

private static final class Idler implements MessageQueue.IdleHandler {

    private final Runnable mCallback;

    private boolean mIdle;

    public Idler(Runnable callback) {

        mCallback = callback;

        mIdle = false;

    }

    public final boolean queueIdle() {

        if (mCallback != null) {

            mCallback.run();

        }

        synchronized (this) {

            mIdle = true;

            notifyAll();  //解除阻塞

        }

        return false;

    }

    public void waitForIdle() {

        synchronized (this) {

            while (!mIdle) {

                try {

                    wait(); //阻塞

                } catch (InterruptedException e) {

                }

            }

        }

    }

}
源码相关类
//ParentRunner
protected Statement classBlock(final RunNotifier notifier) {
    Statement statement = childrenInvoker(notifier);
    if (!areAllChildrenIgnored()) {
        statement = withBeforeClasses(statement);
        statement = withAfterClasses(statement);
        statement = withClassRules(statement);
    }
    return statement;
}
//BlockJUnit4ClassRunner 
protected Statement methodBlock(FrameworkMethod method) {
    Object test;
    try {
        test = new ReflectiveCallable() {
            @Override
            protected Object runReflectiveCall() throws Throwable {
                return createTest();
            }
        }.run();
    } catch (Throwable e) {
        return new Fail(e);
    }

    Statement statement = methodInvoker(method, test);
    statement = possiblyExpectingExceptions(method, test, statement);
    statement = withPotentialTimeout(method, test, statement);
    statement = withBefores(method, test, statement); //@before
    statement = withAfters(method, test, statement); 参数statement对应@Before
    statement = withRules(method, test, statement);
    return statement;
}
//@After
public class RunAfters extends Statement {
    private final Statement next;

    private final Object target;

    private final List<FrameworkMethod> afters;

    public RunAfters(Statement next, List<FrameworkMethod> afters, Object target) {
        this.next = next;
        this.afters = afters;
        this.target = target;
    }

    @Override
    public void evaluate() throws Throwable {
        List<Throwable> errors = new ArrayList<Throwable>();
        try {
            next.evaluate();   //这个statement对应@Before
        } catch (Throwable e) {
            errors.add(e);
        } finally {
            for (FrameworkMethod each : afters) {
                try {
                    each.invokeExplosively(target);
                } catch (Throwable e) {
                    errors.add(e);
                }
            }
        }
        MultipleFailureException.assertEmpty(errors);
    }
}
上一篇下一篇

猜你喜欢

热点阅读