BIT-1316: Add compatibility for browser apps (#796)

This commit is contained in:
Lucas Kivi
2024-06-20 17:08:07 +01:00
committed by Álison Fernandes
parent 0818638273
commit f2d90dda55
14 changed files with 715 additions and 284 deletions
@@ -111,10 +111,7 @@ class FillResponseBuilderTest {
AutofillView.Login.Username(
data = AutofillView.Data(
autofillId = mockk(),
idPackage = null,
isFocused = true,
webDomain = null,
webScheme = null,
),
),
),
@@ -35,10 +35,7 @@ class FilledDataBuilderTest {
private val autofillId: AutofillId = mockk()
private val autofillViewData = AutofillView.Data(
autofillId = autofillId,
idPackage = null,
isFocused = false,
webDomain = null,
webScheme = null,
)
@BeforeEach
@@ -15,6 +15,7 @@ import com.x8bit.bitwarden.data.autofill.util.buildUriOrNull
import com.x8bit.bitwarden.data.autofill.util.getInlinePresentationSpecs
import com.x8bit.bitwarden.data.autofill.util.getMaxInlineSuggestionsCount
import com.x8bit.bitwarden.data.autofill.util.toAutofillView
import com.x8bit.bitwarden.data.autofill.util.website
import com.x8bit.bitwarden.data.platform.repository.SettingsRepository
import io.mockk.every
import io.mockk.mockk
@@ -30,13 +31,6 @@ class AutofillParserTests {
private lateinit var parser: AutofillParser
private val autofillAppInfo: AutofillAppInfo = mockk()
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()
@@ -44,6 +38,7 @@ class AutofillParserTests {
every { this@mockk.autofillHints } returns arrayOf(cardAutofillHint)
every { this@mockk.autofillId } returns cardAutofillId
every { this@mockk.childCount } returns 0
every { this@mockk.idPackage } returns ID_PACKAGE
}
private val loginAutofillHint = View.AUTOFILL_HINT_USERNAME
private val loginAutofillId: AutofillId = mockk()
@@ -51,6 +46,7 @@ class AutofillParserTests {
every { this@mockk.autofillHints } returns arrayOf(loginAutofillHint)
every { this@mockk.autofillId } returns loginAutofillId
every { this@mockk.childCount } returns 0
every { this@mockk.idPackage } returns ID_PACKAGE
}
private val cardWindowNode: AssistStructure.WindowNode = mockk {
every { this@mockk.rootViewNode } returns cardViewNode
@@ -74,11 +70,14 @@ class AutofillParserTests {
@BeforeEach
fun setup() {
mockkStatic(AssistStructure.ViewNode::toAutofillView)
mockkStatic(AssistStructure.ViewNode::website)
mockkStatic(
FillRequest::getMaxInlineSuggestionsCount,
FillRequest::getInlinePresentationSpecs,
)
mockkStatic(List<ViewNodeTraversalData>::buildUriOrNull)
every { cardViewNode.website } returns WEBSITE
every { loginViewNode.website } returns WEBSITE
every {
fillRequest.getInlinePresentationSpecs(
autofillAppInfo = autofillAppInfo,
@@ -112,6 +111,7 @@ class AutofillParserTests {
@AfterEach
fun teardown() {
unmockkStatic(AssistStructure.ViewNode::toAutofillView)
unmockkStatic(AssistStructure.ViewNode::website)
unmockkStatic(
FillRequest::getMaxInlineSuggestionsCount,
FillRequest::getInlinePresentationSpecs,
@@ -160,13 +160,15 @@ class AutofillParserTests {
every { this@mockk.autofillHints } returns arrayOf(childAutofillHint)
every { this@mockk.autofillId } returns childAutofillId
every { this@mockk.childCount } returns 0
every { this@mockk.idPackage } returns null
every { this@mockk.isFocused } returns false
every { this@mockk.toAutofillView() } returns null
every { this@mockk.website } returns null
}
val parentAutofillHint = View.AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_YEAR
val parentAutofillId: AutofillId = mockk()
val parentAutofillView: AutofillView.Card = AutofillView.Card.ExpirationMonth(
data = autofillViewData.copy(
data = AutofillView.Data(
autofillId = parentAutofillId,
isFocused = true,
),
@@ -174,9 +176,11 @@ class AutofillParserTests {
val parentViewNode: AssistStructure.ViewNode = mockk {
every { this@mockk.autofillHints } returns arrayOf(parentAutofillHint)
every { this@mockk.autofillId } returns parentAutofillId
every { this@mockk.idPackage } returns null
every { this@mockk.toAutofillView() } returns parentAutofillView
every { this@mockk.childCount } returns 1
every { this@mockk.getChildAt(0) } returns childViewNode
every { this@mockk.website } returns null
}
val windowNode: AssistStructure.WindowNode = mockk {
every { this@mockk.rootViewNode } returns parentViewNode
@@ -220,13 +224,13 @@ class AutofillParserTests {
// Setup
setupAssistStructureWithAllAutofillViewTypes()
val cardAutofillView: AutofillView.Card = AutofillView.Card.ExpirationMonth(
data = autofillViewData.copy(
data = AutofillView.Data(
autofillId = cardAutofillId,
isFocused = true,
),
)
val loginAutofillView: AutofillView.Login = AutofillView.Login.Username(
data = autofillViewData.copy(
data = AutofillView.Data(
autofillId = loginAutofillId,
isFocused = false,
),
@@ -270,13 +274,13 @@ class AutofillParserTests {
// Setup
setupAssistStructureWithAllAutofillViewTypes()
val cardAutofillView: AutofillView.Card = AutofillView.Card.ExpirationMonth(
data = autofillViewData.copy(
data = AutofillView.Data(
autofillId = cardAutofillId,
isFocused = false,
),
)
val loginAutofillView: AutofillView.Login = AutofillView.Login.Username(
data = autofillViewData.copy(
data = AutofillView.Data(
autofillId = loginAutofillId,
isFocused = true,
),
@@ -320,13 +324,13 @@ class AutofillParserTests {
// Setup
setupAssistStructureWithAllAutofillViewTypes()
val cardAutofillView: AutofillView.Card = AutofillView.Card.ExpirationMonth(
data = autofillViewData.copy(
data = AutofillView.Data(
autofillId = cardAutofillId,
isFocused = true,
),
)
val loginAutofillView: AutofillView.Login = AutofillView.Login.Username(
data = autofillViewData.copy(
data = AutofillView.Data(
autofillId = loginAutofillId,
isFocused = true,
),
@@ -371,13 +375,13 @@ class AutofillParserTests {
mockIsInlineAutofillEnabled = false
setupAssistStructureWithAllAutofillViewTypes()
val cardAutofillView: AutofillView.Card = AutofillView.Card.ExpirationMonth(
data = autofillViewData.copy(
data = AutofillView.Data(
autofillId = cardAutofillId,
isFocused = true,
),
)
val loginAutofillView: AutofillView.Login = AutofillView.Login.Username(
data = autofillViewData.copy(
data = AutofillView.Data(
autofillId = loginAutofillId,
isFocused = true,
),
@@ -427,5 +431,7 @@ class AutofillParserTests {
}
}
private const val ID_PACKAGE: String = "com.x8bit.bitwarden"
private const val MAX_INLINE_SUGGESTION_COUNT: Int = 42
private const val URI: String = "androidapp://com.x8bit.bitwarden"
private const val WEBSITE: String = "https://www.google.com"
@@ -19,10 +19,7 @@ class AutofillViewExtensionsTest {
private val autofillValue: AutofillValue = mockk()
private val autofillViewData = AutofillView.Data(
autofillId = autofillId,
idPackage = null,
isFocused = false,
webDomain = null,
webScheme = null,
)
@BeforeEach
@@ -69,10 +69,7 @@ class FilledDataExtensionsTest {
AutofillView.Login.Username(
data = AutofillView.Data(
autofillId = autofillId,
idPackage = null,
isFocused = true,
webDomain = null,
webScheme = null,
),
),
),
@@ -22,20 +22,14 @@ class ViewNodeExtensionsTest {
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
every { this@mockk.inputType } returns 1
every { this@mockk.idPackage } returns ID_PACKAGE
every { this@mockk.isFocused } returns expectedIsFocused
every { this@mockk.webDomain } returns WEB_DOMAIN
every { this@mockk.webScheme } returns WEB_SCHEME
}
@BeforeEach
@@ -375,6 +369,80 @@ class ViewNodeExtensionsTest {
}
}
@Test
fun `website should return URI if domain and scheme are valid`() {
// Setup
val webDomain = "www.google.com"
val webScheme = "http"
val expected = "http://www.google.com"
every { viewNode.webDomain } returns webDomain
every { viewNode.webScheme } returns webScheme
// Test
val actual = viewNode.website
// Verify
assertEquals(expected, actual)
}
@Test
fun `website should return URI with default scheme if domain is valid and scheme is null`() {
// Setup
val webDomain = "www.google.com"
val webScheme = null
val expected = "https://www.google.com"
every { viewNode.webDomain } returns webDomain
every { viewNode.webScheme } returns webScheme
// Test
val actual = viewNode.website
// Verify
assertEquals(expected, actual)
}
@Test
fun `website should return URI with default scheme if domain is valid and scheme is blank`() {
// Setup
val webDomain = "www.google.com"
val webScheme = " "
val expected = "https://www.google.com"
every { viewNode.webDomain } returns webDomain
every { viewNode.webScheme } returns webScheme
// Test
val actual = viewNode.website
// Verify
assertEquals(expected, actual)
}
@Test
fun `website should return null when domain is null`() {
// Setup
val webDomain = null
every { viewNode.webDomain } returns webDomain
// Test
val actual = viewNode.website
// Verify
assertNull(actual)
}
@Test
fun `website should return null when domain is blank`() {
// Setup
val webDomain = " "
every { viewNode.webDomain } returns webDomain
// Test
val actual = viewNode.website
// Verify
assertNull(actual)
}
/**
* Set up [viewNode] to be an input field but not supported.
*/
@@ -391,9 +459,6 @@ class ViewNodeExtensionsTest {
}
private const val ANDROID_EDIT_TEXT_CLASS_NAME: String = "android.widget.EditText"
private const val ID_PACKAGE: String = "ID_PACKAGE"
private const val WEB_DOMAIN: String = "WEB_DOMAIN"
private const val WEB_SCHEME: String = "WEB_SCHEME"
private val IGNORED_RAW_HINTS: List<String> = listOf(
"search",
"find",
@@ -6,7 +6,6 @@ import com.x8bit.bitwarden.data.autofill.model.ViewNodeTraversalData
import io.mockk.every
import io.mockk.mockk
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertNull
import org.junit.jupiter.api.Test
class ViewNodeTraversalDataExtensionsTest {
@@ -17,28 +16,18 @@ class ViewNodeTraversalDataExtensionsTest {
}
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`() {
fun `buildUriOrNull should return website URI when present`() {
// Setup
val autofillView = AutofillView.Card.Number(
data = autofillViewData.copy(
webDomain = WEB_DOMAIN,
webScheme = WEB_SCHEME,
),
)
val viewNodeTraversalData = ViewNodeTraversalData(
autofillViews = listOf(
autofillView,
),
autofillViews = emptyList(),
idPackage = null,
ignoreAutofillIds = emptyList(),
website = WEBSITE,
)
val expected = "$WEB_SCHEME://$WEB_DOMAIN"
// Test
val actual = listOf(viewNodeTraversalData).buildUriOrNull(
@@ -46,77 +35,17 @@ class ViewNodeTraversalDataExtensionsTest {
)
// Verify
assertEquals(expected, actual)
assertEquals(WEBSITE, actual)
}
@Test
fun `buildUriOrNull should return URI with default scheme when domain valid and scheme null`() {
fun `buildUriOrNull should return idPackage URI when WEBSITE is null`() {
// Setup
val autofillView = AutofillView.Card.Number(
data = autofillViewData.copy(
webDomain = WEB_DOMAIN,
webScheme = null,
),
)
val viewNodeTraversalData = ViewNodeTraversalData(
autofillViews = listOf(
autofillView,
),
ignoreAutofillIds = emptyList(),
)
val expected = "https://$WEB_DOMAIN"
// Test
val actual = listOf(viewNodeTraversalData).buildUriOrNull(
assistStructure = assistStructure,
)
// Verify
assertEquals(expected, actual)
}
@Suppress("MaxLineLength")
@Test
fun `buildUriOrNull should return URI with default scheme when domain valid and scheme empty`() {
// Setup
val autofillView = AutofillView.Card.Number(
data = autofillViewData.copy(
webDomain = WEB_DOMAIN,
webScheme = "",
),
)
val viewNodeTraversalData = ViewNodeTraversalData(
autofillViews = listOf(
autofillView,
),
ignoreAutofillIds = emptyList(),
)
val expected = "https://$WEB_DOMAIN"
// Test
val actual = listOf(viewNodeTraversalData).buildUriOrNull(
assistStructure = assistStructure,
)
// Verify
assertEquals(expected, actual)
}
@Test
fun `buildUriOrNull should return idPackage URI when domain is null`() {
// Setup
val autofillView = AutofillView.Card.Number(
data = autofillViewData.copy(
idPackage = ID_PACKAGE,
webDomain = null,
webScheme = null,
),
)
val viewNodeTraversalData = ViewNodeTraversalData(
autofillViews = listOf(
autofillView,
),
autofillViews = emptyList(),
idPackage = ID_PACKAGE,
ignoreAutofillIds = emptyList(),
website = null,
)
val expected = "androidapp://$ID_PACKAGE"
@@ -130,47 +59,13 @@ class ViewNodeTraversalDataExtensionsTest {
}
@Test
fun `buildUriOrNull should return idPackage URI when domain is empty`() {
fun `buildUriOrNull should return title URI when website and idPackage are null`() {
// Setup
val autofillView = AutofillView.Card.Number(
data = autofillViewData.copy(
idPackage = ID_PACKAGE,
webDomain = "",
webScheme = "",
),
)
val viewNodeTraversalData = ViewNodeTraversalData(
autofillViews = listOf(
autofillView,
),
ignoreAutofillIds = emptyList(),
)
val expected = "androidapp://$ID_PACKAGE"
// Test
val actual = listOf(viewNodeTraversalData).buildUriOrNull(
assistStructure = assistStructure,
)
// Verify
assertEquals(expected, actual)
}
@Test
fun `buildUriOrNull should return title URI when domain and idPackage are null`() {
// Setup
val autofillView = AutofillView.Card.Number(
data = autofillViewData.copy(
idPackage = null,
webDomain = null,
webScheme = null,
),
)
val viewNodeTraversalData = ViewNodeTraversalData(
autofillViews = listOf(
autofillView,
),
autofillViews = emptyList(),
idPackage = null,
ignoreAutofillIds = emptyList(),
website = null,
)
val expected = "androidapp://com.x8bit.bitwarden"
every { windowNode.title } returns "com.x8bit.bitwarden/path.deeper.into.app"
@@ -184,91 +79,8 @@ class ViewNodeTraversalDataExtensionsTest {
assertEquals(expected, actual)
}
@Test
fun `buildUriOrNull should return title URI when domain and idPackage are empty`() {
// Setup
val autofillView = AutofillView.Card.Number(
data = autofillViewData.copy(
idPackage = "",
webDomain = "",
webScheme = null,
),
)
val viewNodeTraversalData = ViewNodeTraversalData(
autofillViews = listOf(
autofillView,
),
ignoreAutofillIds = emptyList(),
)
val expected = "androidapp://com.x8bit.bitwarden"
every { windowNode.title } returns "com.x8bit.bitwarden/path.deeper.into.app"
// Test
val actual = listOf(viewNodeTraversalData).buildUriOrNull(
assistStructure = assistStructure,
)
// Verify
assertEquals(expected, actual)
}
@Test
fun `buildUriOrNull should return null when title, domain, and idPackage are null`() {
// Setup
val autofillView = AutofillView.Card.Number(
data = autofillViewData.copy(
idPackage = null,
webDomain = null,
webScheme = null,
),
)
val viewNodeTraversalData = ViewNodeTraversalData(
autofillViews = listOf(
autofillView,
),
ignoreAutofillIds = emptyList(),
)
every { windowNode.title } returns null
// Test
val actual = listOf(viewNodeTraversalData).buildUriOrNull(
assistStructure = assistStructure,
)
// Verify
assertNull(actual)
}
@Test
fun `buildUriOrNull should return null when title, domain, and idPackage are empty`() {
// Setup
val autofillView = AutofillView.Card.Number(
data = autofillViewData.copy(
idPackage = "",
webDomain = "",
webScheme = null,
),
)
val viewNodeTraversalData = ViewNodeTraversalData(
autofillViews = listOf(
autofillView,
),
ignoreAutofillIds = emptyList(),
)
every { windowNode.title } returns ""
// Test
val actual = listOf(viewNodeTraversalData).buildUriOrNull(
assistStructure = assistStructure,
)
// Verify
assertNull(actual)
}
companion object {
private const val ID_PACKAGE: String = "com.x8bit.bitwarden"
private const val WEB_DOMAIN: String = "www.google.com"
private const val WEB_SCHEME: String = "https"
private const val WEBSITE: String = "https://www.google.com"
}
}