Skip to content

Commit bb6a7af

Browse files
BIT-2442: check type before extracting autofill text (#3394)
1 parent b181d0d commit bb6a7af

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

app/src/main/java/com/x8bit/bitwarden/data/autofill/util/AutofillValueExtensions.kt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,12 @@ fun AutofillValue.extractMonthValue(
2626
/**
2727
* Extract a text value from this [AutofillValue].
2828
*/
29-
fun AutofillValue.extractTextValue(): String? = this
30-
.textValue
31-
.takeIf { it.isNotBlank() }
32-
?.toString()
29+
fun AutofillValue.extractTextValue(): String? =
30+
if (this.isText) {
31+
this
32+
.textValue
33+
.takeIf { it.isNotBlank() }
34+
?.toString()
35+
} else {
36+
null
37+
}

app/src/test/java/com/x8bit/bitwarden/data/autofill/util/AutofillValueExtensionsTest.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ class AutofillValueExtensionsTest {
7979
fun `extractTextValue should return textValue when not blank`() {
8080
// Setup
8181
val autofillValue: AutofillValue = mockk {
82+
every { isText } returns true
8283
every { textValue } returns TEXT_VALUE
8384
}
8485

@@ -93,6 +94,7 @@ class AutofillValueExtensionsTest {
9394
fun `extractTextValue should return null when not blank`() {
9495
// Setup
9596
val autofillValue: AutofillValue = mockk {
97+
every { isText } returns true
9698
every { textValue } returns " "
9799
}
98100

@@ -102,6 +104,20 @@ class AutofillValueExtensionsTest {
102104
// Verify
103105
assertNull(actual)
104106
}
107+
108+
@Test
109+
fun `extractTextValue should return null when not text`() {
110+
// Setup
111+
val autofillValue: AutofillValue = mockk {
112+
every { isText } returns false
113+
}
114+
115+
// Test
116+
val actual = autofillValue.extractTextValue()
117+
118+
// Verify
119+
assertNull(actual)
120+
}
105121
}
106122

107123
private const val LIST_VALUE: Int = 5

0 commit comments

Comments
 (0)