Add test for push distributor change.
This commit is contained in:
parent
1a6b042978
commit
3ace9aa160
4 changed files with 102 additions and 7 deletions
|
|
@ -91,6 +91,7 @@ dependencies {
|
||||||
testImplementation(projects.features.rageshake.test)
|
testImplementation(projects.features.rageshake.test)
|
||||||
testImplementation(projects.features.rageshake.impl)
|
testImplementation(projects.features.rageshake.impl)
|
||||||
testImplementation(projects.libraries.indicator.impl)
|
testImplementation(projects.libraries.indicator.impl)
|
||||||
|
testImplementation(projects.libraries.pushproviders.test)
|
||||||
testImplementation(projects.features.logout.impl)
|
testImplementation(projects.features.logout.impl)
|
||||||
testImplementation(projects.services.analytics.test)
|
testImplementation(projects.services.analytics.test)
|
||||||
testImplementation(projects.services.toolbox.test)
|
testImplementation(projects.services.toolbox.test)
|
||||||
|
|
|
||||||
|
|
@ -21,8 +21,16 @@ import app.cash.molecule.moleculeFlow
|
||||||
import app.cash.turbine.test
|
import app.cash.turbine.test
|
||||||
import com.google.common.truth.Truth.assertThat
|
import com.google.common.truth.Truth.assertThat
|
||||||
import io.element.android.compound.theme.Theme
|
import io.element.android.compound.theme.Theme
|
||||||
|
import io.element.android.libraries.architecture.AsyncAction
|
||||||
|
import io.element.android.libraries.matrix.api.MatrixClient
|
||||||
|
import io.element.android.libraries.matrix.test.FakeMatrixClient
|
||||||
import io.element.android.libraries.preferences.test.InMemoryAppPreferencesStore
|
import io.element.android.libraries.preferences.test.InMemoryAppPreferencesStore
|
||||||
import io.element.android.libraries.preferences.test.InMemorySessionPreferencesStore
|
import io.element.android.libraries.preferences.test.InMemorySessionPreferencesStore
|
||||||
|
import io.element.android.libraries.push.api.PushService
|
||||||
|
import io.element.android.libraries.push.test.FakePushService
|
||||||
|
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.tests.testutils.WarmUpRule
|
import io.element.android.tests.testutils.WarmUpRule
|
||||||
import io.element.android.tests.testutils.awaitLastSequentialItem
|
import io.element.android.tests.testutils.awaitLastSequentialItem
|
||||||
import kotlinx.coroutines.test.runTest
|
import kotlinx.coroutines.test.runTest
|
||||||
|
|
@ -100,11 +108,93 @@ class AdvancedSettingsPresenterTest {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `present - change push provider`() = runTest {
|
||||||
|
val presenter = createAdvancedSettingsPresenter(
|
||||||
|
pushService = createFakePushService(),
|
||||||
|
)
|
||||||
|
moleculeFlow(RecompositionMode.Immediate) {
|
||||||
|
presenter.present()
|
||||||
|
}.test {
|
||||||
|
val initialState = awaitLastSequentialItem()
|
||||||
|
assertThat(initialState.pushDistributor).isEqualTo(AsyncAction.Success("aDistributorName0"))
|
||||||
|
assertThat(initialState.pushDistributors).containsExactly("aDistributorName0", "aDistributorName1")
|
||||||
|
initialState.eventSink.invoke(AdvancedSettingsEvents.ChangePushProvider)
|
||||||
|
val withDialog = awaitItem()
|
||||||
|
assertThat(withDialog.showChangePushProviderDialog).isTrue()
|
||||||
|
// Cancel
|
||||||
|
withDialog.eventSink(AdvancedSettingsEvents.CancelChangePushProvider)
|
||||||
|
val withoutDialog = awaitItem()
|
||||||
|
assertThat(withoutDialog.showChangePushProviderDialog).isFalse()
|
||||||
|
withDialog.eventSink.invoke(AdvancedSettingsEvents.ChangePushProvider)
|
||||||
|
assertThat(awaitItem().showChangePushProviderDialog).isTrue()
|
||||||
|
withDialog.eventSink(AdvancedSettingsEvents.SetPushProvider(1))
|
||||||
|
val withNewProvider = awaitItem()
|
||||||
|
assertThat(withNewProvider.showChangePushProviderDialog).isFalse()
|
||||||
|
assertThat(withNewProvider.pushDistributor).isEqualTo(AsyncAction.Loading)
|
||||||
|
val lastItem = awaitItem()
|
||||||
|
assertThat(lastItem.pushDistributor).isEqualTo(AsyncAction.Success("aDistributorName1"))
|
||||||
|
cancelAndIgnoreRemainingEvents()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `present - change push provider error`() = runTest {
|
||||||
|
val presenter = createAdvancedSettingsPresenter(
|
||||||
|
pushService = createFakePushService(
|
||||||
|
registerWithLambda = { _, _, _ ->
|
||||||
|
Result.failure(Exception("An error"))
|
||||||
|
},
|
||||||
|
),
|
||||||
|
)
|
||||||
|
moleculeFlow(RecompositionMode.Immediate) {
|
||||||
|
presenter.present()
|
||||||
|
}.test {
|
||||||
|
val initialState = awaitLastSequentialItem()
|
||||||
|
initialState.eventSink.invoke(AdvancedSettingsEvents.ChangePushProvider)
|
||||||
|
val withDialog = awaitItem()
|
||||||
|
assertThat(withDialog.showChangePushProviderDialog).isTrue()
|
||||||
|
withDialog.eventSink(AdvancedSettingsEvents.SetPushProvider(1))
|
||||||
|
val withNewProvider = awaitItem()
|
||||||
|
assertThat(withNewProvider.showChangePushProviderDialog).isFalse()
|
||||||
|
assertThat(withNewProvider.pushDistributor).isEqualTo(AsyncAction.Loading)
|
||||||
|
val lastItem = awaitItem()
|
||||||
|
assertThat(lastItem.pushDistributor).isInstanceOf(AsyncAction.Failure::class.java)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun createFakePushService(
|
||||||
|
registerWithLambda: suspend (MatrixClient, PushProvider, Distributor) -> Result<Unit> = { _, _, _ ->
|
||||||
|
Result.success(Unit)
|
||||||
|
}
|
||||||
|
): PushService {
|
||||||
|
val pushProvider1 = FakePushProvider(
|
||||||
|
index = 0,
|
||||||
|
name = "aFakePushProvider0",
|
||||||
|
isAvailable = true,
|
||||||
|
distributors = listOf(Distributor("aDistributorValue0", "aDistributorName0")),
|
||||||
|
)
|
||||||
|
val pushProvider2 = FakePushProvider(
|
||||||
|
index = 1,
|
||||||
|
name = "aFakePushProvider1",
|
||||||
|
isAvailable = true,
|
||||||
|
distributors = listOf(Distributor("aDistributorValue1", "aDistributorName1")),
|
||||||
|
)
|
||||||
|
return FakePushService(
|
||||||
|
availablePushProviders = listOf(pushProvider1, pushProvider2),
|
||||||
|
registerWithLambda = registerWithLambda,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
private fun createAdvancedSettingsPresenter(
|
private fun createAdvancedSettingsPresenter(
|
||||||
appPreferencesStore: InMemoryAppPreferencesStore = InMemoryAppPreferencesStore(),
|
appPreferencesStore: InMemoryAppPreferencesStore = InMemoryAppPreferencesStore(),
|
||||||
sessionPreferencesStore: InMemorySessionPreferencesStore = InMemorySessionPreferencesStore(),
|
sessionPreferencesStore: InMemorySessionPreferencesStore = InMemorySessionPreferencesStore(),
|
||||||
|
matrixClient: MatrixClient = FakeMatrixClient(),
|
||||||
|
pushService: PushService = FakePushService(),
|
||||||
) = AdvancedSettingsPresenter(
|
) = AdvancedSettingsPresenter(
|
||||||
appPreferencesStore = appPreferencesStore,
|
appPreferencesStore = appPreferencesStore,
|
||||||
sessionPreferencesStore = sessionPreferencesStore,
|
sessionPreferencesStore = sessionPreferencesStore,
|
||||||
|
matrixClient = matrixClient,
|
||||||
|
pushService = pushService,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -23,25 +23,29 @@ import io.element.android.libraries.pushproviders.api.PushProvider
|
||||||
import io.element.android.tests.testutils.simulateLongTask
|
import io.element.android.tests.testutils.simulateLongTask
|
||||||
|
|
||||||
class FakePushService(
|
class FakePushService(
|
||||||
private val testPushBlock: suspend () -> Boolean = { true }
|
private val testPushBlock: suspend () -> Boolean = { true },
|
||||||
|
private val availablePushProviders: List<PushProvider> = emptyList(),
|
||||||
|
private val registerWithLambda: suspend (MatrixClient, PushProvider, Distributor) -> Result<Unit> = { _, _, _ ->
|
||||||
|
Result.success(Unit)
|
||||||
|
},
|
||||||
) : PushService {
|
) : PushService {
|
||||||
override fun notificationStyleChanged() {
|
override fun notificationStyleChanged() {
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun getCurrentPushProvider(): PushProvider? {
|
override suspend fun getCurrentPushProvider(): PushProvider? {
|
||||||
return null
|
return availablePushProviders.firstOrNull()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getAvailablePushProviders(): List<PushProvider> {
|
override fun getAvailablePushProviders(): List<PushProvider> {
|
||||||
return emptyList()
|
return availablePushProviders
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun registerWith(
|
override suspend fun registerWith(
|
||||||
matrixClient: MatrixClient,
|
matrixClient: MatrixClient,
|
||||||
pushProvider: PushProvider,
|
pushProvider: PushProvider,
|
||||||
distributor: Distributor,
|
distributor: Distributor,
|
||||||
): Result<Unit> {
|
): Result<Unit> = simulateLongTask {
|
||||||
return Result.success(Unit)
|
return registerWithLambda(matrixClient, pushProvider, distributor)
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun testPush(): Boolean = simulateLongTask {
|
override suspend fun testPush(): Boolean = simulateLongTask {
|
||||||
|
|
|
||||||
|
|
@ -25,7 +25,7 @@ class FakePushProvider(
|
||||||
override val index: Int = 0,
|
override val index: Int = 0,
|
||||||
override val name: String = "aFakePushProvider",
|
override val name: String = "aFakePushProvider",
|
||||||
private val isAvailable: Boolean = true,
|
private val isAvailable: Boolean = true,
|
||||||
private val distributors: List<Distributor> = emptyList()
|
private val distributors: List<Distributor> = listOf(Distributor("aDistributorValue", "aDistributorName")),
|
||||||
) : PushProvider {
|
) : PushProvider {
|
||||||
override fun isAvailable(): Boolean = isAvailable
|
override fun isAvailable(): Boolean = isAvailable
|
||||||
|
|
||||||
|
|
@ -36,7 +36,7 @@ class FakePushProvider(
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun getCurrentDistributor(matrixClient: MatrixClient): Distributor? {
|
override suspend fun getCurrentDistributor(matrixClient: MatrixClient): Distributor? {
|
||||||
return null
|
return distributors.firstOrNull()
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun unregister(matrixClient: MatrixClient): Result<Unit> {
|
override suspend fun unregister(matrixClient: MatrixClient): Result<Unit> {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue