Neon Warge Neon Warge - 2 months ago 21
Android Question

Whats the difference between intending vs intended in Espresso?

Its been 2 weeks since I am studying Espresso and I could not grasp

intending
and
intended
. When do I use
intending and
intended`? The provided example and online tutorials are not helping and researching the web is doing me more damage than good.

Semantically, for intellectual reference, why is it
intend-ing
and the other
intended
, this added more to the confusion. Is this another Google naming blunder or its just me? These two method really doesn't make sense.

I am misunderstanding its usage. I wanted to test if my activity
A
launched activity
B
. That's it. Here is my code:

@Test
public void shouldLaunchTagListActivity()
{
onView(withId(R.id.edittext_description_minimized))
.perform(click());

onView(withId(R.id.linearlayout_add_note_maximize))
.check(matches(isDisplayed()));

onView(withId(R.id.relativelayout_quick_action_button))
.check(matches(isDisplayed()));

onView(withId(R.id.imagebutton_tag))
.perform(click());

// should I intended or intending here?
// ???
intended(toPackage(HomeScreenActivity.class.getName()));

onView(withId(R.id.coordinatorlayout_tag_list))
.check(matches(isDisplayed()));
}


This test always pass even I supplant the intent with the wrong the target.

I can check if the other activity has been launched by checking if my target view is existing and was seen by the user. But now I am going to run on a different user story where I really need to check if the activity sent the request to launched another activity (my activity, not external).

Any explanation is greatly appreciated!

Answer

The difference is Intended only verifies that an Intent was launched (what you want). Intending will return a result when the Intent is launched.

For Intended (if you want to check if the intent did actually launched your activity)

intended(hasComponent(TagListActivity.class.getName()));

This will fail if you press a button and launched, say, MyActivity, and you test intended for TagListActivity. It will throw this following error:

android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: Wanted to match 1 intents. Actually matched 0 intents.

Now, if you were testing that onActivityResult was handled properly, you would use Intending and pass in an ActivityResult similar to this:

Intent resultData = new Intent();
resultData.putExtra("resultData", "fancyData");
ActivityResult result = new ActivityResult(Activity.RESULT_OK, resultData);

intending(toPackage(HomeScreenActivity.class.getName())).respondWith(result));

// Perform action that throws the Intent
onView(withId(R.id.imagebutton_tag)).perform(click());

// Verify your Activity is in the state it should be here.

So in this case, if you were expecting "HomeScreenActivity" to return something, you would use Intending.

Comments