From 6a818d802a8db6723d8be7a429214e65bf33026a Mon Sep 17 00:00:00 2001 From: Brett Chabot Date: Fri, 18 Jan 2019 19:30:45 -0800 Subject: [PATCH] Use GrantPermissionRule in IntentsBasicSample. Fixes #240 --- .../BasicSample/DialerActivityTest.java | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/ui/espresso/IntentsBasicSample/app/src/androidTest/java/com/example/android/testing/espresso/BasicSample/DialerActivityTest.java b/ui/espresso/IntentsBasicSample/app/src/androidTest/java/com/example/android/testing/espresso/BasicSample/DialerActivityTest.java index c74fac5..ad63ad8 100644 --- a/ui/espresso/IntentsBasicSample/app/src/androidTest/java/com/example/android/testing/espresso/BasicSample/DialerActivityTest.java +++ b/ui/espresso/IntentsBasicSample/app/src/androidTest/java/com/example/android/testing/espresso/BasicSample/DialerActivityTest.java @@ -50,6 +50,7 @@ import androidx.test.rule.ActivityTestRule; import androidx.test.ext.junit.runners.AndroidJUnit4; +import androidx.test.rule.GrantPermissionRule; import com.google.common.collect.Iterables; import org.junit.Before; @@ -65,6 +66,17 @@ public class DialerActivityTest { private static final Uri INTENT_DATA_PHONE_NUMBER = Uri.parse("tel:" + VALID_PHONE_NUMBER); + private static String PACKAGE_ANDROID_DIALER = "com.android.phone"; + + static { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + // Starting with Android Lollipop the dialer package has changed. + PACKAGE_ANDROID_DIALER = "com.android.server.telecom"; + } + } + + @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. @@ -86,17 +98,6 @@ public void stubAllExternalIntents() { intending(not(isInternal())).respondWith(new ActivityResult(Activity.RESULT_OK, null)); } - @Before - public void grantPhonePermission() { - // In M+, trying to call a number will trigger a runtime dialog. Make sure - // the permission is granted before running this test. - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - getInstrumentation().getUiAutomation().executeShellCommand( - "pm grant " + getApplicationContext().getPackageName() - + " android.permission.CALL_PHONE"); - } - } - @Test public void typeNumber_ValidInput_InitiatesCall() { // Types a phone number into the dialer edit text field and presses the call button. @@ -108,7 +109,8 @@ public void typeNumber_ValidInput_InitiatesCall() { // number and package. Think of Intents intended API as the equivalent to Mockito's verify. intended(allOf( hasAction(Intent.ACTION_CALL), - hasData(INTENT_DATA_PHONE_NUMBER))); + hasData(INTENT_DATA_PHONE_NUMBER), + toPackage(PACKAGE_ANDROID_DIALER))); } /**