From 0969470066bfd03a98dc57c53f631f3b01c9edc6 Mon Sep 17 00:00:00 2001 From: ganfra Date: Wed, 31 Jul 2024 21:14:29 +0200 Subject: [PATCH] tests : refactor some classes --- .../roomlist/impl/RoomListPresenterTest.kt | 5 +++-- .../libraries/matrix/test/room/FakeMatrixRoom.kt | 5 ++++- .../matrix/test/sync/FakeSyncService.kt | 16 +++++----------- libraries/push/impl/build.gradle.kts | 2 ++ .../impl/DefaultNavigationStateServiceTest.kt | 1 + .../test}/FakeAppForegroundStateService.kt | 2 +- 6 files changed, 16 insertions(+), 15 deletions(-) rename services/appnavstate/{impl/src/test/kotlin/io/element/android/services/appnavstate/impl => test/src/main/kotlin/io/element/android/services/appnavstate/test}/FakeAppForegroundStateService.kt (95%) diff --git a/features/roomlist/impl/src/test/kotlin/io/element/android/features/roomlist/impl/RoomListPresenterTest.kt b/features/roomlist/impl/src/test/kotlin/io/element/android/features/roomlist/impl/RoomListPresenterTest.kt index 7a9a8b2744..4bf4510d2f 100644 --- a/features/roomlist/impl/src/test/kotlin/io/element/android/features/roomlist/impl/RoomListPresenterTest.kt +++ b/features/roomlist/impl/src/test/kotlin/io/element/android/features/roomlist/impl/RoomListPresenterTest.kt @@ -95,6 +95,7 @@ import io.element.android.tests.testutils.testCoroutineDispatchers import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.cancel +import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.test.TestScope import kotlinx.coroutines.test.runTest import org.junit.Rule @@ -219,7 +220,7 @@ class RoomListPresenterTest { val encryptionService = FakeEncryptionService().apply { emitRecoveryState(RecoveryState.INCOMPLETE) } - val syncService = FakeSyncService(initialState = SyncState.Running) + val syncService = FakeSyncService(MutableStateFlow(SyncState.Running)) val presenter = createRoomListPresenter( client = FakeMatrixClient(roomListService = roomListService, encryptionService = encryptionService, syncService = syncService), coroutineScope = scope, @@ -250,7 +251,7 @@ class RoomListPresenterTest { sessionVerificationService = FakeSessionVerificationService().apply { givenNeedsSessionVerification(false) }, - syncService = FakeSyncService(initialState = SyncState.Running) + syncService = FakeSyncService(MutableStateFlow(SyncState.Running)) ) val scope = CoroutineScope(context = coroutineContext + SupervisorJob()) val presenter = createRoomListPresenter( diff --git a/libraries/matrix/test/src/main/kotlin/io/element/android/libraries/matrix/test/room/FakeMatrixRoom.kt b/libraries/matrix/test/src/main/kotlin/io/element/android/libraries/matrix/test/room/FakeMatrixRoom.kt index e2be5bcb66..fef964c867 100644 --- a/libraries/matrix/test/src/main/kotlin/io/element/android/libraries/matrix/test/room/FakeMatrixRoom.kt +++ b/libraries/matrix/test/src/main/kotlin/io/element/android/libraries/matrix/test/room/FakeMatrixRoom.kt @@ -139,6 +139,7 @@ class FakeMatrixRoom( private val saveComposerDraftLambda: (ComposerDraft) -> Result = { _: ComposerDraft -> Result.success(Unit) }, private val loadComposerDraftLambda: () -> Result = { Result.success(null) }, private val clearComposerDraftLambda: () -> Result = { Result.success(Unit) }, + private val subscribeToSyncLambda: () -> Unit = { lambdaError() }, ) : MatrixRoom { private val _roomInfoFlow: MutableSharedFlow = MutableSharedFlow(replay = 1) override val roomInfoFlow: Flow = _roomInfoFlow @@ -181,7 +182,9 @@ class FakeMatrixRoom( timelineFocusedOnEventResult(eventId) } - override suspend fun subscribeToSync() = Unit + override suspend fun subscribeToSync() { + subscribeToSyncLambda() + } override suspend fun powerLevels(): Result { return powerLevelsResult() diff --git a/libraries/matrix/test/src/main/kotlin/io/element/android/libraries/matrix/test/sync/FakeSyncService.kt b/libraries/matrix/test/src/main/kotlin/io/element/android/libraries/matrix/test/sync/FakeSyncService.kt index ffc06e7d18..32936d166e 100644 --- a/libraries/matrix/test/src/main/kotlin/io/element/android/libraries/matrix/test/sync/FakeSyncService.kt +++ b/libraries/matrix/test/src/main/kotlin/io/element/android/libraries/matrix/test/sync/FakeSyncService.kt @@ -22,22 +22,16 @@ import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow class FakeSyncService( - initialState: SyncState = SyncState.Idle + syncStateFlow: MutableStateFlow = MutableStateFlow(SyncState.Idle) ) : SyncService { - private val syncStateFlow = MutableStateFlow(initialState) - - fun simulateError() { - syncStateFlow.value = SyncState.Error - } - + var startSyncLambda: () -> Result = { Result.success(Unit) } override suspend fun startSync(): Result { - syncStateFlow.value = SyncState.Running - return Result.success(Unit) + return startSyncLambda() } + var stopSyncLambda: () -> Result = { Result.success(Unit) } override suspend fun stopSync(): Result { - syncStateFlow.value = SyncState.Terminated - return Result.success(Unit) + return stopSyncLambda() } override val syncState: StateFlow = syncStateFlow diff --git a/libraries/push/impl/build.gradle.kts b/libraries/push/impl/build.gradle.kts index 02dacbde34..f53b4ba27c 100644 --- a/libraries/push/impl/build.gradle.kts +++ b/libraries/push/impl/build.gradle.kts @@ -82,4 +82,6 @@ dependencies { testImplementation(projects.services.appnavstate.test) testImplementation(projects.services.toolbox.impl) testImplementation(projects.services.toolbox.test) + testImplementation(projects.libraries.featureflag.test) + testImplementation(libs.kotlinx.collections.immutable) } diff --git a/services/appnavstate/impl/src/test/kotlin/io/element/android/services/appnavstate/impl/DefaultNavigationStateServiceTest.kt b/services/appnavstate/impl/src/test/kotlin/io/element/android/services/appnavstate/impl/DefaultNavigationStateServiceTest.kt index 713839ee86..8f2abeeed5 100644 --- a/services/appnavstate/impl/src/test/kotlin/io/element/android/services/appnavstate/impl/DefaultNavigationStateServiceTest.kt +++ b/services/appnavstate/impl/src/test/kotlin/io/element/android/services/appnavstate/impl/DefaultNavigationStateServiceTest.kt @@ -31,6 +31,7 @@ import io.element.android.services.appnavstate.test.A_ROOM_OWNER import io.element.android.services.appnavstate.test.A_SESSION_OWNER import io.element.android.services.appnavstate.test.A_SPACE_OWNER import io.element.android.services.appnavstate.test.A_THREAD_OWNER +import io.element.android.services.appnavstate.test.FakeAppForegroundStateService import io.element.android.tests.testutils.runCancellableScopeTest import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.flow.first diff --git a/services/appnavstate/impl/src/test/kotlin/io/element/android/services/appnavstate/impl/FakeAppForegroundStateService.kt b/services/appnavstate/test/src/main/kotlin/io/element/android/services/appnavstate/test/FakeAppForegroundStateService.kt similarity index 95% rename from services/appnavstate/impl/src/test/kotlin/io/element/android/services/appnavstate/impl/FakeAppForegroundStateService.kt rename to services/appnavstate/test/src/main/kotlin/io/element/android/services/appnavstate/test/FakeAppForegroundStateService.kt index 4e3c012b48..6af17c78f3 100644 --- a/services/appnavstate/impl/src/test/kotlin/io/element/android/services/appnavstate/impl/FakeAppForegroundStateService.kt +++ b/services/appnavstate/test/src/main/kotlin/io/element/android/services/appnavstate/test/FakeAppForegroundStateService.kt @@ -14,7 +14,7 @@ * limitations under the License. */ -package io.element.android.services.appnavstate.impl +package io.element.android.services.appnavstate.test import io.element.android.services.appnavstate.api.AppForegroundStateService import kotlinx.coroutines.flow.MutableStateFlow