Question

Logcat does not print anything while it's supposed to when the submit button is clicked!
What am I missing?

MainActivityFunctionalTest

public class MainActivityFunctionalTest extends
        ActivityInstrumentationTestCase2<MainActivity> {
public void testRegistrationFormIsSubmitted() {
final MainActivity mainActivity = (MainActivity) solo.getCurrentActivity();

        try {
            runTestOnUiThread(new Runnable() {
                @Override
                public void run() {
ImageButton submitBtn = (ImageButton) solo.getView(R.id.btn_next);

                    assertNotNull(submitBtn);
                    submitBtn.performClick();
}
            });
        } catch (Throwable e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
}
}  

MainActivity

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
ImageButton submitBtn = (ImageButton) findViewById(R.id.btn_next);
submitBtn.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Log.i(TAG, "Submit button is clicked");// This is not printed
                sendActivation(v);
            }
        });
}

Updates
So I changed submitBtn.performClick() to solo.clickOnView(submitBtn) but the issue remains the same

Was it helpful?

Solution 2

There were 2 issues with my codes:
1) ImageButton click is not supported by Robotium (they recommend sticking to View instead)
2) Click event cannot happen when the screen of my phone is off!

I made sure the screen of my phone is on and changed my code is the following and it works:

View submitBtn = solo.getView(R.id.btn_next);
assertNotNull(submitBtn);
solo.clickOnView(submitBtn);

OTHER TIPS

Because you're calling runTestOnUiThread(), your test method finishes (and passes) before your run() gets called. The solution is simply to stop using runTestOnUiThread().

public void testRegistrationFormIsSubmitted() {
    MainActivity mainActivity = (MainActivity) solo.getCurrentActivity();
    ImageButton submitBtn = (ImageButton) solo.getView(R.id.btn_next);
    assertNotNull(submitBtn);
    submitBtn.performClick();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top