Send autofill selections back to autofill flow (#829)

This commit is contained in:
Brian Yencho
2024-06-20 17:08:07 +01:00
committed by Álison Fernandes
parent b199a67b7d
commit b3fa33a02c
22 changed files with 823 additions and 13 deletions
@@ -3,8 +3,11 @@ package com.x8bit.bitwarden
import android.content.Intent
import androidx.lifecycle.SavedStateHandle
import app.cash.turbine.test
import com.bitwarden.core.CipherView
import com.x8bit.bitwarden.data.auth.repository.AuthRepository
import com.x8bit.bitwarden.data.auth.repository.model.UserState
import com.x8bit.bitwarden.data.autofill.manager.AutofillSelectionManager
import com.x8bit.bitwarden.data.autofill.manager.AutofillSelectionManagerImpl
import com.x8bit.bitwarden.data.autofill.model.AutofillSelectionData
import com.x8bit.bitwarden.data.autofill.util.getAutofillSelectionDataOrNull
import com.x8bit.bitwarden.data.platform.manager.SpecialCircumstanceManagerImpl
@@ -25,6 +28,7 @@ import org.junit.jupiter.api.Test
class MainViewModelTest : BaseViewModelTest() {
private val autofillSelectionManager: AutofillSelectionManager = AutofillSelectionManagerImpl()
private val mutableAppThemeFlow = MutableStateFlow(AppTheme.DEFAULT)
private val mutableUserStateFlow = MutableStateFlow<UserState?>(DEFAULT_USER_STATE)
private val mutableScreenCaptureAllowedFlow = MutableStateFlow(true)
@@ -59,6 +63,22 @@ class MainViewModelTest : BaseViewModelTest() {
)
}
@Test
fun `autofill selection updates should emit CompleteAutofill events`() = runTest {
val viewModel = createViewModel()
val cipherView = mockk<CipherView>()
viewModel.eventFlow.test {
// Ignore initial screen capture event
awaitItem()
autofillSelectionManager.emitAutofillSelection(cipherView = cipherView)
assertEquals(
MainEvent.CompleteAutofill(cipherView = cipherView),
awaitItem(),
)
}
}
@Test
fun `SpecialCircumstance updates should update the SavedStateHandle`() {
createViewModel()
@@ -217,6 +237,7 @@ class MainViewModelTest : BaseViewModelTest() {
private fun createViewModel(
initialSpecialCircumstance: SpecialCircumstance? = null,
) = MainViewModel(
autofillSelectionManager = autofillSelectionManager,
specialCircumstanceManager = specialCircumstanceManager,
settingsRepository = settingsRepository,
intentManager = intentManager,
@@ -0,0 +1,230 @@
package com.x8bit.bitwarden.data.autofill.manager
import android.app.Activity
import android.app.assist.AssistStructure
import android.content.Intent
import android.service.autofill.Dataset
import com.bitwarden.core.CipherView
import com.x8bit.bitwarden.data.autofill.builder.FilledDataBuilder
import com.x8bit.bitwarden.data.autofill.model.AutofillAppInfo
import com.x8bit.bitwarden.data.autofill.model.AutofillRequest
import com.x8bit.bitwarden.data.autofill.model.FilledData
import com.x8bit.bitwarden.data.autofill.model.FilledPartition
import com.x8bit.bitwarden.data.autofill.parser.AutofillParser
import com.x8bit.bitwarden.data.autofill.util.buildDataset
import com.x8bit.bitwarden.data.autofill.util.createAutofillSelectionResultIntent
import com.x8bit.bitwarden.data.autofill.util.getAutofillAssistStructureOrNull
import com.x8bit.bitwarden.data.autofill.util.toAutofillAppInfo
import com.x8bit.bitwarden.data.platform.base.FakeDispatcherManager
import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.every
import io.mockk.just
import io.mockk.mockk
import io.mockk.mockkStatic
import io.mockk.runs
import io.mockk.unmockkStatic
import io.mockk.verify
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
class AutofillCompletionManagerTest {
private val activity: Activity = mockk {
every { finish() } just runs
every { setResult(any()) } just runs
every { setResult(any(), any()) } just runs
}
private val assistStructure: AssistStructure = mockk()
private val autofillAppInfo: AutofillAppInfo = mockk()
private val autofillParser: AutofillParser = mockk()
private val cipherView: CipherView = mockk()
private val dataset: Dataset = mockk()
private val dispatcherManager = FakeDispatcherManager()
private val fillableRequest: AutofillRequest.Fillable = mockk()
private val filledDataBuilder: FilledDataBuilder = mockk()
private val filledPartition: FilledPartition = mockk()
private val mockIntent: Intent = mockk()
private val resultIntent: Intent = mockk()
private val autofillCompletionManager: AutofillCompletionManager =
AutofillCompletionManagerImpl(
autofillParser = autofillParser,
dispatcherManager = dispatcherManager,
filledDataBuilderProvider = { filledDataBuilder },
)
@BeforeEach
fun setUp() {
dispatcherManager.setMain(dispatcherManager.unconfined)
mockkStatic(::createAutofillSelectionResultIntent)
mockkStatic(Activity::toAutofillAppInfo)
mockkStatic(FilledPartition::buildDataset)
mockkStatic(Intent::getAutofillAssistStructureOrNull)
every { activity.toAutofillAppInfo() } returns autofillAppInfo
}
@AfterEach
fun tearDown() {
dispatcherManager.resetMain()
unmockkStatic(::createAutofillSelectionResultIntent)
unmockkStatic(Activity::toAutofillAppInfo)
unmockkStatic(FilledPartition::buildDataset)
unmockkStatic(Intent::getAutofillAssistStructureOrNull)
}
@Suppress("MaxLineLength")
@Test
fun `completeAutofill when there is no Intent present should cancel and finish the Activity`() {
every { activity.intent } returns null
autofillCompletionManager.completeAutofill(
activity = activity,
cipherView = cipherView,
)
verify {
activity.setResult(Activity.RESULT_CANCELED)
activity.finish()
}
verify {
activity.intent
}
}
@Suppress("MaxLineLength")
@Test
fun `completeAutofill when there is no AssistStructure present should cancel and finish the Activity`() {
every { activity.intent } returns mockIntent
every { mockIntent.getAutofillAssistStructureOrNull() } returns null
autofillCompletionManager.completeAutofill(
activity = activity,
cipherView = cipherView,
)
verify {
activity.setResult(Activity.RESULT_CANCELED)
activity.finish()
}
verify {
activity.intent
mockIntent.getAutofillAssistStructureOrNull()
}
}
@Suppress("MaxLineLength")
@Test
fun `completeAutofill when the request is not fillable should cancel and finish the Activity`() {
every { activity.intent } returns mockIntent
every { mockIntent.getAutofillAssistStructureOrNull() } returns assistStructure
every {
autofillParser.parse(
autofillAppInfo = autofillAppInfo,
assistStructure = assistStructure,
)
} returns AutofillRequest.Unfillable
autofillCompletionManager.completeAutofill(
activity = activity,
cipherView = cipherView,
)
verify {
activity.setResult(Activity.RESULT_CANCELED)
activity.finish()
}
verify {
activity.intent
mockIntent.getAutofillAssistStructureOrNull()
autofillParser.parse(
autofillAppInfo = autofillAppInfo,
assistStructure = assistStructure,
)
}
}
@Suppress("MaxLineLength")
@Test
fun `completeAutofill when there are no filled partitions should cancel and finish the Activity`() {
val filledData: FilledData = mockk {
every { filledPartitions } returns emptyList()
}
every { activity.intent } returns mockIntent
every { mockIntent.getAutofillAssistStructureOrNull() } returns assistStructure
every {
autofillParser.parse(
autofillAppInfo = autofillAppInfo,
assistStructure = assistStructure,
)
} returns fillableRequest
coEvery {
filledDataBuilder.build(autofillRequest = fillableRequest)
} returns filledData
autofillCompletionManager.completeAutofill(
activity = activity,
cipherView = cipherView,
)
verify {
activity.setResult(Activity.RESULT_CANCELED)
activity.finish()
}
verify {
activity.intent
mockIntent.getAutofillAssistStructureOrNull()
autofillParser.parse(
autofillAppInfo = autofillAppInfo,
assistStructure = assistStructure,
)
}
coVerify {
filledDataBuilder.build(autofillRequest = fillableRequest)
}
}
@Suppress("MaxLineLength")
@Test
fun `completeAutofill when there is a filled partition should build a dataset, place it in a result Intent, and finish the Activity`() {
val filledData: FilledData = mockk {
every { filledPartitions } returns listOf(filledPartition)
}
every { activity.intent } returns mockIntent
every { mockIntent.getAutofillAssistStructureOrNull() } returns assistStructure
every {
autofillParser.parse(
autofillAppInfo = autofillAppInfo,
assistStructure = assistStructure,
)
} returns fillableRequest
coEvery {
filledDataBuilder.build(autofillRequest = fillableRequest)
} returns filledData
every { filledPartition.buildDataset(autofillAppInfo = autofillAppInfo) } returns dataset
every { createAutofillSelectionResultIntent(dataset = dataset) } returns resultIntent
autofillCompletionManager.completeAutofill(
activity = activity,
cipherView = cipherView,
)
verify {
activity.setResult(Activity.RESULT_OK, resultIntent)
activity.finish()
}
verify {
activity.intent
mockIntent.getAutofillAssistStructureOrNull()
autofillParser.parse(
autofillAppInfo = autofillAppInfo,
assistStructure = assistStructure,
)
filledPartition.buildDataset(autofillAppInfo = autofillAppInfo)
createAutofillSelectionResultIntent(dataset = dataset)
}
coVerify {
filledDataBuilder.build(autofillRequest = fillableRequest)
}
}
}
@@ -0,0 +1,30 @@
package com.x8bit.bitwarden.data.autofill.manager
import app.cash.turbine.test
import com.bitwarden.core.CipherView
import io.mockk.mockk
import kotlinx.coroutines.test.runTest
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
class AutofillSelectionManagerTest {
private val autofillSelectionManager: AutofillSelectionManager = AutofillSelectionManagerImpl()
@Test
fun `autofillSelectionFlow should emit whenever emitAutofillSelection is called`() =
runTest {
autofillSelectionManager.autofillSelectionFlow.test {
expectNoEvents()
val cipherView1 = mockk<CipherView>()
autofillSelectionManager.emitAutofillSelection(cipherView = cipherView1)
assertEquals(cipherView1, awaitItem())
val cipherView2 = mockk<CipherView>()
autofillSelectionManager.emitAutofillSelection(cipherView = cipherView2)
assertEquals(cipherView2, awaitItem())
}
}
}
@@ -90,6 +90,12 @@ class AutofillParserTests {
isInlineAutofillEnabled = false,
)
} returns emptyList()
every {
null.getInlinePresentationSpecs(
autofillAppInfo = autofillAppInfo,
isInlineAutofillEnabled = false,
)
} returns emptyList()
every {
fillRequest.getMaxInlineSuggestionsCount(
autofillAppInfo = autofillAppInfo,
@@ -102,6 +108,12 @@ class AutofillParserTests {
isInlineAutofillEnabled = false,
)
} returns 0
every {
null.getMaxInlineSuggestionsCount(
autofillAppInfo = autofillAppInfo,
isInlineAutofillEnabled = false,
)
} returns 0
every { any<List<ViewNodeTraversalData>>().buildUriOrNull(assistStructure) } returns URI
parser = AutofillParserImpl(
settingsRepository = settingsRepository,
@@ -420,6 +432,57 @@ class AutofillParserTests {
}
}
@Test
fun `parse should return empty inline suggestions when parsing an AssistStructure directly`() {
// Setup
mockIsInlineAutofillEnabled = false
setupAssistStructureWithAllAutofillViewTypes()
val cardAutofillView: AutofillView.Card = AutofillView.Card.ExpirationMonth(
data = AutofillView.Data(
autofillId = cardAutofillId,
isFocused = true,
),
)
val loginAutofillView: AutofillView.Login = AutofillView.Login.Username(
data = AutofillView.Data(
autofillId = loginAutofillId,
isFocused = true,
),
)
val autofillPartition = AutofillPartition.Card(
views = listOf(cardAutofillView),
)
val expected = AutofillRequest.Fillable(
ignoreAutofillIds = emptyList(),
inlinePresentationSpecs = emptyList(),
maxInlineSuggestionsCount = 0,
partition = autofillPartition,
uri = URI,
)
every { cardViewNode.toAutofillView() } returns cardAutofillView
every { loginViewNode.toAutofillView() } returns loginAutofillView
// Test
val actual = parser.parse(
autofillAppInfo = autofillAppInfo,
assistStructure = assistStructure,
)
// Verify
assertEquals(expected, actual)
verify(exactly = 1) {
null.getInlinePresentationSpecs(
autofillAppInfo = autofillAppInfo,
isInlineAutofillEnabled = false,
)
null.getMaxInlineSuggestionsCount(
autofillAppInfo = autofillAppInfo,
isInlineAutofillEnabled = false,
)
any<List<ViewNodeTraversalData>>().buildUriOrNull(assistStructure)
}
}
/**
* Setup [assistStructure] to return window nodes with each [AutofillView] type (card and login)
* so we can test how different window node configurations produce different partitions.
@@ -19,6 +19,22 @@ class FillRequestExtensionsTest {
every { this@mockk.inlineSuggestionsRequest } returns expectedInlineSuggestionsRequest
}
@Test
fun `getInlinePresentationSpecs should return empty list when request is null`() {
// Setup
val autofillAppInfo: AutofillAppInfo = mockk()
val expected: List<InlinePresentationSpec> = emptyList()
// Test
val actual = null.getInlinePresentationSpecs(
autofillAppInfo = autofillAppInfo,
isInlineAutofillEnabled = false,
)
// Verify
assertEquals(expected, actual)
}
@Test
fun `getInlinePresentationSpecs should return empty list when disabled`() {
// Setup
@@ -75,6 +91,22 @@ class FillRequestExtensionsTest {
assertEquals(expected, actual)
}
@Test
fun `getMaxInlineSuggestionsCount should return 0 when request is null`() {
// Setup
val autofillAppInfo: AutofillAppInfo = mockk()
val expected = 0
// Test
val actual = null.getMaxInlineSuggestionsCount(
autofillAppInfo = autofillAppInfo,
isInlineAutofillEnabled = false,
)
// Verify
assertEquals(expected, actual)
}
@Test
fun `getMaxInlineSuggestionsCount should return 0 when disabled`() {
// Setup
@@ -0,0 +1,32 @@
package com.x8bit.bitwarden.data.platform.manager
import app.cash.turbine.test
import com.x8bit.bitwarden.data.platform.manager.model.SpecialCircumstance
import io.mockk.mockk
import kotlinx.coroutines.test.runTest
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertNull
import org.junit.jupiter.api.Test
class SpecialCircumstanceManagerTest {
private val specialCircumstanceManager: SpecialCircumstanceManager =
SpecialCircumstanceManagerImpl()
@Test
fun `specialCircumstanceStateFlow should emit whenever the SpecialCircumstance is updated`() =
runTest {
specialCircumstanceManager.specialCircumstanceStateFlow.test {
assertNull(awaitItem())
val specialCircumstance1 = mockk<SpecialCircumstance>()
specialCircumstanceManager.specialCircumstance = specialCircumstance1
assertEquals(specialCircumstance1, awaitItem())
val specialCircumstance2 = mockk<SpecialCircumstance>()
specialCircumstanceManager.specialCircumstance = specialCircumstance2
assertEquals(specialCircumstance2, awaitItem())
}
}
}
@@ -4,8 +4,11 @@ import android.net.Uri
import androidx.lifecycle.SavedStateHandle
import app.cash.turbine.test
import com.x8bit.bitwarden.R
import com.x8bit.bitwarden.data.autofill.manager.AutofillSelectionManager
import com.x8bit.bitwarden.data.autofill.manager.AutofillSelectionManagerImpl
import com.x8bit.bitwarden.data.platform.manager.SpecialCircumstanceManagerImpl
import com.x8bit.bitwarden.data.platform.manager.clipboard.BitwardenClipboardManager
import com.x8bit.bitwarden.data.platform.manager.model.SpecialCircumstance
import com.x8bit.bitwarden.data.platform.repository.EnvironmentRepository
import com.x8bit.bitwarden.data.platform.repository.SettingsRepository
import com.x8bit.bitwarden.data.platform.repository.model.DataState
@@ -49,6 +52,8 @@ import java.time.ZoneOffset
@Suppress("LargeClass")
class VaultItemListingViewModelTest : BaseViewModelTest() {
private val autofillSelectionManager: AutofillSelectionManager = AutofillSelectionManagerImpl()
private val clock: Clock = Clock.fixed(
Instant.parse("2023-10-27T12:00:00Z"),
ZoneOffset.UTC,
@@ -150,6 +155,35 @@ class VaultItemListingViewModelTest : BaseViewModelTest() {
}
}
@Test
fun `ItemClick for vault item when autofill should post to the AutofillSelectionManager`() =
runTest {
setupMockUri()
val cipherView = createMockCipherView(number = 1)
specialCircumstanceManager.specialCircumstance =
SpecialCircumstance.AutofillSelection(
autofillSelectionData = mockk(),
shouldFinishWhenComplete = true,
)
mutableVaultDataStateFlow.value = DataState.Loaded(
data = VaultData(
cipherViewList = listOf(cipherView),
folderViewList = emptyList(),
collectionViewList = emptyList(),
sendViewList = emptyList(),
),
)
val viewModel = createVaultItemListingViewModel()
autofillSelectionManager.autofillSelectionFlow.test {
viewModel.trySendAction(VaultItemListingsAction.ItemClick(id = "mockId-1"))
assertEquals(
cipherView,
awaitItem(),
)
}
}
@Test
fun `ItemClick for vault item should emit NavigateToVaultItem`() = runTest {
val viewModel = createVaultItemListingViewModel()
@@ -943,6 +977,7 @@ class VaultItemListingViewModelTest : BaseViewModelTest() {
vaultRepository = vaultRepository,
environmentRepository = environmentRepository,
settingsRepository = settingsRepository,
autofillSelectionManager = autofillSelectionManager,
specialCircumstanceManager = specialCircumstanceManager,
)