|
| 1 | +package com.android.contacts.ui.interactions.importing |
| 2 | + |
| 3 | +import androidx.compose.ui.test.ComposeUiTest |
| 4 | +import androidx.compose.ui.test.ExperimentalTestApi |
| 5 | +import androidx.compose.ui.test.assertIsDisplayed |
| 6 | +import androidx.compose.ui.test.assertIsNotDisplayed |
| 7 | +import androidx.compose.ui.test.onNodeWithTag |
| 8 | +import androidx.compose.ui.test.onNodeWithText |
| 9 | +import androidx.compose.ui.test.v2.runComposeUiTest |
| 10 | +import com.android.contacts.R |
| 11 | +import com.android.contacts.domain.accounts.model.AccountModel |
| 12 | +import com.android.contacts.tests.SimCardOptionFactory |
| 13 | +import com.android.contacts.tests.factory.AccountModelFactory |
| 14 | +import com.android.contacts.tests.resources |
| 15 | +import com.android.contacts.ui.interactions.importing.screen.ImportDialog |
| 16 | +import com.android.contacts.ui.interactions.importing.screen.ImportEffectHandler |
| 17 | +import com.android.contacts.ui.interactions.importing.screen.ImportScreenModel |
| 18 | +import com.android.contacts.ui.interactions.importing.screen.model.IMPORT_EMPTY_MESSAGE_TEST_TAG |
| 19 | +import com.android.contacts.ui.interactions.importing.screen.model.IMPORT_PROGRESS_TEST_TAG |
| 20 | +import com.android.contacts.ui.interactions.importing.screen.model.IMPORT_VCARD_BUTTON_TEST_TAG |
| 21 | +import com.android.contacts.ui.interactions.importing.screen.model.ImportAction as Action |
| 22 | +import com.android.contacts.ui.interactions.importing.screen.model.ImportUiState as State |
| 23 | +import io.mockk.every |
| 24 | +import io.mockk.mockk |
| 25 | +import io.mockk.verify |
| 26 | +import kotlinx.collections.immutable.persistentListOf |
| 27 | +import kotlinx.coroutines.flow.MutableStateFlow |
| 28 | +import org.junit.Before |
| 29 | +import org.junit.Test |
| 30 | +import org.junit.runner.RunWith |
| 31 | +import org.robolectric.RobolectricTestRunner |
| 32 | + |
| 33 | +@OptIn(ExperimentalTestApi::class) |
| 34 | +@RunWith(RobolectricTestRunner::class) |
| 35 | +class ImportDialogTest { |
| 36 | + |
| 37 | + private val fakeUiStateFlow = MutableStateFlow(State()) |
| 38 | + private lateinit var screenModel: ImportScreenModel |
| 39 | + private lateinit var effectHandler: ImportEffectHandler |
| 40 | + private var accountChosen: AccountModel? = null |
| 41 | + |
| 42 | + @Before |
| 43 | + fun setup() { |
| 44 | + screenModel = mockk(relaxed = true) |
| 45 | + effectHandler = mockk(relaxed = true) |
| 46 | + every { screenModel.uiState } returns fakeUiStateFlow |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + fun showsOrHidesProgressIndicator() = runComposeUiTest { |
| 51 | + setScreenContent() |
| 52 | + |
| 53 | + fakeUiStateFlow.value = State( |
| 54 | + isVCardImportAvailable = null, |
| 55 | + simCardOptions = null, |
| 56 | + ) |
| 57 | + onNodeWithTag(IMPORT_PROGRESS_TEST_TAG).assertIsDisplayed() |
| 58 | + |
| 59 | + fakeUiStateFlow.value = State( |
| 60 | + isVCardImportAvailable = true, |
| 61 | + simCardOptions = persistentListOf(), |
| 62 | + ) |
| 63 | + onNodeWithTag(IMPORT_PROGRESS_TEST_TAG).assertIsNotDisplayed() |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + fun showsEmptyState() = runComposeUiTest { |
| 68 | + setScreenContent() |
| 69 | + |
| 70 | + fakeUiStateFlow.value = State( |
| 71 | + isVCardImportAvailable = false, |
| 72 | + simCardOptions = persistentListOf(), |
| 73 | + ) |
| 74 | + onNodeWithTag(IMPORT_EMPTY_MESSAGE_TEST_TAG).assertIsDisplayed() |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + fun showsOrHidesVCardOption() = runComposeUiTest { |
| 79 | + setScreenContent() |
| 80 | + |
| 81 | + fakeUiStateFlow.value = State( |
| 82 | + isVCardImportAvailable = true, |
| 83 | + simCardOptions = persistentListOf(), |
| 84 | + ) |
| 85 | + onNodeWithTag(IMPORT_VCARD_BUTTON_TEST_TAG).assertIsDisplayed() |
| 86 | + |
| 87 | + fakeUiStateFlow.value = State( |
| 88 | + isVCardImportAvailable = false, |
| 89 | + simCardOptions = persistentListOf(), |
| 90 | + ) |
| 91 | + onNodeWithTag(IMPORT_VCARD_BUTTON_TEST_TAG).assertIsNotDisplayed() |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + fun showsSimCardOptions() = runComposeUiTest { |
| 96 | + setScreenContent() |
| 97 | + |
| 98 | + fakeUiStateFlow.value = State( |
| 99 | + isVCardImportAvailable = false, |
| 100 | + simCardOptions = persistentListOf( |
| 101 | + SimCardOptionFactory.build(), |
| 102 | + SimCardOptionFactory.build(name = "Test") |
| 103 | + ), |
| 104 | + ) |
| 105 | + |
| 106 | + onNodeWithText( |
| 107 | + resources.getString(R.string.import_from_sim_summary_fmt, 1) |
| 108 | + ).assertIsDisplayed() |
| 109 | + onNodeWithText( |
| 110 | + resources.getString(R.string.import_from_sim_summary_fmt, "Test") |
| 111 | + ).assertIsDisplayed() |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + fun whenAccountChosenIsProvided_callOnAction() = runComposeUiTest { |
| 116 | + accountChosen = AccountModelFactory.build() |
| 117 | + |
| 118 | + setScreenContent() |
| 119 | + |
| 120 | + verify { screenModel.onAction(Action.AccountChosen(accountChosen!!)) } |
| 121 | + } |
| 122 | + |
| 123 | + private fun ComposeUiTest.setScreenContent() { |
| 124 | + setContent { |
| 125 | + ImportDialog( |
| 126 | + effectHandler = effectHandler, |
| 127 | + screenModel = screenModel, |
| 128 | + accountChosen = accountChosen, |
| 129 | + ) |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments