Android测试 – Espresso与AsyncTask

前端之家收集整理的这篇文章主要介绍了Android测试 – Espresso与AsyncTask前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在为我的SignIn Activity编写一个测试,我正在使用一个AsyncTask.
public class SignInActivity extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {

    .........

    new AsyncSignButton().execute();
}

class AsyncSignButton extends AsyncTask<Void,Void,Integer>{
   ....... 
}

对于测试,我已经尝试使用IdlingResource,但是我不明白如何使用AsyncTask,而不是在示例中使用WebView,只需简单的AsyncTask.

这是我的测试代码

public class Test extends ActivityInstrumentationTestCase2<SplashActivity> {

private SplashActivity mActivity;

public Test(){
    super(SplashActivity.class);
}

public Test(Class<SplashActivity> activityClass) {
    super(activityClass);
}

@Override
protected void setUp() throws Exception{
    super.setUp();
    mActivity = getActivity();
    //how call it?
}

@LargeTest
public void testList() throws InterruptedException {

   //wait AsyncTask before call
   onView(withId(R.id.action_bar_accept_button)).perform(click());

}

public final class AsyncIdlingResource implements IdlingResource {

    private AsyncTask asyncTask;
    private ResourceCallback callback;

    public AsyncIdlingResource(AsyncTask asyncTask){
        this.asyncTask = checkNotNull(asyncTask);
    }

    @Override
    public String getName() {
        return "Sign idling resource";
    }

    @Override
    public boolean isIdleNow() {
        if(asyncTask == null) return true;
        return asyncTask.getStatus() == AsyncTask.Status.FINISHED;
    }

    @Override
    public void registerIdleTransitionCallback(ResourceCallback resourceCallback) {
        this.callback = resourceCallback;
    }
}

}

解决方法

好消息,您不需要AsyncTask的任何自定义IdlingResource:Espresso已经等待所有任务在执行操作/断言之前运行.这是例如 here提到的.
原文链接:https://www.f2er.com/android/313032.html

猜你在找的Android相关文章