Skip to content
This repository was archived by the owner on Jun 20, 2023. It is now read-only.

Unit tests consent screen (EXPOSUREAPP-14323) #5711

Merged
merged 1 commit into from
Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,11 @@ class SrsSubmissionConsentFragmentViewModel @AssistedInject constructor(
val showTracingConsentDialog = SingleLiveEvent<(Boolean) -> Unit>()
val showPermissionRequest = SingleLiveEvent<(Activity) -> Unit>()
val event = SingleLiveEvent<SrsSubmissionConsentNavigationEvents>()

private val tekHistoryUpdater = tekHistoryUpdaterFactory.create(
object : TEKHistoryUpdater.Callback {
override fun onTEKAvailable(teks: List<TemporaryExposureKey>) = 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() {
Expand Down Expand Up @@ -80,6 +65,26 @@ class SrsSubmissionConsentFragmentViewModel @AssistedInject constructor(
}
)

suspend fun onTekAvailable(teks: List<TemporaryExposureKey>) {
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)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
}