|
| 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 | + |
| 8 | +package io.element.android.libraries.matrix.impl.timeline |
| 9 | + |
| 10 | +import app.cash.turbine.test |
| 11 | +import com.google.common.truth.Truth.assertThat |
| 12 | +import io.element.android.libraries.matrix.api.timeline.MatrixTimelineItem |
| 13 | +import io.element.android.libraries.matrix.impl.fixtures.fakes.FakeRustEventTimelineItem |
| 14 | +import io.element.android.libraries.matrix.impl.fixtures.fakes.FakeRustTimeline |
| 15 | +import io.element.android.libraries.matrix.impl.fixtures.fakes.FakeRustTimelineDiff |
| 16 | +import io.element.android.libraries.matrix.impl.fixtures.fakes.FakeRustTimelineItem |
| 17 | +import io.element.android.tests.testutils.lambda.lambdaError |
| 18 | +import io.element.android.tests.testutils.lambda.lambdaRecorder |
| 19 | +import io.element.android.tests.testutils.runCancellableScopeTestWithTestScope |
| 20 | +import kotlinx.coroutines.CompletableDeferred |
| 21 | +import kotlinx.coroutines.CoroutineScope |
| 22 | +import kotlinx.coroutines.ExperimentalCoroutinesApi |
| 23 | +import kotlinx.coroutines.flow.MutableSharedFlow |
| 24 | +import kotlinx.coroutines.flow.MutableStateFlow |
| 25 | +import kotlinx.coroutines.test.StandardTestDispatcher |
| 26 | +import kotlinx.coroutines.test.TestScope |
| 27 | +import kotlinx.coroutines.test.runCurrent |
| 28 | +import org.junit.Test |
| 29 | +import org.matrix.rustcomponents.sdk.Timeline |
| 30 | +import org.matrix.rustcomponents.sdk.TimelineChange |
| 31 | +import uniffi.matrix_sdk_ui.EventItemOrigin |
| 32 | + |
| 33 | +@OptIn(ExperimentalCoroutinesApi::class) |
| 34 | +class TimelineItemsSubscriberTest { |
| 35 | + @Test |
| 36 | + fun `when timeline emits an empty list of items, the flow must emits an empty list`() = runCancellableScopeTestWithTestScope { testScope, cancellableScope -> |
| 37 | + val timelineItems: MutableSharedFlow<List<MatrixTimelineItem>> = |
| 38 | + MutableSharedFlow(replay = 1, extraBufferCapacity = Int.MAX_VALUE) |
| 39 | + val timeline = FakeRustTimeline() |
| 40 | + val timelineItemsSubscriber = testScope.createTimelineItemsSubscriber( |
| 41 | + coroutineScope = cancellableScope, |
| 42 | + timeline = timeline, |
| 43 | + timelineItems = timelineItems, |
| 44 | + ) |
| 45 | + timelineItems.test { |
| 46 | + timelineItemsSubscriber.subscribeIfNeeded() |
| 47 | + // Wait for the listener to be set. |
| 48 | + testScope.runCurrent() |
| 49 | + timeline.emitDiff(listOf(FakeRustTimelineDiff(item = null, change = TimelineChange.RESET))) |
| 50 | + val final = awaitItem() |
| 51 | + assertThat(final).isEmpty() |
| 52 | + timelineItemsSubscriber.unsubscribeIfNeeded() |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + fun `when timeline emits a non empty list of items, the flow must emits a non empty list`() = runCancellableScopeTestWithTestScope { testScope, cancellableScope -> |
| 58 | + val timelineItems: MutableSharedFlow<List<MatrixTimelineItem>> = |
| 59 | + MutableSharedFlow(replay = 1, extraBufferCapacity = Int.MAX_VALUE) |
| 60 | + val timeline = FakeRustTimeline() |
| 61 | + val timelineItemsSubscriber = testScope.createTimelineItemsSubscriber( |
| 62 | + coroutineScope = cancellableScope, |
| 63 | + timeline = timeline, |
| 64 | + timelineItems = timelineItems, |
| 65 | + ) |
| 66 | + timelineItems.test { |
| 67 | + timelineItemsSubscriber.subscribeIfNeeded() |
| 68 | + // Wait for the listener to be set. |
| 69 | + testScope.runCurrent() |
| 70 | + timeline.emitDiff(listOf(FakeRustTimelineDiff(item = FakeRustTimelineItem(), change = TimelineChange.RESET))) |
| 71 | + val final = awaitItem() |
| 72 | + assertThat(final).isNotEmpty() |
| 73 | + timelineItemsSubscriber.unsubscribeIfNeeded() |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + fun `when timeline emits an item with SYNC origin, the callback onNewSyncedEvent is invoked`() = runCancellableScopeTestWithTestScope { testScope, cancellableScope -> |
| 79 | + val timelineItems: MutableSharedFlow<List<MatrixTimelineItem>> = |
| 80 | + MutableSharedFlow(replay = 1, extraBufferCapacity = Int.MAX_VALUE) |
| 81 | + val timeline = FakeRustTimeline() |
| 82 | + val onNewSyncedEventRecorder = lambdaRecorder<Unit> { } |
| 83 | + val timelineItemsSubscriber = testScope.createTimelineItemsSubscriber( |
| 84 | + coroutineScope = cancellableScope, |
| 85 | + timeline = timeline, |
| 86 | + timelineItems = timelineItems, |
| 87 | + onNewSyncedEvent = onNewSyncedEventRecorder, |
| 88 | + ) |
| 89 | + timelineItems.test { |
| 90 | + timelineItemsSubscriber.subscribeIfNeeded() |
| 91 | + // Wait for the listener to be set. |
| 92 | + testScope.runCurrent() |
| 93 | + timeline.emitDiff( |
| 94 | + listOf( |
| 95 | + FakeRustTimelineDiff( |
| 96 | + item = FakeRustTimelineItem( |
| 97 | + asEventResult = FakeRustEventTimelineItem(origin = EventItemOrigin.SYNC) |
| 98 | + ), |
| 99 | + change = TimelineChange.RESET, |
| 100 | + ) |
| 101 | + ) |
| 102 | + ) |
| 103 | + val final = awaitItem() |
| 104 | + assertThat(final).isNotEmpty() |
| 105 | + timelineItemsSubscriber.unsubscribeIfNeeded() |
| 106 | + } |
| 107 | + onNewSyncedEventRecorder.assertions().isCalledOnce() |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + fun `multiple subscriptions does not have side effect`() = runCancellableScopeTestWithTestScope { testScope, cancellableScope -> |
| 112 | + val timelineItemsSubscriber = testScope.createTimelineItemsSubscriber( |
| 113 | + coroutineScope = cancellableScope, |
| 114 | + ) |
| 115 | + timelineItemsSubscriber.subscribeIfNeeded() |
| 116 | + timelineItemsSubscriber.subscribeIfNeeded() |
| 117 | + timelineItemsSubscriber.unsubscribeIfNeeded() |
| 118 | + timelineItemsSubscriber.unsubscribeIfNeeded() |
| 119 | + } |
| 120 | + |
| 121 | + private fun TestScope.createTimelineItemsSubscriber( |
| 122 | + coroutineScope: CoroutineScope, |
| 123 | + timeline: Timeline = FakeRustTimeline(), |
| 124 | + timelineItems: MutableSharedFlow<List<MatrixTimelineItem>> = MutableSharedFlow(replay = 1, extraBufferCapacity = Int.MAX_VALUE), |
| 125 | + initLatch: CompletableDeferred<Unit> = CompletableDeferred(), |
| 126 | + isTimelineInitialized: MutableStateFlow<Boolean> = MutableStateFlow(false), |
| 127 | + onNewSyncedEvent: () -> Unit = { lambdaError() }, |
| 128 | + ): TimelineItemsSubscriber { |
| 129 | + return TimelineItemsSubscriber( |
| 130 | + timelineCoroutineScope = coroutineScope, |
| 131 | + dispatcher = StandardTestDispatcher(testScheduler), |
| 132 | + timeline = timeline, |
| 133 | + timelineDiffProcessor = createMatrixTimelineDiffProcessor(timelineItems), |
| 134 | + initLatch = initLatch, |
| 135 | + isTimelineInitialized = isTimelineInitialized, |
| 136 | + onNewSyncedEvent = onNewSyncedEvent, |
| 137 | + ) |
| 138 | + } |
| 139 | +} |
0 commit comments