Android Marshmallow: Test permissions with Espresso?

前端 未结 13 1354
庸人自扰
庸人自扰 2020-11-29 19:31

The new permissions scheme introduced by Android Marshmallow requires checking for specific permissions at runtime, which implies the need to provide different flows dependi

13条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-29 20:34

    You can grant permissions before the test is run with something like:

    @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 " + getTargetContext().getPackageName()
                            + " android.permission.CALL_PHONE");
        }
    }
    

    But you can't revoke. If you try pm reset-permissions or pm revoke... the process is killed.

提交回复
热议问题