Skip to content

Commit

Permalink
porting notification tests to the engine
Browse files Browse the repository at this point in the history
  • Loading branch information
ouchadam committed Oct 12, 2022
1 parent 4d03323 commit 709d6c0
Show file tree
Hide file tree
Showing 20 changed files with 106 additions and 68 deletions.
5 changes: 5 additions & 0 deletions chat-engine/src/testFixtures/kotlin/fake/FakeChatEngine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import app.dapk.st.matrix.common.RoomId
import io.mockk.coEvery
import io.mockk.every
import io.mockk.mockk
import test.delegateEmit
import test.delegateReturn
import java.io.InputStream

Expand All @@ -16,4 +17,8 @@ class FakeChatEngine : ChatEngine by mockk() {

fun givenImportKeys(inputStream: InputStream, passphrase: String) = coEvery { inputStream.importRoomKeys(passphrase) }.delegateReturn()

fun givenNotificationsInvites() = every { notificationsInvites() }.delegateEmit()

fun givenNotificationsMessages() = every { notificationsMessages() }.delegateEmit()

}
35 changes: 35 additions & 0 deletions chat-engine/src/testFixtures/kotlin/fixture/Fixtures.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,38 @@ fun anEncryptedRoomMessageEvent(
edited: Boolean = false,
redacted: Boolean = false,
) = RoomEvent.Message(eventId, utcTimestamp, content, author, meta, edited, redacted)

fun aRoomImageMessageEvent(
eventId: EventId = anEventId(),
utcTimestamp: Long = 0L,
content: RoomEvent.Image.ImageMeta = anImageMeta(),
author: RoomMember = aRoomMember(),
meta: MessageMeta = MessageMeta.FromServer,
edited: Boolean = false,
) = RoomEvent.Image(eventId, utcTimestamp, content, author, meta, edited)

fun aRoomReplyMessageEvent(
message: RoomEvent = aRoomMessageEvent(),
replyingTo: RoomEvent = aRoomMessageEvent(eventId = anEventId("in-reply-to-id")),
) = RoomEvent.Reply(message, replyingTo)

fun aRoomMessageEvent(
eventId: EventId = anEventId(),
utcTimestamp: Long = 0L,
content: String = "message-content",
author: RoomMember = aRoomMember(),
meta: MessageMeta = MessageMeta.FromServer,
edited: Boolean = false,
) = RoomEvent.Message(eventId, utcTimestamp, content, author, meta, edited)

fun anImageMeta(
width: Int? = 100,
height: Int? = 100,
url: String = "https://a-url.com",
keys: RoomEvent.Image.ImageMeta.Keys? = null
) = RoomEvent.Image.ImageMeta(width, height, url, keys)

fun aRoomState(
roomOverview: RoomOverview = aRoomOverview(),
events: List<RoomEvent> = listOf(aRoomMessageEvent()),
) = RoomState(roomOverview, events)
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import android.app.Notification
import android.app.PendingIntent
import android.os.Build
import app.dapk.st.core.DeviceMeta
import app.dapk.st.engine.RoomOverview
import app.dapk.st.matrix.common.AvatarUrl
import app.dapk.st.matrix.sync.RoomOverview
import fake.FakeContext
import fixture.NotificationDelegateFixtures.anAndroidNotification
import fixture.NotificationDelegateFixtures.anInboxStyle
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package app.dapk.st.notifications

import app.dapk.st.engine.RoomEvent
import app.dapk.st.engine.RoomOverview
import app.dapk.st.matrix.common.RoomId
import app.dapk.st.matrix.sync.RoomEvent
import app.dapk.st.matrix.sync.RoomOverview
import fake.FakeNotificationFactory
import fake.FakeNotificationManager
import fake.aFakeNotification
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package app.dapk.st.notifications

import android.content.Context
import app.dapk.st.engine.RoomEvent
import app.dapk.st.engine.RoomOverview
import app.dapk.st.matrix.common.RoomId
import app.dapk.st.matrix.sync.RoomEvent
import app.dapk.st.matrix.sync.RoomOverview
import app.dapk.st.navigator.IntentFactory
import fixture.NotificationDelegateFixtures.anAndroidNotification
import fixture.NotificationFixtures.aDismissRoomNotification
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ class RenderNotificationsUseCaseTest {

private val fakeNotificationMessageRenderer = FakeNotificationMessageRenderer()
private val fakeNotificationInviteRenderer = FakeNotificationInviteRenderer()
private val fakeObserveUnreadNotificationsUseCase = FakeObserveUnreadNotificationsUseCase()
private val fakeObserveInviteNotificationsUseCase = FakeObserveInviteNotificationsUseCase()
private val fakeNotificationChannels = FakeNotificationChannels().also {
it.instance.expect { it.initChannels() }
}
Expand All @@ -32,8 +30,8 @@ class RenderNotificationsUseCaseTest {
@Test
fun `given events, when listening for changes then initiates channels once`() = runTest {
fakeNotificationMessageRenderer.instance.expect { it.render(any()) }
fakeObserveUnreadNotificationsUseCase.given().emits(AN_UNREAD_NOTIFICATIONS)
fakeObserveInviteNotificationsUseCase.given().emits()
fakeChatEngine.givenNotificationsMessages().emits(AN_UNREAD_NOTIFICATIONS)
fakeChatEngine.givenNotificationsInvites().emits()

renderNotificationsUseCase.listenForNotificationChanges(TestScope(UnconfinedTestDispatcher()))

Expand All @@ -43,8 +41,8 @@ class RenderNotificationsUseCaseTest {
@Test
fun `given renderable unread events, when listening for changes, then renders change`() = runTest {
fakeNotificationMessageRenderer.instance.expect { it.render(any()) }
fakeObserveUnreadNotificationsUseCase.given().emits(AN_UNREAD_NOTIFICATIONS)
fakeObserveInviteNotificationsUseCase.given().emits()
fakeChatEngine.givenNotificationsMessages().emits(AN_UNREAD_NOTIFICATIONS)
fakeChatEngine.givenNotificationsInvites().emits()

renderNotificationsUseCase.listenForNotificationChanges(TestScope(UnconfinedTestDispatcher()))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ class LocalEchoMapperTest {
result shouldBeEqualTo aRoomMessageEvent(
eventId = echo.eventId!!,
content = AN_ECHO_CONTENT.content.body,
meta = A_META
).engine()
meta = A_META.engine()
)
}

@Test
Expand All @@ -40,20 +40,20 @@ class LocalEchoMapperTest {
result shouldBeEqualTo aRoomMessageEvent(
eventId = anEventId(echo.localId),
content = AN_ECHO_CONTENT.content.body,
meta = A_META
).engine()
meta = A_META.engine()
)
}

@Test
fun `when merging with echo then updates meta with the echos meta`() = runWith(localEchoMapper) {
val previousMeta = MessageMeta.LocalEcho("previous", MessageMeta.LocalEcho.State.Sending)
val event = aRoomMessageEvent(meta = previousMeta).engine()
val previousMeta = MessageMeta.LocalEcho("previous", MessageMeta.LocalEcho.State.Sending).engine()
val event = aRoomMessageEvent(meta = previousMeta)
val echo = aLocalEcho()
fakeMetaMapper.given(echo).returns(A_META.engine() as app.dapk.st.engine.MessageMeta.LocalEcho)

val result = event.mergeWith(echo)

result shouldBeEqualTo aRoomMessageEvent(meta = A_META).engine()
result shouldBeEqualTo aRoomMessageEvent(meta = A_META.engine())
}

private fun givenEcho(eventId: EventId? = null, localId: String = "", meta: MessageMeta.LocalEcho = A_META): MessageService.LocalEcho {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class MergeWithLocalEchosUseCaseTest {

@Test
fun `given no local echos, when merging text message, then returns original state`() {
val roomState = aRoomState(events = listOf(A_ROOM_MESSAGE_EVENT)).engine()
val roomState = aRoomState(events = listOf(A_ROOM_MESSAGE_EVENT))

val result = mergeWithLocalEchosUseCase.invoke(roomState, A_ROOM_MEMBER, emptyList())

Expand All @@ -29,7 +29,7 @@ class MergeWithLocalEchosUseCaseTest {

@Test
fun `given no local echos, when merging events, then returns original ordered by timestamp descending`() {
val roomState = aRoomState(events = listOf(A_ROOM_IMAGE_MESSAGE_EVENT.copy(utcTimestamp = 1500), A_ROOM_MESSAGE_EVENT.copy(utcTimestamp = 1000))).engine()
val roomState = aRoomState(events = listOf(A_ROOM_IMAGE_MESSAGE_EVENT.copy(utcTimestamp = 1500), A_ROOM_MESSAGE_EVENT.copy(utcTimestamp = 1000)))

val result = mergeWithLocalEchosUseCase.invoke(roomState, A_ROOM_MEMBER, emptyList())

Expand All @@ -39,15 +39,15 @@ class MergeWithLocalEchosUseCaseTest {
@Test
fun `given local echo with sending state, when merging then maps to room event with local echo state`() {
val second = createLocalEcho(A_LOCAL_ECHO_EVENT_ID, A_LOCAL_ECHO_BODY, state = MessageService.LocalEcho.State.Sending)
fakeLocalEchoMapper.givenMapping(second, A_ROOM_MEMBER).returns(ANOTHER_ROOM_MESSAGE_EVENT.engine())
val roomState = aRoomState(events = listOf(A_ROOM_MESSAGE_EVENT)).engine()
fakeLocalEchoMapper.givenMapping(second, A_ROOM_MEMBER).returns(ANOTHER_ROOM_MESSAGE_EVENT)
val roomState = aRoomState(events = listOf(A_ROOM_MESSAGE_EVENT))

val result = mergeWithLocalEchosUseCase.invoke(roomState, A_ROOM_MEMBER, listOf(second))

result shouldBeEqualTo roomState.copy(
events = listOf(
A_ROOM_MESSAGE_EVENT.engine(),
ANOTHER_ROOM_MESSAGE_EVENT.engine(),
A_ROOM_MESSAGE_EVENT,
ANOTHER_ROOM_MESSAGE_EVENT,
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package app.dapk.st.engine

import fake.FakeRoomStore
import fixture.NotificationDiffFixtures.aNotificationDiff
import fixture.aMatrixRoomMessageEvent
import fixture.aMatrixRoomOverview
import fixture.aRoomId
import fixture.aRoomMessageEvent
import fixture.anEventId
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.toList
Expand All @@ -15,8 +15,8 @@ import app.dapk.st.matrix.sync.RoomEvent as MatrixRoomEvent
import app.dapk.st.matrix.sync.RoomOverview as MatrixRoomOverview

private val NO_UNREADS = emptyMap<MatrixRoomOverview, List<MatrixRoomEvent>>()
private val A_MESSAGE = aRoomMessageEvent(eventId = anEventId("1"), content = "hello", utcTimestamp = 1000)
private val A_MESSAGE_2 = aRoomMessageEvent(eventId = anEventId("2"), content = "world", utcTimestamp = 2000)
private val A_MESSAGE = aMatrixRoomMessageEvent(eventId = anEventId("1"), content = "hello", utcTimestamp = 1000)
private val A_MESSAGE_2 = aMatrixRoomMessageEvent(eventId = anEventId("2"), content = "world", utcTimestamp = 2000)
private val A_ROOM_OVERVIEW = aMatrixRoomOverview(roomId = aRoomId("1"))
private val A_ROOM_OVERVIEW_2 = aMatrixRoomOverview(roomId = aRoomId("2"))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import test.delegateReturn

private val A_ROOM_ID = aRoomId()
private val AN_USER_ID = aUserId()
private val A_ROOM_STATE = aRoomState()
private val A_MERGED_ROOM_STATE = A_ROOM_STATE.copy(events = listOf(aRoomMessageEvent(content = "a merged event")))
private val A_ROOM_STATE = aMatrixRoomState()
private val A_MERGED_ROOM_STATE = A_ROOM_STATE.copy(events = listOf(aMatrixRoomMessageEvent(content = "a merged event")))
private val A_LOCAL_ECHOS_LIST = listOf(aLocalEcho())
private val A_ROOM_MEMBER = aRoomMember()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import io.mockk.coEvery
import io.mockk.mockk
import test.delegateEmit

class FakeObserveInviteNotificationsUseCase : app.dapk.st.engine.ObserveInviteNotificationsUseCase by mockk() {
class FakeObserveInviteNotificationsUseCase : ObserveInviteNotificationsUseCase by mockk() {
fun given() = coEvery { this@FakeObserveInviteNotificationsUseCase.invoke() }.delegateEmit()
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import io.mockk.coEvery
import io.mockk.mockk
import test.delegateEmit

class FakeObserveUnreadNotificationsUseCase : app.dapk.st.engine.ObserveUnreadNotificationsUseCase by mockk() {
class FakeObserveUnreadNotificationsUseCase : ObserveUnreadNotificationsUseCase by mockk() {
fun given() = coEvery { this@FakeObserveUnreadNotificationsUseCase.invoke() }.delegateEmit()
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import org.junit.Test

private const val A_DECRYPTED_MESSAGE_CONTENT = "decrypted - content"
private val AN_ENCRYPTED_ROOM_CONTENT = aMegolmV1()
private val AN_ENCRYPTED_ROOM_MESSAGE = aRoomMessageEvent(encryptedContent = AN_ENCRYPTED_ROOM_CONTENT)
private val AN_ENCRYPTED_ROOM_MESSAGE = aMatrixRoomMessageEvent(encryptedContent = AN_ENCRYPTED_ROOM_CONTENT)
private val AN_ENCRYPTED_ROOM_REPLY = aRoomReplyMessageEvent(
message = AN_ENCRYPTED_ROOM_MESSAGE,
replyingTo = AN_ENCRYPTED_ROOM_MESSAGE.copy(eventId = anEventId("other-event"))
Expand All @@ -37,7 +37,7 @@ class RoomEventsDecrypterTest {

@Test
fun `given clear message event, when decrypting, then does nothing`() = runTest {
val aClearMessageEvent = aRoomMessageEvent(encryptedContent = null)
val aClearMessageEvent = aMatrixRoomMessageEvent(encryptedContent = null)
val result = roomEventsDecrypter.decryptRoomEvents(A_USER_CREDENTIALS, listOf(aClearMessageEvent))

result shouldBeEqualTo listOf(aClearMessageEvent)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package app.dapk.st.matrix.sync.internal.sync

import fake.FakeRoomStore
import fixture.aRoomMessageEvent
import fixture.aMatrixRoomMessageEvent
import fixture.anEventId
import internalfixture.aTimelineTextEventContent
import internalfixture.anApiTimelineTextEvent
Expand All @@ -11,8 +11,8 @@ import org.junit.Test

private val AN_EVENT_ID = anEventId()
private val A_TIMELINE_EVENT = anApiTimelineTextEvent(AN_EVENT_ID, content = aTimelineTextEventContent(body = "timeline event"))
private val A_ROOM_EVENT = aRoomMessageEvent(AN_EVENT_ID, content = "previous room event")
private val A_PERSISTED_EVENT = aRoomMessageEvent(AN_EVENT_ID, content = "persisted event")
private val A_ROOM_EVENT = aMatrixRoomMessageEvent(AN_EVENT_ID, content = "previous room event")
private val A_PERSISTED_EVENT = aMatrixRoomMessageEvent(AN_EVENT_ID, content = "persisted event")

class EventLookupUseCaseTest {

Expand Down
Loading

0 comments on commit 709d6c0

Please sign in to comment.