Update PushProvider API, remove isAvailable(), but instead rely on getDistributors() to eventually return an empty list of Distributors.
This commit is contained in:
committed by
Benoit Marty
parent
059c0dfe59
commit
90eeb6cdb1
@@ -29,8 +29,7 @@ interface PushService {
|
||||
suspend fun getCurrentPushProvider(): PushProvider?
|
||||
|
||||
/**
|
||||
* Return the list of push providers, available at compile time, and
|
||||
* available at runtime, sorted by index.
|
||||
* Return the list of push providers, available at compile time, sorted by index.
|
||||
*/
|
||||
fun getAvailablePushProviders(): List<PushProvider>
|
||||
|
||||
|
||||
@@ -44,7 +44,6 @@ class DefaultPushService @Inject constructor(
|
||||
|
||||
override fun getAvailablePushProviders(): List<PushProvider> {
|
||||
return pushProviders
|
||||
.filter { it.isAvailable() }
|
||||
.sortedBy { it.index }
|
||||
}
|
||||
|
||||
|
||||
@@ -33,10 +33,8 @@ interface PushProvider {
|
||||
val name: String
|
||||
|
||||
/**
|
||||
* Return true if the push provider is available on this device.
|
||||
* Return the list of available distributors.
|
||||
*/
|
||||
fun isAvailable(): Boolean
|
||||
|
||||
fun getDistributors(): List<Distributor>
|
||||
|
||||
/**
|
||||
|
||||
@@ -38,12 +38,10 @@ class FirebasePushProvider @Inject constructor(
|
||||
override val index = FirebaseConfig.INDEX
|
||||
override val name = FirebaseConfig.NAME
|
||||
|
||||
override fun isAvailable(): Boolean {
|
||||
return isPlayServiceAvailable.isAvailable()
|
||||
}
|
||||
|
||||
override fun getDistributors(): List<Distributor> {
|
||||
return listOf(firebaseDistributor)
|
||||
return listOfNotNull(
|
||||
firebaseDistributor.takeIf { isPlayServiceAvailable.isAvailable() }
|
||||
)
|
||||
}
|
||||
|
||||
override suspend fun registerWith(matrixClient: MatrixClient, distributor: Distributor): Result<Unit> {
|
||||
|
||||
@@ -38,12 +38,23 @@ class FirebasePushProviderTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `getDistributors return the unique distributor`() {
|
||||
val firebasePushProvider = createFirebasePushProvider()
|
||||
fun `getDistributors return the unique distributor if available`() {
|
||||
val firebasePushProvider = createFirebasePushProvider(
|
||||
isPlayServiceAvailable = FakeIsPlayServiceAvailable(isAvailable = true)
|
||||
)
|
||||
val result = firebasePushProvider.getDistributors()
|
||||
assertThat(result).containsExactly(Distributor("Firebase", "Firebase"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `getDistributors return empty list if service is not available`() {
|
||||
val firebasePushProvider = createFirebasePushProvider(
|
||||
isPlayServiceAvailable = FakeIsPlayServiceAvailable(isAvailable = false)
|
||||
)
|
||||
val result = firebasePushProvider.getDistributors()
|
||||
assertThat(result).isEmpty()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `getCurrentDistributor always return the unique distributor`() = runTest {
|
||||
val firebasePushProvider = createFirebasePushProvider()
|
||||
@@ -51,22 +62,6 @@ class FirebasePushProviderTest {
|
||||
assertThat(result).isEqualTo(Distributor("Firebase", "Firebase"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `isAvailable true`() {
|
||||
val firebasePushProvider = createFirebasePushProvider(
|
||||
isPlayServiceAvailable = FakeIsPlayServiceAvailable(isAvailable = true)
|
||||
)
|
||||
assertThat(firebasePushProvider.isAvailable()).isTrue()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `isAvailable false`() {
|
||||
val firebasePushProvider = createFirebasePushProvider(
|
||||
isPlayServiceAvailable = FakeIsPlayServiceAvailable(isAvailable = false)
|
||||
)
|
||||
assertThat(firebasePushProvider.isAvailable()).isFalse()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `register ok`() = runTest {
|
||||
val matrixClient = FakeMatrixClient()
|
||||
|
||||
@@ -25,14 +25,12 @@ import io.element.android.tests.testutils.lambda.lambdaError
|
||||
class FakePushProvider(
|
||||
override val index: Int = 0,
|
||||
override val name: String = "aFakePushProvider",
|
||||
private val isAvailable: Boolean = true,
|
||||
private val distributors: List<Distributor> = listOf(Distributor("aDistributorValue", "aDistributorName")),
|
||||
private val currentDistributor: () -> Distributor? = { distributors.firstOrNull() },
|
||||
private val currentUserPushConfig: CurrentUserPushConfig? = null,
|
||||
private val registerWithResult: (MatrixClient, Distributor) -> Result<Unit> = { _, _ -> lambdaError() },
|
||||
private val unregisterWithResult: (MatrixClient) -> Result<Unit> = { lambdaError() },
|
||||
) : PushProvider {
|
||||
override fun isAvailable(): Boolean = isAvailable
|
||||
|
||||
override fun getDistributors(): List<Distributor> = distributors
|
||||
|
||||
|
||||
@@ -43,17 +43,6 @@ class UnifiedPushProvider @Inject constructor(
|
||||
override val index = UnifiedPushConfig.INDEX
|
||||
override val name = UnifiedPushConfig.NAME
|
||||
|
||||
override fun isAvailable(): Boolean {
|
||||
val isAvailable = getDistributors().isNotEmpty()
|
||||
return if (isAvailable) {
|
||||
Timber.tag(loggerTag.value).d("UnifiedPush is available")
|
||||
true
|
||||
} else {
|
||||
Timber.tag(loggerTag.value).w("UnifiedPush is not available")
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
override fun getDistributors(): List<Distributor> {
|
||||
return unifiedPushDistributorProvider.getDistributors()
|
||||
}
|
||||
|
||||
@@ -58,7 +58,6 @@ class UnifiedPushProviderTest {
|
||||
)
|
||||
val result = unifiedPushProvider.getDistributors()
|
||||
assertThat(result).containsExactly(Distributor("value", "Name"))
|
||||
assertThat(unifiedPushProvider.isAvailable()).isTrue()
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -70,7 +69,6 @@ class UnifiedPushProviderTest {
|
||||
)
|
||||
val result = unifiedPushProvider.getDistributors()
|
||||
assertThat(result).isEmpty()
|
||||
assertThat(unifiedPushProvider.isAvailable()).isFalse()
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user