From 28a79b46720712cc865628b1e0cd0b61ce4bf45c Mon Sep 17 00:00:00 2001 From: ganfra Date: Mon, 11 Jul 2022 19:10:09 +0200 Subject: [PATCH] Test: use this new syncOnce in tests --- .../android/sdk/common/CommonTestHelper.kt | 39 +------------------ .../timeline/TimelineForwardPaginationTest.kt | 35 +++++++++++------ 2 files changed, 26 insertions(+), 48 deletions(-) diff --git a/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/common/CommonTestHelper.kt b/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/common/CommonTestHelper.kt index a78953caac..73a4e8dbe3 100644 --- a/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/common/CommonTestHelper.kt +++ b/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/common/CommonTestHelper.kt @@ -51,7 +51,6 @@ import org.matrix.android.sdk.api.session.room.timeline.Timeline import org.matrix.android.sdk.api.session.room.timeline.TimelineEvent import org.matrix.android.sdk.api.session.room.timeline.TimelineSettings import org.matrix.android.sdk.api.session.sync.SyncState -import timber.log.Timber import java.util.UUID import java.util.concurrent.CancellationException import java.util.concurrent.CountDownLatch @@ -142,42 +141,8 @@ class CommonTestHelper internal constructor(context: Context) { * @param session the session to sync */ fun syncSession(session: Session, timeout: Long = TestConstants.timeOutMillis * 10) { - val lock = CountDownLatch(1) - coroutineScope.launch { - session.syncService().startSync(true) - val syncLiveData = session.syncService().getSyncStateLive() - val syncObserver = object : Observer { - override fun onChanged(t: SyncState?) { - if (session.syncService().hasAlreadySynced()) { - lock.countDown() - syncLiveData.removeObserver(this) - } - } - } - syncLiveData.observeForever(syncObserver) - } - await(lock, timeout) - } - - /** - * This methods clear the cache and waits for initialSync - * - * @param session the session to sync - */ - fun clearCacheAndSync(session: Session, timeout: Long = TestConstants.timeOutMillis) { - waitWithLatch(timeout) { latch -> - session.clearCache() - val syncLiveData = session.syncService().getSyncStateLive() - val syncObserver = object : Observer { - override fun onChanged(t: SyncState?) { - if (session.syncService().hasAlreadySynced()) { - Timber.v("Clear cache and synced") - syncLiveData.removeObserver(this) - latch.countDown() - } - } - } - syncLiveData.observeForever(syncObserver) + runBlockingTest(timeout) { + session.syncService().syncOnce(0L) session.syncService().startSync(true) } } diff --git a/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/session/room/timeline/TimelineForwardPaginationTest.kt b/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/session/room/timeline/TimelineForwardPaginationTest.kt index 3dbf206e08..16a96246d8 100644 --- a/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/session/room/timeline/TimelineForwardPaginationTest.kt +++ b/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/session/room/timeline/TimelineForwardPaginationTest.kt @@ -17,10 +17,13 @@ package org.matrix.android.sdk.session.room.timeline import androidx.test.filters.LargeTest +import kotlinx.coroutines.delay import kotlinx.coroutines.runBlocking import org.amshove.kluent.internal.assertEquals +import org.amshove.kluent.shouldBe import org.amshove.kluent.shouldBeFalse import org.amshove.kluent.shouldBeTrue +import org.amshove.kluent.shouldNotBe import org.junit.FixMethodOrder import org.junit.Test import org.junit.runner.RunWith @@ -31,6 +34,7 @@ import org.matrix.android.sdk.api.extensions.orFalse import org.matrix.android.sdk.api.session.events.model.EventType import org.matrix.android.sdk.api.session.events.model.toModel import org.matrix.android.sdk.api.session.getRoom +import org.matrix.android.sdk.api.session.room.model.create.CreateRoomParams import org.matrix.android.sdk.api.session.room.model.message.MessageContent import org.matrix.android.sdk.api.session.room.timeline.Timeline import org.matrix.android.sdk.api.session.room.timeline.TimelineSettings @@ -61,28 +65,38 @@ class TimelineForwardPaginationTest : InstrumentedTest { aliceSession.cryptoService().setWarnOnUnknownDevices(false) - val roomFromAlicePOV = aliceSession.getRoom(aliceRoomId)!! - // Alice sends X messages val message = "Message from Alice" val sentMessages = commonTestHelper.sendTextMessage( - roomFromAlicePOV, + aliceSession.getRoom(aliceRoomId)!!, message, numberOfMessagesToSend ) - + Timber.tag("TimelineTest").v("Sent ${sentMessages.size} messages") // Alice clear the cache and restart the sync - commonTestHelper.clearCacheAndSync(aliceSession) - val aliceTimeline = roomFromAlicePOV.timelineService().createTimeline(null, TimelineSettings(30)) + commonTestHelper.runBlockingTest { + aliceSession.getRoom(aliceRoomId) shouldNotBe null + Timber.tag("TimelineTest").v("Clear cache") + aliceSession.clearCache() + aliceSession.getRoom(aliceRoomId) shouldBe null + Timber.tag("TimelineTest").v("Launch initial sync") + aliceSession.syncService().syncOnce(0L) + aliceSession.getRoom(aliceRoomId) shouldNotBe null + } + + val aliceRoom = aliceSession.getRoom(aliceRoomId)!! + Timber.tag("TimelineTest").v("Got roomSummary : ${aliceRoom.roomSummary()}") + val aliceTimeline = aliceRoom.timelineService().createTimeline(null, TimelineSettings(30)) aliceTimeline.start() + Timber.tag("TimelineTest").v("Start timeline with initialSize of 30.") // Alice sees the 10 last message of the room, and can only navigate BACKWARD run { val lock = CountDownLatch(1) val eventsListener = commonTestHelper.createEventListener(lock) { snapshot -> - Timber.e("Alice timeline updated: with ${snapshot.size} events:") + Timber.tag("TimelineTest").v("Alice timeline updated: with ${snapshot.size} events:") snapshot.forEach { - Timber.w(" event ${it.root.content}") + Timber.tag("TimelineTest").v(" event ${it.root.content}") } // Ok, we have the 10 last messages of the initial sync @@ -104,11 +118,10 @@ class TimelineForwardPaginationTest : InstrumentedTest { run { val lock = CountDownLatch(1) val aliceEventsListener = commonTestHelper.createEventListener(lock) { snapshot -> - Timber.e("Alice timeline updated: with ${snapshot.size} events:") + Timber.tag("TimelineTest").v("#restartWithEventId Alice timeline updated: with ${snapshot.size} events:") snapshot.forEach { - Timber.w(" event ${it.root.content}") + Timber.tag("TimelineTest").v(" event ${it.root.content}") } - // The event is not in db, so it is fetch alone snapshot.size == 1 && snapshot.all { it.root.content.toModel()?.body?.startsWith("Message from Alice").orFalse() }