If the AsyncTask
is triggered by a click event on a button, how can I test it - how can I wait until the AsyncTask completes?
Note I can always execute the AsyncTask
directly in my test method, I know how to test such scenario. However, if I insist on on using simulating the onClick
event using performClick()
can I still test my registration process?
MainActivityFunctionalTest
public class MainActivityFunctionalTest extends ActivityInstrumentationTestCase2<MainActivity> { // ... public void testRegistration() { ImageButton submitBtn = (ImageButton) solo.getView(R.id.BtnR); assertNotNull(submitBtn); submitBtn.performClick(); // How to do something when the registration is done? } // ... }
MainActivity (of the project to be tested)
ImageButton submitBtn = (ImageButton) findViewById(R.id.BtnRegister); submitBtn.setOnClickListener(new OnClickListener() { public void onClick(View v) { sendRegistration(); } }); public void sendRegistration() { Processor processor = new Processor();// subclass of AsyncTask processor.execute(); }