Add test for RoomGroupMessageCreator

This commit is contained in:
Benoit Marty 2023-11-27 10:43:31 +01:00 committed by Benoit Marty
parent 97c764cd79
commit 119382d03d
3 changed files with 213 additions and 38 deletions

View file

@ -0,0 +1,174 @@
/*
* 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 io.element.android.libraries.push.impl.notifications
import android.content.Context
import com.google.common.truth.Truth.assertThat
import io.element.android.libraries.matrix.test.A_ROOM_ID
import io.element.android.libraries.matrix.ui.components.aMatrixUser
import io.element.android.libraries.push.impl.notifications.factories.createNotificationCreator
import io.element.android.libraries.push.impl.notifications.fixtures.aNotifiableMessageEvent
import io.element.android.services.toolbox.impl.strings.AndroidStringProvider
import kotlinx.coroutines.test.runTest
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
private const val A_TIMESTAMP = 6480L
@RunWith(RobolectricTestRunner::class)
class RoomGroupMessageCreatorTest {
@Test
fun `test createRoomMessage with one Event`() = runTest {
val sut = createRoomGroupMessageCreator()
val result = sut.createRoomMessage(
currentUser = aMatrixUser(),
events = listOf(
aNotifiableMessageEvent(timestamp = A_TIMESTAMP).copy(
imageUriString = "aUri",
)
),
roomId = A_ROOM_ID,
)
val resultMetaWithoutFormatting = result.meta.copy(
summaryLine = result.meta.summaryLine.toString()
)
assertThat(resultMetaWithoutFormatting).isEqualTo(
RoomNotification.Message.Meta(
roomId = A_ROOM_ID,
summaryLine = "room-name: message-body",
messageCount = 1,
latestTimestamp = A_TIMESTAMP,
shouldBing = false,
)
)
}
@Test
fun `test createRoomMessage with one noisy Event`() = runTest {
val sut = createRoomGroupMessageCreator()
val result = sut.createRoomMessage(
currentUser = aMatrixUser(),
events = listOf(
aNotifiableMessageEvent(timestamp = A_TIMESTAMP).copy(
noisy = true,
)
),
roomId = A_ROOM_ID,
)
val resultMetaWithoutFormatting = result.meta.copy(
summaryLine = result.meta.summaryLine.toString()
)
assertThat(resultMetaWithoutFormatting).isEqualTo(
RoomNotification.Message.Meta(
roomId = A_ROOM_ID,
summaryLine = "room-name: message-body",
messageCount = 1,
latestTimestamp = A_TIMESTAMP,
shouldBing = true,
)
)
}
@Test
fun `test createRoomMessage with two Events`() = runTest {
val sut = createRoomGroupMessageCreator()
val result = sut.createRoomMessage(
currentUser = aMatrixUser(),
events = listOf(
aNotifiableMessageEvent(timestamp = A_TIMESTAMP),
aNotifiableMessageEvent(timestamp = A_TIMESTAMP + 10),
),
roomId = A_ROOM_ID,
)
val resultMetaWithoutFormatting = result.meta.copy(
summaryLine = result.meta.summaryLine.toString()
)
assertThat(resultMetaWithoutFormatting).isEqualTo(
RoomNotification.Message.Meta(
roomId = A_ROOM_ID,
summaryLine = "room-name: 2 messages",
messageCount = 2,
latestTimestamp = A_TIMESTAMP + 10,
shouldBing = false,
)
)
}
@Test
fun `test createRoomMessage with smart reply error`() = runTest {
val sut = createRoomGroupMessageCreator()
val result = sut.createRoomMessage(
currentUser = aMatrixUser(),
events = listOf(
aNotifiableMessageEvent(timestamp = A_TIMESTAMP).copy(
outGoingMessage = true,
outGoingMessageFailed = true,
),
),
roomId = A_ROOM_ID,
)
val resultMetaWithoutFormatting = result.meta.copy(
summaryLine = result.meta.summaryLine.toString()
)
assertThat(resultMetaWithoutFormatting).isEqualTo(
RoomNotification.Message.Meta(
roomId = A_ROOM_ID,
summaryLine = "room-name: message-body",
messageCount = 0,
latestTimestamp = A_TIMESTAMP,
shouldBing = false,
)
)
}
@Test
fun `test createRoomMessage for direct room`() = runTest {
val sut = createRoomGroupMessageCreator()
val result = sut.createRoomMessage(
currentUser = aMatrixUser(),
events = listOf(
aNotifiableMessageEvent(timestamp = A_TIMESTAMP).copy(
roomIsDirect = true,
),
),
roomId = A_ROOM_ID,
)
val resultMetaWithoutFormatting = result.meta.copy(
summaryLine = result.meta.summaryLine.toString()
)
assertThat(resultMetaWithoutFormatting).isEqualTo(
RoomNotification.Message.Meta(
roomId = A_ROOM_ID,
summaryLine = "sender-name: message-body",
messageCount = 1,
latestTimestamp = A_TIMESTAMP,
shouldBing = false,
)
)
}
}
fun createRoomGroupMessageCreator(): RoomGroupMessageCreator {
val context = RuntimeEnvironment.getApplication() as Context
return RoomGroupMessageCreator(
notificationCreator = createNotificationCreator(),
bitmapLoader = NotificationBitmapLoader(RuntimeEnvironment.getApplication()),
stringProvider = AndroidStringProvider(context.resources)
)
}

View file

@ -267,11 +267,21 @@ class NotificationCreatorTest {
result.commonAssertions() result.commonAssertions()
} }
private fun createNotificationCreator( private fun Notification.commonAssertions(
expectedGroup: String? = A_SESSION_ID.value,
expectedCategory: String? = NotificationCompat.CATEGORY_MESSAGE,
) {
assertThat(contentIntent).isNotNull()
assertThat(group).isEqualTo(expectedGroup)
assertThat(category).isEqualTo(expectedCategory)
}
}
fun createNotificationCreator(
context: Context = RuntimeEnvironment.getApplication(), context: Context = RuntimeEnvironment.getApplication(),
buildMeta: BuildMeta = aBuildMeta(), buildMeta: BuildMeta = aBuildMeta(),
notificationChannels: NotificationChannels = createNotificationChannels() notificationChannels: NotificationChannels = createNotificationChannels()
): NotificationCreator { ): NotificationCreator {
return NotificationCreator( return NotificationCreator(
context = context, context = context,
notificationChannels = notificationChannels, notificationChannels = notificationChannels,
@ -296,19 +306,9 @@ class NotificationCreatorTest {
clock = FakeSystemClock(), clock = FakeSystemClock(),
), ),
) )
} }
private fun createNotificationChannels(): NotificationChannels { fun createNotificationChannels(): NotificationChannels {
val context = RuntimeEnvironment.getApplication() val context = RuntimeEnvironment.getApplication()
return NotificationChannels(context, FakeStringProvider("")) return NotificationChannels(context, FakeStringProvider(""))
}
private fun Notification.commonAssertions(
expectedGroup: String? = A_SESSION_ID.value,
expectedCategory: String? = NotificationCompat.CATEGORY_MESSAGE,
) {
assertThat(contentIntent).isNotNull()
assertThat(group).isEqualTo(expectedGroup)
assertThat(category).isEqualTo(expectedCategory)
}
} }

View file

@ -77,13 +77,14 @@ fun aNotifiableMessageEvent(
roomId: RoomId = A_ROOM_ID, roomId: RoomId = A_ROOM_ID,
eventId: EventId = AN_EVENT_ID, eventId: EventId = AN_EVENT_ID,
threadId: ThreadId? = null, threadId: ThreadId? = null,
isRedacted: Boolean = false isRedacted: Boolean = false,
timestamp: Long = 0,
) = NotifiableMessageEvent( ) = NotifiableMessageEvent(
sessionId = sessionId, sessionId = sessionId,
eventId = eventId, eventId = eventId,
editedEventId = null, editedEventId = null,
noisy = false, noisy = false,
timestamp = 0, timestamp = timestamp,
senderName = "sender-name", senderName = "sender-name",
senderId = UserId("@sending-id:domain.com"), senderId = UserId("@sending-id:domain.com"),
body = "message-body", body = "message-body",