@@ -27,6 +27,7 @@ import org.junit.runner.RunWith
2727import org.junit.runners.JUnit4
2828import org.junit.runners.MethodSorters
2929import org.matrix.android.sdk.InstrumentedTest
30+ import org.matrix.android.sdk.api.query.QueryStringValue
3031import org.matrix.android.sdk.api.session.Session
3132import org.matrix.android.sdk.api.session.events.model.EventType
3233import org.matrix.android.sdk.api.session.events.model.toContent
@@ -45,7 +46,6 @@ import org.matrix.android.sdk.common.CommonTestHelper
4546import org.matrix.android.sdk.common.CommonTestHelper.Companion.runCryptoTest
4647import org.matrix.android.sdk.common.CryptoTestHelper
4748import org.matrix.android.sdk.common.SessionTestParams
48- import org.matrix.android.sdk.common.TestConstants
4949
5050@RunWith(JUnit4 ::class )
5151@FixMethodOrder(MethodSorters .JVM )
@@ -249,13 +249,16 @@ class E2eeShareKeysHistoryTest : InstrumentedTest {
249249 val e2eRoomID = cryptoTestData.roomId
250250
251251 // Alice
252- val aliceSession = cryptoTestData.firstSession
252+ val aliceSession = cryptoTestData.firstSession.also {
253+ it.cryptoService().enableShareKeyOnInvite(true )
254+ }
253255 val aliceRoomPOV = aliceSession.roomService().getRoom(e2eRoomID)!!
254256// val aliceCryptoStore = (aliceSession.cryptoService() as DefaultCryptoService).cryptoStoreForTesting
255257
256258 // Bob
257- val bobSession = cryptoTestData.secondSession
258- val bobRoomPOV = bobSession!! .roomService().getRoom(e2eRoomID)!!
259+ val bobSession = cryptoTestData.secondSession!!
260+
261+ val bobRoomPOV = bobSession.roomService().getRoom(e2eRoomID)!!
259262
260263 assertEquals(bobRoomPOV.roomSummary()?.joinedMembersCount, 2 )
261264 Log .v(" #E2E TEST ROTATION" , " Alice and Bob are in roomId: $e2eRoomID " )
@@ -266,17 +269,21 @@ class E2eeShareKeysHistoryTest : InstrumentedTest {
266269
267270 // Bob should be able to decrypt the message
268271 var firstAliceMessageMegolmSessionId: String? = null
272+ val bobRoomPov = bobSession.roomService().getRoom(e2eRoomID)
269273 testHelper.waitWithLatch { latch ->
270274 testHelper.retryPeriodicallyWithLatch(latch) {
271- val timelineEvent = bobSession.roomService().getRoom(e2eRoomID)
275+ val timelineEvent = bobRoomPov
272276 ?.timelineService()
273277 ?.getTimelineEvent(aliceMessageId!! )
274278 (timelineEvent != null &&
275279 timelineEvent.isEncrypted() &&
276280 timelineEvent.root.getClearType() == EventType .MESSAGE ).also {
277281 if (it) {
278282 firstAliceMessageMegolmSessionId = timelineEvent?.root?.content?.get(" session_id" ) as ? String
279- Log .v(" #E2E TEST" , " Bob can decrypt the message: ${timelineEvent?.root?.getDecryptedTextSummary()} " )
283+ Log .v(
284+ " #E2E TEST" ,
285+ " Bob can decrypt the message (sid:$firstAliceMessageMegolmSessionId ): ${timelineEvent?.root?.getDecryptedTextSummary()} "
286+ )
280287 }
281288 }
282289 }
@@ -288,14 +295,18 @@ class E2eeShareKeysHistoryTest : InstrumentedTest {
288295 sendMessageInRoom(aliceRoomPOV, " Other msg" , testHelper)?.let { secondMessage ->
289296 testHelper.waitWithLatch { latch ->
290297 testHelper.retryPeriodicallyWithLatch(latch) {
291- val timelineEvent = bobSession.roomService().getRoom(e2eRoomID)
298+ val timelineEvent = bobRoomPov
292299 ?.timelineService()
293300 ?.getTimelineEvent(secondMessage)
294301 (timelineEvent != null &&
295302 timelineEvent.isEncrypted() &&
296303 timelineEvent.root.getClearType() == EventType .MESSAGE ).also {
297304 if (it) {
298305 secondAliceMessageSessionId = timelineEvent?.root?.content?.get(" session_id" ) as ? String
306+ Log .v(
307+ " #E2E TEST" ,
308+ " Bob can decrypt the message (sid:$secondAliceMessageSessionId ): ${timelineEvent?.root?.getDecryptedTextSummary()} "
309+ )
299310 }
300311 }
301312 }
@@ -305,7 +316,7 @@ class E2eeShareKeysHistoryTest : InstrumentedTest {
305316 Log .v(" #E2E TEST ROTATION" , " No rotation needed yet" )
306317
307318 // Let's change the room history visibility
308- testHelper.waitWithLatch {
319+ testHelper.runBlockingTest {
309320 aliceRoomPOV.stateService()
310321 .sendStateEvent(
311322 eventType = EventType .STATE_ROOM_HISTORY_VISIBILITY ,
@@ -314,7 +325,14 @@ class E2eeShareKeysHistoryTest : InstrumentedTest {
314325 historyVisibilityStr = nextRoomHistoryVisibility.historyVisibilityStr
315326 ).toContent()
316327 )
317- it.countDown()
328+ }
329+
330+ // ensure that the state did synced down
331+ testHelper.waitWithLatch { latch ->
332+ testHelper.retryPeriodicallyWithLatch(latch) {
333+ aliceRoomPOV.stateService().getStateEvent(EventType .STATE_ROOM_HISTORY_VISIBILITY , QueryStringValue .IsEmpty )?.content
334+ ?.toModel<RoomHistoryVisibilityContent >()?.historyVisibility == nextRoomHistoryVisibility.historyVisibility
335+ }
318336 }
319337
320338 testHelper.waitWithLatch { latch ->
@@ -333,7 +351,7 @@ class E2eeShareKeysHistoryTest : InstrumentedTest {
333351 sendMessageInRoom(aliceRoomPOV, " Message after visibility change" , testHelper)?.let { thirdMessage ->
334352 testHelper.waitWithLatch { latch ->
335353 testHelper.retryPeriodicallyWithLatch(latch) {
336- val timelineEvent = bobSession.roomService().getRoom(e2eRoomID)
354+ val timelineEvent = bobRoomPov
337355 ?.timelineService()
338356 ?.getTimelineEvent(thirdMessage)
339357 (timelineEvent != null &&
@@ -362,14 +380,14 @@ class E2eeShareKeysHistoryTest : InstrumentedTest {
362380 }
363381
364382 private fun sendMessageInRoom (aliceRoomPOV : Room , text : String , testHelper : CommonTestHelper ): String? {
383+ val timeline = aliceRoomPOV.timelineService().createTimeline(null , TimelineSettings (60 ))
384+ timeline.start()
365385 aliceRoomPOV.sendService().sendTextMessage(text)
366386 var sentEventId: String? = null
367- testHelper.waitWithLatch(4 * TestConstants .timeOutMillis) { latch ->
368- val timeline = aliceRoomPOV.timelineService().createTimeline(null , TimelineSettings (60 ))
369- timeline.start()
387+ testHelper.waitWithLatch { latch ->
370388 testHelper.retryPeriodicallyWithLatch(latch) {
371389 val decryptedMsg = timeline.getSnapshot()
372- .filter { it.root.getClearType() == EventType .MESSAGE }
390+ .filter { it.root.isEncrypted() || it.root. getClearType() == EventType .MESSAGE }
373391 .also { list ->
374392 val message = list.joinToString(" ," , " [" , " ]" ) { " ${it.root.type} |${it.root.sendState} " }
375393 Log .v(" #E2E TEST" , " Timeline snapshot is $message " )
@@ -379,9 +397,8 @@ class E2eeShareKeysHistoryTest : InstrumentedTest {
379397 sentEventId = decryptedMsg?.eventId
380398 decryptedMsg != null
381399 }
382-
383- timeline.dispose()
384400 }
401+ timeline.dispose()
385402 return sentEventId
386403 }
387404
0 commit comments