package com.example.app import android.app.Activity import android.app.Instrumentation.ActivityResult import android.content.Intent import androidx.test.espresso.Espresso.onView import androidx.test.espresso.action.ViewActions.click import androidx.test.espresso.assertion.ViewAssertions.matches import androidx.test.espresso.intent.Intents.intended import androidx.test.espresso.intent.Intents.intending import androidx.test.espresso.intent.matcher.IntentMatchers.hasAction import androidx.test.espresso.intent.matcher.IntentMatchers.toPackage import androidx.test.espresso.intent.rule.IntentsRule import androidx.test.espresso.matcher.ViewMatchers.withId import androidx.test.espresso.matcher.ViewMatchers.withText import androidx.test.ext.junit.rules.ActivityScenarioRule import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.filters.LargeTest import org.hamcrest.Matchers.allOf import org.junit.Rule import org.junit.Test import org.junit.rules.RuleChain import org.junit.rules.TestRule import org.junit.runner.RunWith @RunWith(AndroidJUnit4::class) @LargeTest class ContactIntentTest { @get:Rule val rules: TestRule = RuleChain .outerRule(IntentsRule()) .around( ActivityScenarioRule( ContactActivity::class.java ) ) @Test fun stubsContactResult() { val resultData = Intent().putExtra( "phone", "555-0100" ) val result = ActivityResult( Activity.RESULT_OK, resultData ) intending( allOf( hasAction(Intent.ACTION_PICK), toPackage("com.android.contacts") ) ).respondWith(result) onView(withId(R.id.pick_contact)) .perform(click()) intended( allOf( hasAction(Intent.ACTION_PICK), toPackage("com.android.contacts") ) ) onView(withId(R.id.phone_number)) .check( matches(withText("555-0100")) ) } }