Add test on DefaultPushService
This commit is contained in:
@@ -72,6 +72,7 @@ dependencies {
|
||||
testImplementation(projects.libraries.matrix.test)
|
||||
testImplementation(projects.libraries.push.test)
|
||||
testImplementation(projects.libraries.pushproviders.test)
|
||||
testImplementation(projects.libraries.pushstore.test)
|
||||
testImplementation(projects.tests.testutils)
|
||||
testImplementation(projects.services.appnavstate.test)
|
||||
testImplementation(projects.services.toolbox.impl)
|
||||
|
||||
@@ -46,9 +46,6 @@ class DefaultPushService @Inject constructor(
|
||||
.sortedBy { it.index }
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current push provider, compare with provided one, then unregister and register if different, and store change.
|
||||
*/
|
||||
override suspend fun registerWith(
|
||||
matrixClient: MatrixClient,
|
||||
pushProvider: PushProvider,
|
||||
|
||||
@@ -0,0 +1,221 @@
|
||||
/*
|
||||
* Copyright (c) 2024 New Vector Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package io.element.android.libraries.push.impl
|
||||
|
||||
import com.element.android.libraries.pushstore.test.userpushstore.FakeUserPushStore
|
||||
import com.element.android.libraries.pushstore.test.userpushstore.FakeUserPushStoreFactory
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import io.element.android.libraries.matrix.api.MatrixClient
|
||||
import io.element.android.libraries.matrix.test.AN_EXCEPTION
|
||||
import io.element.android.libraries.matrix.test.FakeMatrixClient
|
||||
import io.element.android.libraries.push.api.GetCurrentPushProvider
|
||||
import io.element.android.libraries.push.impl.test.FakeTestPush
|
||||
import io.element.android.libraries.push.impl.test.TestPush
|
||||
import io.element.android.libraries.push.test.FakeGetCurrentPushProvider
|
||||
import io.element.android.libraries.pushproviders.api.CurrentUserPushConfig
|
||||
import io.element.android.libraries.pushproviders.api.Distributor
|
||||
import io.element.android.libraries.pushproviders.api.PushProvider
|
||||
import io.element.android.libraries.pushproviders.test.FakePushProvider
|
||||
import io.element.android.libraries.pushstore.api.UserPushStoreFactory
|
||||
import io.element.android.tests.testutils.lambda.lambdaRecorder
|
||||
import io.element.android.tests.testutils.lambda.value
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.junit.Test
|
||||
|
||||
class DefaultPushServiceTest {
|
||||
@Test
|
||||
fun `test push no push provider`() = runTest {
|
||||
val defaultPushService = createDefaultPushService()
|
||||
assertThat(defaultPushService.testPush()).isFalse()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test push no config`() = runTest {
|
||||
val aPushProvider = FakePushProvider()
|
||||
val defaultPushService = createDefaultPushService(
|
||||
pushProviders = setOf(aPushProvider),
|
||||
getCurrentPushProvider = FakeGetCurrentPushProvider(currentPushProvider = aPushProvider.name),
|
||||
)
|
||||
assertThat(defaultPushService.testPush()).isFalse()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test push ok`() = runTest {
|
||||
val aConfig = CurrentUserPushConfig(
|
||||
url = "aUrl",
|
||||
pushKey = "aPushKey",
|
||||
)
|
||||
val testPushResult = lambdaRecorder<CurrentUserPushConfig, Unit> { }
|
||||
val aPushProvider = FakePushProvider(
|
||||
currentUserPushConfig = aConfig
|
||||
)
|
||||
val defaultPushService = createDefaultPushService(
|
||||
pushProviders = setOf(aPushProvider),
|
||||
getCurrentPushProvider = FakeGetCurrentPushProvider(currentPushProvider = aPushProvider.name),
|
||||
testPush = FakeTestPush(executeResult = testPushResult),
|
||||
)
|
||||
assertThat(defaultPushService.testPush()).isTrue()
|
||||
testPushResult.assertions()
|
||||
.isCalledExactly(1)
|
||||
.withSequence(listOf(value(aConfig)))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `getCurrentPushProvider null`() = runTest {
|
||||
val defaultPushService = createDefaultPushService()
|
||||
val result = defaultPushService.getCurrentPushProvider()
|
||||
assertThat(result).isNull()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `getCurrentPushProvider ok`() = runTest {
|
||||
val aPushProvider = FakePushProvider()
|
||||
val defaultPushService = createDefaultPushService(
|
||||
pushProviders = setOf(aPushProvider),
|
||||
getCurrentPushProvider = FakeGetCurrentPushProvider(currentPushProvider = aPushProvider.name),
|
||||
)
|
||||
val result = defaultPushService.getCurrentPushProvider()
|
||||
assertThat(result).isEqualTo(aPushProvider)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `getAvailablePushProviders empty`() = runTest {
|
||||
val defaultPushService = createDefaultPushService()
|
||||
val result = defaultPushService.getAvailablePushProviders()
|
||||
assertThat(result).isEmpty()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `registerWith ok`() = runTest {
|
||||
val client = FakeMatrixClient()
|
||||
val aPushProvider = FakePushProvider(
|
||||
registerWithResult = { _, _ -> Result.success(Unit) },
|
||||
)
|
||||
val aDistributor = Distributor("aValue", "aName")
|
||||
val defaultPushService = createDefaultPushService()
|
||||
val result = defaultPushService.registerWith(client, aPushProvider, aDistributor)
|
||||
assertThat(result).isEqualTo(Result.success(Unit))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `registerWith fail to register`() = runTest {
|
||||
val client = FakeMatrixClient()
|
||||
val aPushProvider = FakePushProvider(
|
||||
registerWithResult = { _, _ -> Result.failure(AN_EXCEPTION) },
|
||||
)
|
||||
val aDistributor = Distributor("aValue", "aName")
|
||||
val defaultPushService = createDefaultPushService()
|
||||
val result = defaultPushService.registerWith(client, aPushProvider, aDistributor)
|
||||
assertThat(result.isFailure).isTrue()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `registerWith fail to unregister previous push provider`() = runTest {
|
||||
val client = FakeMatrixClient()
|
||||
val aCurrentPushProvider = FakePushProvider(
|
||||
unregisterWithResult = { Result.failure(AN_EXCEPTION) },
|
||||
name = "aCurrentPushProvider",
|
||||
)
|
||||
val aPushProvider = FakePushProvider(
|
||||
name = "aPushProvider",
|
||||
)
|
||||
val userPushStore = FakeUserPushStore().apply {
|
||||
setPushProviderName(aCurrentPushProvider.name)
|
||||
}
|
||||
val aDistributor = Distributor("aValue", "aName")
|
||||
val defaultPushService = createDefaultPushService(
|
||||
pushProviders = setOf(aCurrentPushProvider, aPushProvider),
|
||||
getCurrentPushProvider = FakeGetCurrentPushProvider(currentPushProvider = aCurrentPushProvider.name),
|
||||
userPushStoreFactory = FakeUserPushStoreFactory(
|
||||
userPushStore = userPushStore,
|
||||
),
|
||||
)
|
||||
val result = defaultPushService.registerWith(client, aPushProvider, aDistributor)
|
||||
assertThat(result.isFailure).isTrue()
|
||||
assertThat(userPushStore.getPushProviderName()).isEqualTo(aCurrentPushProvider.name)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `registerWith unregister previous push provider and register new OK`() = runTest {
|
||||
val client = FakeMatrixClient()
|
||||
val unregisterLambda = lambdaRecorder<MatrixClient, Result<Unit>> { Result.success(Unit) }
|
||||
val registerLambda = lambdaRecorder<MatrixClient, Distributor, Result<Unit>> { _, _ -> Result.success(Unit) }
|
||||
val aCurrentPushProvider = FakePushProvider(
|
||||
unregisterWithResult = unregisterLambda,
|
||||
name = "aCurrentPushProvider",
|
||||
)
|
||||
val aPushProvider = FakePushProvider(
|
||||
registerWithResult = registerLambda,
|
||||
name = "aPushProvider",
|
||||
)
|
||||
val userPushStore = FakeUserPushStore().apply {
|
||||
setPushProviderName(aCurrentPushProvider.name)
|
||||
}
|
||||
val aDistributor = Distributor("aValue", "aName")
|
||||
val defaultPushService = createDefaultPushService(
|
||||
pushProviders = setOf(aCurrentPushProvider, aPushProvider),
|
||||
getCurrentPushProvider = FakeGetCurrentPushProvider(currentPushProvider = aCurrentPushProvider.name),
|
||||
userPushStoreFactory = FakeUserPushStoreFactory(
|
||||
userPushStore = userPushStore,
|
||||
),
|
||||
)
|
||||
val result = defaultPushService.registerWith(client, aPushProvider, aDistributor)
|
||||
assertThat(result.isSuccess).isTrue()
|
||||
assertThat(userPushStore.getPushProviderName()).isEqualTo(aPushProvider.name)
|
||||
unregisterLambda.assertions()
|
||||
.isCalledExactly(1)
|
||||
.withSequence(listOf(value(client)))
|
||||
registerLambda.assertions()
|
||||
.isCalledExactly(1)
|
||||
.withSequence(listOf(value(client), value(aDistributor)))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `getAvailablePushProviders sorted`() = runTest {
|
||||
val aPushProvider1 = FakePushProvider(
|
||||
index = 1,
|
||||
name = "aPushProvider1",
|
||||
)
|
||||
val aPushProvider2 = FakePushProvider(
|
||||
index = 2,
|
||||
name = "aPushProvider2",
|
||||
)
|
||||
val aPushProvider3 = FakePushProvider(
|
||||
index = 3,
|
||||
name = "aPushProvider3",
|
||||
)
|
||||
val defaultPushService = createDefaultPushService(
|
||||
pushProviders = setOf(aPushProvider1, aPushProvider3, aPushProvider2),
|
||||
)
|
||||
val result = defaultPushService.getAvailablePushProviders()
|
||||
assertThat(result).containsExactly(aPushProvider1, aPushProvider2, aPushProvider3).inOrder()
|
||||
}
|
||||
|
||||
private fun createDefaultPushService(
|
||||
testPush: TestPush = FakeTestPush(),
|
||||
userPushStoreFactory: UserPushStoreFactory = FakeUserPushStoreFactory(),
|
||||
pushProviders: Set<@JvmSuppressWildcards PushProvider> = emptySet(),
|
||||
getCurrentPushProvider: GetCurrentPushProvider = FakeGetCurrentPushProvider(currentPushProvider = null),
|
||||
): DefaultPushService {
|
||||
return DefaultPushService(
|
||||
testPush = testPush,
|
||||
userPushStoreFactory = userPushStoreFactory,
|
||||
pushProviders = pushProviders,
|
||||
getCurrentPushProvider = getCurrentPushProvider,
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* Copyright (c) 2024 New Vector Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package io.element.android.libraries.push.impl.test
|
||||
|
||||
import io.element.android.libraries.pushproviders.api.CurrentUserPushConfig
|
||||
|
||||
class FakeTestPush(
|
||||
private val executeResult: (CurrentUserPushConfig) -> Unit = { TODO() }
|
||||
) : TestPush {
|
||||
override suspend fun execute(config: CurrentUserPushConfig) {
|
||||
executeResult(config)
|
||||
}
|
||||
}
|
||||
@@ -26,13 +26,16 @@ class FakePushProvider(
|
||||
override val name: String = "aFakePushProvider",
|
||||
private val isAvailable: Boolean = true,
|
||||
private val distributors: List<Distributor> = listOf(Distributor("aDistributorValue", "aDistributorName")),
|
||||
private val currentUserPushConfig: CurrentUserPushConfig? = null,
|
||||
private val registerWithResult: (MatrixClient, Distributor) -> Result<Unit> = { _, _ -> TODO() },
|
||||
private val unregisterWithResult: (MatrixClient) -> Result<Unit> = { TODO() },
|
||||
) : PushProvider {
|
||||
override fun isAvailable(): Boolean = isAvailable
|
||||
|
||||
override fun getDistributors(): List<Distributor> = distributors
|
||||
|
||||
override suspend fun registerWith(matrixClient: MatrixClient, distributor: Distributor): Result<Unit> {
|
||||
return Result.success(Unit)
|
||||
return registerWithResult(matrixClient, distributor)
|
||||
}
|
||||
|
||||
override suspend fun getCurrentDistributor(matrixClient: MatrixClient): Distributor? {
|
||||
@@ -40,10 +43,10 @@ class FakePushProvider(
|
||||
}
|
||||
|
||||
override suspend fun unregister(matrixClient: MatrixClient): Result<Unit> {
|
||||
return Result.success(Unit)
|
||||
return unregisterWithResult(matrixClient)
|
||||
}
|
||||
|
||||
override suspend fun getCurrentUserPushConfig(): CurrentUserPushConfig? {
|
||||
return null
|
||||
return currentUserPushConfig
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,8 +20,10 @@ import io.element.android.libraries.matrix.api.core.SessionId
|
||||
import io.element.android.libraries.pushstore.api.UserPushStore
|
||||
import io.element.android.libraries.pushstore.api.UserPushStoreFactory
|
||||
|
||||
class FakeUserPushStoreFactory : UserPushStoreFactory {
|
||||
class FakeUserPushStoreFactory(
|
||||
val userPushStore: UserPushStore = FakeUserPushStore()
|
||||
) : UserPushStoreFactory {
|
||||
override fun getOrCreate(userId: SessionId): UserPushStore {
|
||||
return FakeUserPushStore()
|
||||
return userPushStore
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user