Skip to content

Commit

Permalink
Merge pull request #13 from sshah112/Add-isInitialized-to-PermissionP…
Browse files Browse the repository at this point in the history
…rovider

add isInitialized() to PermissionProvider
  • Loading branch information
marcelpinto authored Sep 20, 2021
2 parents 9d6dd72 + d7a06a3 commit 66e7fe7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ class PermissionProvider @OptIn(ExperimentalCoroutinesApi::class) constructor(
lateinit var instance: PermissionProvider
private set

/**
* When PermissionProvider self-initialization is disabled, PermissionProvider.init(context)
* must be called to manually initialize the PermissionProvider instance. Depending on
* implementation if init() is called more than once (multiple times in app OR in app + test),
* an IllegalStateException is thrown.
*
* Provide an easy way to check if PermissionProvider instance is already
* initialized, before calling PermissionProvider.init(context).
*/
fun isInitialized() = ::instance.isInitialized

/**
* Initialize the PermissionProvider instance and wire the components to check and observe
* PermissionProvider status changes.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package dev.marcelpinto.permissionktx

import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.flow.emptyFlow
import org.junit.Test

class PermissionProviderTest {


private val fakeChecker = object : PermissionChecker {
override fun getStatus(type: Permission) = PermissionStatus.Granted(Permission("any"))
}

private val dummyObserver = object : PermissionObserver {
override fun getStatusFlow(type: Permission) = emptyFlow<PermissionStatus>()
override fun refreshStatus() {}
}

@Test
fun `test isInitialized when PermissionProvider is initialized`() {
assertThat(PermissionProvider.isInitialized()).isFalse()
PermissionProvider.init(fakeChecker, dummyObserver)
assertThat(PermissionProvider.isInitialized()).isTrue()
}
}

0 comments on commit 66e7fe7

Please sign in to comment.