Update unit tests

This commit is contained in:
Maxime NATUREL 2022-11-30 15:12:07 +01:00
parent aa3a808d2c
commit a3815d7012
6 changed files with 63 additions and 73 deletions

View File

@ -31,7 +31,6 @@ class DisableNotificationsForCurrentSessionUseCase @Inject constructor(
private val unregisterUnifiedPushUseCase: UnregisterUnifiedPushUseCase, private val unregisterUnifiedPushUseCase: UnregisterUnifiedPushUseCase,
) { ) {
// TODO update unit tests
suspend fun execute() { suspend fun execute() {
val session = activeSessionHolder.getSafeActiveSession() ?: return val session = activeSessionHolder.getSafeActiveSession() ?: return
val deviceId = session.sessionParams.deviceId ?: return val deviceId = session.sessionParams.deviceId ?: return

View File

@ -37,7 +37,6 @@ class EnableNotificationsForCurrentSessionUseCase @Inject constructor(
object NeedToAskUserForDistributor : EnableNotificationsResult object NeedToAskUserForDistributor : EnableNotificationsResult
} }
// TODO update unit tests
suspend fun execute(distributor: String = ""): EnableNotificationsResult { suspend fun execute(distributor: String = ""): EnableNotificationsResult {
val pusherForCurrentSession = pushersManager.getPusherForCurrentSession() val pusherForCurrentSession = pushersManager.getPusherForCurrentSession()
if (pusherForCurrentSession == null) { if (pusherForCurrentSession == null) {

View File

@ -16,11 +16,11 @@
package im.vector.app.features.settings.notifications package im.vector.app.features.settings.notifications
import im.vector.app.core.pushers.UnregisterUnifiedPushUseCase
import im.vector.app.features.settings.devices.v2.notification.CheckIfCanTogglePushNotificationsViaPusherUseCase import im.vector.app.features.settings.devices.v2.notification.CheckIfCanTogglePushNotificationsViaPusherUseCase
import im.vector.app.features.settings.devices.v2.notification.TogglePushNotificationUseCase import im.vector.app.features.settings.devices.v2.notification.TogglePushNotificationUseCase
import im.vector.app.test.fakes.FakeActiveSessionHolder import im.vector.app.test.fakes.FakeActiveSessionHolder
import im.vector.app.test.fakes.FakePushersManager import im.vector.app.test.fakes.FakePushersManager
import im.vector.app.test.fakes.FakeUnifiedPushHelper
import io.mockk.coJustRun import io.mockk.coJustRun
import io.mockk.coVerify import io.mockk.coVerify
import io.mockk.every import io.mockk.every
@ -33,17 +33,17 @@ private const val A_SESSION_ID = "session-id"
class DisableNotificationsForCurrentSessionUseCaseTest { class DisableNotificationsForCurrentSessionUseCaseTest {
private val fakeActiveSessionHolder = FakeActiveSessionHolder() private val fakeActiveSessionHolder = FakeActiveSessionHolder()
private val fakeUnifiedPushHelper = FakeUnifiedPushHelper()
private val fakePushersManager = FakePushersManager() private val fakePushersManager = FakePushersManager()
private val fakeCheckIfCanTogglePushNotificationsViaPusherUseCase = mockk<CheckIfCanTogglePushNotificationsViaPusherUseCase>() private val fakeCheckIfCanTogglePushNotificationsViaPusherUseCase = mockk<CheckIfCanTogglePushNotificationsViaPusherUseCase>()
private val fakeTogglePushNotificationUseCase = mockk<TogglePushNotificationUseCase>() private val fakeTogglePushNotificationUseCase = mockk<TogglePushNotificationUseCase>()
private val fakeUnregisterUnifiedPushUseCase = mockk<UnregisterUnifiedPushUseCase>()
private val disableNotificationsForCurrentSessionUseCase = DisableNotificationsForCurrentSessionUseCase( private val disableNotificationsForCurrentSessionUseCase = DisableNotificationsForCurrentSessionUseCase(
activeSessionHolder = fakeActiveSessionHolder.instance, activeSessionHolder = fakeActiveSessionHolder.instance,
unifiedPushHelper = fakeUnifiedPushHelper.instance,
pushersManager = fakePushersManager.instance, pushersManager = fakePushersManager.instance,
checkIfCanTogglePushNotificationsViaPusherUseCase = fakeCheckIfCanTogglePushNotificationsViaPusherUseCase, checkIfCanTogglePushNotificationsViaPusherUseCase = fakeCheckIfCanTogglePushNotificationsViaPusherUseCase,
togglePushNotificationUseCase = fakeTogglePushNotificationUseCase, togglePushNotificationUseCase = fakeTogglePushNotificationUseCase,
unregisterUnifiedPushUseCase = fakeUnregisterUnifiedPushUseCase,
) )
@Test @Test
@ -67,12 +67,14 @@ class DisableNotificationsForCurrentSessionUseCaseTest {
val fakeSession = fakeActiveSessionHolder.fakeSession val fakeSession = fakeActiveSessionHolder.fakeSession
fakeSession.givenSessionId(A_SESSION_ID) fakeSession.givenSessionId(A_SESSION_ID)
every { fakeCheckIfCanTogglePushNotificationsViaPusherUseCase.execute(fakeSession) } returns false every { fakeCheckIfCanTogglePushNotificationsViaPusherUseCase.execute(fakeSession) } returns false
fakeUnifiedPushHelper.givenUnregister(fakePushersManager.instance) coJustRun { fakeUnregisterUnifiedPushUseCase.execute(any()) }
// When // When
disableNotificationsForCurrentSessionUseCase.execute() disableNotificationsForCurrentSessionUseCase.execute()
// Then // Then
fakeUnifiedPushHelper.verifyUnregister(fakePushersManager.instance) coVerify {
fakeUnregisterUnifiedPushUseCase.execute(fakePushersManager.instance)
}
} }
} }

View File

@ -16,18 +16,18 @@
package im.vector.app.features.settings.notifications package im.vector.app.features.settings.notifications
import androidx.fragment.app.FragmentActivity import im.vector.app.core.pushers.EnsureFcmTokenIsRetrievedUseCase
import im.vector.app.features.settings.devices.v2.notification.CheckIfCanTogglePushNotificationsViaPusherUseCase import im.vector.app.core.pushers.RegisterUnifiedPushUseCase
import im.vector.app.features.settings.devices.v2.notification.TogglePushNotificationUseCase import im.vector.app.features.settings.devices.v2.notification.TogglePushNotificationUseCase
import im.vector.app.test.fakes.FakeActiveSessionHolder import im.vector.app.test.fakes.FakeActiveSessionHolder
import im.vector.app.test.fakes.FakeFcmHelper
import im.vector.app.test.fakes.FakePushersManager import im.vector.app.test.fakes.FakePushersManager
import im.vector.app.test.fakes.FakeUnifiedPushHelper
import io.mockk.coJustRun import io.mockk.coJustRun
import io.mockk.coVerify
import io.mockk.every import io.mockk.every
import io.mockk.justRun
import io.mockk.mockk import io.mockk.mockk
import io.mockk.verify
import kotlinx.coroutines.test.runTest import kotlinx.coroutines.test.runTest
import org.amshove.kluent.shouldBe
import org.junit.Test import org.junit.Test
private const val A_SESSION_ID = "session-id" private const val A_SESSION_ID = "session-id"
@ -35,53 +35,71 @@ private const val A_SESSION_ID = "session-id"
class EnableNotificationsForCurrentSessionUseCaseTest { class EnableNotificationsForCurrentSessionUseCaseTest {
private val fakeActiveSessionHolder = FakeActiveSessionHolder() private val fakeActiveSessionHolder = FakeActiveSessionHolder()
private val fakeUnifiedPushHelper = FakeUnifiedPushHelper()
private val fakePushersManager = FakePushersManager() private val fakePushersManager = FakePushersManager()
private val fakeFcmHelper = FakeFcmHelper()
private val fakeCheckIfCanTogglePushNotificationsViaPusherUseCase = mockk<CheckIfCanTogglePushNotificationsViaPusherUseCase>()
private val fakeTogglePushNotificationUseCase = mockk<TogglePushNotificationUseCase>() private val fakeTogglePushNotificationUseCase = mockk<TogglePushNotificationUseCase>()
private val fakeRegisterUnifiedPushUseCase = mockk<RegisterUnifiedPushUseCase>()
private val fakeEnsureFcmTokenIsRetrievedUseCase = mockk<EnsureFcmTokenIsRetrievedUseCase>()
private val enableNotificationsForCurrentSessionUseCase = EnableNotificationsForCurrentSessionUseCase( private val enableNotificationsForCurrentSessionUseCase = EnableNotificationsForCurrentSessionUseCase(
activeSessionHolder = fakeActiveSessionHolder.instance, activeSessionHolder = fakeActiveSessionHolder.instance,
unifiedPushHelper = fakeUnifiedPushHelper.instance,
pushersManager = fakePushersManager.instance, pushersManager = fakePushersManager.instance,
fcmHelper = fakeFcmHelper.instance,
checkIfCanTogglePushNotificationsViaPusherUseCase = fakeCheckIfCanTogglePushNotificationsViaPusherUseCase,
togglePushNotificationUseCase = fakeTogglePushNotificationUseCase, togglePushNotificationUseCase = fakeTogglePushNotificationUseCase,
registerUnifiedPushUseCase = fakeRegisterUnifiedPushUseCase,
ensureFcmTokenIsRetrievedUseCase = fakeEnsureFcmTokenIsRetrievedUseCase,
) )
@Test @Test
fun `given no existing pusher for current session when execute then a new pusher is registered`() = runTest { fun `given no existing pusher and a registered distributor when execute then a new pusher is registered and result is success`() = runTest {
// Given // Given
val fragmentActivity = mockk<FragmentActivity>() val aDistributor = "distributor"
fakePushersManager.givenGetPusherForCurrentSessionReturns(null)
fakeUnifiedPushHelper.givenRegister(fragmentActivity)
fakeUnifiedPushHelper.givenIsEmbeddedDistributorReturns(true)
fakeFcmHelper.givenEnsureFcmTokenIsRetrieved(fragmentActivity, fakePushersManager.instance)
every { fakeCheckIfCanTogglePushNotificationsViaPusherUseCase.execute(fakeActiveSessionHolder.fakeSession) } returns false
// When
enableNotificationsForCurrentSessionUseCase.execute(fragmentActivity)
// Then
fakeUnifiedPushHelper.verifyRegister(fragmentActivity)
fakeFcmHelper.verifyEnsureFcmTokenIsRetrieved(fragmentActivity, fakePushersManager.instance, registerPusher = true)
}
@Test
fun `given toggle via Pusher is possible when execute then current pusher is toggled to true`() = runTest {
// Given
val fragmentActivity = mockk<FragmentActivity>()
fakePushersManager.givenGetPusherForCurrentSessionReturns(mockk())
val fakeSession = fakeActiveSessionHolder.fakeSession val fakeSession = fakeActiveSessionHolder.fakeSession
fakeSession.givenSessionId(A_SESSION_ID) fakeSession.givenSessionId(A_SESSION_ID)
every { fakeCheckIfCanTogglePushNotificationsViaPusherUseCase.execute(fakeActiveSessionHolder.fakeSession) } returns true fakePushersManager.givenGetPusherForCurrentSessionReturns(null)
every { fakeRegisterUnifiedPushUseCase.execute(any()) } returns RegisterUnifiedPushUseCase.RegisterUnifiedPushResult.Success
justRun { fakeEnsureFcmTokenIsRetrievedUseCase.execute(any(), any()) }
coJustRun { fakeTogglePushNotificationUseCase.execute(A_SESSION_ID, any()) } coJustRun { fakeTogglePushNotificationUseCase.execute(A_SESSION_ID, any()) }
// When // When
enableNotificationsForCurrentSessionUseCase.execute(fragmentActivity) val result = enableNotificationsForCurrentSessionUseCase.execute(aDistributor)
// Then // Then
coVerify { fakeTogglePushNotificationUseCase.execute(A_SESSION_ID, true) } result shouldBe EnableNotificationsForCurrentSessionUseCase.EnableNotificationsResult.Success
verify {
fakeRegisterUnifiedPushUseCase.execute(aDistributor)
fakeEnsureFcmTokenIsRetrievedUseCase.execute(fakePushersManager.instance, registerPusher = true)
}
}
@Test
fun `given no existing pusher and a no registered distributor when execute then result is need to ask user for distributor`() = runTest {
// Given
val aDistributor = "distributor"
fakePushersManager.givenGetPusherForCurrentSessionReturns(null)
every { fakeRegisterUnifiedPushUseCase.execute(any()) } returns RegisterUnifiedPushUseCase.RegisterUnifiedPushResult.NeedToAskUserForDistributor
// When
val result = enableNotificationsForCurrentSessionUseCase.execute(aDistributor)
// Then
result shouldBe EnableNotificationsForCurrentSessionUseCase.EnableNotificationsResult.NeedToAskUserForDistributor
verify {
fakeRegisterUnifiedPushUseCase.execute(aDistributor)
}
}
@Test
fun `given no deviceId for current session when execute then result is failure`() = runTest {
// Given
val aDistributor = "distributor"
val fakeSession = fakeActiveSessionHolder.fakeSession
fakeSession.givenSessionId(null)
fakePushersManager.givenGetPusherForCurrentSessionReturns(mockk())
every { fakeRegisterUnifiedPushUseCase.execute(any()) } returns RegisterUnifiedPushUseCase.RegisterUnifiedPushResult.NeedToAskUserForDistributor
// When
val result = enableNotificationsForCurrentSessionUseCase.execute(aDistributor)
// Then
result shouldBe EnableNotificationsForCurrentSessionUseCase.EnableNotificationsResult.Failure
} }
} }

View File

@ -16,7 +16,6 @@
package im.vector.app.test.fakes package im.vector.app.test.fakes
import androidx.fragment.app.FragmentActivity
import im.vector.app.core.pushers.FcmHelper import im.vector.app.core.pushers.FcmHelper
import im.vector.app.core.pushers.PushersManager import im.vector.app.core.pushers.PushersManager
import io.mockk.justRun import io.mockk.justRun
@ -27,18 +26,14 @@ class FakeFcmHelper {
val instance = mockk<FcmHelper>() val instance = mockk<FcmHelper>()
fun givenEnsureFcmTokenIsRetrieved( fun givenEnsureFcmTokenIsRetrieved(pushersManager: PushersManager) {
fragmentActivity: FragmentActivity, justRun { instance.ensureFcmTokenIsRetrieved(pushersManager, any()) }
pushersManager: PushersManager,
) {
justRun { instance.ensureFcmTokenIsRetrieved(fragmentActivity, pushersManager, any()) }
} }
fun verifyEnsureFcmTokenIsRetrieved( fun verifyEnsureFcmTokenIsRetrieved(
fragmentActivity: FragmentActivity,
pushersManager: PushersManager, pushersManager: PushersManager,
registerPusher: Boolean, registerPusher: Boolean,
) { ) {
verify { instance.ensureFcmTokenIsRetrieved(fragmentActivity, pushersManager, registerPusher) } verify { instance.ensureFcmTokenIsRetrieved(pushersManager, registerPusher) }
} }
} }

View File

@ -16,37 +16,14 @@
package im.vector.app.test.fakes package im.vector.app.test.fakes
import androidx.fragment.app.FragmentActivity
import im.vector.app.core.pushers.PushersManager
import im.vector.app.core.pushers.UnifiedPushHelper import im.vector.app.core.pushers.UnifiedPushHelper
import io.mockk.coJustRun
import io.mockk.coVerify
import io.mockk.every import io.mockk.every
import io.mockk.mockk import io.mockk.mockk
import io.mockk.verify
class FakeUnifiedPushHelper { class FakeUnifiedPushHelper {
val instance = mockk<UnifiedPushHelper>() val instance = mockk<UnifiedPushHelper>()
fun givenRegister(fragmentActivity: FragmentActivity) {
every { instance.register(fragmentActivity, any()) } answers {
secondArg<Runnable>().run()
}
}
fun verifyRegister(fragmentActivity: FragmentActivity) {
verify { instance.register(fragmentActivity, any()) }
}
fun givenUnregister(pushersManager: PushersManager) {
coJustRun { instance.unregister(pushersManager) }
}
fun verifyUnregister(pushersManager: PushersManager) {
coVerify { instance.unregister(pushersManager) }
}
fun givenIsEmbeddedDistributorReturns(isEmbedded: Boolean) { fun givenIsEmbeddedDistributorReturns(isEmbedded: Boolean) {
every { instance.isEmbeddedDistributor() } returns isEmbedded every { instance.isEmbeddedDistributor() } returns isEmbedded
} }