From 41bb743cf4c608454dbe5643de7d2716400733a4 Mon Sep 17 00:00:00 2001 From: Maxime NATUREL <46314705+mnaturel@users.noreply.github.com> Date: Wed, 25 Jan 2023 10:54:37 +0100 Subject: [PATCH] Adding unit tests for PollOptionViewStateFactory --- .../factory/PollOptionViewStateFactory.kt | 2 - .../factory/PollItemViewStateFactoryTest.kt | 40 +---- .../factory/PollOptionViewStateFactoryTest.kt | 157 ++++++++++++++++++ .../vector/app/test/fixtures/PollFixture.kt | 67 ++++++++ .../app/test/fixtures/RoomPollFixture.kt | 47 ------ 5 files changed, 228 insertions(+), 85 deletions(-) create mode 100644 vector/src/test/java/im/vector/app/features/home/room/detail/timeline/factory/PollOptionViewStateFactoryTest.kt create mode 100644 vector/src/test/java/im/vector/app/test/fixtures/PollFixture.kt delete mode 100644 vector/src/test/java/im/vector/app/test/fixtures/RoomPollFixture.kt diff --git a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/factory/PollOptionViewStateFactory.kt b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/factory/PollOptionViewStateFactory.kt index 3000164f74..875675745c 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/factory/PollOptionViewStateFactory.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/factory/PollOptionViewStateFactory.kt @@ -21,12 +21,10 @@ import im.vector.app.features.home.room.detail.timeline.item.PollResponseData import org.matrix.android.sdk.api.session.room.model.message.PollCreationInfo import javax.inject.Inject -// TODO add unit tests class PollOptionViewStateFactory @Inject constructor() { fun createPollEndedOptions(pollCreationInfo: PollCreationInfo?, pollResponseData: PollResponseData?): List { val winnerVoteCount = pollResponseData?.winnerVoteCount - return pollCreationInfo?.answers?.map { answer -> val voteSummary = pollResponseData?.getVoteSummaryOfAnOption(answer.id ?: "") PollOptionViewState.PollEnded( diff --git a/vector/src/test/java/im/vector/app/features/home/room/detail/timeline/factory/PollItemViewStateFactoryTest.kt b/vector/src/test/java/im/vector/app/features/home/room/detail/timeline/factory/PollItemViewStateFactoryTest.kt index 067cb264e1..99c6c69849 100644 --- a/vector/src/test/java/im/vector/app/features/home/room/detail/timeline/factory/PollItemViewStateFactoryTest.kt +++ b/vector/src/test/java/im/vector/app/features/home/room/detail/timeline/factory/PollItemViewStateFactoryTest.kt @@ -25,6 +25,10 @@ import im.vector.app.features.home.room.detail.timeline.item.ReactionsSummaryDat import im.vector.app.features.home.room.detail.timeline.style.TimelineMessageLayout import im.vector.app.features.poll.PollViewState import im.vector.app.test.fakes.FakeStringProvider +import im.vector.app.test.fixtures.PollFixture.A_MESSAGE_INFORMATION_DATA +import im.vector.app.test.fixtures.PollFixture.A_POLL_CONTENT +import im.vector.app.test.fixtures.PollFixture.A_POLL_OPTION_IDS +import im.vector.app.test.fixtures.PollFixture.A_POLL_RESPONSE_DATA import io.mockk.every import io.mockk.mockk import io.mockk.verify @@ -37,42 +41,6 @@ import org.matrix.android.sdk.api.session.room.model.message.PollQuestion import org.matrix.android.sdk.api.session.room.model.message.PollType import org.matrix.android.sdk.api.session.room.send.SendState -private val A_MESSAGE_INFORMATION_DATA = MessageInformationData( - eventId = "eventId", - senderId = "senderId", - ageLocalTS = 0, - avatarUrl = "", - sendState = SendState.SENT, - messageLayout = TimelineMessageLayout.Default(showAvatar = true, showDisplayName = true, showTimestamp = true), - reactionsSummary = ReactionsSummaryData(), - sentByMe = true, -) - -private val A_POLL_RESPONSE_DATA = PollResponseData( - myVote = null, - votes = emptyMap(), -) - -private val A_POLL_OPTION_IDS = listOf("5ef5f7b0-c9a1-49cf-a0b3-374729a43e76", "ec1a4db0-46d8-4d7a-9bb6-d80724715938", "3677ca8e-061b-40ab-bffe-b22e4e88fcad") - -private val A_POLL_CONTENT = MessagePollContent( - unstablePollCreationInfo = PollCreationInfo( - question = PollQuestion( - unstableQuestion = "What is your favourite coffee?" - ), kind = PollType.UNDISCLOSED_UNSTABLE, maxSelections = 1, answers = listOf( - PollAnswer( - id = A_POLL_OPTION_IDS[0], unstableAnswer = "Double Espresso" - ), - PollAnswer( - id = A_POLL_OPTION_IDS[1], unstableAnswer = "Macchiato" - ), - PollAnswer( - id = A_POLL_OPTION_IDS[2], unstableAnswer = "Iced Coffee" - ), - ) - ) -) - class PollItemViewStateFactoryTest { private val fakeStringProvider = FakeStringProvider() diff --git a/vector/src/test/java/im/vector/app/features/home/room/detail/timeline/factory/PollOptionViewStateFactoryTest.kt b/vector/src/test/java/im/vector/app/features/home/room/detail/timeline/factory/PollOptionViewStateFactoryTest.kt new file mode 100644 index 0000000000..285cff7d63 --- /dev/null +++ b/vector/src/test/java/im/vector/app/features/home/room/detail/timeline/factory/PollOptionViewStateFactoryTest.kt @@ -0,0 +1,157 @@ +/* + * Copyright (c) 2023 New Vector Ltd + * + * 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 im.vector.app.features.home.room.detail.timeline.factory + +import im.vector.app.features.home.room.detail.timeline.item.PollOptionViewState +import im.vector.app.features.home.room.detail.timeline.item.PollVoteSummaryData +import im.vector.app.test.fixtures.PollFixture.A_POLL_CONTENT +import im.vector.app.test.fixtures.PollFixture.A_POLL_OPTION_IDS +import im.vector.app.test.fixtures.PollFixture.A_POLL_RESPONSE_DATA +import org.amshove.kluent.shouldBeEqualTo +import org.junit.Test +import org.matrix.android.sdk.api.session.room.model.message.PollType + +internal class PollOptionViewStateFactoryTest { + + private val pollOptionViewStateFactory = PollOptionViewStateFactory() + + @Test + fun `given poll data when creating ended poll options then correct options are returned`() { + // Given + val winnerVotesCount = 0 + val pollResponseData = A_POLL_RESPONSE_DATA.copy( + isClosed = true, + winnerVoteCount = winnerVotesCount, + ) + val pollCreationInfo = A_POLL_CONTENT.getBestPollCreationInfo() + val expectedOptions = pollCreationInfo?.answers?.map { answer -> + PollOptionViewState.PollEnded( + optionId = answer.id.orEmpty(), + optionAnswer = answer.getBestAnswer().orEmpty(), + voteCount = 0, + votePercentage = 0.0, + isWinner = false, + ) + } + + // When + val result = pollOptionViewStateFactory.createPollEndedOptions( + pollCreationInfo = pollCreationInfo, + pollResponseData = pollResponseData, + ) + + // Then + result shouldBeEqualTo expectedOptions + } + + @Test + fun `given poll data when creating sending poll options then correct options are returned`() { + // Given + val pollCreationInfo = A_POLL_CONTENT.getBestPollCreationInfo() + val expectedOptions = pollCreationInfo?.answers?.map { answer -> + PollOptionViewState.PollSending( + optionId = answer.id.orEmpty(), + optionAnswer = answer.getBestAnswer().orEmpty(), + ) + } + + // When + val result = pollOptionViewStateFactory.createPollSendingOptions( + pollCreationInfo = pollCreationInfo, + ) + + // Then + result shouldBeEqualTo expectedOptions + } + + @Test + fun `given poll data when creating undisclosed poll options then correct options are returned`() { + // Given + val pollResponseData = A_POLL_RESPONSE_DATA + val pollCreationInfo = A_POLL_CONTENT.getBestPollCreationInfo() + val expectedOptions = pollCreationInfo?.answers?.map { answer -> + PollOptionViewState.PollUndisclosed( + optionId = answer.id.orEmpty(), + optionAnswer = answer.getBestAnswer().orEmpty(), + isSelected = false, + ) + } + + // When + val result = pollOptionViewStateFactory.createPollUndisclosedOptions( + pollCreationInfo = pollCreationInfo, + pollResponseData = pollResponseData, + ) + + // Then + result shouldBeEqualTo expectedOptions + } + + @Test + fun `given poll data when creating voted poll options then correct options are returned`() { + // Given + val pollResponseData = A_POLL_RESPONSE_DATA.copy( + totalVotes = 1, + myVote = A_POLL_OPTION_IDS[0], + votes = mapOf(A_POLL_OPTION_IDS[0] to PollVoteSummaryData(total = 1, percentage = 1.0)), + ) + val disclosedPollContent = A_POLL_CONTENT.copy( + unstablePollCreationInfo = A_POLL_CONTENT.getBestPollCreationInfo()?.copy( + kind = PollType.DISCLOSED_UNSTABLE, + ), + ) + val pollCreationInfo = disclosedPollContent.getBestPollCreationInfo() + val expectedOptions = pollCreationInfo?.answers?.mapIndexed { index, answer -> + PollOptionViewState.PollVoted( + optionId = answer.id.orEmpty(), + optionAnswer = answer.getBestAnswer().orEmpty(), + voteCount = if (index == 0) 1 else 0, + votePercentage = if (index == 0) 1.0 else 0.0, + isSelected = index == 0, + ) + } + + // When + val result = pollOptionViewStateFactory.createPollVotedOptions( + pollCreationInfo = pollCreationInfo, + pollResponseData = pollResponseData, + ) + + // Then + result shouldBeEqualTo expectedOptions + } + + @Test + fun `given poll data when creating ready poll options then correct options are returned`() { + // Given + val pollCreationInfo = A_POLL_CONTENT.getBestPollCreationInfo() + val expectedOptions = pollCreationInfo?.answers?.map { answer -> + PollOptionViewState.PollReady( + optionId = answer.id.orEmpty(), + optionAnswer = answer.getBestAnswer().orEmpty(), + ) + } + + // When + val result = pollOptionViewStateFactory.createPollReadyOptions( + pollCreationInfo = pollCreationInfo, + ) + + // Then + result shouldBeEqualTo expectedOptions + } +} diff --git a/vector/src/test/java/im/vector/app/test/fixtures/PollFixture.kt b/vector/src/test/java/im/vector/app/test/fixtures/PollFixture.kt new file mode 100644 index 0000000000..24e037b299 --- /dev/null +++ b/vector/src/test/java/im/vector/app/test/fixtures/PollFixture.kt @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2023 New Vector Ltd + * + * 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 im.vector.app.test.fixtures + +import im.vector.app.features.home.room.detail.timeline.item.MessageInformationData +import im.vector.app.features.home.room.detail.timeline.item.PollResponseData +import im.vector.app.features.home.room.detail.timeline.item.ReactionsSummaryData +import im.vector.app.features.home.room.detail.timeline.style.TimelineMessageLayout +import org.matrix.android.sdk.api.session.room.model.message.MessagePollContent +import org.matrix.android.sdk.api.session.room.model.message.PollAnswer +import org.matrix.android.sdk.api.session.room.model.message.PollCreationInfo +import org.matrix.android.sdk.api.session.room.model.message.PollQuestion +import org.matrix.android.sdk.api.session.room.model.message.PollType +import org.matrix.android.sdk.api.session.room.send.SendState + +object PollFixture { + + val A_MESSAGE_INFORMATION_DATA = MessageInformationData( + eventId = "eventId", + senderId = "senderId", + ageLocalTS = 0, + avatarUrl = "", + sendState = SendState.SENT, + messageLayout = TimelineMessageLayout.Default(showAvatar = true, showDisplayName = true, showTimestamp = true), + reactionsSummary = ReactionsSummaryData(), + sentByMe = true, + ) + + val A_POLL_RESPONSE_DATA = PollResponseData( + myVote = null, + votes = emptyMap(), + ) + + val A_POLL_OPTION_IDS = listOf("5ef5f7b0-c9a1-49cf-a0b3-374729a43e76", "ec1a4db0-46d8-4d7a-9bb6-d80724715938", "3677ca8e-061b-40ab-bffe-b22e4e88fcad") + + val A_POLL_CONTENT = MessagePollContent( + unstablePollCreationInfo = PollCreationInfo( + question = PollQuestion( + unstableQuestion = "What is your favourite coffee?" + ), kind = PollType.UNDISCLOSED_UNSTABLE, maxSelections = 1, answers = listOf( + PollAnswer( + id = A_POLL_OPTION_IDS[0], unstableAnswer = "Double Espresso" + ), + PollAnswer( + id = A_POLL_OPTION_IDS[1], unstableAnswer = "Macchiato" + ), + PollAnswer( + id = A_POLL_OPTION_IDS[2], unstableAnswer = "Iced Coffee" + ), + ) + ) + ) +} diff --git a/vector/src/test/java/im/vector/app/test/fixtures/RoomPollFixture.kt b/vector/src/test/java/im/vector/app/test/fixtures/RoomPollFixture.kt deleted file mode 100644 index 4ccd9fa35a..0000000000 --- a/vector/src/test/java/im/vector/app/test/fixtures/RoomPollFixture.kt +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright (c) 2023 New Vector Ltd - * - * 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 im.vector.app.test.fixtures - -import im.vector.app.features.home.room.detail.timeline.item.PollOptionViewState -import im.vector.app.features.roomprofile.polls.list.ui.PollSummary - -object RoomPollFixture { - - fun anActivePollSummary( - id: String = "", - timestamp: Long, - title: String = "", - ) = PollSummary.ActivePoll( - id = id, - creationTimestamp = timestamp, - title = title, - ) - - fun anEndedPollSummary( - id: String = "", - timestamp: Long, - title: String = "", - totalVotes: Int, - winnerOptions: List - ) = PollSummary.EndedPoll( - id = id, - creationTimestamp = timestamp, - title = title, - totalVotes = totalVotes, - winnerOptions = winnerOptions, - ) -}