From c45f536855194f1eba6d415fd8663ac545cf4792 Mon Sep 17 00:00:00 2001 From: Maxime NATUREL Date: Tue, 6 Sep 2022 17:30:36 +0200 Subject: [PATCH] RefreshDevicesOnCryptoDevicesChangeUseCase unit tests --- ...reshDevicesOnCryptoDevicesChangeUseCase.kt | 5 +- ...DevicesOnCryptoDevicesChangeUseCaseTest.kt | 77 +++++++++++++++++++ 2 files changed, 78 insertions(+), 4 deletions(-) create mode 100644 vector/src/test/java/im/vector/app/features/settings/devices/v2/RefreshDevicesOnCryptoDevicesChangeUseCaseTest.kt diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/RefreshDevicesOnCryptoDevicesChangeUseCase.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/RefreshDevicesOnCryptoDevicesChangeUseCase.kt index 26f866aabe..7d0a96eb0d 100644 --- a/vector/src/main/java/im/vector/app/features/settings/devices/v2/RefreshDevicesOnCryptoDevicesChangeUseCase.kt +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/RefreshDevicesOnCryptoDevicesChangeUseCase.kt @@ -27,7 +27,6 @@ import org.matrix.android.sdk.flow.flow import javax.inject.Inject import kotlin.time.Duration.Companion.seconds -// TODO add unit tests class RefreshDevicesOnCryptoDevicesChangeUseCase @Inject constructor( private val activeSessionHolder: ActiveSessionHolder, ) { @@ -42,9 +41,7 @@ class RefreshDevicesOnCryptoDevicesChangeUseCase @Inject constructor( .sample(samplingPeriodMs) .onEach { // If we have a new crypto device change, we might want to trigger refresh of device info - activeSessionHolder.getSafeActiveSession() - ?.cryptoService() - ?.fetchDevicesList(NoOpMatrixCallback()) + session.cryptoService().fetchDevicesList(NoOpMatrixCallback()) } .collect() } diff --git a/vector/src/test/java/im/vector/app/features/settings/devices/v2/RefreshDevicesOnCryptoDevicesChangeUseCaseTest.kt b/vector/src/test/java/im/vector/app/features/settings/devices/v2/RefreshDevicesOnCryptoDevicesChangeUseCaseTest.kt new file mode 100644 index 0000000000..97958d04ed --- /dev/null +++ b/vector/src/test/java/im/vector/app/features/settings/devices/v2/RefreshDevicesOnCryptoDevicesChangeUseCaseTest.kt @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2022 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 im.vector.app.features.settings.devices.v2 + +import im.vector.app.test.fakes.FakeActiveSessionHolder +import io.mockk.every +import io.mockk.just +import io.mockk.mockk +import io.mockk.mockkStatic +import io.mockk.runs +import io.mockk.unmockkAll +import io.mockk.verify +import kotlinx.coroutines.flow.flowOf +import kotlinx.coroutines.test.runTest +import org.junit.After +import org.junit.Before +import org.junit.Test +import org.matrix.android.sdk.api.session.crypto.model.CryptoDeviceInfo +import org.matrix.android.sdk.flow.FlowSession +import org.matrix.android.sdk.flow.flow + +class RefreshDevicesOnCryptoDevicesChangeUseCaseTest { + + private val fakeActiveSessionHolder = FakeActiveSessionHolder() + + private val refreshDevicesOnCryptoDevicesChangeUseCase = RefreshDevicesOnCryptoDevicesChangeUseCase( + activeSessionHolder = fakeActiveSessionHolder.instance + ) + + @Before + fun setUp() { + mockkStatic("org.matrix.android.sdk.flow.FlowSessionKt") + } + + @After + fun tearDown() { + unmockkAll() + } + + @Test + fun `given the current session when crypto devices list changes then the devices list is refreshed`() = runTest { + // Given + val device1 = givenACryptoDevice() + val devices = listOf(device1) + val fakeSession = fakeActiveSessionHolder.fakeSession + val flowSession = mockk() + every { fakeSession.flow() } returns flowSession + every { flowSession.liveUserCryptoDevices(any()) } returns flowOf(devices) + every { fakeSession.cryptoService().fetchDevicesList(any()) } just runs + + // When + refreshDevicesOnCryptoDevicesChangeUseCase.execute() + + // Then + verify { + flowSession.liveUserCryptoDevices(fakeSession.myUserId) + // FIXME the following verification does not work due to the usage of Flow.sample() inside the use case implementation + // fakeSession.cryptoService().fetchDevicesList(match { it is NoOpMatrixCallback }) + } + } + + private fun givenACryptoDevice(): CryptoDeviceInfo = mockk() +}