diff --git a/domains/android/push/build.gradle b/domains/android/push/build.gradle index 23398ef..e057b0f 100644 --- a/domains/android/push/build.gradle +++ b/domains/android/push/build.gradle @@ -13,5 +13,6 @@ dependencies { kotlinTest(it) androidImportFixturesWorkaround(project, project(":core")) + androidImportFixturesWorkaround(project, project(":matrix:common")) androidImportFixturesWorkaround(project, project(":domains:android:stub")) } diff --git a/domains/android/push/src/test/kotlin/app/dapk/st/push/messaging/MessagingPushTokenRegistrarTest.kt b/domains/android/push/src/test/kotlin/app/dapk/st/push/messaging/MessagingPushTokenRegistrarTest.kt new file mode 100644 index 0000000..6f2b4f9 --- /dev/null +++ b/domains/android/push/src/test/kotlin/app/dapk/st/push/messaging/MessagingPushTokenRegistrarTest.kt @@ -0,0 +1,79 @@ +package app.dapk.st.push.messaging + +import app.dapk.st.firebase.messaging.Messaging +import app.dapk.st.push.PushTokenPayload +import app.dapk.st.push.unifiedpush.FakePushHandler +import fake.FakeErrorTracker +import io.mockk.coEvery +import io.mockk.every +import io.mockk.mockk +import org.amshove.kluent.shouldBeEqualTo +import org.junit.Test +import test.delegateReturn +import test.runExpectTest + +private const val A_TOKEN = "a-token" +private const val SYGNAL_GATEWAY = "https://sygnal.dapk.app/_matrix/push/v1/notify" +private val AN_ERROR = RuntimeException() + +class MessagingPushTokenRegistrarTest { + + private val fakePushHandler = FakePushHandler() + private val fakeErrorTracker = FakeErrorTracker() + private val fakeMessaging = FakeMessaging() + + private val registrar = MessagingPushTokenRegistrar( + fakeErrorTracker, + fakePushHandler, + fakeMessaging.instance, + ) + + @Test + fun `when checking isAvailable, then delegates`() = runExpectTest { + fakeMessaging.givenIsAvailable().returns(true) + + val result = registrar.isAvailable() + + result shouldBeEqualTo true + } + + @Test + fun `when registering current token, then enables and forwards current token to handler`() = runExpectTest { + fakeMessaging.instance.expect { it.enable() } + fakePushHandler.expect { it.onNewToken(PushTokenPayload(A_TOKEN, SYGNAL_GATEWAY)) } + fakeMessaging.givenToken().returns(A_TOKEN) + + registrar.registerCurrentToken() + + verifyExpects() + } + + @Test + fun `given fails to register, when registering current token, then tracks error`() = runExpectTest { + fakeMessaging.instance.expect { it.enable() } + fakeMessaging.givenToken().throws(AN_ERROR) + fakeErrorTracker.expect { it.track(AN_ERROR) } + + registrar.registerCurrentToken() + + verifyExpects() + } + + @Test + fun `when unregistering, then deletes token and disables`() = runExpectTest { + fakeMessaging.instance.expect { it.deleteToken() } + fakeMessaging.instance.expect { it.disable() } + + registrar.unregister() + + verifyExpects() + } + +} + +class FakeMessaging { + val instance = mockk() + + fun givenIsAvailable() = every { instance.isAvailable() }.delegateReturn() + fun givenToken() = coEvery { instance.token() }.delegateReturn() +} \ No newline at end of file diff --git a/domains/android/push/src/test/kotlin/app/dapk/st/push/messaging/MessagingServiceAdapterTest.kt b/domains/android/push/src/test/kotlin/app/dapk/st/push/messaging/MessagingServiceAdapterTest.kt new file mode 100644 index 0000000..26ca2cf --- /dev/null +++ b/domains/android/push/src/test/kotlin/app/dapk/st/push/messaging/MessagingServiceAdapterTest.kt @@ -0,0 +1,41 @@ +package app.dapk.st.push.messaging + +import app.dapk.st.push.PushTokenPayload +import app.dapk.st.push.unifiedpush.FakePushHandler +import fixture.aRoomId +import fixture.anEventId +import org.junit.Test +import test.runExpectTest + +private const val A_TOKEN = "a-push-token" +private const val SYGNAL_GATEWAY = "https://sygnal.dapk.app/_matrix/push/v1/notify" +private val A_ROOM_ID = aRoomId() +private val AN_EVENT_ID = anEventId() + +class MessagingServiceAdapterTest { + + private val fakePushHandler = FakePushHandler() + + private val messagingServiceAdapter = MessagingServiceAdapter(fakePushHandler) + + @Test + fun `onNewToken, then delegates to push handler`() = runExpectTest { + fakePushHandler.expect { + it.onNewToken(PushTokenPayload(token = A_TOKEN, gatewayUrl = SYGNAL_GATEWAY)) + } + messagingServiceAdapter.onNewToken(A_TOKEN) + + verifyExpects() + } + + + @Test + fun `onMessageReceived, then delegates to push handler`() = runExpectTest { + fakePushHandler.expect { + it.onMessageReceived(AN_EVENT_ID, A_ROOM_ID) + } + messagingServiceAdapter.onMessageReceived(AN_EVENT_ID, A_ROOM_ID) + + verifyExpects() + } +} diff --git a/domains/android/push/src/test/kotlin/app/dapk/st/push/unifiedpush/FakePushHandler.kt b/domains/android/push/src/test/kotlin/app/dapk/st/push/unifiedpush/FakePushHandler.kt new file mode 100644 index 0000000..64d0243 --- /dev/null +++ b/domains/android/push/src/test/kotlin/app/dapk/st/push/unifiedpush/FakePushHandler.kt @@ -0,0 +1,6 @@ +package app.dapk.st.push.unifiedpush + +import app.dapk.st.push.PushHandler +import io.mockk.mockk + +class FakePushHandler : PushHandler by mockk() \ No newline at end of file diff --git a/domains/android/push/src/test/kotlin/app/dapk/st/push/unifiedpush/UnifiedPushMessageDelegateTest.kt b/domains/android/push/src/test/kotlin/app/dapk/st/push/unifiedpush/UnifiedPushMessageDelegateTest.kt index 0ed357c..c6fd8b1 100644 --- a/domains/android/push/src/test/kotlin/app/dapk/st/push/unifiedpush/UnifiedPushMessageDelegateTest.kt +++ b/domains/android/push/src/test/kotlin/app/dapk/st/push/unifiedpush/UnifiedPushMessageDelegateTest.kt @@ -2,7 +2,6 @@ package app.dapk.st.push.unifiedpush import app.dapk.st.matrix.common.EventId import app.dapk.st.matrix.common.RoomId -import app.dapk.st.push.PushHandler import app.dapk.st.push.PushModule import app.dapk.st.push.PushTokenPayload import fake.FakeContext @@ -89,8 +88,6 @@ class FakePushModule { fun givenPushHandler() = every { instance.pushHandler() }.delegateReturn() } -class FakePushHandler : PushHandler by mockk() - class FakeEndpointReader : suspend (URL) -> String by mockk() { fun given(url: String) = coEvery { this@FakeEndpointReader.invoke(URL(url)) }.delegateReturn()