From 01c20d3188a68f0ecb728944df8e5e19220e76aa 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 --- .../espresso/BasicSample/DialerActivityTest.java | 14 +++----------- 1 file changed, 3 insertions(+), 11 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 5239a990a..ad63ad8ed 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; @@ -74,6 +75,8 @@ 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. @@ -95,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.