dimanche 28 février 2021

Android Testing Activity Started by Handler.postDelayed

I'm new to using the Roboelectric testing framework and the issue that I am having is not being able to build an activity. I used the same exact test in every class and they worked as expected, except for the CallingActivity that relies on being called by Handler.postDelayed which I believe is the problem.

This is the code that will create the CallingActivity:

btnCall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Handler().postDelayed(new Runnable()
                {
                    @Override
                    public void run() {
                        Intent intent = new Intent(FakePhoneCallMenuActivity.this, CallingActivity.class);
                        intent.putExtra("callerName", nameEntryBox.getText().toString());
                        intent.putExtra("callerPhoneNum", phoneEntryBox.getText().toString());
                        intent.putExtra("callerVoice", voice);
                        startActivity(intent);
                    }
                }, timeToStart);
            }
        });

This is test for the CallingActivity that is having issues working:

@RunWith(RobolectricTestRunner.class)
@Config(sdk = {Build.VERSION_CODES.O_MR1})
public class CallingTest {
    private CallingActivity activity;

    @Before
    public void setUp() throws Exception {
        activity = Robolectric.buildActivity(CallingActivity.class)
                .create()
                .start()
                .resume()
                .get();
    }

    @Test
    public void acceptCallButtonClickShouldStartNewActivity() throws Exception {
        ShadowActivity shadowActivity = shadowOf(activity);
        ImageButton button = (ImageButton) shadowActivity.getContentView().findViewById(R.id.btnAcceptCall);
        button.callOnClick();
        Intent startedIntent = shadowActivity.getNextStartedActivity();
        ShadowIntent shadowIntent = shadowOf(startedIntent);
        assertEquals(FakePhoneCallMenuActivity.class, shadowIntent.getIntentClass());
    }
}

The issue that I am recieving after trying to run this test is:

java.lang.Exception: Main looper has queued unexecuted runnables.

Help with this would be greatly appreciated!

Thanks

Aucun commentaire:

Enregistrer un commentaire