Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 it's just me? These two methods really don'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 passes 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!

like image 328
Neon Warge Avatar asked Sep 27 '16 14:09

Neon Warge


People also ask

How do you test espresso intent?

Validate intentsUsing the intended() method, which is similar to Mockito. verify() , you can assert that a given intent has been seen. However, Espresso-Intents doesn't stub out responses to intents unless you explicitly configure it to do so. // User action that results in an external "phone" activity being launched.

What is intent testing in Android?

Intents enables validation and stubbing of intents sent out by the application under test. An example test that simply validates an outgoing intent: public void testValidateIntentSentToPackage() { // User action that results in an external "phone" activity being launched.


1 Answers

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.

like image 63
DeeV Avatar answered Nov 09 '22 22:11

DeeV