Cleanup the test

This commit is contained in:
Benoit Marty 2021-03-31 10:51:12 +02:00 committed by Benoit Marty
parent f49df59e10
commit 0a0c8cde34

View file

@ -27,7 +27,9 @@ import org.matrix.android.sdk.api.extensions.orFalse
import org.matrix.android.sdk.api.session.events.model.toModel import org.matrix.android.sdk.api.session.events.model.toModel
import org.matrix.android.sdk.api.session.room.model.message.MessageContent import org.matrix.android.sdk.api.session.room.model.message.MessageContent
import org.matrix.android.sdk.api.session.room.timeline.TimelineSettings import org.matrix.android.sdk.api.session.room.timeline.TimelineSettings
import org.matrix.android.sdk.api.session.search.SearchResult
import org.matrix.android.sdk.common.CommonTestHelper import org.matrix.android.sdk.common.CommonTestHelper
import org.matrix.android.sdk.common.CryptoTestData
import org.matrix.android.sdk.common.CryptoTestHelper import org.matrix.android.sdk.common.CryptoTestHelper
import java.util.concurrent.CountDownLatch import java.util.concurrent.CountDownLatch
@ -44,31 +46,8 @@ class SearchMessagesTest : InstrumentedTest {
@Test @Test
fun sendTextMessageAndSearchPartOfItUsingSession() { fun sendTextMessageAndSearchPartOfItUsingSession() {
val cryptoTestData = cryptoTestHelper.doE2ETestWithAliceAndBobInARoom(false) doTest { cryptoTestData ->
val aliceSession = cryptoTestData.firstSession cryptoTestData.firstSession
val aliceRoomId = cryptoTestData.roomId
aliceSession.cryptoService().setWarnOnUnknownDevices(false)
val roomFromAlicePOV = aliceSession.getRoom(aliceRoomId)!!
val aliceTimeline = roomFromAlicePOV.createTimeline(null, TimelineSettings(10))
aliceTimeline.start()
commonTestHelper.sendTextMessage(
roomFromAlicePOV,
MESSAGE,
2)
run {
val lock = CountDownLatch(1)
val eventListener = commonTestHelper.createEventListener(lock) { snapshot ->
snapshot.count { it.root.content.toModel<MessageContent>()?.body?.startsWith(MESSAGE).orFalse() } == 2
}
aliceTimeline.addListener(eventListener)
commonTestHelper.await(lock)
val data = commonTestHelper.runBlockingTest {
aliceSession
.searchService() .searchService()
.search( .search(
searchTerm = "lore", searchTerm = "lore",
@ -78,51 +57,17 @@ class SearchMessagesTest : InstrumentedTest {
beforeLimit = 10, beforeLimit = 10,
orderByRecent = true, orderByRecent = true,
nextBatch = null, nextBatch = null,
roomId = aliceRoomId roomId = cryptoTestData.roomId
) )
} }
assertTrue(data.results?.size == 2)
assertTrue(
data.results
?.all {
(it.event.content?.get("body") as? String)?.startsWith(MESSAGE).orFalse()
}.orFalse()
)
aliceTimeline.removeAllListeners()
cryptoTestData.cleanUp(commonTestHelper)
}
aliceSession.startSync(true)
} }
@Test @Test
fun sendTextMessageAndSearchPartOfItUsingRoom() { fun sendTextMessageAndSearchPartOfItUsingRoom() {
val cryptoTestData = cryptoTestHelper.doE2ETestWithAliceAndBobInARoom(false) doTest { cryptoTestData ->
val aliceSession = cryptoTestData.firstSession cryptoTestData.firstSession
val aliceRoomId = cryptoTestData.roomId .getRoom(cryptoTestData.roomId)!!
aliceSession.cryptoService().setWarnOnUnknownDevices(false) .search(
val roomFromAlicePOV = aliceSession.getRoom(aliceRoomId)!!
val aliceTimeline = roomFromAlicePOV.createTimeline(null, TimelineSettings(10))
aliceTimeline.start()
commonTestHelper.sendTextMessage(
roomFromAlicePOV,
MESSAGE,
2)
run {
val lock = CountDownLatch(1)
val eventListener = commonTestHelper.createEventListener(lock) { snapshot ->
snapshot.count { it.root.content.toModel<MessageContent>()?.body?.startsWith(MESSAGE).orFalse() } == 2
}
aliceTimeline.addListener(eventListener)
commonTestHelper.await(lock)
val data = commonTestHelper.runBlockingTest {
roomFromAlicePOV.search(
searchTerm = "lore", searchTerm = "lore",
limit = 10, limit = 10,
includeProfile = true, includeProfile = true,
@ -132,6 +77,34 @@ class SearchMessagesTest : InstrumentedTest {
nextBatch = null nextBatch = null
) )
} }
}
private fun doTest(block: suspend (CryptoTestData) -> SearchResult) {
val cryptoTestData = cryptoTestHelper.doE2ETestWithAliceInARoom(false)
val aliceSession = cryptoTestData.firstSession
val aliceRoomId = cryptoTestData.roomId
val roomFromAlicePOV = aliceSession.getRoom(aliceRoomId)!!
val aliceTimeline = roomFromAlicePOV.createTimeline(null, TimelineSettings(10))
aliceTimeline.start()
val lock = CountDownLatch(1)
val eventListener = commonTestHelper.createEventListener(lock) { snapshot ->
snapshot.count { it.root.content.toModel<MessageContent>()?.body?.startsWith(MESSAGE).orFalse() } == 2
}
aliceTimeline.addListener(eventListener)
commonTestHelper.sendTextMessage(
roomFromAlicePOV,
MESSAGE,
2)
commonTestHelper.await(lock)
val data = commonTestHelper.runBlockingTest {
block.invoke(cryptoTestData)
}
assertTrue(data.results?.size == 2) assertTrue(data.results?.size == 2)
assertTrue( assertTrue(
@ -144,7 +117,4 @@ class SearchMessagesTest : InstrumentedTest {
aliceTimeline.removeAllListeners() aliceTimeline.removeAllListeners()
cryptoTestData.cleanUp(commonTestHelper) cryptoTestData.cleanUp(commonTestHelper)
} }
aliceSession.startSync(true)
}
} }