2

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);
    }

 }
Val
  • 1,260
  • 5
  • 23
  • 39

1 Answers1

1

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.

sanemars
  • 767
  • 5
  • 18
  • 1
    I'm running tests on Android (API 14). Is there a substitute for earlier Android SDK versions? – Val Apr 11 '18 at 03:27
  • if your device API level under 23,you can use adb shell pm grant "com.your.package" android.permission.your_permission – sanemars Apr 11 '18 at 03:33
  • I don't see how testing the permission using shell commands is testing the mainActivityPresenter class? Couldn't we mock the "view" and "mainActivity" arguments to test if "mainActivityPresenter" class is working? – Val Apr 11 '18 at 03:41