From bedf3b9e3e990cd04ace6648772097487042b465 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Mon, 19 Feb 2024 12:26:41 +0100 Subject: [PATCH] Add test for RoomListEvents.DismissRecoveryKeyPrompt. Also get the encryptionService from the matrixClient, instead of injecting it separately. --- .../roomlist/impl/RoomListPresenter.kt | 4 +- .../roomlist/impl/RoomListPresenterTests.kt | 44 ++++++++++++++++--- .../test/encryption/FakeEncryptionService.kt | 4 ++ .../android/samples/minimal/RoomListScreen.kt | 1 - 4 files changed, 43 insertions(+), 10 deletions(-) diff --git a/features/roomlist/impl/src/main/kotlin/io/element/android/features/roomlist/impl/RoomListPresenter.kt b/features/roomlist/impl/src/main/kotlin/io/element/android/features/roomlist/impl/RoomListPresenter.kt index 6edc533476..fa5cfd044a 100644 --- a/features/roomlist/impl/src/main/kotlin/io/element/android/features/roomlist/impl/RoomListPresenter.kt +++ b/features/roomlist/impl/src/main/kotlin/io/element/android/features/roomlist/impl/RoomListPresenter.kt @@ -75,13 +75,14 @@ class RoomListPresenter @Inject constructor( private val inviteStateDataSource: InviteStateDataSource, private val leaveRoomPresenter: LeaveRoomPresenter, private val roomListDataSource: RoomListDataSource, - private val encryptionService: EncryptionService, private val featureFlagService: FeatureFlagService, private val indicatorService: IndicatorService, private val searchPresenter: Presenter, private val migrationScreenPresenter: MigrationScreenPresenter, private val sessionPreferencesStore: SessionPreferencesStore, ) : Presenter { + private val encryptionService: EncryptionService = client.encryptionService() + @Composable override fun present(): RoomListState { val coroutineScope = rememberCoroutineScope() @@ -139,7 +140,6 @@ class RoomListPresenter @Inject constructor( contextMenu.value = RoomListState.ContextMenu.Hidden } is RoomListEvents.LeaveRoom -> leaveRoomState.eventSink(LeaveRoomEvent.ShowConfirmation(event.roomId)) - is RoomListEvents.SetRoomIsFavorite -> coroutineScope.launch { client.getRoom(event.roomId)?.use { room -> room.setIsFavorite(event.isFavorite) diff --git a/features/roomlist/impl/src/test/kotlin/io/element/android/features/roomlist/impl/RoomListPresenterTests.kt b/features/roomlist/impl/src/test/kotlin/io/element/android/features/roomlist/impl/RoomListPresenterTests.kt index 025caa3f96..6828821d68 100644 --- a/features/roomlist/impl/src/test/kotlin/io/element/android/features/roomlist/impl/RoomListPresenterTests.kt +++ b/features/roomlist/impl/src/test/kotlin/io/element/android/features/roomlist/impl/RoomListPresenterTests.kt @@ -42,13 +42,14 @@ import io.element.android.libraries.dateformatter.test.FakeLastMessageTimestampF import io.element.android.libraries.designsystem.utils.snackbar.SnackbarDispatcher import io.element.android.libraries.eventformatter.api.RoomLastMessageFormatter import io.element.android.libraries.eventformatter.test.FakeRoomLastMessageFormatter +import io.element.android.libraries.featureflag.api.FeatureFlagService import io.element.android.libraries.featureflag.api.FeatureFlags import io.element.android.libraries.featureflag.test.FakeFeatureFlagService import io.element.android.libraries.featureflag.test.InMemorySessionPreferencesStore import io.element.android.libraries.indicator.impl.DefaultIndicatorService import io.element.android.libraries.matrix.api.MatrixClient import io.element.android.libraries.matrix.api.encryption.BackupState -import io.element.android.libraries.matrix.api.encryption.EncryptionService +import io.element.android.libraries.matrix.api.encryption.RecoveryState import io.element.android.libraries.matrix.api.room.RoomNotificationMode import io.element.android.libraries.matrix.api.roomlist.RoomListService import io.element.android.libraries.matrix.api.timeline.ReceiptType @@ -108,9 +109,12 @@ class RoomListPresenterTests { fun `present - show avatar indicator`() = runTest { val scope = CoroutineScope(coroutineContext + SupervisorJob()) val encryptionService = FakeEncryptionService() + val matrixClient = FakeMatrixClient( + encryptionService = encryptionService, + ) val sessionVerificationService = FakeSessionVerificationService() val presenter = createRoomListPresenter( - encryptionService = encryptionService, + client = matrixClient, sessionVerificationService = sessionVerificationService, coroutineScope = scope ) @@ -255,6 +259,33 @@ class RoomListPresenterTests { } } + @Test + fun `present - handle DismissRecoveryKeyPrompt`() = runTest { + val encryptionService = FakeEncryptionService() + val matrixClient = FakeMatrixClient( + encryptionService = encryptionService, + ) + val scope = CoroutineScope(context = coroutineContext + SupervisorJob()) + val presenter = createRoomListPresenter( + client = matrixClient, + coroutineScope = scope, + ) + moleculeFlow(RecompositionMode.Immediate) { + presenter.present() + }.test { + skipItems(1) + val initialState = awaitItem() + assertThat(initialState.displayRecoveryKeyPrompt).isFalse() + encryptionService.emitRecoveryState(RecoveryState.INCOMPLETE) + val nextState = awaitItem() + assertThat(nextState.displayRecoveryKeyPrompt).isTrue() + nextState.eventSink(RoomListEvents.DismissRecoveryKeyPrompt) + val finalState = awaitItem() + assertThat(finalState.displayRecoveryKeyPrompt).isFalse() + scope.cancel() + } + } + @Test fun `present - sets invite state`() = runTest { val inviteStateFlow = MutableStateFlow(InvitesState.NoInvites) @@ -506,8 +537,8 @@ class RoomListPresenterTests { givenFormat(A_FORMATTED_DATE) }, roomLastMessageFormatter: RoomLastMessageFormatter = FakeRoomLastMessageFormatter(), - encryptionService: EncryptionService = FakeEncryptionService(), sessionPreferencesStore: SessionPreferencesStore = InMemorySessionPreferencesStore(), + featureFlagService: FeatureFlagService = FakeFeatureFlagService(mapOf(FeatureFlags.SecureStorage.key to true)), coroutineScope: CoroutineScope, migrationScreenPresenter: MigrationScreenPresenter = MigrationScreenPresenter( matrixClient = client, @@ -531,12 +562,11 @@ class RoomListPresenterTests { notificationSettingsService = client.notificationSettingsService(), appScope = coroutineScope ), - encryptionService = encryptionService, - featureFlagService = FakeFeatureFlagService(mapOf(FeatureFlags.SecureStorage.key to true)), + featureFlagService = featureFlagService, indicatorService = DefaultIndicatorService( sessionVerificationService = sessionVerificationService, - encryptionService = encryptionService, - featureFlagService = FakeFeatureFlagService(mapOf(FeatureFlags.SecureStorage.key to true)), + encryptionService = client.encryptionService(), + featureFlagService = featureFlagService, ), migrationScreenPresenter = migrationScreenPresenter, searchPresenter = searchPresenter, diff --git a/libraries/matrix/test/src/main/kotlin/io/element/android/libraries/matrix/test/encryption/FakeEncryptionService.kt b/libraries/matrix/test/src/main/kotlin/io/element/android/libraries/matrix/test/encryption/FakeEncryptionService.kt index 945821f7f2..7593a5ba51 100644 --- a/libraries/matrix/test/src/main/kotlin/io/element/android/libraries/matrix/test/encryption/FakeEncryptionService.kt +++ b/libraries/matrix/test/src/main/kotlin/io/element/android/libraries/matrix/test/encryption/FakeEncryptionService.kt @@ -103,6 +103,10 @@ class FakeEncryptionService : EncryptionService { backupStateStateFlow.emit(state) } + suspend fun emitRecoveryState(state: RecoveryState) { + recoveryStateStateFlow.emit(state) + } + suspend fun emitEnableRecoveryProgress(state: EnableRecoveryProgress) { enableRecoveryProgressStateFlow.emit(state) } diff --git a/samples/minimal/src/main/kotlin/io/element/android/samples/minimal/RoomListScreen.kt b/samples/minimal/src/main/kotlin/io/element/android/samples/minimal/RoomListScreen.kt index 58981000f0..fe9d7d1202 100644 --- a/samples/minimal/src/main/kotlin/io/element/android/samples/minimal/RoomListScreen.kt +++ b/samples/minimal/src/main/kotlin/io/element/android/samples/minimal/RoomListScreen.kt @@ -101,7 +101,6 @@ class RoomListScreen( notificationSettingsService = matrixClient.notificationSettingsService(), appScope = Singleton.appScope ), - encryptionService = encryptionService, indicatorService = DefaultIndicatorService( sessionVerificationService = sessionVerificationService, encryptionService = encryptionService,