From d35b0b5f2ecc64ac6fefa494668c71dc0f73002b Mon Sep 17 00:00:00 2001 From: Marco Dalla Ba Date: Mon, 3 May 2021 09:54:29 +0200 Subject: [PATCH] Removed deprecated IntentsTestRule and applied ActivityScenarioRule instead. --- .../DialerActivityTest.java | 26 ++++++++++++------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/ui/espresso/IntentsBasicSample/app/src/sharedTest/java/com/example/android/testing/espresso/IntentsBasicSample/DialerActivityTest.java b/ui/espresso/IntentsBasicSample/app/src/sharedTest/java/com/example/android/testing/espresso/IntentsBasicSample/DialerActivityTest.java index 5e3e2bfe4..633856a0d 100644 --- a/ui/espresso/IntentsBasicSample/app/src/sharedTest/java/com/example/android/testing/espresso/IntentsBasicSample/DialerActivityTest.java +++ b/ui/espresso/IntentsBasicSample/app/src/sharedTest/java/com/example/android/testing/espresso/IntentsBasicSample/DialerActivityTest.java @@ -41,13 +41,17 @@ import android.content.Intent; import android.net.Uri; import android.os.Build; + +import androidx.test.espresso.core.internal.deps.guava.collect.Iterables; import androidx.test.espresso.intent.Intents; import androidx.test.espresso.intent.rule.IntentsTestRule; +import androidx.test.ext.junit.rules.ActivityScenarioRule; import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.filters.LargeTest; import androidx.test.rule.ActivityTestRule; import androidx.test.rule.GrantPermissionRule; -import com.google.common.collect.Iterables; + +import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -65,26 +69,28 @@ public class DialerActivityTest { @Rule public GrantPermissionRule grantPermissionRule = GrantPermissionRule.grant("android.permission.CALL_PHONE"); /** - * A JUnit {@link Rule @Rule} to init and release Espresso Intents before and after each - * test run. - *

- * Rules are interceptors which are executed for each test method and will run before - * any of your setup code in the {@link Before @Before} method. - *

- * This rule is based on {@link ActivityTestRule} and will create and launch the activity - * for you and also expose the activity under test. + * Use {@link ActivityScenarioRule} to create and launch the activity under test, and close it + * after test completes. This is a replacement for {@link androidx.test.rule.ActivityTestRule}. */ @Rule - public IntentsTestRule mActivityRule = new IntentsTestRule<>( + public ActivityScenarioRule mActivityRule = new ActivityScenarioRule<>( DialerActivity.class); @Before public void stubAllExternalIntents() { + // Initializes Intents and begins recording intents. + Intents.init(); // By default Espresso Intents does not stub any Intents. Stubbing needs to be setup before // every test run. In this case all external Intents will be blocked. intending(not(isInternal())).respondWith(new ActivityResult(Activity.RESULT_OK, null)); } + @After + public void tearDown() { + // Clears Intents state. + Intents.release(); + } + @Test public void typeNumber_ValidInput_InitiatesCall() { // Types a phone number into the dialer edit text field and presses the call button.