|
| 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