Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GrantPermissionRule stopped working

The GrantPermissionRule that I successfully implemented in my Android app on 1/3/2018 no longer works. When I run my Espresso tests through Android Studio, the emulator blocks waiting for permissions. However, when I run the tests from the command line with ./gradlew dist; ./gradlew connectedDebugAndroidTest --stacktrace it doesn't ask for the permissions. Note I do wipe data from the emulator manually before each run to ensure it's a proper test of GrantPermissionRule.

Here are the original references I used to implement GrantPermissionRule: https://www.kotlindevelopment.com/runtime-permissions-espresso-done-right/ https://developer.android.com/reference/android/support/test/rule/GrantPermissionRule.html

Versions in app/build.gradle:

  • com.android.support.test.espresso:espresso-core:'3.0.1'
  • com.android.support.test:runner:'1.0.1'

Has GrantPermissionRule stopped working as-advertised for anyone else?

like image 378
Michael Osofsky Avatar asked Feb 07 '18 23:02

Michael Osofsky


2 Answers

In my case, I was trying to automate Location permissions in Espresso UI tests on Emulator Pixel 2 API R.

UseCase 1:

When I was adding both ACCESS_FINE_LOCATION & ACCESS_COARSE_LOCATION in GrantPermissionRule then RunTime permission popup was not auto disappearing.

Not Working:

@Rule
@JvmField
val grantPermissionRule: GrantPermissionRule = 
GrantPermissionRule.grant(android.Manifest.permission.ACCESS_FINE_LOCATION, 
android.Manifest.permission.ACCESS_COARSE_LOCATION)

Then I removed ACCESS_COARSE_LOCATION from GrantPermissionRule and the automation starts working.

Working:

@Rule
@JvmField
val grantPermissionRule: GrantPermissionRule = 
GrantPermissionRule.grant(android.Manifest.permission.ACCESS_FINE_LOCATION) 

UseCase 2:

Also below implementation working as above mentioned (failure/success)use-case.

Not Working:

@Before
fun grantPhonePermission() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        getInstrumentation().uiAutomation.executeShellCommand(
            "pm grant " + getApplicationContext<Context>()
                .packageName
                    + " android.permission.ACCESS_FINE_LOCATION"
        )
        getInstrumentation().uiAutomation.executeShellCommand(
            "pm grant " + getApplicationContext<Context>()
                .packageName
                    + " android.permission.ACCESS_COARSE_LOCATION"
        )
    }
}

Working:

@Before
fun grantPhonePermission() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        getInstrumentation().uiAutomation.executeShellCommand(
            "pm grant " + getApplicationContext<Context>()
                .packageName
                    + " android.permission.ACCESS_FINE_LOCATION"
        )
    }
}
like image 65
Muhammad Maqsood Avatar answered Nov 18 '22 01:11

Muhammad Maqsood


I tried using

@Rule 
public GrantPermissionRule mRuntimePermissionRule = GrantPermissionRule.grant(Manifest.permission.ACCESS_FINE_LOCATION);

And this did not work. The permission alerts still show up when I run my espresso tests. This has been working for me though:

@Before
public void grantPhonePermission() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        getInstrumentation().getUiAutomation().executeShellCommand(
                "pm grant " + getTargetContext().getPackageName()
                        + " android.permission.ACCESS_FINE_LOCATION");
    }
}
like image 36
Zeek Aran Avatar answered Nov 18 '22 02:11

Zeek Aran