Skip to content

Commit

Permalink
Use GrantPermissionRule in IntentsBasicSample.
Browse files Browse the repository at this point in the history
Fixes #240
  • Loading branch information
brettchabot authored and mukundsrinivasb committed Mar 31, 2023
1 parent 80f3c39 commit 6a818d8
Showing 1 changed file with 14 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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)));
}

/**
Expand Down

0 comments on commit 6a818d8

Please sign in to comment.