diff --git a/library/src/main/java/io/ona/kujaku/helpers/PermissionsHelper.java b/library/src/main/java/io/ona/kujaku/helpers/PermissionsHelper.java index f5b0f6f4..021e0091 100644 --- a/library/src/main/java/io/ona/kujaku/helpers/PermissionsHelper.java +++ b/library/src/main/java/io/ona/kujaku/helpers/PermissionsHelper.java @@ -19,29 +19,23 @@ public class PermissionsHelper { public static void checkPermissions(String TAG, Context context) { if (context instanceof Activity) { + List permissions = new ArrayList<>(); + permissions.add(Manifest.permission.ACCESS_FINE_LOCATION); + + if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.S) { + permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE); + } final Activity activity = (Activity) context; MultiplePermissionsListener dialogMultiplePermissionListener = new KujakuMultiplePermissionListener(activity); - // Check permissions based on the Android version - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) { - // For Android versions below 13 - Dexter.withActivity(activity) - .withPermissions( - Manifest.permission.ACCESS_FINE_LOCATION, - Manifest.permission.WRITE_EXTERNAL_STORAGE - ) - .withListener(dialogMultiplePermissionListener) - .check(); - } else { - Dexter.withActivity(activity) - .withPermissions(Manifest.permission.ACCESS_FINE_LOCATION) - .withListener(dialogMultiplePermissionListener) - .check(); - } + Dexter.withActivity(activity) + .withPermissions(permissions) + .withListener(dialogMultiplePermissionListener) + .check(); } else { - Log.wtf(TAG, "KujakuMapView was not started in an activity!! This is very bad or it is being used in tests. We are going to ignore the permissions check! Good luck"); + Timber.tag(TAG).wtf("KujakuMapView was not started in an activity!! This is very bad or it is being used in tests. We are going to ignore the permissions check! Good luck"); } } } diff --git a/library/src/test/java/io/ona/kujaku/helpers/PermissionsHelperTest.java b/library/src/test/java/io/ona/kujaku/helpers/PermissionsHelperTest.java new file mode 100644 index 00000000..b7f2483d --- /dev/null +++ b/library/src/test/java/io/ona/kujaku/helpers/PermissionsHelperTest.java @@ -0,0 +1,56 @@ +package io.ona.kujaku.helpers; + +import static org.junit.Assert.assertFalse; + +import android.app.Activity; +import android.content.Context; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.robolectric.Robolectric; +import org.robolectric.RobolectricTestRunner; +import com.karumi.dexter.MultiplePermissionsReport; +import io.ona.kujaku.utils.KujakuMultiplePermissionListener; + +@RunWith(RobolectricTestRunner.class) +public class PermissionsHelperTest { + + @Mock + Context mockContext; + + @Before + public void setUp() { + MockitoAnnotations.openMocks(this); + // Use a real context + mockContext = Robolectric.setupActivity(Activity.class).getApplicationContext(); + } + + @Test + public void testOnPermissionsCheckedWhenAnyPermissionPermanentlyDenied() { + MultiplePermissionsReport report = Mockito.mock(MultiplePermissionsReport.class); + Mockito.when(report.isAnyPermissionPermanentlyDenied()).thenReturn(true); + Mockito.when(report.areAllPermissionsGranted()).thenReturn(false); + KujakuMultiplePermissionListener listener = new KujakuMultiplePermissionListener(mockContext); + listener.onPermissionsChecked(report); + + // Check that the dialog was created with the expected properties + Mockito.verify(report).isAnyPermissionPermanentlyDenied(); + } + + @Test + public void testOnPermissionsCheckedWhenAnyPermissionNotPermanentlyDenied() { + MultiplePermissionsReport report = Mockito.mock(MultiplePermissionsReport.class); + Mockito.when(report.isAnyPermissionPermanentlyDenied()).thenReturn(false); + Mockito.when(report.areAllPermissionsGranted()).thenReturn(false); + KujakuMultiplePermissionListener listener = new KujakuMultiplePermissionListener(mockContext); + listener.onPermissionsChecked(report); + + boolean result = report.isAnyPermissionPermanentlyDenied(); + + // Use the result in your assertions or further logic + assertFalse(result); + } +}