☕ Advanced Espresso Concepts 03: Intent Testing

EspressoLab.Ai
2 min readJun 5, 2024

--

Introduction

Welcome back, Espresso enthusiasts! 🚀 In this part of our series, we’re diving into Intent Testing. Intents are used for interacting with other apps, and testing them ensures your app behaves correctly when launching or responding to intents. Let’s get started!

Understanding Intents 🎯

Intents allow you to start activities, send data between components, and interact with other apps. Verifying these interactions is crucial for comprehensive testing.

Setting Up Intent Testing Environment 🛠️

Use Intents.init() and Intents.release() to set up and tear down your intent testing environment.

@Before
fun setup() {
Intents.init()
}

@After
fun teardown() {
Intents.release()
}

Verifying Intents Sent by Your App 🚀

Here’s how you can test that your app sends the correct intents:

Step 1: Define the Expected Intent

val expectedIntent = allOf(
hasAction(Intent.ACTION_VIEW),
hasData(Uri.parse("https://example.com"))
)

Step 2: Verify the Intent

@Test
fun testSendIntent() {
// Perform action that triggers the intent
onView(withId(R.id.button)).perform(click())
// Verify the intent was sent
intended(expectedIntent)
}

Stubbing Intents 🧩

Sometimes, you want to stub the response to an intent to isolate the part of the app you are testing.

@Before
fun setup() {
Intents.init()
intending(toPackage("com.example.otherapp")).respondWith(ActivityResult(Activity.RESULT_OK, null))
}

@After
fun teardown() {
Intents.release()
}

Use Case: Testing Intent to Open a Web Page 🌐

Suppose your app has a button that opens a web page. Here’s how you can test this interaction:

@Test
fun testOpenWebPageIntent() {
val expectedIntent = allOf(
hasAction(Intent.ACTION_VIEW),
hasData(Uri.parse("https://example.com"))
)
// Perform action that triggers the intent
onView(withId(R.id.openWebButton)).perform(click())
// Verify the intent was sent
intended(expectedIntent)
}

Testing Intent Responses 📲

You can also test how your app handles incoming intents.

Step 1: Set Up the Intent

@Before
fun setup() {
val intent = Intent().apply {
action = Intent.ACTION_SEND
type = "text/plain"
putExtra(Intent.EXTRA_TEXT, "Test text")
}
activityRule.launchActivity(intent)
}

Step 2: Verify the Handling

@Test
fun testHandleIncomingIntent() {
// Verify that the text is displayed correctly
onView(withId(R.id.textView)).check(matches(withText("Test text")))
}

Conclusion

Congratulations on mastering Intent Testing! 🌟 With this knowledge, you can ensure your app interacts with other apps correctly, making your UI tests more comprehensive and reliable.

Next Article Teaser

Stay tuned for our next article, where we’ll dive into creating custom rules in Android UI testing. We’ll cover how to define and use custom rules by extending TestWatcher, and provide practical examples such as database initialization, handling dependency injection with Koin and Hilt, and disabling animations during tests.

Follow Us for More Insights 🌟

Stay updated with the latest tips and best practices for QA engineers in Android UI testing by following us on Medium. For more resources and tools to enhance your testing skills, visit our website EspressoLab.ai.

Follow us on Medium: EspressoLab Medium

Thank you for reading! 🚀

--

--

EspressoLab.Ai

Empowering Software Engineers with top-notch digital products and online courses.