Android Robolectric unit test for Marshmallow PermissionHelper -
i wanna learn robolectric use unit tests on android marshmallow app. wrote permissionhelper
methods make permission handling bit easier. started unit tests class, trying test simple method:
public static boolean haspermissions(activity activity, string[] permissions) { (string permission : permissions) { int status = activitycompat.checkselfpermission(activity, permission); if (status == packagemanager.permission_denied) { return false; } } return true; }
here robolectric test wrote 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). after granting permissions via shadowapplication still denied in next assert.
i think pictureactivity
created robolectric.buildactivity()
not using shadowapplication permission checks. pictureactivity.getapplication()
not give me shadowapplication
call grantpermissions
on. how can test this?
i new robolectric , unit testing on android...so if there other framework makes easier/possible: open suggestions.
your problem you're using different application grant permissions, not own.
instead of this:
application = new shadowapplication();
you should shadow of your application, this:
application = shadows.shadowof(activity.getapplication());
Comments
Post a Comment