diff --git a/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/srs/ui/consent/SrsSubmissionConsentFragmentViewModel.kt b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/srs/ui/consent/SrsSubmissionConsentFragmentViewModel.kt index 1d258be21f6..8ee338bdabb 100644 --- a/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/srs/ui/consent/SrsSubmissionConsentFragmentViewModel.kt +++ b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/srs/ui/consent/SrsSubmissionConsentFragmentViewModel.kt @@ -29,26 +29,11 @@ class SrsSubmissionConsentFragmentViewModel @AssistedInject constructor( val showTracingConsentDialog = SingleLiveEvent<(Boolean) -> Unit>() val showPermissionRequest = SingleLiveEvent<(Activity) -> Unit>() val event = SingleLiveEvent() + private val tekHistoryUpdater = tekHistoryUpdaterFactory.create( object : TEKHistoryUpdater.Callback { override fun onTEKAvailable(teks: List) = launch { - Timber.tag(TAG).d("onTEKAvailable(teks.size=%d)", teks.size) - showKeysRetrievalProgress.postValue(false) - - if (openTypeSelection) { - Timber.tag(TAG).d("Navigate to TestType") - event.postValue(SrsSubmissionConsentNavigationEvents.NavigateToTestType) - } else { - val completedCheckInsExist = checkInRepository.completedCheckIns.first().isNotEmpty() - val navDirections = if (completedCheckInsExist) { - Timber.tag(TAG).d("Navigate to ShareCheckins") - SrsSubmissionConsentNavigationEvents.NavigateToShareCheckins - } else { - Timber.tag(TAG).d("Navigate to ShareSymptoms") - SrsSubmissionConsentNavigationEvents.NavigateToShareSymptoms - } - event.postValue(navDirections) - } + onTekAvailable(teks) } override fun onTEKPermissionDeclined() { @@ -80,6 +65,26 @@ class SrsSubmissionConsentFragmentViewModel @AssistedInject constructor( } ) + suspend fun onTekAvailable(teks: List) { + Timber.tag(TAG).d("onTEKAvailable(teks.size=%d)", teks.size) + showKeysRetrievalProgress.postValue(false) + + if (openTypeSelection) { + Timber.tag(TAG).d("Navigate to TestType") + event.postValue(SrsSubmissionConsentNavigationEvents.NavigateToTestType) + } else { + val completedCheckInsExist = checkInRepository.completedCheckIns.first().isNotEmpty() + val navDirections = if (completedCheckInsExist) { + Timber.tag(TAG).d("Navigate to ShareCheckins") + SrsSubmissionConsentNavigationEvents.NavigateToShareCheckins + } else { + Timber.tag(TAG).d("Navigate to ShareSymptoms") + SrsSubmissionConsentNavigationEvents.NavigateToShareSymptoms + } + event.postValue(navDirections) + } + } + fun onDataPrivacyClick() { event.postValue(SrsSubmissionConsentNavigationEvents.NavigateToDataPrivacy) } diff --git a/Corona-Warn-App/src/test/java/de/rki/coronawarnapp/srs/ui/consent/SrsSubmissionConsentFragmentViewModelTest.kt b/Corona-Warn-App/src/test/java/de/rki/coronawarnapp/srs/ui/consent/SrsSubmissionConsentFragmentViewModelTest.kt new file mode 100644 index 00000000000..87ee7e0034c --- /dev/null +++ b/Corona-Warn-App/src/test/java/de/rki/coronawarnapp/srs/ui/consent/SrsSubmissionConsentFragmentViewModelTest.kt @@ -0,0 +1,91 @@ +package de.rki.coronawarnapp.srs.ui.consent + +import de.rki.coronawarnapp.presencetracing.checkins.CheckIn +import de.rki.coronawarnapp.presencetracing.checkins.CheckInRepository +import de.rki.coronawarnapp.submission.data.tekhistory.TEKHistoryUpdater +import io.kotest.matchers.shouldBe +import io.mockk.MockKAnnotations +import io.mockk.every +import io.mockk.impl.annotations.MockK +import kotlinx.coroutines.flow.flowOf +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.extension.ExtendWith +import testhelpers.TestDispatcherProvider +import testhelpers.coroutines.runTest2 +import testhelpers.extensions.InstantExecutorExtension + +@ExtendWith(InstantExecutorExtension::class) +open class SrsSubmissionConsentFragmentViewModelTest { + + @MockK lateinit var tekHistoryUpdater: TEKHistoryUpdater + @MockK lateinit var tekHistoryUpdaterFactory: TEKHistoryUpdater.Factory + @MockK lateinit var checkInRepository: CheckInRepository + + @MockK lateinit var checkIn: CheckIn + + fun createInstance(openTypeSelection: Boolean) = SrsSubmissionConsentFragmentViewModel( + openTypeSelection, + checkInRepository, + dispatcherProvider = TestDispatcherProvider(), + tekHistoryUpdaterFactory + ) + + @BeforeEach + fun setUp() { + MockKAnnotations.init(this) + every { tekHistoryUpdaterFactory.create(any()) } returns tekHistoryUpdater + } + + @Test + fun `when open type selection is true navigation to test type happens`() = runTest2 { + val vm = createInstance(true) + every { tekHistoryUpdater.getTeksOrRequestPermission() } coAnswers { + vm.onTekAvailable(listOf()) + } + vm.submissionConsentAcceptButtonClicked() + + vm.event.value shouldBe SrsSubmissionConsentNavigationEvents.NavigateToTestType + } + + @Test + fun `when open type selection is false navigation to checkins happen when checkins exist`() = runTest2 { + val vm = createInstance(false) + every { checkIn.completed } returns true + every { tekHistoryUpdater.getTeksOrRequestPermission() } coAnswers { + vm.onTekAvailable(listOf()) + } + every { checkInRepository.checkInsWithinRetention } returns flowOf(listOf(checkIn)) + vm.submissionConsentAcceptButtonClicked() + + vm.event.value shouldBe SrsSubmissionConsentNavigationEvents.NavigateToShareCheckins + } + + @Test + fun `when open type selection is false navigation to symptoms happen when no checkin exists`() = runTest2 { + val vm = createInstance(false) + every { tekHistoryUpdater.getTeksOrRequestPermission() } coAnswers { + vm.onTekAvailable(listOf()) + } + every { checkInRepository.checkInsWithinRetention } returns flowOf(listOf()) + vm.submissionConsentAcceptButtonClicked() + + vm.event.value shouldBe SrsSubmissionConsentNavigationEvents.NavigateToShareSymptoms + } + + @Test + fun `vm navigates to privacy`() = runTest2 { + val vm = createInstance(false) + vm.onDataPrivacyClick() + + vm.event.value shouldBe SrsSubmissionConsentNavigationEvents.NavigateToDataPrivacy + } + + @Test + fun `vm navigates to main screen when flow is canceled`() = runTest2 { + val vm = createInstance(false) + vm.onConsentCancel() + + vm.event.value shouldBe SrsSubmissionConsentNavigationEvents.NavigateToMainScreen + } +}