Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Robolectric unit test for Marshmallow PermissionHelper

I wanna learn Robolectric to use it for unit tests on an Android Marshmallow app. I wrote a PermissionHelper with some methods to make permission handling a bit easier. To get started with unit tests for this class, I am trying to test the most simple method:

public static boolean hasPermissions(Activity activity, String[] permissions) {
    for (String permission : permissions) {
        int status = ActivityCompat.checkSelfPermission(activity, permission);
        if (status == PackageManager.PERMISSION_DENIED) {
            return false;
        }
    }
    return true;
}

Here is the Robolectric test that I wrote so far:

@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class)
public class PermissionHelperTest {

    private PermissionHelper permissionHelper;
    private ShadowApplication application;

    @Before
    public void setup() {
        PictureActivity activity = Robolectric.buildActivity(PictureActivity.class).get();
        permissionHelper = new PermissionHelper(activity, activity, 1);
        application = new ShadowApplication();
    }

    @Test
    public void testHasPermission() throws Exception {
        String[] permissions = new String[]{Manifest.permission.RECORD_AUDIO, Manifest.permission.WRITE_EXTERNAL_STORAGE};
        boolean hasPermissions = permissionHelper.hasPermissions(permissions);
        Assert.assertEquals(false, hasPermissions);

        application.grantPermissions(permissions);
        hasPermissions = permissionHelper.hasPermissions(permissions);
        Assert.assertEquals(true, hasPermissions);
    }
}

The first Assert works (no permission granted). But after granting all permissions via the ShadowApplication they are still denied in the next Assert.

I think that the PictureActivity created with Robolectric.buildActivity() is not using the ShadowApplication for the permission checks. But PictureActivity.getApplication() does not give me a ShadowApplication to call grantPermissions on. How can I test this?

I am new to Robolectric and unit testing on Android...so if there is any other framework that makes this easier/possible: I am open for suggestions.

like image 374
muetzenflo Avatar asked Jan 27 '16 07:01

muetzenflo


People also ask

What should I test in unit tests Android?

Unit tests or small tests only verify a very small portion of the app, such as a method or class. End-to-end tests or big tests verify larger parts of the app at the same time, such as a whole screen or user flow. Medium tests are in between and check the integration between two or more units.

Which Java framework is used to write unit tests in android?

JUnit is a “Unit Testing” framework for Java Applications which is already included by default in android studio. It is an automation framework for Unit as well as UI Testing. It contains annotations such as @Test, @Before, @After, etc.


2 Answers

From Robolectric 4.2 you can use:

Application application = ApplicationProvider.getApplicationContext();
ShadowApplication app = Shadows.shadowOf(application);
app.grantPermissions(Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE);
like image 92
Руслан Ягупов Avatar answered Oct 06 '22 12:10

Руслан Ягупов


Your problem is that you're using a different application to grant permissions, not your own.

Instead of this:

application = new ShadowApplication();

you should get a shadow of your application, like this:

application = Shadows.shadowOf(activity.getApplication());
like image 38
Gent Ahmeti Avatar answered Oct 06 '22 11:10

Gent Ahmeti