From 7628d480a8275b0c482db4e9469fe21eedf7f7bf Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Fri, 27 Sep 2024 17:38:21 +0200 Subject: [PATCH] Fix quality issues. --- .../fakes/FakeRustEventTimelineItem.kt | 2 +- .../impl/fixtures/fakes/FakeRustTimeline.kt | 3 +- .../timeline/TimelineItemsSubscriberTest.kt | 146 +++++++++--------- 3 files changed, 79 insertions(+), 72 deletions(-) diff --git a/libraries/matrix/impl/src/test/kotlin/io/element/android/libraries/matrix/impl/fixtures/fakes/FakeRustEventTimelineItem.kt b/libraries/matrix/impl/src/test/kotlin/io/element/android/libraries/matrix/impl/fixtures/fakes/FakeRustEventTimelineItem.kt index 3efb1ec151..f40d0ffbea 100644 --- a/libraries/matrix/impl/src/test/kotlin/io/element/android/libraries/matrix/impl/fixtures/fakes/FakeRustEventTimelineItem.kt +++ b/libraries/matrix/impl/src/test/kotlin/io/element/android/libraries/matrix/impl/fixtures/fakes/FakeRustEventTimelineItem.kt @@ -32,7 +32,7 @@ class FakeRustEventTimelineItem( override fun isLocal(): Boolean = false override fun isOwn(): Boolean = false override fun isRemote(): Boolean = false - override fun localSendState(): EventSendState? = null + override fun localSendState(): EventSendState? = null override fun reactions(): List = emptyList() override fun readReceipts(): Map = emptyMap() override fun sender(): String = A_USER_ID.value diff --git a/libraries/matrix/impl/src/test/kotlin/io/element/android/libraries/matrix/impl/fixtures/fakes/FakeRustTimeline.kt b/libraries/matrix/impl/src/test/kotlin/io/element/android/libraries/matrix/impl/fixtures/fakes/FakeRustTimeline.kt index 605cc7d98d..bcb64a9219 100644 --- a/libraries/matrix/impl/src/test/kotlin/io/element/android/libraries/matrix/impl/fixtures/fakes/FakeRustTimeline.kt +++ b/libraries/matrix/impl/src/test/kotlin/io/element/android/libraries/matrix/impl/fixtures/fakes/FakeRustTimeline.kt @@ -13,8 +13,7 @@ import org.matrix.rustcomponents.sdk.Timeline import org.matrix.rustcomponents.sdk.TimelineDiff import org.matrix.rustcomponents.sdk.TimelineListener -class FakeRustTimeline( -) : Timeline(NoPointer) { +class FakeRustTimeline : Timeline(NoPointer) { private var listener: TimelineListener? = null override suspend fun addListener(listener: TimelineListener): TaskHandle { this.listener = listener diff --git a/libraries/matrix/impl/src/test/kotlin/io/element/android/libraries/matrix/impl/timeline/TimelineItemsSubscriberTest.kt b/libraries/matrix/impl/src/test/kotlin/io/element/android/libraries/matrix/impl/timeline/TimelineItemsSubscriberTest.kt index c47ec9f040..a1b582a678 100644 --- a/libraries/matrix/impl/src/test/kotlin/io/element/android/libraries/matrix/impl/timeline/TimelineItemsSubscriberTest.kt +++ b/libraries/matrix/impl/src/test/kotlin/io/element/android/libraries/matrix/impl/timeline/TimelineItemsSubscriberTest.kt @@ -33,89 +33,97 @@ import uniffi.matrix_sdk_ui.EventItemOrigin @OptIn(ExperimentalCoroutinesApi::class) class TimelineItemsSubscriberTest { @Test - fun `when timeline emits an empty list of items, the flow must emits an empty list`() = runCancellableScopeTestWithTestScope { testScope, cancellableScope -> - val timelineItems: MutableSharedFlow> = - MutableSharedFlow(replay = 1, extraBufferCapacity = Int.MAX_VALUE) - val timeline = FakeRustTimeline() - val timelineItemsSubscriber = testScope.createTimelineItemsSubscriber( - coroutineScope = cancellableScope, - timeline = timeline, - timelineItems = timelineItems, - ) - timelineItems.test { - timelineItemsSubscriber.subscribeIfNeeded() - // Wait for the listener to be set. - testScope.runCurrent() - timeline.emitDiff(listOf(FakeRustTimelineDiff(item = null, change = TimelineChange.RESET))) - val final = awaitItem() - assertThat(final).isEmpty() - timelineItemsSubscriber.unsubscribeIfNeeded() + fun `when timeline emits an empty list of items, the flow must emits an empty list`() { + runCancellableScopeTestWithTestScope { testScope, cancellableScope -> + val timelineItems: MutableSharedFlow> = + MutableSharedFlow(replay = 1, extraBufferCapacity = Int.MAX_VALUE) + val timeline = FakeRustTimeline() + val timelineItemsSubscriber = testScope.createTimelineItemsSubscriber( + coroutineScope = cancellableScope, + timeline = timeline, + timelineItems = timelineItems, + ) + timelineItems.test { + timelineItemsSubscriber.subscribeIfNeeded() + // Wait for the listener to be set. + testScope.runCurrent() + timeline.emitDiff(listOf(FakeRustTimelineDiff(item = null, change = TimelineChange.RESET))) + val final = awaitItem() + assertThat(final).isEmpty() + timelineItemsSubscriber.unsubscribeIfNeeded() + } } } @Test - fun `when timeline emits a non empty list of items, the flow must emits a non empty list`() = runCancellableScopeTestWithTestScope { testScope, cancellableScope -> - val timelineItems: MutableSharedFlow> = - MutableSharedFlow(replay = 1, extraBufferCapacity = Int.MAX_VALUE) - val timeline = FakeRustTimeline() - val timelineItemsSubscriber = testScope.createTimelineItemsSubscriber( - coroutineScope = cancellableScope, - timeline = timeline, - timelineItems = timelineItems, - ) - timelineItems.test { - timelineItemsSubscriber.subscribeIfNeeded() - // Wait for the listener to be set. - testScope.runCurrent() - timeline.emitDiff(listOf(FakeRustTimelineDiff(item = FakeRustTimelineItem(), change = TimelineChange.RESET))) - val final = awaitItem() - assertThat(final).isNotEmpty() - timelineItemsSubscriber.unsubscribeIfNeeded() + fun `when timeline emits a non empty list of items, the flow must emits a non empty list`() { + runCancellableScopeTestWithTestScope { testScope, cancellableScope -> + val timelineItems: MutableSharedFlow> = + MutableSharedFlow(replay = 1, extraBufferCapacity = Int.MAX_VALUE) + val timeline = FakeRustTimeline() + val timelineItemsSubscriber = testScope.createTimelineItemsSubscriber( + coroutineScope = cancellableScope, + timeline = timeline, + timelineItems = timelineItems, + ) + timelineItems.test { + timelineItemsSubscriber.subscribeIfNeeded() + // Wait for the listener to be set. + testScope.runCurrent() + timeline.emitDiff(listOf(FakeRustTimelineDiff(item = FakeRustTimelineItem(), change = TimelineChange.RESET))) + val final = awaitItem() + assertThat(final).isNotEmpty() + timelineItemsSubscriber.unsubscribeIfNeeded() + } } } @Test - fun `when timeline emits an item with SYNC origin, the callback onNewSyncedEvent is invoked`() = runCancellableScopeTestWithTestScope { testScope, cancellableScope -> - val timelineItems: MutableSharedFlow> = - MutableSharedFlow(replay = 1, extraBufferCapacity = Int.MAX_VALUE) - val timeline = FakeRustTimeline() - val onNewSyncedEventRecorder = lambdaRecorder { } - val timelineItemsSubscriber = testScope.createTimelineItemsSubscriber( - coroutineScope = cancellableScope, - timeline = timeline, - timelineItems = timelineItems, - onNewSyncedEvent = onNewSyncedEventRecorder, - ) - timelineItems.test { - timelineItemsSubscriber.subscribeIfNeeded() - // Wait for the listener to be set. - testScope.runCurrent() - timeline.emitDiff( - listOf( - FakeRustTimelineDiff( - item = FakeRustTimelineItem( - asEventResult = FakeRustEventTimelineItem(origin = EventItemOrigin.SYNC) - ), - change = TimelineChange.RESET, + fun `when timeline emits an item with SYNC origin, the callback onNewSyncedEvent is invoked`() { + runCancellableScopeTestWithTestScope { testScope, cancellableScope -> + val timelineItems: MutableSharedFlow> = + MutableSharedFlow(replay = 1, extraBufferCapacity = Int.MAX_VALUE) + val timeline = FakeRustTimeline() + val onNewSyncedEventRecorder = lambdaRecorder { } + val timelineItemsSubscriber = testScope.createTimelineItemsSubscriber( + coroutineScope = cancellableScope, + timeline = timeline, + timelineItems = timelineItems, + onNewSyncedEvent = onNewSyncedEventRecorder, + ) + timelineItems.test { + timelineItemsSubscriber.subscribeIfNeeded() + // Wait for the listener to be set. + testScope.runCurrent() + timeline.emitDiff( + listOf( + FakeRustTimelineDiff( + item = FakeRustTimelineItem( + asEventResult = FakeRustEventTimelineItem(origin = EventItemOrigin.SYNC) + ), + change = TimelineChange.RESET, + ) ) ) - ) - val final = awaitItem() - assertThat(final).isNotEmpty() - timelineItemsSubscriber.unsubscribeIfNeeded() + val final = awaitItem() + assertThat(final).isNotEmpty() + timelineItemsSubscriber.unsubscribeIfNeeded() + } + onNewSyncedEventRecorder.assertions().isCalledOnce() } - onNewSyncedEventRecorder.assertions().isCalledOnce() } @Test - fun `multiple subscriptions does not have side effect`() = runCancellableScopeTestWithTestScope { testScope, cancellableScope -> - val timelineItemsSubscriber = testScope.createTimelineItemsSubscriber( - coroutineScope = cancellableScope, - ) - timelineItemsSubscriber.subscribeIfNeeded() - timelineItemsSubscriber.subscribeIfNeeded() - timelineItemsSubscriber.unsubscribeIfNeeded() - timelineItemsSubscriber.unsubscribeIfNeeded() + fun `multiple subscriptions does not have side effect`() { + runCancellableScopeTestWithTestScope { testScope, cancellableScope -> + val timelineItemsSubscriber = testScope.createTimelineItemsSubscriber( + coroutineScope = cancellableScope, + ) + timelineItemsSubscriber.subscribeIfNeeded() + timelineItemsSubscriber.subscribeIfNeeded() + timelineItemsSubscriber.unsubscribeIfNeeded() + timelineItemsSubscriber.unsubscribeIfNeeded() + } } private fun TestScope.createTimelineItemsSubscriber(