@@ -33,89 +33,97 @@ import uniffi.matrix_sdk_ui.EventItemOrigin
3333@OptIn(ExperimentalCoroutinesApi ::class )
3434class TimelineItemsSubscriberTest {
3535 @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()
36+ fun `when timeline emits an empty list of items, the flow must emits an empty list` () {
37+ runCancellableScopeTestWithTestScope { testScope, cancellableScope ->
38+ val timelineItems: MutableSharedFlow <List <MatrixTimelineItem >> =
39+ MutableSharedFlow (replay = 1 , extraBufferCapacity = Int .MAX_VALUE )
40+ val timeline = FakeRustTimeline ()
41+ val timelineItemsSubscriber = testScope.createTimelineItemsSubscriber(
42+ coroutineScope = cancellableScope,
43+ timeline = timeline,
44+ timelineItems = timelineItems,
45+ )
46+ timelineItems.test {
47+ timelineItemsSubscriber.subscribeIfNeeded()
48+ // Wait for the listener to be set.
49+ testScope.runCurrent()
50+ timeline.emitDiff(listOf (FakeRustTimelineDiff (item = null , change = TimelineChange .RESET )))
51+ val final = awaitItem()
52+ assertThat(final).isEmpty()
53+ timelineItemsSubscriber.unsubscribeIfNeeded()
54+ }
5355 }
5456 }
5557
5658 @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()
59+ fun `when timeline emits a non empty list of items, the flow must emits a non empty list` () {
60+ runCancellableScopeTestWithTestScope { testScope, cancellableScope ->
61+ val timelineItems: MutableSharedFlow <List <MatrixTimelineItem >> =
62+ MutableSharedFlow (replay = 1 , extraBufferCapacity = Int .MAX_VALUE )
63+ val timeline = FakeRustTimeline ()
64+ val timelineItemsSubscriber = testScope.createTimelineItemsSubscriber(
65+ coroutineScope = cancellableScope,
66+ timeline = timeline,
67+ timelineItems = timelineItems,
68+ )
69+ timelineItems.test {
70+ timelineItemsSubscriber.subscribeIfNeeded()
71+ // Wait for the listener to be set.
72+ testScope.runCurrent()
73+ timeline.emitDiff(listOf (FakeRustTimelineDiff (item = FakeRustTimelineItem (), change = TimelineChange .RESET )))
74+ val final = awaitItem()
75+ assertThat(final).isNotEmpty()
76+ timelineItemsSubscriber.unsubscribeIfNeeded()
77+ }
7478 }
7579 }
7680
7781 @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 ,
82+ fun `when timeline emits an item with SYNC origin, the callback onNewSyncedEvent is invoked` () {
83+ runCancellableScopeTestWithTestScope { testScope, cancellableScope ->
84+ val timelineItems: MutableSharedFlow <List <MatrixTimelineItem >> =
85+ MutableSharedFlow (replay = 1 , extraBufferCapacity = Int .MAX_VALUE )
86+ val timeline = FakeRustTimeline ()
87+ val onNewSyncedEventRecorder = lambdaRecorder<Unit > { }
88+ val timelineItemsSubscriber = testScope.createTimelineItemsSubscriber(
89+ coroutineScope = cancellableScope,
90+ timeline = timeline,
91+ timelineItems = timelineItems,
92+ onNewSyncedEvent = onNewSyncedEventRecorder,
93+ )
94+ timelineItems.test {
95+ timelineItemsSubscriber.subscribeIfNeeded()
96+ // Wait for the listener to be set.
97+ testScope.runCurrent()
98+ timeline.emitDiff(
99+ listOf (
100+ FakeRustTimelineDiff (
101+ item = FakeRustTimelineItem (
102+ asEventResult = FakeRustEventTimelineItem (origin = EventItemOrigin .SYNC )
103+ ),
104+ change = TimelineChange .RESET ,
105+ )
100106 )
101107 )
102- )
103- val final = awaitItem()
104- assertThat(final).isNotEmpty()
105- timelineItemsSubscriber.unsubscribeIfNeeded()
108+ val final = awaitItem()
109+ assertThat(final).isNotEmpty()
110+ timelineItemsSubscriber.unsubscribeIfNeeded()
111+ }
112+ onNewSyncedEventRecorder.assertions().isCalledOnce()
106113 }
107- onNewSyncedEventRecorder.assertions().isCalledOnce()
108114 }
109115
110116 @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()
117+ fun `multiple subscriptions does not have side effect` () {
118+ runCancellableScopeTestWithTestScope { testScope, cancellableScope ->
119+ val timelineItemsSubscriber = testScope.createTimelineItemsSubscriber(
120+ coroutineScope = cancellableScope,
121+ )
122+ timelineItemsSubscriber.subscribeIfNeeded()
123+ timelineItemsSubscriber.subscribeIfNeeded()
124+ timelineItemsSubscriber.unsubscribeIfNeeded()
125+ timelineItemsSubscriber.unsubscribeIfNeeded()
126+ }
119127 }
120128
121129 private fun TestScope.createTimelineItemsSubscriber (
0 commit comments