Search code examples
javaandroidunit-testingandroid-permissionsandroid-mvp

Android 3.1: Unit Testing a Class that Request User Permissions


I have a one button app that makes a phone call when pressed. The app uses MVP architecture. The logic to request the permission from a user to make a phone call was placed in the presenter (P) package.

Now, I'd like to setup a unit test that verifies that the class accurately requests the user permission to make a phone call yet I'm having issues getting started. There is an SO question: Android Marshmallow: Test permissions with Espresso? that attempts to address this problem stating one has to use UIAutomation and Espresso together. It's just not a clear enough answer to apply to this problem.

Can someone provide a solution to help setup this unit test?

MainActivityPresenter class

public class mainActivityPresenter {
    final int REQUEST_PHONE_CALL = 1;

    public void checkPhonePermissions(View view, MainActivity mainActivity){
        ActivityCompat.requestPermissions(mainActivity, new String[]{Manifest.permission.CALL_PHONE},REQUEST_PHONE_CALL);
    }

 }

Solution

  • check GrantPermissionRule.Rule allows granting of runtime permissions on Android M (API 23) and above. Use this Rule when a test requires a runtime permission to do its work.