2019-01-18 10:12:08 +00:00
|
|
|
/*
|
2019-01-25 13:04:59 +00:00
|
|
|
* Copyright 2019 New Vector Ltd
|
2019-01-18 10:12:08 +00:00
|
|
|
*
|
2019-01-25 13:04:59 +00:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2019-01-18 10:12:08 +00:00
|
|
|
*
|
2019-01-25 13:04:59 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2019-01-18 10:12:08 +00:00
|
|
|
*/
|
|
|
|
|
2018-12-14 18:23:49 +00:00
|
|
|
package im.vector.matrix.android.session.room.timeline
|
|
|
|
|
|
|
|
import com.zhuinden.monarchy.Monarchy
|
|
|
|
import im.vector.matrix.android.InstrumentedTest
|
2019-04-01 13:18:52 +00:00
|
|
|
import im.vector.matrix.android.api.session.room.timeline.Timeline
|
|
|
|
import im.vector.matrix.android.api.session.room.timeline.TimelineEvent
|
2019-04-30 17:55:55 +00:00
|
|
|
import im.vector.matrix.android.internal.session.room.members.SenderRoomMemberExtractor
|
2019-04-01 13:18:52 +00:00
|
|
|
import im.vector.matrix.android.internal.session.room.timeline.DefaultTimeline
|
|
|
|
import im.vector.matrix.android.internal.session.room.timeline.TimelineEventFactory
|
2018-12-14 18:23:49 +00:00
|
|
|
import im.vector.matrix.android.internal.session.room.timeline.TokenChunkEventPersistor
|
2019-01-03 20:25:51 +00:00
|
|
|
import im.vector.matrix.android.internal.task.TaskExecutor
|
2018-12-14 18:23:49 +00:00
|
|
|
import im.vector.matrix.android.testCoroutineDispatchers
|
|
|
|
import io.realm.Realm
|
|
|
|
import io.realm.RealmConfiguration
|
2019-04-01 13:18:52 +00:00
|
|
|
import org.amshove.kluent.shouldEqual
|
2018-12-14 18:23:49 +00:00
|
|
|
import org.junit.Before
|
|
|
|
import org.junit.Test
|
2019-04-01 13:18:52 +00:00
|
|
|
import timber.log.Timber
|
|
|
|
import java.util.concurrent.CountDownLatch
|
2018-12-14 18:23:49 +00:00
|
|
|
|
2019-04-01 13:18:52 +00:00
|
|
|
internal class TimelineTest : InstrumentedTest {
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
private const val ROOM_ID = "roomId"
|
|
|
|
}
|
2018-12-14 18:23:49 +00:00
|
|
|
|
|
|
|
private lateinit var monarchy: Monarchy
|
|
|
|
|
|
|
|
@Before
|
|
|
|
fun setup() {
|
2019-04-01 13:18:52 +00:00
|
|
|
Timber.plant(Timber.DebugTree())
|
2018-12-14 18:23:49 +00:00
|
|
|
Realm.init(context())
|
|
|
|
val testConfiguration = RealmConfiguration.Builder().name("test-realm").build()
|
|
|
|
Realm.deleteRealm(testConfiguration)
|
|
|
|
monarchy = Monarchy.Builder().setRealmConfiguration(testConfiguration).build()
|
2019-04-01 13:18:52 +00:00
|
|
|
RoomDataHelper.fakeInitialSync(monarchy, ROOM_ID)
|
2018-12-14 18:23:49 +00:00
|
|
|
}
|
|
|
|
|
2019-04-01 13:18:52 +00:00
|
|
|
private fun createTimeline(initialEventId: String? = null): Timeline {
|
2018-12-14 18:23:49 +00:00
|
|
|
val taskExecutor = TaskExecutor(testCoroutineDispatchers)
|
|
|
|
val tokenChunkEventPersistor = TokenChunkEventPersistor(monarchy)
|
|
|
|
val paginationTask = FakePaginationTask(tokenChunkEventPersistor)
|
|
|
|
val getContextOfEventTask = FakeGetContextOfEventTask(tokenChunkEventPersistor)
|
2019-04-30 17:55:55 +00:00
|
|
|
val roomMemberExtractor = SenderRoomMemberExtractor(ROOM_ID)
|
2019-04-01 13:18:52 +00:00
|
|
|
val timelineEventFactory = TimelineEventFactory(roomMemberExtractor)
|
|
|
|
return DefaultTimeline(ROOM_ID, initialEventId, monarchy.realmConfiguration, taskExecutor, getContextOfEventTask, timelineEventFactory, paginationTask, null)
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun backPaginate_shouldLoadMoreEvents_whenPaginateIsCalled() {
|
|
|
|
val timeline = createTimeline()
|
|
|
|
timeline.start()
|
|
|
|
val paginationCount = 30
|
|
|
|
var initialLoad = 0
|
|
|
|
val latch = CountDownLatch(2)
|
|
|
|
var timelineEvents: List<TimelineEvent> = emptyList()
|
|
|
|
timeline.listener = object : Timeline.Listener {
|
|
|
|
override fun onUpdated(snapshot: List<TimelineEvent>) {
|
|
|
|
if (snapshot.isNotEmpty()) {
|
|
|
|
if (initialLoad == 0) {
|
|
|
|
initialLoad = snapshot.size
|
|
|
|
}
|
|
|
|
timelineEvents = snapshot
|
|
|
|
latch.countDown()
|
|
|
|
timeline.paginate(Timeline.Direction.BACKWARDS, paginationCount)
|
|
|
|
}
|
|
|
|
}
|
2018-12-14 18:23:49 +00:00
|
|
|
}
|
2019-04-01 13:18:52 +00:00
|
|
|
latch.await()
|
|
|
|
timelineEvents.size shouldEqual initialLoad + paginationCount
|
|
|
|
timeline.dispose()
|
2018-12-14 18:23:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|