简体   繁体   中英

How to check if an Activity receives an intent sent from another activity?

I send an intent from activity1 to activity2, after clicking on a button. I send extras with this intent.

Using espresso, I need to test whether the activity2 received this intent, sent from activity1.

I have no idea, how to do it. I have not written any code.

Let's say from ActivityA you click a button which starts ActivityB:

Intent intent = new Intent(this, ActivityB.class);
                intent.putExtra("MY_EXTRA", "MY EXTRA VALUE");

A test which checks that you're sending the right data to ActivityB would be:

@Rule
public IntentsTestRule<ActivityA> intentsTestRule = new IntentsTestRule<>(ActivityA.class);

@Test
public void testIntents() {
    //from ActivityA, click the button which starts the ActivityB
    onView(withText("ClickMe")).perform(click());

    //validate intent and check its data
    intended(allOf(
            toPackage("com.your.package.name"),
            hasExtra("MY_EXTRA", "MY EXTRA VALUE")
    ));
}

Check out the examples provided by google: https://github.com/googlesamples/android-testing/tree/master/ui/espresso/IntentsBasicSample

and some documentation here: https://developer.android.com/training/testing/espresso/intents.html .

first use getIntent()

Intent intent = getIntent();

then using intent object to receive data from another activity

String id = intent.getStringExtra("your key");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM