Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Shadows for permission mocking instead of mocks #36

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 34 additions & 57 deletions library/src/test/java/com/vinted/coper/CoperImplTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class CoperImplTest {
@Before
fun setup() = runTest {
runIdlingMainThread { fixture.mockGetFragmentWithStub() }
mockCheckPermissions("test", PackageManager.PERMISSION_GRANTED)
shadowOf(activity).grantPermissions("test")
}

@Test
Expand All @@ -57,7 +57,7 @@ class CoperImplTest {
@Config(sdk = [23, 27])
fun request_permissionsIsGranted_grantedListConsistOfThisPermission() = runTest {
val permissionName = "granted"
mockCheckPermissions(permissionName, PackageManager.PERMISSION_GRANTED)
shadowOf(activity).grantPermissions(permissionName)

val response = fixture.request(permissionName)

Expand All @@ -70,8 +70,7 @@ class CoperImplTest {
fun request_twoPermissionsIsGranted_grantedListConsistOfThisPermissions() = runTest {
val firstPermission = "granted"
val secondPermission = "granted2"
mockCheckPermissions(firstPermission, PackageManager.PERMISSION_GRANTED)
mockCheckPermissions(secondPermission, PackageManager.PERMISSION_GRANTED)
shadowOf(activity).grantPermissions(firstPermission, secondPermission)

val response = fixture.request(firstPermission, secondPermission)

Expand All @@ -82,7 +81,7 @@ class CoperImplTest {
@Test
fun request_permissionsIsDeniedRationale_permissionDeniedRationaleResult() = runTest {
val permissionName = "denied"
mockCheckPermissions(permissionName, PackageManager.PERMISSION_DENIED)
shadowOf(activity).denyPermissions(permissionName)
activity.setShouldShowRequestPermissionRationale(
permission = permissionName,
rationale = true
Expand All @@ -101,7 +100,7 @@ class CoperImplTest {
@Test
fun request_permissionsIsDeniedPermanently_permissionDeniedPermanentlyResult() = runTest {
val permissionName = "denied_perm"
mockCheckPermissions(permissionName, PackageManager.PERMISSION_DENIED)
shadowOf(activity).denyPermissions(permissionName)
activity.setShouldShowRequestPermissionRationale(
permission = permissionName,
rationale = false
Expand All @@ -121,12 +120,11 @@ class CoperImplTest {
fun request_permissionsIsDeniedPermanentlyAndOtherRationale_bothPermissionsExist() = runTest {
val permissionPermanently = "denied_perm"
val permissionRationale = "denied_rat"
mockCheckPermissions(permissionPermanently, PackageManager.PERMISSION_DENIED)
shadowOf(activity).denyPermissions(permissionPermanently, permissionRationale)
activity.setShouldShowRequestPermissionRationale(
permission = permissionPermanently,
rationale = false
)
mockCheckPermissions(permissionRationale, PackageManager.PERMISSION_DENIED)
activity.setShouldShowRequestPermissionRationale(
permission = permissionRationale,
rationale = true
Expand Down Expand Up @@ -159,9 +157,8 @@ class CoperImplTest {
fun request_twoPermissionsOneGrantedAndOneDenied_onePermissionEmitsToOnGrantedOtherToOnDenied() {
return runTest {
val permissionDenied = "denied"
mockCheckPermissions(permissionDenied, PackageManager.PERMISSION_DENIED)
val permissionGranted = "granted"
mockCheckPermissions(permissionGranted, PackageManager.PERMISSION_DENIED)
shadowOf(activity).denyPermissions(permissionDenied, permissionGranted)

val response = executePermissionRequest(
permissionsToRequest = listOf(permissionGranted, permissionDenied),
Expand All @@ -183,7 +180,7 @@ class CoperImplTest {
@Test
fun request_permissionsIsDeniedButOnrequestGranted_permissionGrantedResult() = runTest {
val permissionName = "denied_and_granted"
mockCheckPermissions(permissionName, PackageManager.PERMISSION_DENIED)
shadowOf(activity).denyPermissions(permissionName)

val response = executePermissionRequest(
permissionsToRequest = listOf(permissionName),
Expand All @@ -199,8 +196,7 @@ class CoperImplTest {
return runTest {
val firstPermission = "first"
val secondPermission = "second"
mockCheckPermissions(firstPermission, PackageManager.PERMISSION_DENIED)
mockCheckPermissions(secondPermission, PackageManager.PERMISSION_DENIED)
shadowOf(activity).denyPermissions(firstPermission, secondPermission)

val response = executePermissionRequest(
permissionsToRequest = listOf(
Expand All @@ -223,8 +219,7 @@ class CoperImplTest {
return runTest {
val firstPermission = "first"
val secondPermission = "second"
mockCheckPermissions(firstPermission, PackageManager.PERMISSION_DENIED)
mockCheckPermissions(secondPermission, PackageManager.PERMISSION_DENIED)
shadowOf(activity).denyPermissions(firstPermission, secondPermission)

val response = executePermissionRequest(
permissionsToRequest = listOf(
Expand Down Expand Up @@ -255,8 +250,7 @@ class CoperImplTest {
permission = secondPermission,
rationale = true
)
mockCheckPermissions(firstPermission, PackageManager.PERMISSION_DENIED)
mockCheckPermissions(secondPermission, PackageManager.PERMISSION_DENIED)
shadowOf(activity).denyPermissions(firstPermission, secondPermission)

val response = executePermissionRequest(
permissionsToRequest = listOf(
Expand All @@ -281,8 +275,7 @@ class CoperImplTest {
fun request_twoParallelPermissionsRequestBothGranted_bothGotGranted() = runTest {
val firstPermission = "first"
val secondPermission = "second"
mockCheckPermissions(firstPermission, PackageManager.PERMISSION_DENIED)
mockCheckPermissions(secondPermission, PackageManager.PERMISSION_DENIED)
shadowOf(activity).denyPermissions(firstPermission, secondPermission)

val response1Job = async {
executePermissionRequest(
Expand Down Expand Up @@ -313,8 +306,7 @@ class CoperImplTest {
firstCoperReference.mockGetFragmentWithStub()
val secondCoperReference = getCoperInstance()
secondCoperReference.mockGetFragmentWithStub()
mockCheckPermissions(firstPermission, PackageManager.PERMISSION_DENIED)
mockCheckPermissions(secondPermission, PackageManager.PERMISSION_DENIED)
shadowOf(activity).denyPermissions(firstPermission, secondPermission)

val response1Job = async {
executePermissionRequest(
Expand Down Expand Up @@ -343,8 +335,7 @@ class CoperImplTest {
fun request_twoSynchronousPermissionsRequestBothGranted_bothGotGranted() = runTest {
val firstPermission = "first"
val secondPermission = "second"
mockCheckPermissions(firstPermission, PackageManager.PERMISSION_DENIED)
mockCheckPermissions(secondPermission, PackageManager.PERMISSION_DENIED)
shadowOf(activity).denyPermissions(firstPermission, secondPermission)

val response1 = executePermissionRequest(
permissionsToRequest = listOf(firstPermission),
Expand Down Expand Up @@ -428,8 +419,7 @@ class CoperImplTest {
@Test(expected = IllegalStateException::class)
fun request_resultPermissionsEmptyList_throwException() = runTest {
val crashPermission = "crash"
mockCheckPermissions(crashPermission, PackageManager.PERMISSION_DENIED)

shadowOf(activity).denyPermissions(crashPermission)

executePermissionRequest(
permissionsToRequest = emptyList(),
Expand All @@ -440,7 +430,7 @@ class CoperImplTest {
@Test(expected = IllegalStateException::class)
fun request_resultPermissionResultEmptyList_throwException() = runTest {
val crashPermission = "crash"
mockCheckPermissions(crashPermission, PackageManager.PERMISSION_DENIED)
shadowOf(activity).denyPermissions(crashPermission)

executePermissionRequest(listOf(crashPermission), emptyList())
}
Expand Down Expand Up @@ -478,7 +468,7 @@ class CoperImplTest {
)
val fragment = runIdlingMainThread { fixture.getFragmentSafely() }
val permission = "onConfigurationChange"
activity.setPermissionResult(permission, PackageManager.PERMISSION_DENIED)
shadowOf(activity).denyPermissions(permission)

val job = async {
fixture.request(permission)
Expand All @@ -498,7 +488,7 @@ class CoperImplTest {
@Test
fun withPermissions_requestGranted_bodyRun() = runTest {
val permission = "permission"
mockCheckPermissions(permission, PackageManager.PERMISSION_DENIED)
shadowOf(activity).denyPermissions(permission)

executeWithPermission(listOf(permission), listOf(PermissionChecker.PERMISSION_GRANTED)) {
assertTrue { it.grantedPermissions.isNotEmpty() }
Expand All @@ -508,7 +498,7 @@ class CoperImplTest {
@Test(expected = PermissionsRequestFailedException::class)
fun withPermissions_requestDenied_throwException() = runTest {
val permission = "permission"
mockCheckPermissions(permission, PackageManager.PERMISSION_DENIED)
shadowOf(activity).denyPermissions(permission)

executeWithPermission(listOf(permission), listOf(PermissionChecker.PERMISSION_DENIED)) {
}
Expand All @@ -517,7 +507,7 @@ class CoperImplTest {
@Test
fun request_permissionResultCameWithDifferentPermissions_jobIsNotCompleted() = runTest {
val permission = "permission"
mockCheckPermissions(permission, PackageManager.PERMISSION_DENIED)
shadowOf(activity).denyPermissions(permission)
val fragment = fixture.getFragmentSafely()
whenever(
fragment.requestPermissions(
Expand All @@ -544,7 +534,7 @@ class CoperImplTest {
fun request_onResumeCalledDuringRequest_permissionRequestStarted() = runTest {
val permission = "onResume"
val fragment = fixture.getFragmentSafely()
mockCheckPermissions(permission, PermissionChecker.PERMISSION_DENIED)
shadowOf(activity).denyPermissions(permission)

val responseAsync = async {
fixture.request(permission)
Expand All @@ -559,7 +549,7 @@ class CoperImplTest {
@Test
fun request_twoIdenticalRequest_twoRequestCompleted() = runTest {
val permission = "sameRequest"
mockCheckPermissions(permission, PermissionChecker.PERMISSION_DENIED)
shadowOf(activity).denyPermissions(permission)
val responseAsync1 = async {
executePermissionRequest(
permissionsToRequest = listOf(permission),
Expand All @@ -583,8 +573,7 @@ class CoperImplTest {
fun request_twoIdenticalRequestButOrderIsDifferent_twoRequestCompleted() = runTest {
val firstPermission = "firstPermission"
val secondPermission = "secondPermission"
mockCheckPermissions(firstPermission, PermissionChecker.PERMISSION_DENIED)
mockCheckPermissions(secondPermission, PermissionChecker.PERMISSION_DENIED)
shadowOf(activity).denyPermissions(firstPermission, secondPermission)

val responseAsync1 = async {
executePermissionRequest(
Expand All @@ -610,8 +599,7 @@ class CoperImplTest {
return runTest {
val firstPermission = "firstPermission"
val secondPermission = "secondPermission"
mockCheckPermissions(firstPermission, PermissionChecker.PERMISSION_DENIED)
mockCheckPermissions(secondPermission, PermissionChecker.PERMISSION_DENIED)
shadowOf(activity).denyPermissions(firstPermission, secondPermission)
val coperFragment = fixture.getFragmentSafely()
whenever(
coperFragment.requestPermissions(
Expand Down Expand Up @@ -644,9 +632,10 @@ class CoperImplTest {

@Test
fun isRequestPending_requestIsPending_returnsTrue() = runTest {
mockCheckPermissions("test", PermissionChecker.PERMISSION_DENIED)
val permission = "test"
shadowOf(activity).denyPermissions(permission)
val responseAsync = async {
fixture.request("test")
fixture.request(permission)
}
fixture.getFragmentSafely().permissionRequestStateFlow.filterNotNull().first()

Expand All @@ -659,29 +648,21 @@ class CoperImplTest {
@Test
@Config(sdk = [23, 27])
fun isPermissionsGranted_permissionsNotGranted_returnsFalse() = runTest {
mockCheckPermissions("not_granted", PermissionChecker.PERMISSION_DENIED)

val isGranted = fixture.isPermissionsGrantedSafe("not_granted")

assertFalse(isGranted)
}

@Test
@Config(sdk = [23, 27])
fun isPermissionsGranted_permissionsNotGrantedByOp_returnsFalse() = runTest {
mockCheckPermissions("not_granted_op", PermissionChecker.PERMISSION_DENIED_APP_OP)
val permission = "not_granted"
shadowOf(activity).denyPermissions(permission)

val isGranted = fixture.isPermissionsGrantedSafe("not_granted_op")
val isGranted = fixture.isPermissionsGrantedSafe(permission)

assertFalse(isGranted)
}

@Test
@Config(sdk = [23, 27])
fun isPermissionsGranted_permissionsGranted_returnsTrue() = runTest {
mockCheckPermissions("granted", PermissionChecker.PERMISSION_GRANTED)
val permission = "granted"
shadowOf(activity).grantPermissions(permission)

val isGranted = fixture.isPermissionsGrantedSafe("granted")
val isGranted = fixture.isPermissionsGrantedSafe(permission)

assertTrue(isGranted)
}
Expand All @@ -692,7 +673,7 @@ class CoperImplTest {
val requests = mutableListOf<Deferred<PermissionResult>>()
for (i in 0 until testCount) {
val permission = "test-$i"
mockCheckPermissions(permission, PermissionChecker.PERMISSION_DENIED)
shadowOf(activity).denyPermissions(permission)

requests.add(async {
executePermissionRequest(
Expand Down Expand Up @@ -825,10 +806,6 @@ class CoperImplTest {
}
}

private fun mockCheckPermissions(permission: String, result: Int) {
activity.setPermissionResult(permission, result)
}

private fun getCoperInstance(
fragmentManager: FragmentManager = activity.supportFragmentManager,
lifecycle: Lifecycle? = activity.lifecycle,
Expand Down
10 changes: 0 additions & 10 deletions library/src/test/java/com/vinted/coper/CoperTestActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,12 @@ package com.vinted.coper
import androidx.fragment.app.FragmentActivity

class CoperTestActivity : FragmentActivity() {

private val permissionResult = mutableMapOf<String, Int>()
private val shouldShowRequestPermissionRationaleResult = mutableMapOf<String, Boolean>()

fun setPermissionResult(permission: String, result: Int) {
permissionResult[permission] = result
}

fun setShouldShowRequestPermissionRationale(permission: String, rationale: Boolean) {
shouldShowRequestPermissionRationaleResult[permission] = rationale
}

override fun checkPermission(permission: String, pid: Int, uid: Int): Int {
return permissionResult[permission] ?: super.checkPermission(permission, pid, uid)
}

override fun shouldShowRequestPermissionRationale(permission: String): Boolean {
return shouldShowRequestPermissionRationaleResult[permission]
?: super.shouldShowRequestPermissionRationale(permission)
Expand Down