@ -33,89 +33,97 @@ import uniffi.matrix_sdk_ui.EventItemOrigin
@OptIn ( ExperimentalCoroutinesApi :: class )
@OptIn ( ExperimentalCoroutinesApi :: class )
class TimelineItemsSubscriberTest {
class TimelineItemsSubscriberTest {
@Test
@Test
fun `when timeline emits an empty list of items, the flow must emits an empty list` ( ) = runCancellableScopeTestWithTestScope { testScope , cancellableScope ->
fun `when timeline emits an empty list of items, the flow must emits an empty list` ( ) {
val timelineItems : MutableSharedFlow < List < MatrixTimelineItem > > =
runCancellableScopeTestWithTestScope { testScope , cancellableScope ->
MutableSharedFlow ( replay = 1 , extraBufferCapacity = Int . MAX _VALUE )
val timelineItems : MutableSharedFlow < List < MatrixTimelineItem > > =
val timeline = FakeRustTimeline ( )
MutableSharedFlow ( replay = 1 , extraBufferCapacity = Int . MAX _VALUE )
val timelineItemsSubscriber = testScope . createTimelineItemsSubscriber (
val timeline = FakeRustTimeline ( )
coroutineScope = cancellableScope ,
val timelineItemsSubscriber = testScope . createTimelineItemsSubscriber (
timeline = timeline ,
coroutineScope = cancellableScope ,
timelineItems = timelineItems ,
timeline = timeline ,
)
timelineItems = timelineItems ,
timelineItems . test {
)
timelineItemsSubscriber . subscribeIfNeeded ( )
timelineItems . test {
// Wait for the listener to be set.
timelineItemsSubscriber . subscribeIfNeeded ( )
testScope . runCurrent ( )
// Wait for the listener to be set.
timeline . emitDiff ( listOf ( FakeRustTimelineDiff ( item = null , change = TimelineChange . RESET ) ) )
testScope . runCurrent ( )
val final = awaitItem ( )
timeline . emitDiff ( listOf ( FakeRustTimelineDiff ( item = null , change = TimelineChange . RESET ) ) )
assertThat ( final ) . isEmpty ( )
val final = awaitItem ( )
timelineItemsSubscriber . unsubscribeIfNeeded ( )
assertThat ( final ) . isEmpty ( )
timelineItemsSubscriber . unsubscribeIfNeeded ( )
}
}
}
}
}
@Test
@Test
fun `when timeline emits a non empty list of items, the flow must emits a non empty list` ( ) = runCancellableScopeTestWithTestScope { testScope , cancellableScope ->
fun `when timeline emits a non empty list of items, the flow must emits a non empty list` ( ) {
val timelineItems : MutableSharedFlow < List < MatrixTimelineItem > > =
runCancellableScopeTestWithTestScope { testScope , cancellableScope ->
MutableSharedFlow ( replay = 1 , extraBufferCapacity = Int . MAX _VALUE )
val timelineItems : MutableSharedFlow < List < MatrixTimelineItem > > =
val timeline = FakeRustTimeline ( )
MutableSharedFlow ( replay = 1 , extraBufferCapacity = Int . MAX _VALUE )
val timelineItemsSubscriber = testScope . createTimelineItemsSubscriber (
val timeline = FakeRustTimeline ( )
coroutineScope = cancellableScope ,
val timelineItemsSubscriber = testScope . createTimelineItemsSubscriber (
timeline = timeline ,
coroutineScope = cancellableScope ,
timelineItems = timelineItems ,
timeline = timeline ,
)
timelineItems = timelineItems ,
timelineItems . test {
)
timelineItemsSubscriber . subscribeIfNeeded ( )
timelineItems . test {
// Wait for the listener to be set.
timelineItemsSubscriber . subscribeIfNeeded ( )
testScope . runCurrent ( )
// Wait for the listener to be set.
timeline . emitDiff ( listOf ( FakeRustTimelineDiff ( item = FakeRustTimelineItem ( ) , change = TimelineChange . RESET ) ) )
testScope . runCurrent ( )
val final = awaitItem ( )
timeline . emitDiff ( listOf ( FakeRustTimelineDiff ( item = FakeRustTimelineItem ( ) , change = TimelineChange . RESET ) ) )
assertThat ( final ) . isNotEmpty ( )
val final = awaitItem ( )
timelineItemsSubscriber . unsubscribeIfNeeded ( )
assertThat ( final ) . isNotEmpty ( )
timelineItemsSubscriber . unsubscribeIfNeeded ( )
}
}
}
}
}
@Test
@Test
fun `when timeline emits an item with SYNC origin, the callback onNewSyncedEvent is invoked` ( ) = runCancellableScopeTestWithTestScope { testScope , cancellableScope ->
fun `when timeline emits an item with SYNC origin, the callback onNewSyncedEvent is invoked` ( ) {
val timelineItems : MutableSharedFlow < List < MatrixTimelineItem > > =
runCancellableScopeTestWithTestScope { testScope , cancellableScope ->
MutableSharedFlow ( replay = 1 , extraBufferCapacity = Int . MAX _VALUE )
val timelineItems : MutableSharedFlow < List < MatrixTimelineItem > > =
val timeline = FakeRustTimeline ( )
MutableSharedFlow ( replay = 1 , extraBufferCapacity = Int . MAX _VALUE )
val onNewSyncedEventRecorder = lambdaRecorder < Unit > { }
val timeline = FakeRustTimeline ( )
val timelineItemsSubscriber = testScope . createTimelineItemsSubscriber (
val onNewSyncedEventRecorder = lambdaRecorder < Unit > { }
coroutineScope = cancellableScope ,
val timelineItemsSubscriber = testScope . createTimelineItemsSubscriber (
timeline = timeline ,
coroutineScope = cancellableScope ,
timelineItems = timelineItems ,
timeline = timeline ,
onNewSyncedEvent = onNewSyncedEventRecorder ,
timelineItems = timelineItems ,
)
onNewSyncedEvent = onNewSyncedEventRecorder ,
timelineItems . test {
)
timelineItemsSubscriber . subscribeIfNeeded ( )
timelineItems . test {
// Wait for the listener to be set.
timelineItemsSubscriber . subscribeIfNeeded ( )
testScope . runCurrent ( )
// Wait for the listener to be set.
timeline . emitDiff (
testScope . runCurrent ( )
listOf (
timeline . emitDiff (
FakeRustTimelineDiff (
listOf (
item = FakeRustTimelineItem (
FakeRustTimelineDiff (
asEventResult = FakeRustEventTimelineItem ( origin = EventItemOrigin . SYNC )
item = FakeRustTimelineItem (
) ,
asEventResult = FakeRustEventTimelineItem ( origin = EventItemOrigin . SYNC )
change = TimelineChange . RESET ,
) ,
change = TimelineChange . RESET ,
)
)
)
)
)
)
val final = awaitItem ( )
val final = awaitItem ( )
assertThat ( final ) . isNotEmpty ( )
assertThat ( final ) . isNotEmpty ( )
timelineItemsSubscriber . unsubscribeIfNeeded ( )
timelineItemsSubscriber . unsubscribeIfNeeded ( )
}
onNewSyncedEventRecorder . assertions ( ) . isCalledOnce ( )
}
}
onNewSyncedEventRecorder . assertions ( ) . isCalledOnce ( )
}
}
@Test
@Test
fun `multiple subscriptions does not have side effect` ( ) = runCancellableScopeTestWithTestScope { testScope , cancellableScope ->
fun `multiple subscriptions does not have side effect` ( ) {
val timelineItemsSubscriber = testScope . createTimelineItemsSubscriber (
runCancellableScopeTestWithTestScope { testScope , cancellableScope ->
coroutineScope = cancellableScope ,
val timelineItemsSubscriber = testScope . createTimelineItemsSubscriber (
)
coroutineScope = cancellableScope ,
timelineItemsSubscriber . subscribeIfNeeded ( )
)
timelineItemsSubscriber . subscribeIfNeeded ( )
timelineItemsSubscriber . subscribeIfNeeded ( )
timelineItemsSubscriber . unsubscribeIfNeeded ( )
timelineItemsSubscriber . subscribeIfNeeded ( )
timelineItemsSubscriber . unsubscribeIfNeeded ( )
timelineItemsSubscriber . unsubscribeIfNeeded ( )
timelineItemsSubscriber . unsubscribeIfNeeded ( )
}
}
}
private fun TestScope . createTimelineItemsSubscriber (
private fun TestScope . createTimelineItemsSubscriber (