Skip to content

Commit

Permalink
undo delete test file.
Browse files Browse the repository at this point in the history
Signed-off-by: Lentumunai-Mark <[email protected]>
  • Loading branch information
Lentumunai-Mark committed Oct 14, 2024
1 parent 0ad0a69 commit c8d44fb
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 17 deletions.
28 changes: 11 additions & 17 deletions library/src/main/java/io/ona/kujaku/helpers/PermissionsHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,23 @@ public class PermissionsHelper {

public static void checkPermissions(String TAG, Context context) {
if (context instanceof Activity) {
List<String> 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");
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit c8d44fb

Please sign in to comment.