Skip to content

Commit b6f77ac

Browse files
committed
Adding unit tests for LoadMorePollsTask
1 parent 4182581 commit b6f77ac

File tree

3 files changed

+202
-11
lines changed

3 files changed

+202
-11
lines changed

matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/session/room/event/DefaultFilterAndStoreEventsTaskTest.kt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,12 @@ import org.junit.Before
2828
import org.junit.Test
2929
import org.matrix.android.sdk.api.session.events.model.Event
3030
import org.matrix.android.sdk.api.session.events.model.EventType
31-
import org.matrix.android.sdk.api.session.events.model.RelationType
32-
import org.matrix.android.sdk.api.session.events.model.isPollResponse
3331
import org.matrix.android.sdk.api.session.room.send.SendState
3432
import org.matrix.android.sdk.internal.database.mapper.toEntity
3533
import org.matrix.android.sdk.internal.database.model.EventEntity
3634
import org.matrix.android.sdk.internal.database.model.EventEntityFields
3735
import org.matrix.android.sdk.internal.database.model.EventInsertType
3836
import org.matrix.android.sdk.internal.database.query.copyToRealmOrIgnore
39-
import org.matrix.android.sdk.internal.session.room.relation.RelationsResponse
40-
import org.matrix.android.sdk.internal.session.room.relation.poll.FETCH_RELATED_EVENTS_LIMIT
41-
import org.matrix.android.sdk.internal.session.room.relation.poll.FetchPollResponseEventsTask
4237
import org.matrix.android.sdk.test.fakes.FakeClock
4338
import org.matrix.android.sdk.test.fakes.FakeEventDecryptor
4439
import org.matrix.android.sdk.test.fakes.FakeMonarchy

matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/session/room/poll/DefaultGetLoadedPollsStatusTaskTest.kt

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,16 @@ import org.matrix.android.sdk.test.fakes.givenEqualTo
3030
import org.matrix.android.sdk.test.fakes.givenFindFirst
3131

3232
private const val A_ROOM_ID = "room-id"
33+
34+
/**
35+
* Timestamp in milliseconds corresponding to 2023/01/26.
36+
*/
37+
private const val A_CURRENT_TIMESTAMP = 1674737619290L
38+
3339
/**
34-
* 2023/01/26
40+
* Timestamp in milliseconds corresponding to 2023/01/20.
3541
*/
36-
private const val A_TIMESTAMP = 1674737619290L
42+
private const val AN_EVENT_TIMESTAMP = 1674169200000L
3743

3844
@OptIn(ExperimentalCoroutinesApi::class)
3945
internal class DefaultGetLoadedPollsStatusTaskTest {
@@ -53,11 +59,9 @@ internal class DefaultGetLoadedPollsStatusTaskTest {
5359
fun `given poll history status exists in db with an oldestTimestamp reached when execute then the computed status is returned`() = runTest {
5460
// Given
5561
val params = givenTaskParams()
56-
// 2023/01/20
57-
val oldestTimestampReached = 1674169200000
5862
val pollHistoryStatus = aPollHistoryStatusEntity(
5963
isEndOfPollsBackward = false,
60-
oldestTimestampReached = oldestTimestampReached,
64+
oldestTimestampReached = AN_EVENT_TIMESTAMP,
6165
)
6266
fakeMonarchy.fakeRealm
6367
.givenWhere<PollHistoryStatusEntity>()
@@ -104,7 +108,7 @@ internal class DefaultGetLoadedPollsStatusTaskTest {
104108
private fun givenTaskParams(): GetLoadedPollsStatusTask.Params {
105109
return GetLoadedPollsStatusTask.Params(
106110
roomId = A_ROOM_ID,
107-
currentTimestampMs = A_TIMESTAMP,
111+
currentTimestampMs = A_CURRENT_TIMESTAMP,
108112
)
109113
}
110114

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
/*
2+
* Copyright (c) 2023 The Matrix.org Foundation C.I.C.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.matrix.android.sdk.internal.session.room.poll
18+
19+
import io.mockk.coVerifyOrder
20+
import io.mockk.every
21+
import io.mockk.mockk
22+
import io.mockk.unmockkAll
23+
import kotlinx.coroutines.ExperimentalCoroutinesApi
24+
import kotlinx.coroutines.test.runTest
25+
import org.amshove.kluent.shouldBeEqualTo
26+
import org.junit.After
27+
import org.junit.Test
28+
import org.matrix.android.sdk.api.session.room.poll.LoadedPollsStatus
29+
import org.matrix.android.sdk.api.session.room.timeline.Timeline
30+
import org.matrix.android.sdk.api.session.room.timeline.TimelineEvent
31+
import org.matrix.android.sdk.internal.database.model.PollHistoryStatusEntity
32+
import org.matrix.android.sdk.internal.database.model.PollHistoryStatusEntityFields
33+
import org.matrix.android.sdk.test.fakes.FakeMonarchy
34+
import org.matrix.android.sdk.test.fakes.FakeTimeline
35+
import org.matrix.android.sdk.test.fakes.givenEqualTo
36+
import org.matrix.android.sdk.test.fakes.givenFindFirst
37+
38+
private const val A_ROOM_ID = "room-id"
39+
40+
/**
41+
* Timestamp in milliseconds corresponding to 2023/01/26.
42+
*/
43+
private const val A_CURRENT_TIMESTAMP = 1674737619290L
44+
45+
/**
46+
* Timestamp in milliseconds corresponding to 2023/01/20.
47+
*/
48+
private const val AN_EVENT_TIMESTAMP = 1674169200000L
49+
private const val A_PERIOD_IN_DAYS = 3
50+
private const val A_PAGE_SIZE = 200
51+
52+
@OptIn(ExperimentalCoroutinesApi::class)
53+
internal class DefaultLoadMorePollsTaskTest {
54+
55+
private val fakeMonarchy = FakeMonarchy()
56+
private val fakeTimeline = FakeTimeline()
57+
58+
private val defaultLoadMorePollsTask = DefaultLoadMorePollsTask(
59+
monarchy = fakeMonarchy.instance,
60+
)
61+
62+
@After
63+
fun tearDown() {
64+
unmockkAll()
65+
}
66+
67+
@Test
68+
fun `given timeline when execute then more events are fetched in backward direction until has no more to load`() = runTest {
69+
// Given
70+
val params = givenTaskParams()
71+
val oldestEventId = "oldest"
72+
val pollHistoryStatus = aPollHistoryStatusEntity(
73+
oldestEventIdReached = oldestEventId,
74+
)
75+
fakeMonarchy.fakeRealm
76+
.givenWhere<PollHistoryStatusEntity>()
77+
.givenEqualTo(PollHistoryStatusEntityFields.ROOM_ID, A_ROOM_ID)
78+
.givenFindFirst(pollHistoryStatus)
79+
fakeTimeline.givenRestartWithEventIdSuccess(oldestEventId)
80+
val anEventId = "event-id"
81+
val aTimelineEvent = aTimelineEvent(anEventId, AN_EVENT_TIMESTAMP)
82+
fakeTimeline.givenAwaitPaginateReturns(
83+
events = listOf(aTimelineEvent),
84+
direction = Timeline.Direction.BACKWARDS,
85+
count = params.eventsPageSize,
86+
)
87+
val aPaginationState = aPaginationState(hasMoreToLoad = false)
88+
fakeTimeline.givenGetPaginationStateReturns(
89+
paginationState = aPaginationState,
90+
direction = Timeline.Direction.BACKWARDS,
91+
)
92+
val expectedLoadStatus = LoadedPollsStatus(
93+
canLoadMore = false,
94+
daysSynced = 6,
95+
hasCompletedASyncBackward = true,
96+
)
97+
98+
// When
99+
val result = defaultLoadMorePollsTask.execute(params)
100+
101+
// Then
102+
coVerifyOrder {
103+
fakeTimeline.instance.restartWithEventId(oldestEventId)
104+
fakeTimeline.instance.awaitPaginate(direction = Timeline.Direction.BACKWARDS, count = params.eventsPageSize)
105+
fakeTimeline.instance.getPaginationState(direction = Timeline.Direction.BACKWARDS)
106+
}
107+
pollHistoryStatus.mostRecentEventIdReached shouldBeEqualTo anEventId
108+
pollHistoryStatus.oldestEventIdReached shouldBeEqualTo anEventId
109+
pollHistoryStatus.isEndOfPollsBackward shouldBeEqualTo true
110+
pollHistoryStatus.oldestTimestampTargetReachedMs shouldBeEqualTo AN_EVENT_TIMESTAMP
111+
result shouldBeEqualTo expectedLoadStatus
112+
}
113+
114+
@Test
115+
fun `given timeline when execute then more events are fetched in backward direction until current target is reached`() = runTest {
116+
// Given
117+
val params = givenTaskParams()
118+
val oldestEventId = "oldest"
119+
val pollHistoryStatus = aPollHistoryStatusEntity(
120+
oldestEventIdReached = oldestEventId,
121+
)
122+
fakeMonarchy.fakeRealm
123+
.givenWhere<PollHistoryStatusEntity>()
124+
.givenEqualTo(PollHistoryStatusEntityFields.ROOM_ID, A_ROOM_ID)
125+
.givenFindFirst(pollHistoryStatus)
126+
fakeTimeline.givenRestartWithEventIdSuccess(oldestEventId)
127+
val anEventId = "event-id"
128+
val aTimelineEvent = aTimelineEvent(anEventId, AN_EVENT_TIMESTAMP)
129+
fakeTimeline.givenAwaitPaginateReturns(
130+
events = listOf(aTimelineEvent),
131+
direction = Timeline.Direction.BACKWARDS,
132+
count = params.eventsPageSize,
133+
)
134+
val aPaginationState = aPaginationState(hasMoreToLoad = true)
135+
fakeTimeline.givenGetPaginationStateReturns(
136+
paginationState = aPaginationState,
137+
direction = Timeline.Direction.BACKWARDS,
138+
)
139+
val expectedLoadStatus = LoadedPollsStatus(
140+
canLoadMore = true,
141+
daysSynced = 6,
142+
hasCompletedASyncBackward = true,
143+
)
144+
145+
// When
146+
val result = defaultLoadMorePollsTask.execute(params)
147+
148+
// Then
149+
coVerifyOrder {
150+
fakeTimeline.instance.restartWithEventId(oldestEventId)
151+
fakeTimeline.instance.awaitPaginate(direction = Timeline.Direction.BACKWARDS, count = params.eventsPageSize)
152+
fakeTimeline.instance.getPaginationState(direction = Timeline.Direction.BACKWARDS)
153+
}
154+
pollHistoryStatus.mostRecentEventIdReached shouldBeEqualTo anEventId
155+
pollHistoryStatus.oldestEventIdReached shouldBeEqualTo anEventId
156+
pollHistoryStatus.isEndOfPollsBackward shouldBeEqualTo false
157+
pollHistoryStatus.oldestTimestampTargetReachedMs shouldBeEqualTo AN_EVENT_TIMESTAMP
158+
result shouldBeEqualTo expectedLoadStatus
159+
}
160+
161+
private fun givenTaskParams(): LoadMorePollsTask.Params {
162+
return LoadMorePollsTask.Params(
163+
timeline = fakeTimeline.instance,
164+
roomId = A_ROOM_ID,
165+
currentTimestampMs = A_CURRENT_TIMESTAMP,
166+
loadingPeriodInDays = A_PERIOD_IN_DAYS,
167+
eventsPageSize = A_PAGE_SIZE,
168+
)
169+
}
170+
171+
private fun aPollHistoryStatusEntity(
172+
oldestEventIdReached: String,
173+
): PollHistoryStatusEntity {
174+
return PollHistoryStatusEntity(
175+
roomId = A_ROOM_ID,
176+
oldestEventIdReached = oldestEventIdReached,
177+
)
178+
}
179+
180+
private fun aTimelineEvent(eventId: String, timestamp: Long): TimelineEvent {
181+
val event = mockk<TimelineEvent>()
182+
every { event.root.originServerTs } returns timestamp
183+
every { event.root.eventId } returns eventId
184+
return event
185+
}
186+
187+
private fun aPaginationState(hasMoreToLoad: Boolean): Timeline.PaginationState {
188+
return Timeline.PaginationState(
189+
hasMoreToLoad = hasMoreToLoad,
190+
)
191+
}
192+
}

0 commit comments

Comments
 (0)