Move AutofillView data into a wrapper class (#590)

This commit is contained in:
Lucas Kivi 2024-01-12 12:56:00 -06:00 committed by Álison Fernandes
parent c06be2b8de
commit 33b6f994e6
9 changed files with 155 additions and 236 deletions

View file

@ -50,7 +50,7 @@ class FilledDataBuilderImpl : FilledDataBuilder {
val filledItems = autofillViews
.map { autofillView ->
FilledItem(
autofillId = autofillView.autofillId,
autofillId = autofillView.data.autofillId,
)
}
@ -68,7 +68,7 @@ class FilledDataBuilderImpl : FilledDataBuilder {
val filledItems = autofillViews
.map { autofillView ->
FilledItem(
autofillId = autofillView.autofillId,
autofillId = autofillView.data.autofillId,
)
}

View file

@ -6,30 +6,28 @@ import android.view.autofill.AutofillId
* The processed, relevant data from an autofill view node.
*/
sealed class AutofillView {
/**
* The [AutofillId] associated with this view.
*/
abstract val autofillId: AutofillId
/**
* The package id for this view, if there is one. (ex: "com.x8bit.bitwarden")
* The data important to a given [AutofillView].
*
* @param autofillId The [AutofillId] associated with this view.
* @param idPackage The package id for this view, if there is one.
* @param isFocused Whether the view is currently focused.
* @param webDomain The web domain for this view, if there is one. (example: m.facebook.com)
* @param webScheme The web scheme for this view, if there is one. (example: https)
*/
abstract val idPackage: String?
data class Data(
val autofillId: AutofillId,
val idPackage: String?,
val isFocused: Boolean,
val webDomain: String?,
val webScheme: String?,
)
/**
* Whether the view is currently focused.
* The core data that describes this [AutofillView].
*/
abstract val isFocused: Boolean
/**
* The web domain for this view, if there is one. (ex: "m.facebook.com")
*/
abstract val webDomain: String?
/**
* The web scheme for this view, if there is one. (ex: "https")
*/
abstract val webScheme: String?
abstract val data: Data
/**
* A view that corresponds to the card data partition for autofill fields.
@ -40,44 +38,28 @@ sealed class AutofillView {
* The expiration month [AutofillView] for the [Card] data partition.
*/
data class ExpirationMonth(
override val autofillId: AutofillId,
override val idPackage: String?,
override val isFocused: Boolean,
override val webDomain: String?,
override val webScheme: String?,
override val data: Data,
) : Card()
/**
* The expiration year [AutofillView] for the [Card] data partition.
*/
data class ExpirationYear(
override val autofillId: AutofillId,
override val idPackage: String?,
override val isFocused: Boolean,
override val webDomain: String?,
override val webScheme: String?,
override val data: Data,
) : Card()
/**
* The number [AutofillView] for the [Card] data partition.
*/
data class Number(
override val autofillId: AutofillId,
override val idPackage: String?,
override val isFocused: Boolean,
override val webDomain: String?,
override val webScheme: String?,
override val data: Data,
) : Card()
/**
* The security code [AutofillView] for the [Card] data partition.
*/
data class SecurityCode(
override val autofillId: AutofillId,
override val idPackage: String?,
override val isFocused: Boolean,
override val webDomain: String?,
override val webScheme: String?,
override val data: Data,
) : Card()
}
@ -90,33 +72,21 @@ sealed class AutofillView {
* The email address [AutofillView] for the [Login] data partition.
*/
data class EmailAddress(
override val autofillId: AutofillId,
override val idPackage: String?,
override val isFocused: Boolean,
override val webDomain: String?,
override val webScheme: String?,
override val data: Data,
) : Login()
/**
* The password [AutofillView] for the [Login] data partition.
*/
data class Password(
override val autofillId: AutofillId,
override val idPackage: String?,
override val isFocused: Boolean,
override val webDomain: String?,
override val webScheme: String?,
override val data: Data,
) : Login()
/**
* The username [AutofillView] for the [Login] data partition.
*/
data class Username(
override val autofillId: AutofillId,
override val idPackage: String?,
override val isFocused: Boolean,
override val webDomain: String?,
override val webScheme: String?,
override val data: Data,
) : Login()
}
}

View file

@ -24,7 +24,7 @@ class AutofillParserImpl : AutofillParser {
// Find the focused view (or indicate there is no fulfillment to be performed.)
val focusedView = autofillViews
.firstOrNull { it.isFocused }
.firstOrNull { it.data.isFocused }
?: return AutofillRequest.Unfillable
val uri = traversalDataList.buildUriOrNull(

View file

@ -2,7 +2,6 @@ package com.x8bit.bitwarden.data.autofill.util
import android.app.assist.AssistStructure
import android.view.View
import android.view.autofill.AutofillId
import com.x8bit.bitwarden.data.autofill.model.AutofillView
/**
@ -16,96 +15,66 @@ fun AssistStructure.ViewNode.toAutofillView(): AutofillView? = autofillId
autofillHints
?.firstOrNull { SUPPORTED_HINTS.contains(it) }
?.let { supportedHint ->
buildAutofillView(
val autofillViewData = AutofillView.Data(
autofillId = nonNullAutofillId,
idPackage = idPackage,
isFocused = isFocused,
hint = supportedHint,
webDomain = webDomain,
webScheme = webScheme,
)
buildAutofillView(
autofillViewData = autofillViewData,
hint = supportedHint,
)
}
}
/**
* Convert the data into an [AutofillView] if the [hint] is supported.
* Convert [autofillViewData] into an [AutofillView] if the [hint] is supported.
*/
@Suppress("LongMethod", "LongParameterList")
private fun buildAutofillView(
autofillId: AutofillId,
idPackage: String?,
isFocused: Boolean,
autofillViewData: AutofillView.Data,
hint: String,
webDomain: String?,
webScheme: String?,
): AutofillView? = when (hint) {
View.AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_MONTH -> {
AutofillView.Card.ExpirationMonth(
autofillId = autofillId,
idPackage = idPackage,
isFocused = isFocused,
webDomain = webDomain,
webScheme = webScheme,
data = autofillViewData,
)
}
View.AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_YEAR -> {
AutofillView.Card.ExpirationYear(
autofillId = autofillId,
idPackage = idPackage,
isFocused = isFocused,
webDomain = webDomain,
webScheme = webScheme,
data = autofillViewData,
)
}
View.AUTOFILL_HINT_CREDIT_CARD_NUMBER -> {
AutofillView.Card.Number(
autofillId = autofillId,
idPackage = idPackage,
isFocused = isFocused,
webDomain = webDomain,
webScheme = webScheme,
data = autofillViewData,
)
}
View.AUTOFILL_HINT_CREDIT_CARD_SECURITY_CODE -> {
AutofillView.Card.SecurityCode(
autofillId = autofillId,
idPackage = idPackage,
isFocused = isFocused,
webDomain = webDomain,
webScheme = webScheme,
data = autofillViewData,
)
}
View.AUTOFILL_HINT_EMAIL_ADDRESS -> {
AutofillView.Login.EmailAddress(
autofillId = autofillId,
idPackage = idPackage,
isFocused = isFocused,
webDomain = webDomain,
webScheme = webScheme,
data = autofillViewData,
)
}
View.AUTOFILL_HINT_PASSWORD -> {
AutofillView.Login.Password(
autofillId = autofillId,
idPackage = idPackage,
isFocused = isFocused,
webDomain = webDomain,
webScheme = webScheme,
data = autofillViewData,
)
}
View.AUTOFILL_HINT_USERNAME -> {
AutofillView.Login.Username(
autofillId = autofillId,
idPackage = idPackage,
isFocused = isFocused,
webDomain = webDomain,
webScheme = webScheme,
data = autofillViewData,
)
}

View file

@ -81,7 +81,8 @@ private fun AssistStructure.buildPackageNameOrNull(): String? = if (windowNodeCo
*/
private fun List<ViewNodeTraversalData>.buildPackageNameOrNull(): String? =
flatMap { it.autofillViews }
.firstOrNull { !it.idPackage.isNullOrEmpty() }
.firstOrNull { !it.data.idPackage.isNullOrEmpty() }
?.data
?.idPackage
/**
@ -90,10 +91,10 @@ private fun List<ViewNodeTraversalData>.buildPackageNameOrNull(): String? =
*/
private fun List<ViewNodeTraversalData>.buildWebsiteUriOrNull(): String? =
flatMap { it.autofillViews }
.firstOrNull { !it.webDomain.isNullOrEmpty() }
.firstOrNull { !it.data.webDomain.isNullOrEmpty() }
?.let { autofillView ->
val webDomain = requireNotNull(autofillView.webDomain)
val webScheme = autofillView.webScheme.orNullIfBlank() ?: DEFAULT_SCHEME
val webDomain = requireNotNull(autofillView.data.webDomain)
val webScheme = autofillView.data.webScheme.orNullIfBlank() ?: DEFAULT_SCHEME
buildUri(
domain = webDomain,
scheme = webScheme,

View file

@ -16,6 +16,15 @@ import org.junit.jupiter.api.Test
class FilledDataBuilderTest {
private lateinit var filledDataBuilder: FilledDataBuilder
private val autofillId: AutofillId = mockk()
private val autofillViewData = AutofillView.Data(
autofillId = autofillId,
idPackage = null,
isFocused = false,
webDomain = null,
webScheme = null,
)
@BeforeEach
fun setup() {
filledDataBuilder = FilledDataBuilderImpl()
@ -24,13 +33,8 @@ class FilledDataBuilderTest {
@Test
fun `build should return filled data and ignored AutofillIds when Login`() = runTest {
// Setup
val autofillId: AutofillId = mockk()
val autofillView = AutofillView.Login.Username(
autofillId = autofillId,
idPackage = null,
isFocused = false,
webDomain = null,
webScheme = null,
data = autofillViewData,
)
val autofillPartition = AutofillPartition.Login(
views = listOf(autofillView),
@ -70,11 +74,7 @@ class FilledDataBuilderTest {
// Setup
val autofillId: AutofillId = mockk()
val autofillView = AutofillView.Card.Number(
autofillId = autofillId,
idPackage = null,
isFocused = false,
webDomain = null,
webScheme = null,
data = autofillViewData,
)
val autofillPartition = AutofillPartition.Card(
views = listOf(autofillView),

View file

@ -22,6 +22,13 @@ import org.junit.jupiter.api.Test
class AutofillParserTests {
private lateinit var parser: AutofillParser
private val autofillViewData = AutofillView.Data(
autofillId = mockk(),
isFocused = true,
idPackage = null,
webDomain = null,
webScheme = null,
)
private val assistStructure: AssistStructure = mockk()
private val cardAutofillHint = View.AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_YEAR
private val cardAutofillId: AutofillId = mockk()
@ -86,11 +93,10 @@ class AutofillParserTests {
val parentAutofillHint = View.AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_YEAR
val parentAutofillId: AutofillId = mockk()
val parentAutofillView: AutofillView.Card = AutofillView.Card.ExpirationMonth(
autofillId = parentAutofillId,
isFocused = true,
idPackage = null,
webDomain = null,
webScheme = null,
data = autofillViewData.copy(
autofillId = parentAutofillId,
isFocused = true,
),
)
val parentViewNode: AssistStructure.ViewNode = mockk {
every { this@mockk.autofillHints } returns arrayOf(parentAutofillHint)
@ -128,18 +134,16 @@ class AutofillParserTests {
// Setup
setupAssistStructureWithAllAutofillViewTypes()
val cardAutofillView: AutofillView.Card = AutofillView.Card.ExpirationMonth(
autofillId = cardAutofillId,
isFocused = true,
idPackage = null,
webDomain = null,
webScheme = null,
data = autofillViewData.copy(
autofillId = cardAutofillId,
isFocused = true,
),
)
val loginAutofillView: AutofillView.Login = AutofillView.Login.Username(
autofillId = loginAutofillId,
isFocused = false,
idPackage = null,
webDomain = null,
webScheme = null,
data = autofillViewData.copy(
autofillId = loginAutofillId,
isFocused = false,
),
)
val autofillPartition = AutofillPartition.Card(
views = listOf(cardAutofillView),
@ -167,18 +171,16 @@ class AutofillParserTests {
// Setup
setupAssistStructureWithAllAutofillViewTypes()
val cardAutofillView: AutofillView.Card = AutofillView.Card.ExpirationMonth(
autofillId = cardAutofillId,
isFocused = false,
idPackage = null,
webDomain = null,
webScheme = null,
data = autofillViewData.copy(
autofillId = cardAutofillId,
isFocused = false,
),
)
val loginAutofillView: AutofillView.Login = AutofillView.Login.Username(
autofillId = loginAutofillId,
isFocused = true,
idPackage = null,
webDomain = null,
webScheme = null,
data = autofillViewData.copy(
autofillId = loginAutofillId,
isFocused = true,
),
)
val autofillPartition = AutofillPartition.Login(
views = listOf(loginAutofillView),
@ -206,18 +208,16 @@ class AutofillParserTests {
// Setup
setupAssistStructureWithAllAutofillViewTypes()
val cardAutofillView: AutofillView.Card = AutofillView.Card.ExpirationMonth(
autofillId = cardAutofillId,
isFocused = true,
idPackage = null,
webDomain = null,
webScheme = null,
data = autofillViewData.copy(
autofillId = cardAutofillId,
isFocused = true,
),
)
val loginAutofillView: AutofillView.Login = AutofillView.Login.Username(
autofillId = loginAutofillId,
isFocused = true,
idPackage = null,
webDomain = null,
webScheme = null,
data = autofillViewData.copy(
autofillId = loginAutofillId,
isFocused = true,
),
)
val autofillPartition = AutofillPartition.Card(
views = listOf(cardAutofillView),

View file

@ -13,6 +13,13 @@ import org.junit.jupiter.api.Test
class ViewNodeExtensionsTest {
private val expectedAutofillId: AutofillId = mockk()
private val expectedIsFocused = true
private val autofillViewData = AutofillView.Data(
autofillId = expectedAutofillId,
idPackage = ID_PACKAGE,
isFocused = expectedIsFocused,
webDomain = WEB_DOMAIN,
webScheme = WEB_SCHEME,
)
private val viewNode: AssistStructure.ViewNode = mockk {
every { this@mockk.autofillId } returns expectedAutofillId
every { this@mockk.childCount } returns 0
@ -27,11 +34,7 @@ class ViewNodeExtensionsTest {
// Setup
val autofillHint = View.AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_MONTH
val expected = AutofillView.Card.ExpirationMonth(
autofillId = expectedAutofillId,
idPackage = ID_PACKAGE,
isFocused = expectedIsFocused,
webDomain = WEB_DOMAIN,
webScheme = WEB_SCHEME,
data = autofillViewData,
)
every { viewNode.autofillHints } returns arrayOf(autofillHint)
@ -47,11 +50,7 @@ class ViewNodeExtensionsTest {
// Setup
val autofillHint = View.AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_YEAR
val expected = AutofillView.Card.ExpirationYear(
autofillId = expectedAutofillId,
idPackage = ID_PACKAGE,
isFocused = expectedIsFocused,
webDomain = WEB_DOMAIN,
webScheme = WEB_SCHEME,
data = autofillViewData,
)
every { viewNode.autofillHints } returns arrayOf(autofillHint)
@ -67,11 +66,7 @@ class ViewNodeExtensionsTest {
// Setup
val autofillHint = View.AUTOFILL_HINT_CREDIT_CARD_NUMBER
val expected = AutofillView.Card.Number(
autofillId = expectedAutofillId,
idPackage = ID_PACKAGE,
isFocused = expectedIsFocused,
webDomain = WEB_DOMAIN,
webScheme = WEB_SCHEME,
data = autofillViewData,
)
every { viewNode.autofillHints } returns arrayOf(autofillHint)
@ -87,11 +82,7 @@ class ViewNodeExtensionsTest {
// Setup
val autofillHint = View.AUTOFILL_HINT_CREDIT_CARD_SECURITY_CODE
val expected = AutofillView.Card.SecurityCode(
autofillId = expectedAutofillId,
idPackage = ID_PACKAGE,
isFocused = expectedIsFocused,
webDomain = WEB_DOMAIN,
webScheme = WEB_SCHEME,
data = autofillViewData,
)
every { viewNode.autofillHints } returns arrayOf(autofillHint)
@ -107,11 +98,7 @@ class ViewNodeExtensionsTest {
// Setup
val autofillHint = View.AUTOFILL_HINT_EMAIL_ADDRESS
val expected = AutofillView.Login.EmailAddress(
autofillId = expectedAutofillId,
idPackage = ID_PACKAGE,
isFocused = expectedIsFocused,
webDomain = WEB_DOMAIN,
webScheme = WEB_SCHEME,
data = autofillViewData,
)
every { viewNode.autofillHints } returns arrayOf(autofillHint)
@ -127,11 +114,7 @@ class ViewNodeExtensionsTest {
// Setup
val autofillHint = View.AUTOFILL_HINT_PASSWORD
val expected = AutofillView.Login.Password(
autofillId = expectedAutofillId,
idPackage = ID_PACKAGE,
isFocused = expectedIsFocused,
webDomain = WEB_DOMAIN,
webScheme = WEB_SCHEME,
data = autofillViewData,
)
every { viewNode.autofillHints } returns arrayOf(autofillHint)
@ -147,11 +130,7 @@ class ViewNodeExtensionsTest {
// Setup
val autofillHint = View.AUTOFILL_HINT_USERNAME
val expected = AutofillView.Login.Username(
autofillId = expectedAutofillId,
idPackage = ID_PACKAGE,
isFocused = expectedIsFocused,
webDomain = WEB_DOMAIN,
webScheme = WEB_SCHEME,
data = autofillViewData,
)
every { viewNode.autofillHints } returns arrayOf(autofillHint)
@ -181,11 +160,7 @@ class ViewNodeExtensionsTest {
val autofillHintOne = "Shenanigans"
val autofillHintTwo = View.AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_YEAR
val expected = AutofillView.Card.ExpirationYear(
autofillId = expectedAutofillId,
idPackage = ID_PACKAGE,
isFocused = expectedIsFocused,
webDomain = WEB_DOMAIN,
webScheme = WEB_SCHEME,
data = autofillViewData,
)
every { viewNode.autofillHints } returns arrayOf(autofillHintOne, autofillHintTwo)

View file

@ -15,16 +15,22 @@ class ViewNodeTraversalDataExtensionsTest {
every { this@mockk.windowNodeCount } returns 1
every { this@mockk.getWindowNodeAt(0) } returns windowNode
}
private val autofillViewData = AutofillView.Data(
autofillId = mockk(),
idPackage = null,
isFocused = false,
webDomain = null,
webScheme = null,
)
@Test
fun `buildUriOrNull should return URI when contains valid domain and scheme`() {
// Setup
val autofillView = AutofillView.Card.Number(
autofillId = mockk(),
idPackage = null,
isFocused = false,
webDomain = WEB_DOMAIN,
webScheme = WEB_SCHEME,
data = autofillViewData.copy(
webDomain = WEB_DOMAIN,
webScheme = WEB_SCHEME,
),
)
val viewNodeTraversalData = ViewNodeTraversalData(
autofillViews = listOf(
@ -47,11 +53,10 @@ class ViewNodeTraversalDataExtensionsTest {
fun `buildUriOrNull should return URI with default scheme when domain valid and scheme null`() {
// Setup
val autofillView = AutofillView.Card.Number(
autofillId = mockk(),
idPackage = null,
isFocused = false,
webDomain = WEB_DOMAIN,
webScheme = null,
data = autofillViewData.copy(
webDomain = WEB_DOMAIN,
webScheme = null,
),
)
val viewNodeTraversalData = ViewNodeTraversalData(
autofillViews = listOf(
@ -75,11 +80,10 @@ class ViewNodeTraversalDataExtensionsTest {
fun `buildUriOrNull should return URI with default scheme when domain valid and scheme empty`() {
// Setup
val autofillView = AutofillView.Card.Number(
autofillId = mockk(),
idPackage = null,
isFocused = false,
webDomain = WEB_DOMAIN,
webScheme = "",
data = autofillViewData.copy(
webDomain = WEB_DOMAIN,
webScheme = "",
),
)
val viewNodeTraversalData = ViewNodeTraversalData(
autofillViews = listOf(
@ -102,11 +106,11 @@ class ViewNodeTraversalDataExtensionsTest {
fun `buildUriOrNull should return idPackage URI when domain is null`() {
// Setup
val autofillView = AutofillView.Card.Number(
autofillId = mockk(),
idPackage = ID_PACKAGE,
isFocused = false,
webDomain = null,
webScheme = null,
data = autofillViewData.copy(
idPackage = ID_PACKAGE,
webDomain = null,
webScheme = null,
),
)
val viewNodeTraversalData = ViewNodeTraversalData(
autofillViews = listOf(
@ -129,11 +133,11 @@ class ViewNodeTraversalDataExtensionsTest {
fun `buildUriOrNull should return idPackage URI when domain is empty`() {
// Setup
val autofillView = AutofillView.Card.Number(
autofillId = mockk(),
idPackage = ID_PACKAGE,
isFocused = false,
webDomain = null,
webScheme = null,
data = autofillViewData.copy(
idPackage = ID_PACKAGE,
webDomain = "",
webScheme = "",
),
)
val viewNodeTraversalData = ViewNodeTraversalData(
autofillViews = listOf(
@ -156,11 +160,11 @@ class ViewNodeTraversalDataExtensionsTest {
fun `buildUriOrNull should return title URI when domain and idPackage are null`() {
// Setup
val autofillView = AutofillView.Card.Number(
autofillId = mockk(),
idPackage = null,
isFocused = false,
webDomain = null,
webScheme = null,
data = autofillViewData.copy(
idPackage = null,
webDomain = null,
webScheme = null,
),
)
val viewNodeTraversalData = ViewNodeTraversalData(
autofillViews = listOf(
@ -184,11 +188,11 @@ class ViewNodeTraversalDataExtensionsTest {
fun `buildUriOrNull should return title URI when domain and idPackage are empty`() {
// Setup
val autofillView = AutofillView.Card.Number(
autofillId = mockk(),
idPackage = "",
isFocused = false,
webDomain = "",
webScheme = null,
data = autofillViewData.copy(
idPackage = "",
webDomain = "",
webScheme = null,
),
)
val viewNodeTraversalData = ViewNodeTraversalData(
autofillViews = listOf(
@ -212,11 +216,11 @@ class ViewNodeTraversalDataExtensionsTest {
fun `buildUriOrNull should return null when title, domain, and idPackage are null`() {
// Setup
val autofillView = AutofillView.Card.Number(
autofillId = mockk(),
idPackage = null,
isFocused = false,
webDomain = null,
webScheme = null,
data = autofillViewData.copy(
idPackage = null,
webDomain = null,
webScheme = null,
),
)
val viewNodeTraversalData = ViewNodeTraversalData(
autofillViews = listOf(
@ -239,11 +243,11 @@ class ViewNodeTraversalDataExtensionsTest {
fun `buildUriOrNull should return null when title, domain, and idPackage are empty`() {
// Setup
val autofillView = AutofillView.Card.Number(
autofillId = mockk(),
idPackage = "",
isFocused = false,
webDomain = "",
webScheme = null,
data = autofillViewData.copy(
idPackage = "",
webDomain = "",
webScheme = null,
),
)
val viewNodeTraversalData = ViewNodeTraversalData(
autofillViews = listOf(