Skip to content

Commit 7683b83

Browse files
committed
Updating unit tests
1 parent 5820185 commit 7683b83

File tree

3 files changed

+70
-28
lines changed

3 files changed

+70
-28
lines changed

vector/src/test/java/im/vector/app/features/home/room/detail/timeline/render/ProcessBodyOfReplyToEventUseCaseTest.kt

Lines changed: 67 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,23 @@ import org.junit.Test
3131
import org.matrix.android.sdk.api.session.events.model.Event
3232
import org.matrix.android.sdk.api.session.events.model.EventType
3333
import org.matrix.android.sdk.api.session.events.model.getPollQuestion
34+
import org.matrix.android.sdk.api.session.events.model.getRelationContent
3435
import org.matrix.android.sdk.api.session.events.model.isAudioMessage
3536
import org.matrix.android.sdk.api.session.events.model.isFileMessage
3637
import org.matrix.android.sdk.api.session.events.model.isImageMessage
3738
import org.matrix.android.sdk.api.session.events.model.isLiveLocation
38-
import org.matrix.android.sdk.api.session.events.model.isPoll
39+
import org.matrix.android.sdk.api.session.events.model.isPollEnd
40+
import org.matrix.android.sdk.api.session.events.model.isPollStart
3941
import org.matrix.android.sdk.api.session.events.model.isSticker
4042
import org.matrix.android.sdk.api.session.events.model.isVideoMessage
4143
import org.matrix.android.sdk.api.session.events.model.isVoiceMessage
44+
import org.matrix.android.sdk.api.session.room.model.message.MessagePollContent
45+
import org.matrix.android.sdk.api.session.room.model.message.PollCreationInfo
46+
import org.matrix.android.sdk.api.session.room.model.message.PollQuestion
47+
import org.matrix.android.sdk.api.session.room.model.relation.RelationDefaultContent
4248
import org.matrix.android.sdk.api.session.room.model.relation.ReplyToContent
4349
import org.matrix.android.sdk.api.session.room.timeline.TimelineEvent
50+
import org.matrix.android.sdk.api.session.room.timeline.getLastMessageContent
4451

4552
private const val A_ROOM_ID = "room-id"
4653
private const val AN_EVENT_ID = "event-id"
@@ -93,6 +100,7 @@ class ProcessBodyOfReplyToEventUseCaseTest {
93100
fun setup() {
94101
givenNewPrefix()
95102
mockkStatic("org.matrix.android.sdk.api.session.events.model.EventKt")
103+
mockkStatic("org.matrix.android.sdk.api.session.room.timeline.TimelineEventKt")
96104
}
97105

98106
@After
@@ -104,7 +112,7 @@ class ProcessBodyOfReplyToEventUseCaseTest {
104112
fun `given a replied event of type file message when process the formatted body then content is replaced by correct string`() {
105113
// Given
106114
givenTypeOfRepliedEvent(isFileMessage = true)
107-
givenNewContentForId(R.string.message_reply_to_sender_sent_file)
115+
givenContentForId(R.string.message_reply_to_sender_sent_file, content = A_NEW_CONTENT)
108116

109117
executeAndAssertResult()
110118
}
@@ -113,7 +121,7 @@ class ProcessBodyOfReplyToEventUseCaseTest {
113121
fun `given a replied event of type voice message when process the formatted body then content is replaced by correct string`() {
114122
// Given
115123
givenTypeOfRepliedEvent(isVoiceMessage = true)
116-
givenNewContentForId(R.string.message_reply_to_sender_sent_voice_message)
124+
givenContentForId(R.string.message_reply_to_sender_sent_voice_message, content = A_NEW_CONTENT)
117125

118126
executeAndAssertResult()
119127
}
@@ -122,7 +130,7 @@ class ProcessBodyOfReplyToEventUseCaseTest {
122130
fun `given a replied event of type audio message when process the formatted body then content is replaced by correct string`() {
123131
// Given
124132
givenTypeOfRepliedEvent(isAudioMessage = true)
125-
givenNewContentForId(R.string.message_reply_to_sender_sent_audio_file)
133+
givenContentForId(R.string.message_reply_to_sender_sent_audio_file, content = A_NEW_CONTENT)
126134

127135
executeAndAssertResult()
128136
}
@@ -131,7 +139,7 @@ class ProcessBodyOfReplyToEventUseCaseTest {
131139
fun `given a replied event of type image message when process the formatted body then content is replaced by correct string`() {
132140
// Given
133141
givenTypeOfRepliedEvent(isImageMessage = true)
134-
givenNewContentForId(R.string.message_reply_to_sender_sent_image)
142+
givenContentForId(R.string.message_reply_to_sender_sent_image, content = A_NEW_CONTENT)
135143

136144
executeAndAssertResult()
137145
}
@@ -140,7 +148,7 @@ class ProcessBodyOfReplyToEventUseCaseTest {
140148
fun `given a replied event of type video message when process the formatted body then content is replaced by correct string`() {
141149
// Given
142150
givenTypeOfRepliedEvent(isVideoMessage = true)
143-
givenNewContentForId(R.string.message_reply_to_sender_sent_video)
151+
givenContentForId(R.string.message_reply_to_sender_sent_video, content = A_NEW_CONTENT)
144152

145153
executeAndAssertResult()
146154
}
@@ -149,49 +157,58 @@ class ProcessBodyOfReplyToEventUseCaseTest {
149157
fun `given a replied event of type sticker message when process the formatted body then content is replaced by correct string`() {
150158
// Given
151159
givenTypeOfRepliedEvent(isStickerMessage = true)
152-
givenNewContentForId(R.string.message_reply_to_sender_sent_sticker)
160+
givenContentForId(R.string.message_reply_to_sender_sent_sticker, content = A_NEW_CONTENT)
153161

154162
executeAndAssertResult()
155163
}
156164

157165
@Test
158-
fun `given a replied event of type poll message with null question when process the formatted body then content is replaced by correct string`() {
166+
fun `given a replied event of type poll start message with null question when process the formatted body then content is replaced by correct string`() {
159167
// Given
160-
givenTypeOfRepliedEvent(isPollMessage = true)
161-
givenNewContentForId(R.string.message_reply_to_sender_created_poll)
162-
every { fakeRepliedEvent.getClearType() } returns EventType.POLL_START.unstable
168+
givenTypeOfRepliedEvent(isPollStartMessage = true)
169+
givenContentForId(R.string.message_reply_to_sender_created_poll, content = A_NEW_CONTENT)
163170
every { fakeRepliedEvent.getPollQuestion() } returns null
164171

165172
executeAndAssertResult()
166173
}
167174

168175
@Test
169-
fun `given a replied event of type poll message with existing question when process the formatted body then content is replaced by correct string`() {
176+
fun `given a replied event of type poll start message with existing question when process the formatted body then content is replaced by correct string`() {
170177
// Given
171-
givenTypeOfRepliedEvent(isPollMessage = true)
172-
givenNewContentForId(R.string.message_reply_to_sender_created_poll)
173-
every { fakeRepliedEvent.getClearType() } returns EventType.POLL_START.unstable
178+
givenTypeOfRepliedEvent(isPollStartMessage = true)
179+
givenContentForId(R.string.message_reply_to_sender_created_poll, content = "")
174180
every { fakeRepliedEvent.getPollQuestion() } returns A_NEW_CONTENT
175181

176182
executeAndAssertResult()
177183
}
178184

179185
@Test
180-
fun `given a replied event of type poll end message when process the formatted body then content is replaced by correct string`() {
186+
fun `given a replied event of type poll end message with null question when process the formatted body then content is replaced by correct string`() {
181187
// Given
182-
givenTypeOfRepliedEvent(isPollMessage = true)
183-
givenNewContentForId(R.string.message_reply_to_sender_ended_poll)
184-
every { fakeRepliedEvent.getClearType() } returns EventType.POLL_END.unstable
188+
givenTypeOfRepliedEvent(isPollEndMessage = true)
189+
givenContentForId(R.string.message_reply_to_sender_ended_poll, content = A_NEW_CONTENT)
190+
givenPollQuestionReturns(fakeRepliedEvent, null)
185191
every { fakeRepliedEvent.getPollQuestion() } returns null
186192

187193
executeAndAssertResult()
188194
}
189195

196+
@Test
197+
fun `given a replied event of type poll end message with existing question when process the formatted body then content is replaced by correct string`() {
198+
// Given
199+
givenTypeOfRepliedEvent(isPollEndMessage = true)
200+
givenContentForId(R.string.message_reply_to_sender_ended_poll, content = "")
201+
every { fakeRepliedEvent.getClearType() } returns EventType.POLL_END.unstable
202+
givenPollQuestionReturns(fakeRepliedEvent, A_NEW_CONTENT)
203+
204+
executeAndAssertResult()
205+
}
206+
190207
@Test
191208
fun `given a replied event of type live location message when process the formatted body then content is replaced by correct string`() {
192209
// Given
193210
givenTypeOfRepliedEvent(isLiveLocationMessage = true)
194-
givenNewContentForId(R.string.live_location_description)
211+
givenContentForId(R.string.live_location_description, content = A_NEW_CONTENT)
195212

196213
executeAndAssertResult()
197214
}
@@ -242,13 +259,16 @@ class ProcessBodyOfReplyToEventUseCaseTest {
242259

243260
private fun givenARepliedEvent(timelineEvent: TimelineEvent? = mockk()): Event {
244261
val event = mockk<Event>()
262+
val eventId = "event-id"
263+
every { event.eventId } returns eventId
264+
every { event.roomId } returns A_ROOM_ID
245265
timelineEvent?.let { every { it.root } returns event }
246266
fakeActiveSessionHolder
247267
.fakeSession
248268
.roomService()
249269
.getRoom(A_ROOM_ID)
250270
.timelineService()
251-
.givenTimelineEvent(timelineEvent)
271+
.givenTimelineEventReturns(eventId, timelineEvent)
252272
return event
253273
}
254274

@@ -259,7 +279,8 @@ class ProcessBodyOfReplyToEventUseCaseTest {
259279
isImageMessage: Boolean = false,
260280
isVideoMessage: Boolean = false,
261281
isStickerMessage: Boolean = false,
262-
isPollMessage: Boolean = false,
282+
isPollEndMessage: Boolean = false,
283+
isPollStartMessage: Boolean = false,
263284
isLiveLocationMessage: Boolean = false,
264285
) {
265286
every { fakeRepliedEvent.isFileMessage() } returns isFileMessage
@@ -268,15 +289,36 @@ class ProcessBodyOfReplyToEventUseCaseTest {
268289
every { fakeRepliedEvent.isImageMessage() } returns isImageMessage
269290
every { fakeRepliedEvent.isVideoMessage() } returns isVideoMessage
270291
every { fakeRepliedEvent.isSticker() } returns isStickerMessage
271-
every { fakeRepliedEvent.isPoll() } returns isPollMessage
292+
every { fakeRepliedEvent.isPollEnd() } returns isPollEndMessage
293+
every { fakeRepliedEvent.isPollStart() } returns isPollStartMessage
272294
every { fakeRepliedEvent.isLiveLocation() } returns isLiveLocationMessage
273295
}
274296

275297
private fun givenNewPrefix() {
276298
fakeStringProvider.given(R.string.message_reply_to_prefix, A_NEW_PREFIX)
277299
}
278300

279-
private fun givenNewContentForId(@StringRes resId: Int) {
280-
fakeStringProvider.given(resId, A_NEW_CONTENT)
301+
private fun givenContentForId(@StringRes resId: Int, content: String) {
302+
fakeStringProvider.given(resId, content)
303+
}
304+
305+
private fun givenPollQuestionReturns(pollEndEvent: Event, question: String?) {
306+
val eventId = "start-event-id"
307+
val relationContent = mockk<RelationDefaultContent>()
308+
every { relationContent.eventId } returns eventId
309+
every { pollEndEvent.getRelationContent() } returns relationContent
310+
val timelineEvent = mockk<TimelineEvent>()
311+
val messagePollContent = MessagePollContent(
312+
pollCreationInfo = PollCreationInfo(
313+
question = PollQuestion(unstableQuestion = question)
314+
)
315+
)
316+
every { timelineEvent.getLastMessageContent() } returns messagePollContent
317+
fakeActiveSessionHolder
318+
.fakeSession
319+
.roomService()
320+
.getRoom(A_ROOM_ID)
321+
.timelineService()
322+
.givenTimelineEventReturns(eventId, timelineEvent)
281323
}
282324
}

vector/src/test/java/im/vector/app/features/poll/create/CreatePollViewModelTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class CreatePollViewModelTest {
6868
.roomService()
6969
.getRoom(A_FAKE_ROOM_ID)
7070
.timelineService()
71-
.givenTimelineEvent(A_POLL_START_TIMELINE_EVENT)
71+
.givenTimelineEventReturns(A_POLL_START_TIMELINE_EVENT.eventId, A_POLL_START_TIMELINE_EVENT)
7272
}
7373

7474
@After

vector/src/test/java/im/vector/app/test/fakes/FakeTimelineService.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import org.matrix.android.sdk.api.session.room.timeline.TimelineService
2323

2424
class FakeTimelineService : TimelineService by mockk() {
2525

26-
fun givenTimelineEvent(event: TimelineEvent?) {
27-
every { getTimelineEvent(any()) } returns event
26+
fun givenTimelineEventReturns(eventId: String, event: TimelineEvent?) {
27+
every { getTimelineEvent(eventId) } returns event
2828
}
2929
}

0 commit comments

Comments
 (0)