Mina Wissa
Mina Wissa

Reputation: 10971

Android Espresso testing: how test an activity's onNewIntent?

I'm wondering if there's a way to test an Activiy onNewIntent() method, I want to test launching an activity with the flag single top set and test some behaviour, how can this be achieved with espresso?

Upvotes: 8

Views: 1841

Answers (2)

Lucas L.
Lucas L.

Reputation: 1021

Calling it directly:

When you override onNewIntent() in your activity, you can make it public:

   @Override
    public void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
    }

that way you can actually call it directly:

activityTestRule.getActivity().onNewIntent(new Intent())

This actually works, but it's not too good because you are calling the method from the testing app thread. If calling onNewIntent() will cause any changes on the UI you will get an exception because only the thread that created the view can change it. To fix this you can force running on the UI thread

activityTestRule.getActivity().runOnUiThread(() -> {
    activityTestRule.getActivity().onNewIntent(new Intent()));
});

This would allow you to test the onNewIntent() method of your activity.

A better way:

By the way you phrased your question, you also want to check some behaviour from the activity being defined as singleTop. Instead of calling the method directly, you can launch an intent that actually should trigger onNewIntent() in the activity under test:

Intent intent = new Intent(activityTestRule.getActivity().getApplicationContext(), ActivityUnderTest.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

InstrumentationRegistry.getTargetContext().startActivity(intent));

This should end up calling onNewIntent() of the activity under test as long as it's defined as singleTop, otherwise it will start a new activity instance.

Upvotes: 6

Ben Kane
Ben Kane

Reputation: 10060

If you're using the ActivityTestRule, how about something like this?

Intents.init();
Intent intent = // Build your intent

rule.launchActivity(intent);

// Assertions    

Intents.release()

I'm not actually an Espresso user, but I'm assuming that will launch your activity and onNewIntent() will be called. Then make your assertions.

Note: this is using the Espresso Intents library, designed for this purpose.
androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2.2'

Upvotes: 2

Related Questions