我正在为我的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; } } }