mirror of
https://github.com/bitwarden/android.git
synced 2026-11-19 17:40:12 -06:00
Display attachments in the UI (#754)
This commit is contained in:
committed by
Álison Fernandes
parent
be8608e53a
commit
89fda64baa
+175
-3
@@ -1,13 +1,30 @@
|
||||
package com.x8bit.bitwarden.ui.vault.feature.attachments
|
||||
|
||||
import androidx.compose.ui.test.assert
|
||||
import androidx.compose.ui.test.assertIsDisplayed
|
||||
import androidx.compose.ui.test.filterToOne
|
||||
import androidx.compose.ui.test.hasAnyAncestor
|
||||
import androidx.compose.ui.test.hasClickAction
|
||||
import androidx.compose.ui.test.isDialog
|
||||
import androidx.compose.ui.test.onAllNodesWithText
|
||||
import androidx.compose.ui.test.onNodeWithContentDescription
|
||||
import androidx.compose.ui.test.onNodeWithText
|
||||
import androidx.compose.ui.test.performClick
|
||||
import com.x8bit.bitwarden.data.platform.repository.util.bufferedMutableSharedFlow
|
||||
import com.x8bit.bitwarden.ui.platform.base.BaseComposeTest
|
||||
import com.x8bit.bitwarden.ui.platform.base.util.asText
|
||||
import com.x8bit.bitwarden.ui.platform.manager.intent.IntentManager
|
||||
import com.x8bit.bitwarden.ui.util.isProgressBar
|
||||
import com.x8bit.bitwarden.ui.util.onNodeWithContentDescriptionAfterScroll
|
||||
import com.x8bit.bitwarden.ui.util.onNodeWithTextAfterScroll
|
||||
import io.mockk.every
|
||||
import io.mockk.just
|
||||
import io.mockk.mockk
|
||||
import io.mockk.runs
|
||||
import io.mockk.verify
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import org.junit.Assert
|
||||
import kotlinx.coroutines.flow.update
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
|
||||
@@ -16,17 +33,19 @@ class AttachmentsScreenTest : BaseComposeTest() {
|
||||
|
||||
private val mutableStateFlow = MutableStateFlow(DEFAULT_STATE)
|
||||
private val mutableEventFlow = bufferedMutableSharedFlow<AttachmentsEvent>()
|
||||
val viewModel: AttachmentsViewModel = mockk {
|
||||
private val viewModel: AttachmentsViewModel = mockk {
|
||||
every { stateFlow } returns mutableStateFlow
|
||||
every { eventFlow } returns mutableEventFlow
|
||||
every { trySendAction(any()) } just runs
|
||||
}
|
||||
private val intentManager: IntentManager = mockk(relaxed = true)
|
||||
|
||||
@Before
|
||||
fun setup() {
|
||||
composeTestRule.setContent {
|
||||
AttachmentsScreen(
|
||||
viewModel = viewModel,
|
||||
intentManager = intentManager,
|
||||
onNavigateBack = { onNavigateBackCalled = true },
|
||||
)
|
||||
}
|
||||
@@ -35,10 +54,163 @@ class AttachmentsScreenTest : BaseComposeTest() {
|
||||
@Test
|
||||
fun `NavigateBack should call onNavigateBack`() {
|
||||
mutableEventFlow.tryEmit(AttachmentsEvent.NavigateBack)
|
||||
Assert.assertTrue(onNavigateBackCalled)
|
||||
assertTrue(onNavigateBackCalled)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `on back click should send BackClick`() {
|
||||
composeTestRule.onNodeWithContentDescription("Back").performClick()
|
||||
verify(exactly = 1) {
|
||||
viewModel.trySendAction(AttachmentsAction.BackClick)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `on save click should send SaveClick`() {
|
||||
composeTestRule.onNodeWithText("Save").performClick()
|
||||
verify(exactly = 1) {
|
||||
viewModel.trySendAction(AttachmentsAction.SaveClick)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `on choose file click should send ChooseFileClick`() {
|
||||
mutableStateFlow.update {
|
||||
it.copy(viewState = AttachmentsState.ViewState.Content(emptyList()))
|
||||
}
|
||||
|
||||
composeTestRule.onNodeWithTextAfterScroll("Choose file").performClick()
|
||||
verify(exactly = 1) {
|
||||
viewModel.trySendAction(AttachmentsAction.ChooseFileClick)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `progressbar should be displayed according to state`() {
|
||||
mutableStateFlow.update { it.copy(viewState = AttachmentsState.ViewState.Loading) }
|
||||
composeTestRule.onNode(isProgressBar).assertIsDisplayed()
|
||||
|
||||
mutableStateFlow.update {
|
||||
it.copy(viewState = AttachmentsState.ViewState.Error("Fail".asText()))
|
||||
}
|
||||
composeTestRule.onNode(isProgressBar).assertDoesNotExist()
|
||||
|
||||
mutableStateFlow.update {
|
||||
it.copy(viewState = AttachmentsState.ViewState.Content(emptyList()))
|
||||
}
|
||||
composeTestRule.onNode(isProgressBar).assertDoesNotExist()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `error should be displayed according to state`() {
|
||||
val errorMessage = "Fail"
|
||||
mutableStateFlow.update {
|
||||
it.copy(viewState = AttachmentsState.ViewState.Error(errorMessage.asText()))
|
||||
}
|
||||
composeTestRule.onNodeWithText(errorMessage).assertIsDisplayed()
|
||||
|
||||
mutableStateFlow.update { it.copy(viewState = AttachmentsState.ViewState.Loading) }
|
||||
composeTestRule.onNodeWithText(errorMessage).assertDoesNotExist()
|
||||
|
||||
mutableStateFlow.update {
|
||||
it.copy(viewState = AttachmentsState.ViewState.Content(emptyList()))
|
||||
}
|
||||
composeTestRule.onNodeWithText(errorMessage).assertDoesNotExist()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `content with no items should be displayed according to state`() {
|
||||
mutableStateFlow.update {
|
||||
it.copy(viewState = AttachmentsState.ViewState.Content(emptyList()))
|
||||
}
|
||||
composeTestRule
|
||||
.onNodeWithTextAfterScroll("There are no attachments.")
|
||||
.assertIsDisplayed()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `content with items should be displayed according to state`() {
|
||||
mutableStateFlow.update {
|
||||
it.copy(viewState = DEFAULT_CONTENT_WITH_ATTACHMENTS)
|
||||
}
|
||||
composeTestRule
|
||||
.onNodeWithTextAfterScroll("cool_file.png")
|
||||
.assertIsDisplayed()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `on delete click should display confirmation dialog`() {
|
||||
mutableStateFlow.update {
|
||||
it.copy(viewState = DEFAULT_CONTENT_WITH_ATTACHMENTS)
|
||||
}
|
||||
|
||||
composeTestRule.onNode(isDialog()).assertDoesNotExist()
|
||||
|
||||
composeTestRule
|
||||
.onNodeWithContentDescriptionAfterScroll("Delete")
|
||||
.performClick()
|
||||
|
||||
// Title
|
||||
composeTestRule
|
||||
.onAllNodesWithText("Delete")
|
||||
.filterToOne(!hasClickAction())
|
||||
.assert(hasAnyAncestor(isDialog()))
|
||||
.assertIsDisplayed()
|
||||
// Description
|
||||
composeTestRule
|
||||
.onAllNodesWithText("Do you really want to delete? This cannot be undone.")
|
||||
.filterToOne(!hasClickAction())
|
||||
.assert(hasAnyAncestor(isDialog()))
|
||||
.assertIsDisplayed()
|
||||
// Cancel Button
|
||||
composeTestRule
|
||||
.onNodeWithText("Cancel")
|
||||
.assert(hasClickAction())
|
||||
.assert(hasAnyAncestor(isDialog()))
|
||||
.assertIsDisplayed()
|
||||
// Delete Button
|
||||
composeTestRule
|
||||
.onAllNodesWithText("Delete")
|
||||
.filterToOne(hasClickAction())
|
||||
.assert(hasAnyAncestor(isDialog()))
|
||||
.assertIsDisplayed()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `on confirm delete click should send DeleteClick`() {
|
||||
val cipherId = "cipherId-1234"
|
||||
mutableStateFlow.update {
|
||||
it.copy(viewState = DEFAULT_CONTENT_WITH_ATTACHMENTS)
|
||||
}
|
||||
|
||||
composeTestRule
|
||||
.onNodeWithContentDescriptionAfterScroll("Delete")
|
||||
.performClick()
|
||||
|
||||
composeTestRule
|
||||
.onAllNodesWithText("Delete")
|
||||
.filterToOne(hasClickAction())
|
||||
.assert(hasAnyAncestor(isDialog()))
|
||||
.performClick()
|
||||
|
||||
verify(exactly = 1) {
|
||||
viewModel.trySendAction(AttachmentsAction.DeleteClick(cipherId))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private val DEFAULT_STATE: AttachmentsState = AttachmentsState(
|
||||
cipherId = "cipherId-1234",
|
||||
viewState = AttachmentsState.ViewState.Loading,
|
||||
)
|
||||
|
||||
private val DEFAULT_CONTENT_WITH_ATTACHMENTS: AttachmentsState.ViewState.Content =
|
||||
AttachmentsState.ViewState.Content(
|
||||
attachments = listOf(
|
||||
AttachmentsState.AttachmentItem(
|
||||
id = "cipherId-1234",
|
||||
title = "cool_file.png",
|
||||
displaySize = "10 MB",
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
+179
@@ -2,13 +2,43 @@ package com.x8bit.bitwarden.ui.vault.feature.attachments
|
||||
|
||||
import androidx.lifecycle.SavedStateHandle
|
||||
import app.cash.turbine.test
|
||||
import com.bitwarden.core.CipherView
|
||||
import com.x8bit.bitwarden.R
|
||||
import com.x8bit.bitwarden.data.platform.repository.model.DataState
|
||||
import com.x8bit.bitwarden.data.vault.datasource.sdk.model.createMockCipherView
|
||||
import com.x8bit.bitwarden.data.vault.repository.VaultRepository
|
||||
import com.x8bit.bitwarden.ui.platform.base.BaseViewModelTest
|
||||
import com.x8bit.bitwarden.ui.platform.base.util.asText
|
||||
import com.x8bit.bitwarden.ui.platform.base.util.concat
|
||||
import com.x8bit.bitwarden.ui.platform.manager.intent.IntentManager
|
||||
import com.x8bit.bitwarden.ui.vault.feature.attachments.util.toViewState
|
||||
import io.mockk.every
|
||||
import io.mockk.mockk
|
||||
import io.mockk.mockkStatic
|
||||
import io.mockk.unmockkStatic
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.junit.jupiter.api.AfterEach
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.BeforeEach
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
class AttachmentsViewModelTest : BaseViewModelTest() {
|
||||
private val mutableVaultItemStateFlow =
|
||||
MutableStateFlow<DataState<CipherView?>>(DataState.Loading)
|
||||
private val vaultRepository: VaultRepository = mockk {
|
||||
every { getVaultItemStateFlow(any()) } returns mutableVaultItemStateFlow
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
fun setup() {
|
||||
mockkStatic(CipherView::toViewState)
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
fun tearDown() {
|
||||
unmockkStatic(CipherView::toViewState)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `initial state should be correct when state is null`() = runTest {
|
||||
@@ -41,9 +71,146 @@ class AttachmentsViewModelTest : BaseViewModelTest() {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `ChooseFileClick should emit ShowToast`() = runTest {
|
||||
val viewModel = createViewModel()
|
||||
viewModel.eventFlow.test {
|
||||
viewModel.trySendAction(AttachmentsAction.ChooseFileClick)
|
||||
assertEquals(AttachmentsEvent.ShowChooserSheet, awaitItem())
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `ChooseFile should emit ShowToast`() = runTest {
|
||||
val fileData = mockk<IntentManager.FileData>()
|
||||
val viewModel = createViewModel()
|
||||
viewModel.eventFlow.test {
|
||||
viewModel.trySendAction(AttachmentsAction.FileChoose(fileData))
|
||||
assertEquals(AttachmentsEvent.ShowToast("Not Yet Implemented".asText()), awaitItem())
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `DeleteClick should emit ShowToast`() = runTest {
|
||||
val attachmentId = "attachmentId-1234"
|
||||
val viewModel = createViewModel()
|
||||
viewModel.eventFlow.test {
|
||||
viewModel.trySendAction(AttachmentsAction.DeleteClick(attachmentId))
|
||||
assertEquals(AttachmentsEvent.ShowToast("Not Yet Implemented".asText()), awaitItem())
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `vaultItemStateFlow Error should update state to Error`() = runTest {
|
||||
mutableVaultItemStateFlow.tryEmit(value = DataState.Error(Throwable("Fail")))
|
||||
|
||||
val viewModel = createViewModel()
|
||||
|
||||
assertEquals(
|
||||
DEFAULT_STATE.copy(
|
||||
viewState = AttachmentsState.ViewState.Error(
|
||||
message = R.string.generic_error_message.asText(),
|
||||
),
|
||||
),
|
||||
viewModel.stateFlow.value,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `vaultItemStateFlow Loaded with data should update state to Content`() = runTest {
|
||||
val cipherView = createMockCipherView(number = 1)
|
||||
every { cipherView.toViewState() } returns DEFAULT_CONTENT_WITH_ATTACHMENTS
|
||||
mutableVaultItemStateFlow.tryEmit(DataState.Loaded(cipherView))
|
||||
|
||||
val viewModel = createViewModel()
|
||||
|
||||
assertEquals(
|
||||
DEFAULT_STATE.copy(viewState = DEFAULT_CONTENT_WITH_ATTACHMENTS),
|
||||
viewModel.stateFlow.value,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `vaultItemStateFlow Loaded without data should update state to Content`() = runTest {
|
||||
mutableVaultItemStateFlow.tryEmit(DataState.Loaded(null))
|
||||
|
||||
val viewModel = createViewModel()
|
||||
|
||||
assertEquals(
|
||||
DEFAULT_STATE.copy(
|
||||
viewState = AttachmentsState.ViewState.Error(
|
||||
message = R.string.generic_error_message.asText(),
|
||||
),
|
||||
),
|
||||
viewModel.stateFlow.value,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `vaultItemStateFlow Loading should update state to Loading`() = runTest {
|
||||
mutableVaultItemStateFlow.tryEmit(value = DataState.Loading)
|
||||
|
||||
val viewModel = createViewModel()
|
||||
|
||||
assertEquals(
|
||||
DEFAULT_STATE.copy(viewState = AttachmentsState.ViewState.Loading),
|
||||
viewModel.stateFlow.value,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `vaultItemStateFlow NoNetwork should update state to Error`() = runTest {
|
||||
mutableVaultItemStateFlow.tryEmit(value = DataState.NoNetwork(null))
|
||||
|
||||
val viewModel = createViewModel()
|
||||
|
||||
assertEquals(
|
||||
DEFAULT_STATE.copy(
|
||||
viewState = AttachmentsState.ViewState.Error(
|
||||
message = R.string.internet_connection_required_title
|
||||
.asText()
|
||||
.concat("\n".asText())
|
||||
.concat(R.string.internet_connection_required_message.asText()),
|
||||
),
|
||||
),
|
||||
viewModel.stateFlow.value,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `vaultItemStateFlow Pending with data should update state to Content`() = runTest {
|
||||
val cipherView = createMockCipherView(number = 1)
|
||||
every { cipherView.toViewState() } returns DEFAULT_CONTENT_WITH_ATTACHMENTS
|
||||
mutableVaultItemStateFlow.tryEmit(DataState.Pending(cipherView))
|
||||
|
||||
val viewModel = createViewModel()
|
||||
|
||||
assertEquals(
|
||||
DEFAULT_STATE.copy(viewState = DEFAULT_CONTENT_WITH_ATTACHMENTS),
|
||||
viewModel.stateFlow.value,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `vaultItemStateFlow Pending without data should update state to Content`() = runTest {
|
||||
mutableVaultItemStateFlow.tryEmit(DataState.Pending(null))
|
||||
|
||||
val viewModel = createViewModel()
|
||||
|
||||
assertEquals(
|
||||
DEFAULT_STATE.copy(
|
||||
viewState = AttachmentsState.ViewState.Error(
|
||||
message = R.string.generic_error_message.asText(),
|
||||
),
|
||||
),
|
||||
viewModel.stateFlow.value,
|
||||
)
|
||||
}
|
||||
|
||||
private fun createViewModel(
|
||||
initialState: AttachmentsState? = null,
|
||||
): AttachmentsViewModel = AttachmentsViewModel(
|
||||
vaultRepo = vaultRepository,
|
||||
savedStateHandle = SavedStateHandle().apply {
|
||||
set("state", initialState)
|
||||
set("cipher_id", initialState?.cipherId ?: "cipherId-1234")
|
||||
@@ -53,4 +220,16 @@ class AttachmentsViewModelTest : BaseViewModelTest() {
|
||||
|
||||
private val DEFAULT_STATE: AttachmentsState = AttachmentsState(
|
||||
cipherId = "cipherId-1234",
|
||||
viewState = AttachmentsState.ViewState.Loading,
|
||||
)
|
||||
|
||||
private val DEFAULT_CONTENT_WITH_ATTACHMENTS: AttachmentsState.ViewState.Content =
|
||||
AttachmentsState.ViewState.Content(
|
||||
attachments = listOf(
|
||||
AttachmentsState.AttachmentItem(
|
||||
id = "cipherId-1234",
|
||||
title = "cool_file.png",
|
||||
displaySize = "10 MB",
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
package com.x8bit.bitwarden.ui.vault.feature.attachments.util
|
||||
|
||||
import com.x8bit.bitwarden.data.vault.datasource.sdk.model.createMockAttachmentView
|
||||
import com.x8bit.bitwarden.data.vault.datasource.sdk.model.createMockCipherView
|
||||
import com.x8bit.bitwarden.ui.vault.feature.attachments.AttachmentsState
|
||||
import org.junit.Test
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
|
||||
class CipherViewExtensionsTest {
|
||||
|
||||
@Test
|
||||
fun `toViewState should return content with items when CipherView has attachments`() {
|
||||
val cipherView = createMockCipherView(number = 1)
|
||||
|
||||
val result = cipherView.toViewState()
|
||||
|
||||
assertEquals(
|
||||
AttachmentsState.ViewState.Content(
|
||||
attachments = listOf(
|
||||
AttachmentsState.AttachmentItem(
|
||||
id = "mockId-1",
|
||||
title = "mockFileName-1",
|
||||
displaySize = "mockSizeName-1",
|
||||
),
|
||||
),
|
||||
),
|
||||
result,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `toViewState should return content without item when CipherView has no attachments`() {
|
||||
val cipherView = createMockCipherView(number = 1).copy(
|
||||
attachments = null,
|
||||
)
|
||||
|
||||
val result = cipherView.toViewState()
|
||||
|
||||
assertEquals(
|
||||
AttachmentsState.ViewState.Content(attachments = emptyList()),
|
||||
result,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `toViewState should return content without items that have a null attachment ID`() {
|
||||
val cipherView = createMockCipherView(number = 1).copy(
|
||||
attachments = listOf(
|
||||
createMockAttachmentView(number = 1).copy(
|
||||
id = null,
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
val result = cipherView.toViewState()
|
||||
|
||||
assertEquals(
|
||||
AttachmentsState.ViewState.Content(attachments = emptyList()),
|
||||
result,
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user