From b3e4d3807c3da623f074028a5e820bf6ad54aa9f Mon Sep 17 00:00:00 2001 From: Ramsey Smith <142836716+ramsey-livefront@users.noreply.github.com> Date: Tue, 12 Mar 2024 11:18:16 -0600 Subject: [PATCH] BIT-2022: Autofill crash (#1126) --- .../data/autofill/builder/FilledDataBuilderImpl.kt | 4 ++-- .../data/autofill/model/AutofillRequest.kt | 2 +- .../data/autofill/util/FillRequestExtensions.kt | 13 +++++++------ .../autofill/util/FillRequestExtensionsTest.kt | 14 +++++++++----- 4 files changed, 19 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/com/x8bit/bitwarden/data/autofill/builder/FilledDataBuilderImpl.kt b/app/src/main/java/com/x8bit/bitwarden/data/autofill/builder/FilledDataBuilderImpl.kt index e26d813da..0059a740b 100644 --- a/app/src/main/java/com/x8bit/bitwarden/data/autofill/builder/FilledDataBuilderImpl.kt +++ b/app/src/main/java/com/x8bit/bitwarden/data/autofill/builder/FilledDataBuilderImpl.kt @@ -31,7 +31,7 @@ class FilledDataBuilderImpl( // Use getOrLastOrNull so if the list has run dry take the last spec. autofillRequest .inlinePresentationSpecs - .getOrLastOrNull(inlineSuggestionsAdded) + ?.getOrLastOrNull(inlineSuggestionsAdded) } else { null } @@ -73,7 +73,7 @@ class FilledDataBuilderImpl( // Use getOrLastOrNull so if the list has run dry take the last spec. val vaultItemInlinePresentationSpec = autofillRequest .inlinePresentationSpecs - .getOrLastOrNull(inlineSuggestionsAdded) + ?.getOrLastOrNull(inlineSuggestionsAdded) return FilledData( filledPartitions = filledPartitions, diff --git a/app/src/main/java/com/x8bit/bitwarden/data/autofill/model/AutofillRequest.kt b/app/src/main/java/com/x8bit/bitwarden/data/autofill/model/AutofillRequest.kt index ac8efa47f..e4c4bc3a6 100644 --- a/app/src/main/java/com/x8bit/bitwarden/data/autofill/model/AutofillRequest.kt +++ b/app/src/main/java/com/x8bit/bitwarden/data/autofill/model/AutofillRequest.kt @@ -13,7 +13,7 @@ sealed class AutofillRequest { */ data class Fillable( val ignoreAutofillIds: List, - val inlinePresentationSpecs: List, + val inlinePresentationSpecs: List?, val maxInlineSuggestionsCount: Int, val packageName: String?, val partition: AutofillPartition, diff --git a/app/src/main/java/com/x8bit/bitwarden/data/autofill/util/FillRequestExtensions.kt b/app/src/main/java/com/x8bit/bitwarden/data/autofill/util/FillRequestExtensions.kt index e230fd450..02f554f08 100644 --- a/app/src/main/java/com/x8bit/bitwarden/data/autofill/util/FillRequestExtensions.kt +++ b/app/src/main/java/com/x8bit/bitwarden/data/autofill/util/FillRequestExtensions.kt @@ -13,12 +13,13 @@ import com.x8bit.bitwarden.data.autofill.model.AutofillAppInfo fun FillRequest?.getInlinePresentationSpecs( autofillAppInfo: AutofillAppInfo, isInlineAutofillEnabled: Boolean, -): List = - if (this != null && - isInlineAutofillEnabled && - autofillAppInfo.sdkInt >= Build.VERSION_CODES.R - ) { - inlineSuggestionsRequest?.inlinePresentationSpecs ?: emptyList() +): List? = + if (autofillAppInfo.sdkInt < Build.VERSION_CODES.R) { + // When SDK version is bellow 30, InlinePresentationSpec is not available and null + // must be returned. + null + } else if (isInlineAutofillEnabled) { + this?.inlineSuggestionsRequest?.inlinePresentationSpecs.orEmpty() } else { emptyList() } diff --git a/app/src/test/java/com/x8bit/bitwarden/data/autofill/util/FillRequestExtensionsTest.kt b/app/src/test/java/com/x8bit/bitwarden/data/autofill/util/FillRequestExtensionsTest.kt index 34274634f..6f37e63f5 100644 --- a/app/src/test/java/com/x8bit/bitwarden/data/autofill/util/FillRequestExtensionsTest.kt +++ b/app/src/test/java/com/x8bit/bitwarden/data/autofill/util/FillRequestExtensionsTest.kt @@ -7,6 +7,7 @@ import com.x8bit.bitwarden.data.autofill.model.AutofillAppInfo 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 FillRequestExtensionsTest { @@ -22,7 +23,9 @@ class FillRequestExtensionsTest { @Test fun `getInlinePresentationSpecs should return empty list when request is null`() { // Setup - val autofillAppInfo: AutofillAppInfo = mockk() + val autofillAppInfo: AutofillAppInfo = mockk { + every { sdkInt } returns 30 + } val expected: List = emptyList() // Test @@ -38,7 +41,9 @@ class FillRequestExtensionsTest { @Test fun `getInlinePresentationSpecs should return empty list when disabled`() { // Setup - val autofillAppInfo: AutofillAppInfo = mockk() + val autofillAppInfo: AutofillAppInfo = mockk { + every { sdkInt } returns 30 + } val expected: List = emptyList() // Test @@ -52,14 +57,13 @@ class FillRequestExtensionsTest { } @Test - fun `getInlinePresentationSpecs should return empty list when enabled pre-R`() { + fun `getInlinePresentationSpecs should return null when enabled pre-R`() { // Setup val autofillAppInfo = AutofillAppInfo( context = mockk(), packageName = "com.x8bit.bitwarden", sdkInt = 17, ) - val expected: List = emptyList() // Test val actual = fillRequest.getInlinePresentationSpecs( @@ -68,7 +72,7 @@ class FillRequestExtensionsTest { ) // Verify - assertEquals(expected, actual) + assertNull(actual) } @Test