|
| 1 | +/* |
| 2 | + * Copyright 2024 New Vector Ltd. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: AGPL-3.0-only |
| 5 | + * Please see LICENSE in the repository root for full details. |
| 6 | + */ |
| 7 | +package io.element.android.libraries.matrix.impl.roomlist |
| 8 | + |
| 9 | +import app.cash.turbine.test |
| 10 | +import com.google.common.truth.Truth.assertThat |
| 11 | +import io.element.android.libraries.matrix.api.roomlist.RoomListService |
| 12 | +import io.element.android.libraries.matrix.impl.fixtures.fakes.FakeRustRoomListService |
| 13 | +import io.element.android.libraries.matrix.impl.room.RoomSyncSubscriber |
| 14 | +import io.element.android.tests.testutils.runCancellableScopeTestWithTestScope |
| 15 | +import io.element.android.tests.testutils.testCoroutineDispatchers |
| 16 | +import kotlinx.coroutines.CoroutineScope |
| 17 | +import kotlinx.coroutines.ExperimentalCoroutinesApi |
| 18 | +import kotlinx.coroutines.test.StandardTestDispatcher |
| 19 | +import kotlinx.coroutines.test.TestScope |
| 20 | +import kotlinx.coroutines.test.runCurrent |
| 21 | +import org.junit.Test |
| 22 | +import org.matrix.rustcomponents.sdk.RoomListServiceSyncIndicator |
| 23 | +import org.matrix.rustcomponents.sdk.RoomListService as RustRoomListService |
| 24 | + |
| 25 | +@OptIn(ExperimentalCoroutinesApi::class) |
| 26 | +class RustRoomListServiceTest { |
| 27 | + @Test |
| 28 | + fun `syncIndicator should emit the expected values`() = runCancellableScopeTestWithTestScope { testScope, cancellableScope -> |
| 29 | + val roomListService = FakeRustRoomListService() |
| 30 | + val sut = testScope.createRustRoomListService( |
| 31 | + sessionCoroutineScope = cancellableScope, |
| 32 | + roomListService = roomListService, |
| 33 | + ) |
| 34 | + // Give time for mxCallback to setup |
| 35 | + testScope.runCurrent() |
| 36 | + sut.syncIndicator.test { |
| 37 | + assertThat(awaitItem()).isEqualTo(RoomListService.SyncIndicator.Hide) |
| 38 | + roomListService.emitRoomListServiceSyncIndicator(RoomListServiceSyncIndicator.SHOW) |
| 39 | + assertThat(awaitItem()).isEqualTo(RoomListService.SyncIndicator.Show) |
| 40 | + roomListService.emitRoomListServiceSyncIndicator(RoomListServiceSyncIndicator.HIDE) |
| 41 | + assertThat(awaitItem()).isEqualTo(RoomListService.SyncIndicator.Hide) |
| 42 | + } |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +private fun TestScope.createRustRoomListService( |
| 47 | + sessionCoroutineScope: CoroutineScope, |
| 48 | + roomListService: RustRoomListService = FakeRustRoomListService(), |
| 49 | +) = RustRoomListService( |
| 50 | + innerRoomListService = roomListService, |
| 51 | + sessionDispatcher = StandardTestDispatcher(testScheduler), |
| 52 | + roomListFactory = RoomListFactory( |
| 53 | + innerRoomListService = roomListService, |
| 54 | + sessionCoroutineScope = sessionCoroutineScope, |
| 55 | + ), |
| 56 | + roomSyncSubscriber = RoomSyncSubscriber( |
| 57 | + roomListService = roomListService, |
| 58 | + dispatchers = testCoroutineDispatchers(), |
| 59 | + ), |
| 60 | + sessionCoroutineScope = sessionCoroutineScope, |
| 61 | +) |
0 commit comments