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,
) {
// TODO update unit tests
suspend fun execute() {
val session = activeSessionHolder.getSafeActiveSession() ?: return
toggleNotificationsForCurrentSessionUseCase.execute(enabled = false)

View File

@ -30,11 +30,9 @@ class EnableNotificationsForCurrentSessionUseCase @Inject constructor(
sealed interface EnableNotificationsResult {
object Success : EnableNotificationsResult
object Failure : EnableNotificationsResult
object NeedToAskUserForDistributor : EnableNotificationsResult
}
// TODO update unit tests
suspend fun execute(distributor: String = ""): EnableNotificationsResult {
val pusherForCurrentSession = pushersManager.getPusherForCurrentSession()
if (pusherForCurrentSession == null) {

View File

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

View File

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

View File

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

View File

@ -88,7 +88,7 @@ class SessionOverviewViewModelTest {
pendingAuthHandler = fakePendingAuthHandler.instance,
activeSessionHolder = fakeActiveSessionHolder.instance,
refreshDevicesUseCase = refreshDevicesUseCase,
toggleNotificationUseCase = toggleNotificationUseCase.instance,
toggleNotificationsUseCase = toggleNotificationUseCase.instance,
getNotificationsStatusUseCase = fakeGetNotificationsStatusUseCase.instance,
vectorPreferences = fakeVectorPreferences.instance,
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.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.FakePushersManager
import io.mockk.coJustRun
@ -28,21 +27,19 @@ import io.mockk.mockk
import kotlinx.coroutines.test.runTest
import org.junit.Test
private const val A_SESSION_ID = "session-id"
class DisableNotificationsForCurrentSessionUseCaseTest {
private val fakeActiveSessionHolder = FakeActiveSessionHolder()
private val fakePushersManager = FakePushersManager()
private val fakeCheckIfCanToggleNotificationsViaPusherUseCase = mockk<CheckIfCanToggleNotificationsViaPusherUseCase>()
private val fakeToggleNotificationsUseCase = mockk<ToggleNotificationsUseCase>()
private val fakeToggleNotificationsForCurrentSessionUseCase = mockk<ToggleNotificationsForCurrentSessionUseCase>()
private val fakeUnregisterUnifiedPushUseCase = mockk<UnregisterUnifiedPushUseCase>()
private val disableNotificationsForCurrentSessionUseCase = DisableNotificationsForCurrentSessionUseCase(
activeSessionHolder = fakeActiveSessionHolder.instance,
pushersManager = fakePushersManager.instance,
checkIfCanToggleNotificationsViaPusherUseCase = fakeCheckIfCanToggleNotificationsViaPusherUseCase,
toggleNotificationUseCase = fakeToggleNotificationsUseCase,
toggleNotificationsForCurrentSessionUseCase = fakeToggleNotificationsForCurrentSessionUseCase,
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 {
// Given
val fakeSession = fakeActiveSessionHolder.fakeSession
fakeSession.givenSessionId(A_SESSION_ID)
every { fakeCheckIfCanToggleNotificationsViaPusherUseCase.execute(fakeSession) } returns true
coJustRun { fakeToggleNotificationsUseCase.execute(A_SESSION_ID, any()) }
coJustRun { fakeToggleNotificationsForCurrentSessionUseCase.execute(any()) }
// When
disableNotificationsForCurrentSessionUseCase.execute()
// Then
coVerify { fakeToggleNotificationsUseCase.execute(A_SESSION_ID, false) }
coVerify { fakeToggleNotificationsForCurrentSessionUseCase.execute(false) }
coVerify(inverse = true) {
fakeUnregisterUnifiedPushUseCase.execute(any())
}
}
@Test
fun `given toggle via pusher is NOT possible when execute then disable notification by unregistering the pusher`() = runTest {
// Given
val fakeSession = fakeActiveSessionHolder.fakeSession
fakeSession.givenSessionId(A_SESSION_ID)
every { fakeCheckIfCanToggleNotificationsViaPusherUseCase.execute(fakeSession) } returns false
coJustRun { fakeToggleNotificationsUseCase.execute(A_SESSION_ID, any()) }
coJustRun { fakeToggleNotificationsForCurrentSessionUseCase.execute(any()) }
coJustRun { fakeUnregisterUnifiedPushUseCase.execute(any()) }
// When
@ -75,7 +73,7 @@ class DisableNotificationsForCurrentSessionUseCaseTest {
// Then
coVerify {
fakeToggleNotificationsUseCase.execute(A_SESSION_ID, false)
fakeToggleNotificationsForCurrentSessionUseCase.execute(false)
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.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 io.mockk.coJustRun
import io.mockk.coVerify
import io.mockk.every
import io.mockk.justRun
import io.mockk.mockk
@ -30,20 +29,16 @@ import kotlinx.coroutines.test.runTest
import org.amshove.kluent.shouldBe
import org.junit.Test
private const val A_SESSION_ID = "session-id"
class EnableNotificationsForCurrentSessionUseCaseTest {
private val fakeActiveSessionHolder = FakeActiveSessionHolder()
private val fakePushersManager = FakePushersManager()
private val fakeToggleNotificationsUseCase = mockk<ToggleNotificationsUseCase>()
private val fakeToggleNotificationsForCurrentSessionUseCase = mockk<ToggleNotificationsForCurrentSessionUseCase>()
private val fakeRegisterUnifiedPushUseCase = mockk<RegisterUnifiedPushUseCase>()
private val fakeEnsureFcmTokenIsRetrievedUseCase = mockk<EnsureFcmTokenIsRetrievedUseCase>()
private val enableNotificationsForCurrentSessionUseCase = EnableNotificationsForCurrentSessionUseCase(
activeSessionHolder = fakeActiveSessionHolder.instance,
pushersManager = fakePushersManager.instance,
toggleNotificationUseCase = fakeToggleNotificationsUseCase,
toggleNotificationsForCurrentSessionUseCase = fakeToggleNotificationsForCurrentSessionUseCase,
registerUnifiedPushUseCase = fakeRegisterUnifiedPushUseCase,
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 {
// Given
val aDistributor = "distributor"
val fakeSession = fakeActiveSessionHolder.fakeSession
fakeSession.givenSessionId(A_SESSION_ID)
fakePushersManager.givenGetPusherForCurrentSessionReturns(null)
every { fakeRegisterUnifiedPushUseCase.execute(any()) } returns RegisterUnifiedPushUseCase.RegisterUnifiedPushResult.Success
justRun { fakeEnsureFcmTokenIsRetrievedUseCase.execute(any(), any()) }
coJustRun { fakeToggleNotificationsUseCase.execute(A_SESSION_ID, any()) }
coJustRun { fakeToggleNotificationsForCurrentSessionUseCase.execute(any()) }
// When
val result = enableNotificationsForCurrentSessionUseCase.execute(aDistributor)
@ -68,6 +61,9 @@ class EnableNotificationsForCurrentSessionUseCaseTest {
fakeRegisterUnifiedPushUseCase.execute(aDistributor)
fakeEnsureFcmTokenIsRetrievedUseCase.execute(fakePushersManager.instance, registerPusher = true)
}
coVerify {
fakeToggleNotificationsForCurrentSessionUseCase.execute(enabled = true)
}
}
@Test
@ -86,20 +82,4 @@ class EnableNotificationsForCurrentSessionUseCaseTest {
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 fakeRegisterUnifiedPushUseCase = mockk<RegisterUnifiedPushUseCase>()
private val fakeEnsureFcmTokenIsRetrievedUseCase = mockk<EnsureFcmTokenIsRetrievedUseCase>()
private val fakeToggleNotificationsForCurrentSessionUseCase = mockk<ToggleNotificationsForCurrentSessionUseCase>()
private fun createViewModel() = VectorSettingsNotificationPreferenceViewModel(
initialState = VectorDummyViewState(),
@ -56,6 +57,7 @@ class VectorSettingsNotificationPreferenceViewModelTest {
unregisterUnifiedPushUseCase = fakeUnregisterUnifiedPushUseCase,
registerUnifiedPushUseCase = fakeRegisterUnifiedPushUseCase,
ensureFcmTokenIsRetrievedUseCase = fakeEnsureFcmTokenIsRetrievedUseCase,
toggleNotificationsForCurrentSessionUseCase = fakeToggleNotificationsForCurrentSessionUseCase,
)
@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
fun `given RegisterPushDistributor action and register success when handling action then register use case is called`() {
// Given
@ -158,6 +137,7 @@ class VectorSettingsNotificationPreferenceViewModelTest {
coJustRun { fakeUnregisterUnifiedPushUseCase.execute(any()) }
val areNotificationsEnabled = true
fakeVectorPreferences.givenAreNotificationsEnabledForDevice(areNotificationsEnabled)
coJustRun { fakeToggleNotificationsForCurrentSessionUseCase.execute(any()) }
justRun { fakeEnsureFcmTokenIsRetrievedUseCase.execute(any(), any()) }
val expectedEvent = VectorSettingsNotificationPreferenceViewEvent.NotificationMethodChanged
@ -173,6 +153,7 @@ class VectorSettingsNotificationPreferenceViewModelTest {
fakeUnregisterUnifiedPushUseCase.execute(fakePushersManager.instance)
fakeRegisterUnifiedPushUseCase.execute(aDistributor)
fakeEnsureFcmTokenIsRetrievedUseCase.execute(fakePushersManager.instance, registerPusher = areNotificationsEnabled)
fakeToggleNotificationsForCurrentSessionUseCase.execute(enabled = areNotificationsEnabled)
}
}