Skip to content

Commit

Permalink
PM-14411: Autofill logic to work better with QuickTile (#4300)
Browse files Browse the repository at this point in the history
  • Loading branch information
david-livefront authored Nov 13, 2024
1 parent 911c9e4 commit 0967234
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ import android.view.accessibility.AccessibilityNodeInfo
data class FillableFields(
val usernameField: AccessibilityNodeInfo?,
val passwordFields: List<AccessibilityNodeInfo>,
)
) {
val hasFields: Boolean = usernameField != null && passwordFields.isNotEmpty()
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,10 @@ class BitwardenAccessibilityProcessorImpl(
if (!powerManager.isInteractive) return
// We skip if the system package
if (eventNode.isSystemPackage) return
// We skip any package that is a launcher or unsupported
if (eventNode.shouldSkipPackage ||
launcherPackageNameManager.launcherPackages.any { it == eventNode.packageName }
) {
// Clear the action since this event needs to be ignored completely
accessibilityAutofillManager.accessibilityAction = null
// We skip any package that is unsupported
if (eventNode.shouldSkipPackage) return
// We skip any package that is a launcher
if (launcherPackageNameManager.launcherPackages.any { it == eventNode.packageName }) {
return
}

Expand All @@ -65,6 +63,11 @@ class BitwardenAccessibilityProcessorImpl(
private fun handleAttemptParseUri(rootNode: AccessibilityNodeInfo) {
accessibilityParser
.parseForUriOrPackageName(rootNode = rootNode)
?.takeIf {
accessibilityParser
.parseForFillableFields(rootNode = rootNode, uri = it)
.hasFields
}
?.let { uri ->
context.startActivity(
createAutofillSelectionIntent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import com.x8bit.bitwarden.data.platform.annotation.OmitFromCoverage
*/
@OmitFromCoverage
class ExitManagerImpl(
val activity: Activity,
private val activity: Activity,
) : ExitManager {
override fun exitApplication() {
activity.finish()
activity.finishAndRemoveTask()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -850,7 +850,7 @@ class VaultItemListingViewModel @Inject constructor(

private fun handleBackClick() {
sendEvent(
event = if (state.isTotp) {
event = if (state.isTotp || state.isAutofill) {
VaultItemListingEvent.ExitApp
} else {
VaultItemListingEvent.NavigateBack
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,13 @@ class BitwardenAccessibilityProcessorTest {
every { source } returns node
}
every { powerManager.isInteractive } returns true
every { accessibilityAutofillManager.accessibilityAction = null } just runs

bitwardenAccessibilityProcessor.processAccessibilityEvent(event = event) { null }

verify(exactly = 1) {
powerManager.isInteractive
node.isSystemPackage
node.shouldSkipPackage
accessibilityAutofillManager.accessibilityAction = null
}
}

Expand All @@ -154,7 +152,6 @@ class BitwardenAccessibilityProcessorTest {
}
every { launcherPackageNameManager.launcherPackages } returns listOf(testPackageName)
every { powerManager.isInteractive } returns true
every { accessibilityAutofillManager.accessibilityAction = null } just runs

bitwardenAccessibilityProcessor.processAccessibilityEvent(event = event) { null }

Expand All @@ -163,7 +160,6 @@ class BitwardenAccessibilityProcessorTest {
node.isSystemPackage
node.shouldSkipPackage
launcherPackageNameManager.launcherPackages
accessibilityAutofillManager.accessibilityAction = null
}
}

Expand Down Expand Up @@ -266,6 +262,59 @@ class BitwardenAccessibilityProcessorTest {
}
}

@Suppress("MaxLineLength")
@Test
fun `processAccessibilityEvent with AttemptParseUri and a valid uri but no fields to fill display toast`() {
val testPackageName = "com.android.chrome"
val rootNode = mockk<AccessibilityNodeInfo> {
every { packageName } returns testPackageName
}
val node = mockk<AccessibilityNodeInfo> {
every { isSystemPackage } returns false
every { shouldSkipPackage } returns false
every { packageName } returns testPackageName
}
val event = mockk<AccessibilityEvent> {
every { source } returns node
every { packageName } returns testPackageName
}
every { powerManager.isInteractive } returns true
every { launcherPackageNameManager.launcherPackages } returns emptyList()
every {
accessibilityAutofillManager.accessibilityAction
} returns AccessibilityAction.AttemptParseUri
every { accessibilityAutofillManager.accessibilityAction = null } just runs
every {
createAutofillSelectionIntent(
context = context,
framework = AutofillSelectionData.Framework.ACCESSIBILITY,
type = AutofillSelectionData.Type.LOGIN,
uri = any(),
)
} returns mockk()
val uri = mockk<Uri>()
every { accessibilityParser.parseForUriOrPackageName(rootNode = node) } returns uri
every {
accessibilityParser.parseForFillableFields(rootNode = node, uri = uri)
} returns mockk { every { hasFields } returns false }

bitwardenAccessibilityProcessor.processAccessibilityEvent(event = event) { rootNode }

verify(exactly = 1) {
powerManager.isInteractive
node.isSystemPackage
node.shouldSkipPackage
launcherPackageNameManager.launcherPackages
accessibilityAutofillManager.accessibilityAction
accessibilityAutofillManager.accessibilityAction = null
accessibilityParser.parseForUriOrPackageName(rootNode = node)
accessibilityParser.parseForFillableFields(rootNode = node, uri = uri)
Toast
.makeText(context, R.string.autofill_tile_uri_not_found, Toast.LENGTH_LONG)
.show()
}
}

@Suppress("MaxLineLength")
@Test
fun `processAccessibilityEvent with AttemptParseUri and a valid uri should start the main activity`() {
Expand Down Expand Up @@ -296,7 +345,11 @@ class BitwardenAccessibilityProcessorTest {
uri = any(),
)
} returns mockk()
every { accessibilityParser.parseForUriOrPackageName(rootNode = node) } returns mockk()
val uri = mockk<Uri>()
every { accessibilityParser.parseForUriOrPackageName(rootNode = node) } returns uri
every {
accessibilityParser.parseForFillableFields(rootNode = node, uri = uri)
} returns mockk { every { hasFields } returns true }

bitwardenAccessibilityProcessor.processAccessibilityEvent(event = event) { rootNode }

Expand All @@ -308,6 +361,7 @@ class BitwardenAccessibilityProcessorTest {
accessibilityAutofillManager.accessibilityAction
accessibilityAutofillManager.accessibilityAction = null
accessibilityParser.parseForUriOrPackageName(rootNode = node)
accessibilityParser.parseForFillableFields(rootNode = node, uri = uri)
createAutofillSelectionIntent(
context = context,
framework = AutofillSelectionData.Framework.ACCESSIBILITY,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,23 @@ class VaultItemListingViewModelTest : BaseViewModelTest() {
}
}

@Test
fun `BackClick with AutofillSelectionData should emit ExitApp`() = runTest {
specialCircumstanceManager.specialCircumstance = SpecialCircumstance.AutofillSelection(
autofillSelectionData = AutofillSelectionData(
framework = AutofillSelectionData.Framework.ACCESSIBILITY,
type = AutofillSelectionData.Type.LOGIN,
uri = null,
),
shouldFinishWhenComplete = false,
)
val viewModel = createVaultItemListingViewModel()
viewModel.eventFlow.test {
viewModel.trySendAction(VaultItemListingsAction.BackClick)
assertEquals(VaultItemListingEvent.ExitApp, awaitItem())
}
}

@Test
fun `BackClick should emit NavigateBack`() = runTest {
val viewModel = createVaultItemListingViewModel()
Expand Down

0 comments on commit 0967234

Please sign in to comment.