Introduction to Intents

Apps consist of Activities, and Activities can invoke other activities (from the same app, but public activities from other apps as well). They do that with ‘intents’. An intent is an abstract description of an operation to be performed. That means that it’s a way for an application to tell Android what it wants to open without going into unnecessary detail. For example: an app can ask Android to open an email application, without the need to specify which exact app. Android will just open the default email app, or ask the user which app they want to use.

An intent has two parts: the type of action to be performed and data expressed as a Uri. An intent to open a website from our app (in another app, such as chrome) would look like this:

Intent(Intent.ACTION_VIEW, Uri.parse("https://saucelabs.com/resources"))

Android knows which apps have the ability to handle “ACTION_VIEW” intents and will open your default app or ask you to select from a list of eligible apps.

Testing Intents

If you want to test if your app launches the right intent and contains the right data, you could check if a browser is opened and the right website is opened. That means you’d have to automate the browser too. If we go that route, we’re not just testing our app, but also testing Android’s ability to process intents, and testing our browser’s ability to process an incoming intent and open a website. To prevent this broadening of scope, Espresso offers the ability to validate and stub intents.

Validating Intents

Instead of an ActivityTestRule to launch an activity, we’ll have to use an IntentsTestRule:

@Rule 
public IntentsTestRule<MyActivity> intentsTestRule = new IntentsTestRule<>(MainActivity.class); {

// code for method overrides

};

The IntentsTestRule is actually an extension of the ActivityTestRule. This means that we can just replace ActivityTestRule with IntentsTestRule and everything keeps on working. When you make this changes in your own project, don’t forget to preserve the override we added in part 5!

Checking Intents

To check an intent, we can use the intended() method. The following is an example of how you could check an intent that launches a URL:

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

intended(allOf(               
hasAction(equalTo(Intent.ACTION_VIEW)),                 
hasData(hasHost(equalTo(“https://saucelabs.com “))),               hasData(hasPath(equalTo("/resources “)))));

For more on intents see the Android documentation:
https://developer.android.com/reference/android/content/Intent.html

Exercise 5: Intended Intents

In this exercise we are going to submit our order, which will trigger an Intent to launch an email app. Our goal is to verify that the intent is indeed launched, and that it has the right format.

To be able to validate the intent, you need to know how the intent is constructed by the app.

The intent is of type:

  • Intent.ACTION_SENDTO

It contains extra’s of the following types:

  • Intent.EXTRA_EMAIL (extra email addresses)
  • Intent.EXTRA_SUBJECT (email subject)
  • Intent.EXTRA_TEXT (email body text)

What you need to do:

  • Create a test where you create an order, or select one from the menu.
  • Fill out the form on the overview screen and submit the order
  • Verify the intent using the Intended method

Tip: Start as small as possible. First verify the action. When that passes, add one extra to the verification, before you add another.

The solution can be found at GitHub:
Solution Exercise 5