Updating existing unit tests

This commit is contained in:
Maxime NATUREL 2022-12-01 17:33:27 +01:00
parent 06681fd115
commit 5248a69fe2
9 changed files with 21 additions and 71 deletions

View File

@ -30,7 +30,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
toggleNotificationsForCurrentSessionUseCase.execute(enabled = false) toggleNotificationsForCurrentSessionUseCase.execute(enabled = false)

View File

@ -30,11 +30,9 @@ class EnableNotificationsForCurrentSessionUseCase @Inject constructor(
sealed interface EnableNotificationsResult { sealed interface EnableNotificationsResult {
object Success : EnableNotificationsResult object Success : EnableNotificationsResult
object Failure : EnableNotificationsResult
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

@ -119,7 +119,6 @@ class VectorSettingsNotificationPreferenceFragment :
VectorSettingsNotificationPreferenceViewEvent.NotificationsForDeviceEnabled -> onNotificationsForDeviceEnabled() VectorSettingsNotificationPreferenceViewEvent.NotificationsForDeviceEnabled -> onNotificationsForDeviceEnabled()
VectorSettingsNotificationPreferenceViewEvent.NotificationsForDeviceDisabled -> onNotificationsForDeviceDisabled() VectorSettingsNotificationPreferenceViewEvent.NotificationsForDeviceDisabled -> onNotificationsForDeviceDisabled()
is VectorSettingsNotificationPreferenceViewEvent.AskUserForPushDistributor -> askUserToSelectPushDistributor() is VectorSettingsNotificationPreferenceViewEvent.AskUserForPushDistributor -> askUserToSelectPushDistributor()
VectorSettingsNotificationPreferenceViewEvent.EnableNotificationForDeviceFailure -> displayErrorDialog(throwable = null)
VectorSettingsNotificationPreferenceViewEvent.NotificationMethodChanged -> onNotificationMethodChanged() VectorSettingsNotificationPreferenceViewEvent.NotificationMethodChanged -> onNotificationMethodChanged()
} }
} }

View File

@ -20,7 +20,6 @@ import im.vector.app.core.platform.VectorViewEvents
sealed interface VectorSettingsNotificationPreferenceViewEvent : VectorViewEvents { sealed interface VectorSettingsNotificationPreferenceViewEvent : VectorViewEvents {
object NotificationsForDeviceEnabled : VectorSettingsNotificationPreferenceViewEvent object NotificationsForDeviceEnabled : VectorSettingsNotificationPreferenceViewEvent
object EnableNotificationForDeviceFailure : VectorSettingsNotificationPreferenceViewEvent
object NotificationsForDeviceDisabled : VectorSettingsNotificationPreferenceViewEvent object NotificationsForDeviceDisabled : VectorSettingsNotificationPreferenceViewEvent
object AskUserForPushDistributor : VectorSettingsNotificationPreferenceViewEvent object AskUserForPushDistributor : VectorSettingsNotificationPreferenceViewEvent
object NotificationMethodChanged : VectorSettingsNotificationPreferenceViewEvent object NotificationMethodChanged : VectorSettingsNotificationPreferenceViewEvent

View File

@ -68,9 +68,6 @@ class VectorSettingsNotificationPreferenceViewModel @AssistedInject constructor(
private fun handleEnableNotificationsForDevice(distributor: String) { private fun handleEnableNotificationsForDevice(distributor: String) {
viewModelScope.launch { viewModelScope.launch {
when (enableNotificationsForCurrentSessionUseCase.execute(distributor)) { when (enableNotificationsForCurrentSessionUseCase.execute(distributor)) {
EnableNotificationsForCurrentSessionUseCase.EnableNotificationsResult.Failure -> {
_viewEvents.post(VectorSettingsNotificationPreferenceViewEvent.EnableNotificationForDeviceFailure)
}
is EnableNotificationsForCurrentSessionUseCase.EnableNotificationsResult.NeedToAskUserForDistributor -> { is EnableNotificationsForCurrentSessionUseCase.EnableNotificationsResult.NeedToAskUserForDistributor -> {
_viewEvents.post(VectorSettingsNotificationPreferenceViewEvent.AskUserForPushDistributor) _viewEvents.post(VectorSettingsNotificationPreferenceViewEvent.AskUserForPushDistributor)
} }
@ -81,7 +78,6 @@ class VectorSettingsNotificationPreferenceViewModel @AssistedInject constructor(
} }
} }
// TODO update unit tests
private fun handleRegisterPushDistributor(distributor: String) { private fun handleRegisterPushDistributor(distributor: String) {
viewModelScope.launch { viewModelScope.launch {
unregisterUnifiedPushUseCase.execute(pushersManager) unregisterUnifiedPushUseCase.execute(pushersManager)

View File

@ -88,7 +88,7 @@ class SessionOverviewViewModelTest {
pendingAuthHandler = fakePendingAuthHandler.instance, pendingAuthHandler = fakePendingAuthHandler.instance,
activeSessionHolder = fakeActiveSessionHolder.instance, activeSessionHolder = fakeActiveSessionHolder.instance,
refreshDevicesUseCase = refreshDevicesUseCase, refreshDevicesUseCase = refreshDevicesUseCase,
toggleNotificationUseCase = toggleNotificationUseCase.instance, toggleNotificationsUseCase = toggleNotificationUseCase.instance,
getNotificationsStatusUseCase = fakeGetNotificationsStatusUseCase.instance, getNotificationsStatusUseCase = fakeGetNotificationsStatusUseCase.instance,
vectorPreferences = fakeVectorPreferences.instance, vectorPreferences = fakeVectorPreferences.instance,
toggleIpAddressVisibilityUseCase = toggleIpAddressVisibilityUseCase, toggleIpAddressVisibilityUseCase = toggleIpAddressVisibilityUseCase,

View File

@ -18,7 +18,6 @@ package im.vector.app.features.settings.notifications
import im.vector.app.core.pushers.UnregisterUnifiedPushUseCase import im.vector.app.core.pushers.UnregisterUnifiedPushUseCase
import im.vector.app.features.settings.devices.v2.notification.CheckIfCanToggleNotificationsViaPusherUseCase import im.vector.app.features.settings.devices.v2.notification.CheckIfCanToggleNotificationsViaPusherUseCase
import im.vector.app.features.settings.devices.v2.notification.ToggleNotificationsUseCase
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 io.mockk.coJustRun import io.mockk.coJustRun
@ -28,21 +27,19 @@ import io.mockk.mockk
import kotlinx.coroutines.test.runTest import kotlinx.coroutines.test.runTest
import org.junit.Test import org.junit.Test
private const val A_SESSION_ID = "session-id"
class DisableNotificationsForCurrentSessionUseCaseTest { class DisableNotificationsForCurrentSessionUseCaseTest {
private val fakeActiveSessionHolder = FakeActiveSessionHolder() private val fakeActiveSessionHolder = FakeActiveSessionHolder()
private val fakePushersManager = FakePushersManager() private val fakePushersManager = FakePushersManager()
private val fakeCheckIfCanToggleNotificationsViaPusherUseCase = mockk<CheckIfCanToggleNotificationsViaPusherUseCase>() private val fakeCheckIfCanToggleNotificationsViaPusherUseCase = mockk<CheckIfCanToggleNotificationsViaPusherUseCase>()
private val fakeToggleNotificationsUseCase = mockk<ToggleNotificationsUseCase>() private val fakeToggleNotificationsForCurrentSessionUseCase = mockk<ToggleNotificationsForCurrentSessionUseCase>()
private val fakeUnregisterUnifiedPushUseCase = mockk<UnregisterUnifiedPushUseCase>() private val fakeUnregisterUnifiedPushUseCase = mockk<UnregisterUnifiedPushUseCase>()
private val disableNotificationsForCurrentSessionUseCase = DisableNotificationsForCurrentSessionUseCase( private val disableNotificationsForCurrentSessionUseCase = DisableNotificationsForCurrentSessionUseCase(
activeSessionHolder = fakeActiveSessionHolder.instance, activeSessionHolder = fakeActiveSessionHolder.instance,
pushersManager = fakePushersManager.instance, pushersManager = fakePushersManager.instance,
checkIfCanToggleNotificationsViaPusherUseCase = fakeCheckIfCanToggleNotificationsViaPusherUseCase, checkIfCanToggleNotificationsViaPusherUseCase = fakeCheckIfCanToggleNotificationsViaPusherUseCase,
toggleNotificationUseCase = fakeToggleNotificationsUseCase, toggleNotificationsForCurrentSessionUseCase = fakeToggleNotificationsForCurrentSessionUseCase,
unregisterUnifiedPushUseCase = fakeUnregisterUnifiedPushUseCase, unregisterUnifiedPushUseCase = fakeUnregisterUnifiedPushUseCase,
) )
@ -50,24 +47,25 @@ class DisableNotificationsForCurrentSessionUseCaseTest {
fun `given toggle via pusher is possible when execute then disable notification via toggle of existing pusher`() = runTest { fun `given toggle via pusher is possible when execute then disable notification via toggle of existing pusher`() = runTest {
// Given // Given
val fakeSession = fakeActiveSessionHolder.fakeSession val fakeSession = fakeActiveSessionHolder.fakeSession
fakeSession.givenSessionId(A_SESSION_ID)
every { fakeCheckIfCanToggleNotificationsViaPusherUseCase.execute(fakeSession) } returns true every { fakeCheckIfCanToggleNotificationsViaPusherUseCase.execute(fakeSession) } returns true
coJustRun { fakeToggleNotificationsUseCase.execute(A_SESSION_ID, any()) } coJustRun { fakeToggleNotificationsForCurrentSessionUseCase.execute(any()) }
// When // When
disableNotificationsForCurrentSessionUseCase.execute() disableNotificationsForCurrentSessionUseCase.execute()
// Then // Then
coVerify { fakeToggleNotificationsUseCase.execute(A_SESSION_ID, false) } coVerify { fakeToggleNotificationsForCurrentSessionUseCase.execute(false) }
coVerify(inverse = true) {
fakeUnregisterUnifiedPushUseCase.execute(any())
}
} }
@Test @Test
fun `given toggle via pusher is NOT possible when execute then disable notification by unregistering the pusher`() = runTest { fun `given toggle via pusher is NOT possible when execute then disable notification by unregistering the pusher`() = runTest {
// Given // Given
val fakeSession = fakeActiveSessionHolder.fakeSession val fakeSession = fakeActiveSessionHolder.fakeSession
fakeSession.givenSessionId(A_SESSION_ID)
every { fakeCheckIfCanToggleNotificationsViaPusherUseCase.execute(fakeSession) } returns false every { fakeCheckIfCanToggleNotificationsViaPusherUseCase.execute(fakeSession) } returns false
coJustRun { fakeToggleNotificationsUseCase.execute(A_SESSION_ID, any()) } coJustRun { fakeToggleNotificationsForCurrentSessionUseCase.execute(any()) }
coJustRun { fakeUnregisterUnifiedPushUseCase.execute(any()) } coJustRun { fakeUnregisterUnifiedPushUseCase.execute(any()) }
// When // When
@ -75,7 +73,7 @@ class DisableNotificationsForCurrentSessionUseCaseTest {
// Then // Then
coVerify { coVerify {
fakeToggleNotificationsUseCase.execute(A_SESSION_ID, false) fakeToggleNotificationsForCurrentSessionUseCase.execute(false)
fakeUnregisterUnifiedPushUseCase.execute(fakePushersManager.instance) fakeUnregisterUnifiedPushUseCase.execute(fakePushersManager.instance)
} }
} }

View File

@ -18,10 +18,9 @@ package im.vector.app.features.settings.notifications
import im.vector.app.core.pushers.EnsureFcmTokenIsRetrievedUseCase import im.vector.app.core.pushers.EnsureFcmTokenIsRetrievedUseCase
import im.vector.app.core.pushers.RegisterUnifiedPushUseCase import im.vector.app.core.pushers.RegisterUnifiedPushUseCase
import im.vector.app.features.settings.devices.v2.notification.ToggleNotificationsUseCase
import im.vector.app.test.fakes.FakeActiveSessionHolder
import im.vector.app.test.fakes.FakePushersManager import im.vector.app.test.fakes.FakePushersManager
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.justRun
import io.mockk.mockk import io.mockk.mockk
@ -30,20 +29,16 @@ import kotlinx.coroutines.test.runTest
import org.amshove.kluent.shouldBe import org.amshove.kluent.shouldBe
import org.junit.Test import org.junit.Test
private const val A_SESSION_ID = "session-id"
class EnableNotificationsForCurrentSessionUseCaseTest { class EnableNotificationsForCurrentSessionUseCaseTest {
private val fakeActiveSessionHolder = FakeActiveSessionHolder()
private val fakePushersManager = FakePushersManager() private val fakePushersManager = FakePushersManager()
private val fakeToggleNotificationsUseCase = mockk<ToggleNotificationsUseCase>() private val fakeToggleNotificationsForCurrentSessionUseCase = mockk<ToggleNotificationsForCurrentSessionUseCase>()
private val fakeRegisterUnifiedPushUseCase = mockk<RegisterUnifiedPushUseCase>() private val fakeRegisterUnifiedPushUseCase = mockk<RegisterUnifiedPushUseCase>()
private val fakeEnsureFcmTokenIsRetrievedUseCase = mockk<EnsureFcmTokenIsRetrievedUseCase>() private val fakeEnsureFcmTokenIsRetrievedUseCase = mockk<EnsureFcmTokenIsRetrievedUseCase>()
private val enableNotificationsForCurrentSessionUseCase = EnableNotificationsForCurrentSessionUseCase( private val enableNotificationsForCurrentSessionUseCase = EnableNotificationsForCurrentSessionUseCase(
activeSessionHolder = fakeActiveSessionHolder.instance,
pushersManager = fakePushersManager.instance, pushersManager = fakePushersManager.instance,
toggleNotificationUseCase = fakeToggleNotificationsUseCase, toggleNotificationsForCurrentSessionUseCase = fakeToggleNotificationsForCurrentSessionUseCase,
registerUnifiedPushUseCase = fakeRegisterUnifiedPushUseCase, registerUnifiedPushUseCase = fakeRegisterUnifiedPushUseCase,
ensureFcmTokenIsRetrievedUseCase = fakeEnsureFcmTokenIsRetrievedUseCase, ensureFcmTokenIsRetrievedUseCase = fakeEnsureFcmTokenIsRetrievedUseCase,
) )
@ -52,12 +47,10 @@ class EnableNotificationsForCurrentSessionUseCaseTest {
fun `given no existing pusher and a registered distributor when execute then a new pusher is registered and result is success`() = 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 aDistributor = "distributor" val aDistributor = "distributor"
val fakeSession = fakeActiveSessionHolder.fakeSession
fakeSession.givenSessionId(A_SESSION_ID)
fakePushersManager.givenGetPusherForCurrentSessionReturns(null) fakePushersManager.givenGetPusherForCurrentSessionReturns(null)
every { fakeRegisterUnifiedPushUseCase.execute(any()) } returns RegisterUnifiedPushUseCase.RegisterUnifiedPushResult.Success every { fakeRegisterUnifiedPushUseCase.execute(any()) } returns RegisterUnifiedPushUseCase.RegisterUnifiedPushResult.Success
justRun { fakeEnsureFcmTokenIsRetrievedUseCase.execute(any(), any()) } justRun { fakeEnsureFcmTokenIsRetrievedUseCase.execute(any(), any()) }
coJustRun { fakeToggleNotificationsUseCase.execute(A_SESSION_ID, any()) } coJustRun { fakeToggleNotificationsForCurrentSessionUseCase.execute(any()) }
// When // When
val result = enableNotificationsForCurrentSessionUseCase.execute(aDistributor) val result = enableNotificationsForCurrentSessionUseCase.execute(aDistributor)
@ -68,6 +61,9 @@ class EnableNotificationsForCurrentSessionUseCaseTest {
fakeRegisterUnifiedPushUseCase.execute(aDistributor) fakeRegisterUnifiedPushUseCase.execute(aDistributor)
fakeEnsureFcmTokenIsRetrievedUseCase.execute(fakePushersManager.instance, registerPusher = true) fakeEnsureFcmTokenIsRetrievedUseCase.execute(fakePushersManager.instance, registerPusher = true)
} }
coVerify {
fakeToggleNotificationsForCurrentSessionUseCase.execute(enabled = true)
}
} }
@Test @Test
@ -86,20 +82,4 @@ class EnableNotificationsForCurrentSessionUseCaseTest {
fakeRegisterUnifiedPushUseCase.execute(aDistributor) 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

@ -46,6 +46,7 @@ class VectorSettingsNotificationPreferenceViewModelTest {
private val fakeUnregisterUnifiedPushUseCase = mockk<UnregisterUnifiedPushUseCase>() private val fakeUnregisterUnifiedPushUseCase = mockk<UnregisterUnifiedPushUseCase>()
private val fakeRegisterUnifiedPushUseCase = mockk<RegisterUnifiedPushUseCase>() private val fakeRegisterUnifiedPushUseCase = mockk<RegisterUnifiedPushUseCase>()
private val fakeEnsureFcmTokenIsRetrievedUseCase = mockk<EnsureFcmTokenIsRetrievedUseCase>() private val fakeEnsureFcmTokenIsRetrievedUseCase = mockk<EnsureFcmTokenIsRetrievedUseCase>()
private val fakeToggleNotificationsForCurrentSessionUseCase = mockk<ToggleNotificationsForCurrentSessionUseCase>()
private fun createViewModel() = VectorSettingsNotificationPreferenceViewModel( private fun createViewModel() = VectorSettingsNotificationPreferenceViewModel(
initialState = VectorDummyViewState(), initialState = VectorDummyViewState(),
@ -56,6 +57,7 @@ class VectorSettingsNotificationPreferenceViewModelTest {
unregisterUnifiedPushUseCase = fakeUnregisterUnifiedPushUseCase, unregisterUnifiedPushUseCase = fakeUnregisterUnifiedPushUseCase,
registerUnifiedPushUseCase = fakeRegisterUnifiedPushUseCase, registerUnifiedPushUseCase = fakeRegisterUnifiedPushUseCase,
ensureFcmTokenIsRetrievedUseCase = fakeEnsureFcmTokenIsRetrievedUseCase, ensureFcmTokenIsRetrievedUseCase = fakeEnsureFcmTokenIsRetrievedUseCase,
toggleNotificationsForCurrentSessionUseCase = fakeToggleNotificationsForCurrentSessionUseCase,
) )
@Test @Test
@ -125,29 +127,6 @@ class VectorSettingsNotificationPreferenceViewModelTest {
} }
} }
@Test
fun `given EnableNotificationsForDevice action and enable failure when handling action then enable use case is called`() {
// Given
val viewModel = createViewModel()
val aDistributor = "aDistributor"
val action = VectorSettingsNotificationPreferenceViewAction.EnableNotificationsForDevice(aDistributor)
coEvery { fakeEnableNotificationsForCurrentSessionUseCase.execute(any()) } returns
EnableNotificationsForCurrentSessionUseCase.EnableNotificationsResult.Failure
val expectedEvent = VectorSettingsNotificationPreferenceViewEvent.EnableNotificationForDeviceFailure
// When
val viewModelTest = viewModel.test()
viewModel.handle(action)
// Then
viewModelTest
.assertEvent { event -> event == expectedEvent }
.finish()
coVerify {
fakeEnableNotificationsForCurrentSessionUseCase.execute(aDistributor)
}
}
@Test @Test
fun `given RegisterPushDistributor action and register success when handling action then register use case is called`() { fun `given RegisterPushDistributor action and register success when handling action then register use case is called`() {
// Given // Given
@ -158,6 +137,7 @@ class VectorSettingsNotificationPreferenceViewModelTest {
coJustRun { fakeUnregisterUnifiedPushUseCase.execute(any()) } coJustRun { fakeUnregisterUnifiedPushUseCase.execute(any()) }
val areNotificationsEnabled = true val areNotificationsEnabled = true
fakeVectorPreferences.givenAreNotificationsEnabledForDevice(areNotificationsEnabled) fakeVectorPreferences.givenAreNotificationsEnabledForDevice(areNotificationsEnabled)
coJustRun { fakeToggleNotificationsForCurrentSessionUseCase.execute(any()) }
justRun { fakeEnsureFcmTokenIsRetrievedUseCase.execute(any(), any()) } justRun { fakeEnsureFcmTokenIsRetrievedUseCase.execute(any(), any()) }
val expectedEvent = VectorSettingsNotificationPreferenceViewEvent.NotificationMethodChanged val expectedEvent = VectorSettingsNotificationPreferenceViewEvent.NotificationMethodChanged
@ -173,6 +153,7 @@ class VectorSettingsNotificationPreferenceViewModelTest {
fakeUnregisterUnifiedPushUseCase.execute(fakePushersManager.instance) fakeUnregisterUnifiedPushUseCase.execute(fakePushersManager.instance)
fakeRegisterUnifiedPushUseCase.execute(aDistributor) fakeRegisterUnifiedPushUseCase.execute(aDistributor)
fakeEnsureFcmTokenIsRetrievedUseCase.execute(fakePushersManager.instance, registerPusher = areNotificationsEnabled) fakeEnsureFcmTokenIsRetrievedUseCase.execute(fakePushersManager.instance, registerPusher = areNotificationsEnabled)
fakeToggleNotificationsForCurrentSessionUseCase.execute(enabled = areNotificationsEnabled)
} }
} }