diff --git a/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/common/CommonTestHelper.kt b/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/common/CommonTestHelper.kt index 2529be9547..9b3ebad03b 100644 --- a/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/common/CommonTestHelper.kt +++ b/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/common/CommonTestHelper.kt @@ -368,3 +368,13 @@ class CommonTestHelper(context: Context) { session.close() } } + +fun List.checkSendOrder(baseTextMessage: String, numberOfMessages: Int, startIndex: Int): Boolean { + return drop(startIndex) + .take(numberOfMessages) + .foldRightIndexed(true) { index, timelineEvent, acc -> + val body = timelineEvent.root.content.toModel()?.body + val currentMessageSuffix = numberOfMessages - index + acc && (body == null || body.startsWith(baseTextMessage) && body.endsWith("#$currentMessageSuffix")) + } +} diff --git a/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/session/room/timeline/TimelineBackToPreviousLastForwardTest.kt b/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/session/room/timeline/TimelineBackToPreviousLastForwardTest.kt index d55087a8c7..22e9c2f353 100644 --- a/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/session/room/timeline/TimelineBackToPreviousLastForwardTest.kt +++ b/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/session/room/timeline/TimelineBackToPreviousLastForwardTest.kt @@ -25,6 +25,7 @@ import im.vector.matrix.android.api.session.room.timeline.Timeline import im.vector.matrix.android.api.session.room.timeline.TimelineSettings import im.vector.matrix.android.common.CommonTestHelper import im.vector.matrix.android.common.CryptoTestHelper +import im.vector.matrix.android.common.checkSendOrder import org.amshove.kluent.shouldBeFalse import org.amshove.kluent.shouldBeTrue import org.junit.Assert.assertTrue @@ -90,10 +91,12 @@ class TimelineBackToPreviousLastForwardTest : InstrumentedTest { // Bob stop to sync bobSession.stopSync() + val messageRoot = "First messages from Alice" + // Alice sends 30 messages commonTestHelper.sendTextMessage( roomFromAlicePOV, - "First messages from Alice", + messageRoot, 30) // Bob start to sync @@ -109,7 +112,7 @@ class TimelineBackToPreviousLastForwardTest : InstrumentedTest { // Ok, we have the 10 last messages from Alice. snapshot.size == 10 - && snapshot.all { it.root.content.toModel()?.body?.startsWith("First messages from Alice").orFalse() } + && snapshot.all { it.root.content.toModel()?.body?.startsWith(messageRoot).orFalse() } } bobTimeline.addListener(eventsListener) @@ -159,33 +162,8 @@ class TimelineBackToPreviousLastForwardTest : InstrumentedTest { // Bob can see the first event of the room (so Back pagination has worked) snapshot.lastOrNull()?.root?.getClearType() == EventType.STATE_ROOM_CREATE // 8 for room creation item, and 30 for the forward pagination - && snapshot.size == 8 - } - - bobTimeline.addListener(eventsListener) - - bobTimeline.paginate(Timeline.Direction.FORWARDS, 50) - - commonTestHelper.await(lock) - bobTimeline.removeAllListeners() - - bobTimeline.hasMoreToLoad(Timeline.Direction.FORWARDS).shouldBeTrue() - bobTimeline.hasMoreToLoad(Timeline.Direction.BACKWARDS).shouldBeFalse() - } - - // Do it again, now we should have a next token, so we can paginate FORWARD - run { - val lock = CountDownLatch(1) - val eventsListener = commonTestHelper.createEventListener(lock) { snapshot -> - Timber.e("Bob timeline updated: with ${snapshot.size} events:") - snapshot.forEach { - Timber.w(" event ${it.root}") - } - - // Bob can see the first event of the room (so Back pagination has worked) - snapshot.lastOrNull()?.root?.getClearType() == EventType.STATE_ROOM_CREATE - // 8 for room creation item, and 30 for the forward pagination - && snapshot.size == 8 + 30 + && snapshot.size == 38 + && snapshot.checkSendOrder(messageRoot, 30, 0) } bobTimeline.addListener(eventsListener) @@ -197,8 +175,8 @@ class TimelineBackToPreviousLastForwardTest : InstrumentedTest { bobTimeline.hasMoreToLoad(Timeline.Direction.FORWARDS).shouldBeFalse() bobTimeline.hasMoreToLoad(Timeline.Direction.BACKWARDS).shouldBeFalse() - } + } bobTimeline.dispose() cryptoTestData.cleanUp(commonTestHelper) diff --git a/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/session/room/timeline/TimelineForwardPaginationTest.kt b/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/session/room/timeline/TimelineForwardPaginationTest.kt index ae6a9f8d42..adb5c81378 100644 --- a/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/session/room/timeline/TimelineForwardPaginationTest.kt +++ b/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/session/room/timeline/TimelineForwardPaginationTest.kt @@ -25,6 +25,7 @@ import im.vector.matrix.android.api.session.room.timeline.Timeline import im.vector.matrix.android.api.session.room.timeline.TimelineSettings import im.vector.matrix.android.common.CommonTestHelper import im.vector.matrix.android.common.CryptoTestHelper +import im.vector.matrix.android.common.checkSendOrder import org.amshove.kluent.shouldBeFalse import org.amshove.kluent.shouldBeTrue import org.junit.FixMethodOrder @@ -58,9 +59,10 @@ class TimelineForwardPaginationTest : InstrumentedTest { val roomFromAlicePOV = aliceSession.getRoom(aliceRoomId)!! // Alice sends X messages + val message = "Message from Alice" val sentMessages = commonTestHelper.sendTextMessage( roomFromAlicePOV, - "Message from Alice", + message, numberOfMessagesToSend) // Alice clear the cache @@ -85,7 +87,7 @@ class TimelineForwardPaginationTest : InstrumentedTest { // Ok, we have the 10 last messages of the initial sync snapshot.size == 10 - && snapshot.all { it.root.content.toModel()?.body?.startsWith("Message from Alice").orFalse() } + && snapshot.all { it.root.content.toModel()?.body?.startsWith(message).orFalse() } } // Open the timeline at last sent message @@ -163,9 +165,9 @@ class TimelineForwardPaginationTest : InstrumentedTest { snapshot.forEach { Timber.w(" event ${it.root.content}") } - // 6 for room creation item (backward pagination),and numberOfMessagesToSend (all the message of the room) snapshot.size == 6 + numberOfMessagesToSend + && snapshot.checkSendOrder(message, numberOfMessagesToSend, 0) } aliceTimeline.addListener(aliceEventsListener) diff --git a/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/session/room/timeline/TimelinePreviousLastForwardTest.kt b/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/session/room/timeline/TimelinePreviousLastForwardTest.kt index 0ca5cfdda0..3e673e4c08 100644 --- a/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/session/room/timeline/TimelinePreviousLastForwardTest.kt +++ b/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/session/room/timeline/TimelinePreviousLastForwardTest.kt @@ -25,6 +25,7 @@ import im.vector.matrix.android.api.session.room.timeline.Timeline import im.vector.matrix.android.api.session.room.timeline.TimelineSettings import im.vector.matrix.android.common.CommonTestHelper import im.vector.matrix.android.common.CryptoTestHelper +import im.vector.matrix.android.common.checkSendOrder import org.amshove.kluent.shouldBeFalse import org.amshove.kluent.shouldBeTrue import org.junit.FixMethodOrder @@ -85,10 +86,11 @@ class TimelinePreviousLastForwardTest : InstrumentedTest { // Bob stop to sync bobSession.stopSync() + val firstMessage = "First messages from Alice" // Alice sends 30 messages val firstMessageFromAliceId = commonTestHelper.sendTextMessage( roomFromAlicePOV, - "First messages from Alice", + firstMessage, 30) .last() .eventId @@ -106,7 +108,7 @@ class TimelinePreviousLastForwardTest : InstrumentedTest { // Ok, we have the 10 last messages from Alice. This will be our future previous lastForward chunk snapshot.size == 10 - && snapshot.all { it.root.content.toModel()?.body?.startsWith("First messages from Alice").orFalse() } + && snapshot.all { it.root.content.toModel()?.body?.startsWith(firstMessage).orFalse() } } bobTimeline.addListener(eventsListener) @@ -120,10 +122,11 @@ class TimelinePreviousLastForwardTest : InstrumentedTest { // Bob stop to sync bobSession.stopSync() + val secondMessage = "Second messages from Alice" // Alice sends again 30 messages commonTestHelper.sendTextMessage( roomFromAlicePOV, - "Second messages from Alice", + secondMessage, 30) // Bob start to sync @@ -139,7 +142,7 @@ class TimelinePreviousLastForwardTest : InstrumentedTest { // Ok, we have the 10 last messages from Alice. This will be our future previous lastForward chunk snapshot.size == 10 - && snapshot.all { it.root.content.toModel()?.body?.startsWith("Second messages from Alice").orFalse() } + && snapshot.all { it.root.content.toModel()?.body?.startsWith(secondMessage).orFalse() } } bobTimeline.addListener(eventsListener) @@ -216,6 +219,8 @@ class TimelinePreviousLastForwardTest : InstrumentedTest { snapshot.lastOrNull()?.root?.getClearType() == EventType.STATE_ROOM_CREATE // 8 for room creation item 60 message from Alice && snapshot.size == 8 + 60 + && snapshot.checkSendOrder(secondMessage, 30, 0) + && snapshot.checkSendOrder(firstMessage, 30, 30) } bobTimeline.addListener(eventsListener)