From fd3e5128c7cbdaecc92922d19565d49eb9ade09a Mon Sep 17 00:00:00 2001 From: Onuray Sahin Date: Thu, 10 Mar 2022 14:30:03 +0300 Subject: [PATCH] Test initial poll event. --- .../room/timeline/PollAggregationTest.kt | 119 ++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/session/room/timeline/PollAggregationTest.kt diff --git a/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/session/room/timeline/PollAggregationTest.kt b/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/session/room/timeline/PollAggregationTest.kt new file mode 100644 index 0000000000..f50b8c978f --- /dev/null +++ b/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/session/room/timeline/PollAggregationTest.kt @@ -0,0 +1,119 @@ +/* + * Copyright 2020 The Matrix.org Foundation C.I.C. + * + * 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 + * + * 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. + */ + +package org.matrix.android.sdk.session.room.timeline + +import org.amshove.kluent.fail +import org.amshove.kluent.shouldBe +import org.amshove.kluent.shouldBeEqualTo +import org.amshove.kluent.shouldContainAll +import org.junit.FixMethodOrder +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 +import org.junit.runners.MethodSorters +import org.matrix.android.sdk.InstrumentedTest +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.room.model.PollResponseAggregatedSummary +import org.matrix.android.sdk.api.session.room.model.message.MessagePollContent +import org.matrix.android.sdk.api.session.room.model.message.PollType +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.common.CommonTestHelper +import org.matrix.android.sdk.common.CryptoTestHelper +import java.util.concurrent.CountDownLatch + +@RunWith(JUnit4::class) +@FixMethodOrder(MethodSorters.JVM) +class PollAggregationTest : InstrumentedTest { + + @Test + fun testAllPollUseCases() { + val commonTestHelper = CommonTestHelper(context()) + val cryptoTestHelper = CryptoTestHelper(commonTestHelper) + val cryptoTestData = cryptoTestHelper.doE2ETestWithAliceAndBobInARoom(false) + + val aliceSession = cryptoTestData.firstSession + val aliceRoomId = cryptoTestData.roomId + val roomFromAlicePOV = aliceSession.getRoom(aliceRoomId)!! + + val roomFromBobPOV = cryptoTestData.secondSession!!.getRoom(cryptoTestData.roomId)!! + // Bob creates a poll + roomFromBobPOV.sendPoll(PollType.DISCLOSED, pollQuestion, pollOptions) + + aliceSession.startSync(true) + val aliceTimeline = roomFromAlicePOV.createTimeline(null, TimelineSettings(30)) + aliceTimeline.start() + + val TOTAL_TEST_COUNT = 1 + val lock = CountDownLatch(TOTAL_TEST_COUNT) + + val aliceEventsListener = object : Timeline.Listener { + override fun onTimelineUpdated(snapshot: List) { + snapshot.firstOrNull { it.root.getClearType() == EventType.POLL_START }?.let { pollEvent -> + //val pollEventId = pollEvent.eventId + val pollContent = pollEvent.root.content?.toModel() + val pollSummary = pollEvent.annotations?.pollResponseSummary + + if (pollContent == null) { + fail("Poll content is null") + return + } + + when (lock.count.toInt()) { + TOTAL_TEST_COUNT -> { + // Poll has just been created. + testInitialPollConditions(pollContent, pollSummary) + lock.countDown() + } + else -> { + fail("Lock count ${lock.count} didn't handled.") + } + } + } + } + } + + aliceTimeline.addListener(aliceEventsListener) + + commonTestHelper.await(lock) + + aliceTimeline.removeAllListeners() + + aliceSession.stopSync() + aliceTimeline.dispose() + cryptoTestData.cleanUp(commonTestHelper) + } + + private fun testInitialPollConditions(pollContent: MessagePollContent, pollSummary: PollResponseAggregatedSummary?) { + // No votes yet, poll summary should be null + pollSummary shouldBe null + // Question should be the same as intended + pollContent.pollCreationInfo?.question?.question shouldBeEqualTo pollQuestion + // Options should be the same as intended + pollContent.pollCreationInfo?.answers?.let { answers -> + answers.size shouldBeEqualTo pollOptions.size + answers.map { it.answer } shouldContainAll pollOptions + } + } + + companion object { + const val pollQuestion = "Do you like creating polls?" + val pollOptions = listOf("Yes", "Absolutely", "As long as tests pass") + } +}