mirror of
https://github.com/bitwarden/android.git
synced 2026-11-19 17:40:12 -06:00
BIT-1314: Add autofill node traversal with basic UI fulfillment (#541)
This commit is contained in:
committed by
Álison Fernandes
parent
35ef4e4252
commit
cea26f5e32
@@ -1,54 +0,0 @@
|
||||
package com.x8bit.bitwarden.data.autofill
|
||||
|
||||
import android.os.CancellationSignal
|
||||
import android.service.autofill.FillCallback
|
||||
import android.service.autofill.FillRequest
|
||||
import android.service.autofill.SaveCallback
|
||||
import android.service.autofill.SaveRequest
|
||||
import io.mockk.mockk
|
||||
import org.junit.jupiter.api.BeforeEach
|
||||
import org.junit.jupiter.api.Nested
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
class BitwardenAutofillServiceTests {
|
||||
private lateinit var bitwardenAutofillService: BitwardenAutofillService
|
||||
|
||||
@BeforeEach
|
||||
fun setup() {
|
||||
bitwardenAutofillService = BitwardenAutofillService()
|
||||
}
|
||||
|
||||
@Nested
|
||||
inner class OnFillRequest {
|
||||
@Test
|
||||
fun `nothing happens`() {
|
||||
// Setup
|
||||
val cancellationSignal: CancellationSignal = mockk()
|
||||
val fillCallback: FillCallback = mockk()
|
||||
val fillRequest: FillRequest = mockk()
|
||||
|
||||
// Test
|
||||
bitwardenAutofillService.onFillRequest(
|
||||
cancellationSignal = cancellationSignal,
|
||||
fillCallback = fillCallback,
|
||||
request = fillRequest,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
inner class OnSaveRequest {
|
||||
@Test
|
||||
fun `nothing happens`() {
|
||||
// Setup
|
||||
val saverRequest: SaveRequest = mockk()
|
||||
val saveCallback: SaveCallback = mockk()
|
||||
|
||||
// Test
|
||||
bitwardenAutofillService.onSaveRequest(
|
||||
saveCallback = saveCallback,
|
||||
saverRequest = saverRequest,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
+162
@@ -0,0 +1,162 @@
|
||||
package com.x8bit.bitwarden.data.autofill.builder
|
||||
|
||||
import android.content.Context
|
||||
import android.service.autofill.Dataset
|
||||
import android.service.autofill.FillResponse
|
||||
import android.view.autofill.AutofillId
|
||||
import android.widget.RemoteViews
|
||||
import com.x8bit.bitwarden.data.autofill.model.AutofillAppInfo
|
||||
import com.x8bit.bitwarden.data.autofill.model.FilledData
|
||||
import com.x8bit.bitwarden.data.autofill.model.FilledItem
|
||||
import com.x8bit.bitwarden.data.autofill.util.applyOverlayToDataset
|
||||
import com.x8bit.bitwarden.data.util.mockBuilder
|
||||
import com.x8bit.bitwarden.ui.autofill.buildAutofillRemoteViews
|
||||
import io.mockk.every
|
||||
import io.mockk.just
|
||||
import io.mockk.mockk
|
||||
import io.mockk.mockkConstructor
|
||||
import io.mockk.mockkStatic
|
||||
import io.mockk.runs
|
||||
import io.mockk.unmockkConstructor
|
||||
import io.mockk.unmockkStatic
|
||||
import io.mockk.verify
|
||||
import org.junit.jupiter.api.AfterEach
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.Assertions.assertNull
|
||||
import org.junit.jupiter.api.BeforeEach
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
class FillResponseBuilderTest {
|
||||
private lateinit var fillResponseBuilder: FillResponseBuilder
|
||||
|
||||
private val dataset: Dataset = mockk()
|
||||
private val context: Context = mockk()
|
||||
private val fillResponse: FillResponse = mockk()
|
||||
private val remoteViews: RemoteViews = mockk()
|
||||
private val appInfo: AutofillAppInfo = AutofillAppInfo(
|
||||
context = context,
|
||||
packageName = PACKAGE_NAME,
|
||||
sdkInt = 17,
|
||||
)
|
||||
private val autofillIdOne: AutofillId = mockk()
|
||||
private val autofillIdTwo: AutofillId = mockk()
|
||||
private val filledItemOne: FilledItem = mockk {
|
||||
every { this@mockk.autofillId } returns autofillIdOne
|
||||
}
|
||||
private val filledItemTwo: FilledItem = mockk {
|
||||
every { this@mockk.autofillId } returns autofillIdTwo
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
fun setup() {
|
||||
mockkConstructor(Dataset.Builder::class)
|
||||
mockkConstructor(FillResponse.Builder::class)
|
||||
mockkStatic(::buildAutofillRemoteViews)
|
||||
mockkStatic(FilledItem::applyOverlayToDataset)
|
||||
every { anyConstructed<Dataset.Builder>().build() } returns dataset
|
||||
every { anyConstructed<FillResponse.Builder>().build() } returns fillResponse
|
||||
|
||||
fillResponseBuilder = FillResponseBuilderImpl()
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
fun teardown() {
|
||||
unmockkConstructor(Dataset.Builder::class)
|
||||
unmockkConstructor(FillResponse.Builder::class)
|
||||
unmockkStatic(::buildAutofillRemoteViews)
|
||||
unmockkStatic(FilledItem::applyOverlayToDataset)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `build should return null when filledItems empty`() {
|
||||
// Test
|
||||
val filledData = FilledData(
|
||||
filledItems = emptyList(),
|
||||
ignoreAutofillIds = emptyList(),
|
||||
)
|
||||
val actual = fillResponseBuilder.build(
|
||||
autofillAppInfo = appInfo,
|
||||
filledData = filledData,
|
||||
)
|
||||
|
||||
// Verify
|
||||
assertNull(actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `build should apply filledItems and ignore ignoreAutofillIds`() {
|
||||
// Setup
|
||||
val ignoredAutofillIdOne: AutofillId = mockk()
|
||||
val ignoredAutofillIdTwo: AutofillId = mockk()
|
||||
val ignoreAutofillIds = listOf(
|
||||
ignoredAutofillIdOne,
|
||||
ignoredAutofillIdTwo,
|
||||
)
|
||||
val filledItems = listOf(
|
||||
filledItemOne,
|
||||
filledItemTwo,
|
||||
)
|
||||
val filledData = FilledData(
|
||||
filledItems = filledItems,
|
||||
ignoreAutofillIds = ignoreAutofillIds,
|
||||
)
|
||||
every {
|
||||
buildAutofillRemoteViews(
|
||||
context = context,
|
||||
packageName = PACKAGE_NAME,
|
||||
)
|
||||
} returns remoteViews
|
||||
every {
|
||||
filledItemOne.applyOverlayToDataset(
|
||||
appInfo = appInfo,
|
||||
datasetBuilder = anyConstructed(),
|
||||
remoteViews = remoteViews,
|
||||
)
|
||||
} just runs
|
||||
every {
|
||||
filledItemTwo.applyOverlayToDataset(
|
||||
appInfo = appInfo,
|
||||
datasetBuilder = anyConstructed(),
|
||||
remoteViews = remoteViews,
|
||||
)
|
||||
} just runs
|
||||
mockBuilder<FillResponse.Builder> { it.addDataset(dataset) }
|
||||
mockBuilder<FillResponse.Builder> {
|
||||
it.setIgnoredIds(
|
||||
ignoredAutofillIdOne,
|
||||
ignoredAutofillIdTwo,
|
||||
)
|
||||
}
|
||||
|
||||
// Test
|
||||
val actual = fillResponseBuilder.build(
|
||||
autofillAppInfo = appInfo,
|
||||
filledData = filledData,
|
||||
)
|
||||
|
||||
// Verify
|
||||
assertEquals(fillResponse, actual)
|
||||
|
||||
verify(exactly = 1) {
|
||||
filledItemOne.applyOverlayToDataset(
|
||||
appInfo = appInfo,
|
||||
datasetBuilder = any(),
|
||||
remoteViews = remoteViews,
|
||||
)
|
||||
filledItemTwo.applyOverlayToDataset(
|
||||
appInfo = appInfo,
|
||||
datasetBuilder = any(),
|
||||
remoteViews = remoteViews,
|
||||
)
|
||||
anyConstructed<FillResponse.Builder>().addDataset(dataset)
|
||||
anyConstructed<FillResponse.Builder>().setIgnoredIds(
|
||||
ignoredAutofillIdOne,
|
||||
ignoredAutofillIdTwo,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val PACKAGE_NAME: String = "com.x8bit.bitwarden"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package com.x8bit.bitwarden.data.autofill.builder
|
||||
|
||||
import android.view.autofill.AutofillId
|
||||
import com.x8bit.bitwarden.data.autofill.model.AutofillPartition
|
||||
import com.x8bit.bitwarden.data.autofill.model.AutofillRequest
|
||||
import com.x8bit.bitwarden.data.autofill.model.AutofillView
|
||||
import com.x8bit.bitwarden.data.autofill.model.FilledData
|
||||
import com.x8bit.bitwarden.data.autofill.model.FilledItem
|
||||
import io.mockk.mockk
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.BeforeEach
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
class FilledDataBuilderTest {
|
||||
private lateinit var filledDataBuilder: FilledDataBuilder
|
||||
|
||||
@BeforeEach
|
||||
fun setup() {
|
||||
filledDataBuilder = FilledDataBuilderImpl()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `build should return FilledData with FilledItems and ignored AutofillIds`() = runTest {
|
||||
// Setup
|
||||
val autofillId: AutofillId = mockk()
|
||||
val autofillView = AutofillView.Identity.PostalCode(
|
||||
autofillId = autofillId,
|
||||
isFocused = false,
|
||||
)
|
||||
val autofillPartition = AutofillPartition.Identity(
|
||||
views = listOf(autofillView),
|
||||
)
|
||||
val ignoreAutofillIds: List<AutofillId> = mockk()
|
||||
val autofillRequest = AutofillRequest.Fillable(
|
||||
ignoreAutofillIds = ignoreAutofillIds,
|
||||
partition = autofillPartition,
|
||||
)
|
||||
val filledItem = FilledItem(
|
||||
autofillId = autofillId,
|
||||
)
|
||||
val expected = FilledData(
|
||||
filledItems = listOf(
|
||||
filledItem,
|
||||
),
|
||||
ignoreAutofillIds = ignoreAutofillIds,
|
||||
)
|
||||
|
||||
// Test
|
||||
val actual = filledDataBuilder.build(
|
||||
autofillRequest = autofillRequest,
|
||||
)
|
||||
|
||||
// Verify
|
||||
assertEquals(expected, actual)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,270 @@
|
||||
package com.x8bit.bitwarden.data.autofill.parser
|
||||
|
||||
import android.app.assist.AssistStructure
|
||||
import android.view.View
|
||||
import android.view.autofill.AutofillId
|
||||
import com.x8bit.bitwarden.data.autofill.model.AutofillPartition
|
||||
import com.x8bit.bitwarden.data.autofill.model.AutofillRequest
|
||||
import com.x8bit.bitwarden.data.autofill.model.AutofillView
|
||||
import com.x8bit.bitwarden.data.autofill.util.toAutofillView
|
||||
import io.mockk.every
|
||||
import io.mockk.mockk
|
||||
import io.mockk.mockkStatic
|
||||
import io.mockk.unmockkStatic
|
||||
import org.junit.jupiter.api.AfterEach
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.BeforeEach
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
class AutofillParserTests {
|
||||
private lateinit var parser: AutofillParser
|
||||
|
||||
private val assistStructure: AssistStructure = mockk()
|
||||
private val cardAutofillHint = View.AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_YEAR
|
||||
private val cardAutofillId: AutofillId = mockk()
|
||||
private val cardViewNode: AssistStructure.ViewNode = mockk {
|
||||
every { this@mockk.autofillHints } returns arrayOf(cardAutofillHint)
|
||||
every { this@mockk.autofillId } returns cardAutofillId
|
||||
every { this@mockk.childCount } returns 0
|
||||
}
|
||||
private val identityAutofillHint = View.AUTOFILL_HINT_NAME
|
||||
private val identityAutofillId: AutofillId = mockk()
|
||||
private val identityViewNode: AssistStructure.ViewNode = mockk {
|
||||
every { this@mockk.autofillHints } returns arrayOf(identityAutofillHint)
|
||||
every { this@mockk.autofillId } returns identityAutofillId
|
||||
every { this@mockk.childCount } returns 0
|
||||
}
|
||||
private val loginAutofillHint = View.AUTOFILL_HINT_USERNAME
|
||||
private val loginAutofillId: AutofillId = mockk()
|
||||
private val loginViewNode: AssistStructure.ViewNode = mockk {
|
||||
every { this@mockk.autofillHints } returns arrayOf(loginAutofillHint)
|
||||
every { this@mockk.autofillId } returns loginAutofillId
|
||||
every { this@mockk.childCount } returns 0
|
||||
}
|
||||
private val cardWindowNode: AssistStructure.WindowNode = mockk {
|
||||
every { this@mockk.rootViewNode } returns cardViewNode
|
||||
}
|
||||
private val identityWindowNode: AssistStructure.WindowNode = mockk {
|
||||
every { this@mockk.rootViewNode } returns identityViewNode
|
||||
}
|
||||
private val loginWindowNode: AssistStructure.WindowNode = mockk {
|
||||
every { this@mockk.rootViewNode } returns loginViewNode
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
fun setup() {
|
||||
mockkStatic(AssistStructure.ViewNode::toAutofillView)
|
||||
parser = AutofillParserImpl()
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
fun teardown() {
|
||||
unmockkStatic(AssistStructure.ViewNode::toAutofillView)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `parse should return Unfillable when windowNodeCount is 0`() {
|
||||
// Setup
|
||||
val expected = AutofillRequest.Unfillable
|
||||
every { assistStructure.windowNodeCount } returns 0
|
||||
|
||||
// Test
|
||||
val actual = parser.parse(assistStructure)
|
||||
|
||||
// Verify
|
||||
assertEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `parse should return Fillable when at least one node valid, ignores the invalid nodes`() {
|
||||
// Setup
|
||||
val childAutofillHint = View.AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_MONTH
|
||||
val childAutofillId: AutofillId = mockk()
|
||||
val childViewNode: AssistStructure.ViewNode = mockk {
|
||||
every { this@mockk.autofillHints } returns arrayOf(childAutofillHint)
|
||||
every { this@mockk.autofillId } returns childAutofillId
|
||||
every { this@mockk.childCount } returns 0
|
||||
every { this@mockk.isFocused } returns false
|
||||
every { this@mockk.toAutofillView() } returns null
|
||||
}
|
||||
val parentAutofillHint = View.AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_YEAR
|
||||
val parentAutofillId: AutofillId = mockk()
|
||||
val parentAutofillView: AutofillView.Card = AutofillView.Card.ExpirationMonth(
|
||||
autofillId = parentAutofillId,
|
||||
isFocused = true,
|
||||
)
|
||||
val parentViewNode: AssistStructure.ViewNode = mockk {
|
||||
every { this@mockk.autofillHints } returns arrayOf(parentAutofillHint)
|
||||
every { this@mockk.autofillId } returns parentAutofillId
|
||||
every { this@mockk.toAutofillView() } returns parentAutofillView
|
||||
every { this@mockk.childCount } returns 1
|
||||
every { this@mockk.getChildAt(0) } returns childViewNode
|
||||
}
|
||||
val windowNode: AssistStructure.WindowNode = mockk {
|
||||
every { this@mockk.rootViewNode } returns parentViewNode
|
||||
}
|
||||
val autofillPartition = AutofillPartition.Card(
|
||||
views = listOf(parentAutofillView),
|
||||
)
|
||||
val expected = AutofillRequest.Fillable(
|
||||
ignoreAutofillIds = listOf(childAutofillId),
|
||||
partition = autofillPartition,
|
||||
)
|
||||
every { assistStructure.windowNodeCount } returns 1
|
||||
every { assistStructure.getWindowNodeAt(0) } returns windowNode
|
||||
|
||||
// Test
|
||||
val actual = parser.parse(assistStructure)
|
||||
|
||||
// Verify
|
||||
assertEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `parse should choose AutofillPartition Card when a Card view is focused`() {
|
||||
// Setup
|
||||
setupAssistStructureWithAllAutofillViewTypes()
|
||||
val cardAutofillView: AutofillView.Card = AutofillView.Card.ExpirationMonth(
|
||||
autofillId = cardAutofillId,
|
||||
isFocused = true,
|
||||
)
|
||||
val identityAutofillView: AutofillView.Identity = AutofillView.Identity.Name(
|
||||
autofillId = identityAutofillId,
|
||||
isFocused = false,
|
||||
)
|
||||
val loginAutofillView: AutofillView.Login = AutofillView.Login.Username(
|
||||
autofillId = loginAutofillId,
|
||||
isFocused = false,
|
||||
)
|
||||
val autofillPartition = AutofillPartition.Card(
|
||||
views = listOf(cardAutofillView),
|
||||
)
|
||||
val expected = AutofillRequest.Fillable(
|
||||
ignoreAutofillIds = emptyList(),
|
||||
partition = autofillPartition,
|
||||
)
|
||||
every { cardViewNode.toAutofillView() } returns cardAutofillView
|
||||
every { identityViewNode.toAutofillView() } returns identityAutofillView
|
||||
every { loginViewNode.toAutofillView() } returns loginAutofillView
|
||||
|
||||
// Test
|
||||
val actual = parser.parse(assistStructure)
|
||||
|
||||
// Verify
|
||||
assertEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `parse should choose AutofillPartition Identity when an Identity view is focused`() {
|
||||
// Setup
|
||||
setupAssistStructureWithAllAutofillViewTypes()
|
||||
val cardAutofillView: AutofillView.Card = AutofillView.Card.ExpirationMonth(
|
||||
autofillId = cardAutofillId,
|
||||
isFocused = false,
|
||||
)
|
||||
val identityAutofillView: AutofillView.Identity = AutofillView.Identity.Name(
|
||||
autofillId = identityAutofillId,
|
||||
isFocused = true,
|
||||
)
|
||||
val loginAutofillView: AutofillView.Login = AutofillView.Login.Username(
|
||||
autofillId = loginAutofillId,
|
||||
isFocused = false,
|
||||
)
|
||||
val autofillPartition = AutofillPartition.Identity(
|
||||
views = listOf(identityAutofillView),
|
||||
)
|
||||
val expected = AutofillRequest.Fillable(
|
||||
ignoreAutofillIds = emptyList(),
|
||||
partition = autofillPartition,
|
||||
)
|
||||
every { cardViewNode.toAutofillView() } returns cardAutofillView
|
||||
every { identityViewNode.toAutofillView() } returns identityAutofillView
|
||||
every { loginViewNode.toAutofillView() } returns loginAutofillView
|
||||
|
||||
// Test
|
||||
val actual = parser.parse(assistStructure)
|
||||
|
||||
// Verify
|
||||
assertEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `parse should choose AutofillPartition Login when a Login view is focused`() {
|
||||
// Setup
|
||||
setupAssistStructureWithAllAutofillViewTypes()
|
||||
val cardAutofillView: AutofillView.Card = AutofillView.Card.ExpirationMonth(
|
||||
autofillId = cardAutofillId,
|
||||
isFocused = false,
|
||||
)
|
||||
val identityAutofillView: AutofillView.Identity = AutofillView.Identity.Name(
|
||||
autofillId = identityAutofillId,
|
||||
isFocused = false,
|
||||
)
|
||||
val loginAutofillView: AutofillView.Login = AutofillView.Login.Username(
|
||||
autofillId = loginAutofillId,
|
||||
isFocused = true,
|
||||
)
|
||||
val autofillPartition = AutofillPartition.Login(
|
||||
views = listOf(loginAutofillView),
|
||||
)
|
||||
val expected = AutofillRequest.Fillable(
|
||||
ignoreAutofillIds = emptyList(),
|
||||
partition = autofillPartition,
|
||||
)
|
||||
every { cardViewNode.toAutofillView() } returns cardAutofillView
|
||||
every { identityViewNode.toAutofillView() } returns identityAutofillView
|
||||
every { loginViewNode.toAutofillView() } returns loginAutofillView
|
||||
|
||||
// Test
|
||||
val actual = parser.parse(assistStructure)
|
||||
|
||||
// Verify
|
||||
assertEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `parse should choose first focused AutofillView for partition when there are multiple`() {
|
||||
// Setup
|
||||
setupAssistStructureWithAllAutofillViewTypes()
|
||||
val cardAutofillView: AutofillView.Card = AutofillView.Card.ExpirationMonth(
|
||||
autofillId = cardAutofillId,
|
||||
isFocused = true,
|
||||
)
|
||||
val identityAutofillView: AutofillView.Identity = AutofillView.Identity.Name(
|
||||
autofillId = identityAutofillId,
|
||||
isFocused = true,
|
||||
)
|
||||
val loginAutofillView: AutofillView.Login = AutofillView.Login.Username(
|
||||
autofillId = loginAutofillId,
|
||||
isFocused = false,
|
||||
)
|
||||
val autofillPartition = AutofillPartition.Card(
|
||||
views = listOf(cardAutofillView),
|
||||
)
|
||||
val expected = AutofillRequest.Fillable(
|
||||
ignoreAutofillIds = emptyList(),
|
||||
partition = autofillPartition,
|
||||
)
|
||||
every { cardViewNode.toAutofillView() } returns cardAutofillView
|
||||
every { identityViewNode.toAutofillView() } returns identityAutofillView
|
||||
every { loginViewNode.toAutofillView() } returns loginAutofillView
|
||||
|
||||
// Test
|
||||
val actual = parser.parse(assistStructure)
|
||||
|
||||
// Verify
|
||||
assertEquals(expected, actual)
|
||||
}
|
||||
|
||||
/**
|
||||
* Setup [assistStructure] to return window nodes with each [AutofillView] type (card, identity,
|
||||
* and login) so we can test how different window node configurations produce different
|
||||
* partitions.
|
||||
*/
|
||||
private fun setupAssistStructureWithAllAutofillViewTypes() {
|
||||
every { assistStructure.windowNodeCount } returns 3
|
||||
every { assistStructure.getWindowNodeAt(0) } returns cardWindowNode
|
||||
every { assistStructure.getWindowNodeAt(1) } returns identityWindowNode
|
||||
every { assistStructure.getWindowNodeAt(2) } returns loginWindowNode
|
||||
}
|
||||
}
|
||||
+189
@@ -0,0 +1,189 @@
|
||||
package com.x8bit.bitwarden.data.autofill.processor
|
||||
|
||||
import android.app.assist.AssistStructure
|
||||
import android.os.CancellationSignal
|
||||
import android.service.autofill.FillCallback
|
||||
import android.service.autofill.FillContext
|
||||
import android.service.autofill.FillRequest
|
||||
import android.service.autofill.FillResponse
|
||||
import com.x8bit.bitwarden.data.autofill.builder.FillResponseBuilder
|
||||
import com.x8bit.bitwarden.data.autofill.builder.FilledDataBuilder
|
||||
import com.x8bit.bitwarden.data.autofill.model.AutofillAppInfo
|
||||
import com.x8bit.bitwarden.data.autofill.model.AutofillRequest
|
||||
import com.x8bit.bitwarden.data.autofill.model.FilledData
|
||||
import com.x8bit.bitwarden.data.autofill.model.FilledItem
|
||||
import com.x8bit.bitwarden.data.autofill.parser.AutofillParser
|
||||
import com.x8bit.bitwarden.data.platform.manager.dispatcher.DispatcherManager
|
||||
import io.mockk.coEvery
|
||||
import io.mockk.coVerify
|
||||
import io.mockk.confirmVerified
|
||||
import io.mockk.every
|
||||
import io.mockk.just
|
||||
import io.mockk.mockk
|
||||
import io.mockk.runs
|
||||
import io.mockk.verify
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.test.UnconfinedTestDispatcher
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.junit.jupiter.api.AfterEach
|
||||
import org.junit.jupiter.api.BeforeEach
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
class AutofillProcessorTest {
|
||||
private lateinit var autofillProcessor: AutofillProcessor
|
||||
|
||||
private val dispatcherManager: DispatcherManager = mockk()
|
||||
private val cancellationSignal: CancellationSignal = mockk()
|
||||
private val filledDataBuilder: FilledDataBuilder = mockk()
|
||||
private val fillResponseBuilder: FillResponseBuilder = mockk()
|
||||
private val parser: AutofillParser = mockk()
|
||||
private val testDispatcher = UnconfinedTestDispatcher()
|
||||
|
||||
private val appInfo: AutofillAppInfo = AutofillAppInfo(
|
||||
context = mockk(),
|
||||
packageName = "com.x8bit.bitwarden",
|
||||
sdkInt = 42,
|
||||
)
|
||||
private val assistStructure: AssistStructure = mockk()
|
||||
private val fillCallback: FillCallback = mockk()
|
||||
|
||||
@BeforeEach
|
||||
fun setup() {
|
||||
every { dispatcherManager.unconfined } returns testDispatcher
|
||||
|
||||
autofillProcessor = AutofillProcessorImpl(
|
||||
dispatcherManager = dispatcherManager,
|
||||
filledDataBuilder = filledDataBuilder,
|
||||
fillResponseBuilder = fillResponseBuilder,
|
||||
parser = parser,
|
||||
)
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
fun teardown() {
|
||||
verify(exactly = 1) {
|
||||
dispatcherManager.unconfined
|
||||
}
|
||||
confirmVerified(
|
||||
cancellationSignal,
|
||||
dispatcherManager,
|
||||
filledDataBuilder,
|
||||
fillResponseBuilder,
|
||||
parser,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `processFillRequest should invoke callback with null when no fillContexts`() {
|
||||
// Setup
|
||||
val fillRequest: FillRequest = mockk {
|
||||
every { this@mockk.fillContexts } returns emptyList()
|
||||
}
|
||||
every { fillCallback.onSuccess(null) } just runs
|
||||
|
||||
// Test
|
||||
autofillProcessor.processFillRequest(
|
||||
autofillAppInfo = appInfo,
|
||||
cancellationSignal = cancellationSignal,
|
||||
fillCallback = fillCallback,
|
||||
request = fillRequest,
|
||||
)
|
||||
|
||||
// Verify
|
||||
verify(exactly = 1) {
|
||||
fillCallback.onSuccess(null)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `processFillRequest should invoke callback with null when parse returns Unfillable`() {
|
||||
// Setup
|
||||
val autofillRequest = AutofillRequest.Unfillable
|
||||
val lastFillContext: FillContext = mockk {
|
||||
every { this@mockk.structure } returns assistStructure
|
||||
}
|
||||
val fillContexts: List<FillContext> = listOf(mockk(), lastFillContext)
|
||||
val fillRequest: FillRequest = mockk {
|
||||
every { this@mockk.fillContexts } returns fillContexts
|
||||
}
|
||||
every { cancellationSignal.setOnCancelListener(any()) } just runs
|
||||
every { parser.parse(assistStructure) } returns autofillRequest
|
||||
every { fillCallback.onSuccess(null) } just runs
|
||||
|
||||
// Test
|
||||
autofillProcessor.processFillRequest(
|
||||
autofillAppInfo = appInfo,
|
||||
cancellationSignal = cancellationSignal,
|
||||
fillCallback = fillCallback,
|
||||
request = fillRequest,
|
||||
)
|
||||
|
||||
// Verify
|
||||
verify(exactly = 1) {
|
||||
cancellationSignal.setOnCancelListener(any())
|
||||
parser.parse(assistStructure)
|
||||
fillCallback.onSuccess(null)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `processFillRequest should invoke callback with filled response when has filledItems`() =
|
||||
runTest {
|
||||
// Setup
|
||||
val lastFillContext: FillContext = mockk {
|
||||
every { this@mockk.structure } returns assistStructure
|
||||
}
|
||||
val fillContextList: List<FillContext> = listOf(mockk(), lastFillContext)
|
||||
val fillRequest: FillRequest = mockk {
|
||||
every { this@mockk.fillContexts } returns fillContextList
|
||||
}
|
||||
val filledItems: List<FilledItem> = listOf(mockk())
|
||||
val filledData = FilledData(
|
||||
filledItems = filledItems,
|
||||
ignoreAutofillIds = emptyList(),
|
||||
)
|
||||
val fillResponse: FillResponse = mockk()
|
||||
val autofillRequest: AutofillRequest.Fillable = mockk {
|
||||
every { this@mockk.ignoreAutofillIds } returns emptyList()
|
||||
}
|
||||
coEvery {
|
||||
filledDataBuilder.build(
|
||||
autofillRequest = autofillRequest,
|
||||
)
|
||||
} returns filledData
|
||||
every { cancellationSignal.setOnCancelListener(any()) } just runs
|
||||
every { parser.parse(assistStructure) } returns autofillRequest
|
||||
every {
|
||||
fillResponseBuilder.build(
|
||||
autofillAppInfo = appInfo,
|
||||
filledData = filledData,
|
||||
)
|
||||
} returns fillResponse
|
||||
every { fillCallback.onSuccess(fillResponse) } just runs
|
||||
|
||||
// Test
|
||||
autofillProcessor.processFillRequest(
|
||||
autofillAppInfo = appInfo,
|
||||
cancellationSignal = cancellationSignal,
|
||||
fillCallback = fillCallback,
|
||||
request = fillRequest,
|
||||
)
|
||||
|
||||
// Verify
|
||||
coVerify(exactly = 1) {
|
||||
filledDataBuilder.build(
|
||||
autofillRequest = autofillRequest,
|
||||
)
|
||||
}
|
||||
verify(exactly = 1) {
|
||||
cancellationSignal.setOnCancelListener(any())
|
||||
parser.parse(assistStructure)
|
||||
fillResponseBuilder.build(
|
||||
autofillAppInfo = appInfo,
|
||||
filledData = filledData,
|
||||
)
|
||||
fillCallback.onSuccess(fillResponse)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
package com.x8bit.bitwarden.data.autofill.util
|
||||
|
||||
import android.content.Context
|
||||
import android.service.autofill.Dataset
|
||||
import android.service.autofill.Field
|
||||
import android.service.autofill.Presentations
|
||||
import android.view.autofill.AutofillId
|
||||
import android.widget.RemoteViews
|
||||
import com.x8bit.bitwarden.data.autofill.model.AutofillAppInfo
|
||||
import com.x8bit.bitwarden.data.autofill.model.FilledItem
|
||||
import com.x8bit.bitwarden.data.util.mockBuilder
|
||||
import io.mockk.every
|
||||
import io.mockk.mockk
|
||||
import io.mockk.mockkConstructor
|
||||
import io.mockk.unmockkConstructor
|
||||
import io.mockk.verify
|
||||
import org.junit.jupiter.api.AfterEach
|
||||
import org.junit.jupiter.api.BeforeEach
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
class FilledItemExtensionsTest {
|
||||
private val autofillId: AutofillId = mockk()
|
||||
private val context: Context = mockk()
|
||||
private val datasetBuilder: Dataset.Builder = mockk()
|
||||
private val field: Field = mockk()
|
||||
private val filledItem = FilledItem(
|
||||
autofillId = autofillId,
|
||||
)
|
||||
private val presentations: Presentations = mockk()
|
||||
private val remoteViews: RemoteViews = mockk()
|
||||
|
||||
@BeforeEach
|
||||
fun setup() {
|
||||
mockkConstructor(Dataset.Builder::class)
|
||||
mockkConstructor(Presentations.Builder::class)
|
||||
mockkConstructor(Field.Builder::class)
|
||||
every { anyConstructed<Presentations.Builder>().build() } returns presentations
|
||||
every { anyConstructed<Field.Builder>().build() } returns field
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
fun teardown() {
|
||||
unmockkConstructor(Dataset.Builder::class)
|
||||
unmockkConstructor(Presentations.Builder::class)
|
||||
unmockkConstructor(Field.Builder::class)
|
||||
}
|
||||
|
||||
@Suppress("Deprecation")
|
||||
@Test
|
||||
fun `applyOverlayToDataset should use setValue to set RemoteViews when before tiramisu`() {
|
||||
// Setup
|
||||
val appInfo = AutofillAppInfo(
|
||||
context = context,
|
||||
packageName = PACKAGE_NAME,
|
||||
sdkInt = 1,
|
||||
)
|
||||
every {
|
||||
datasetBuilder.setValue(
|
||||
autofillId,
|
||||
null,
|
||||
remoteViews,
|
||||
)
|
||||
} returns datasetBuilder
|
||||
|
||||
// Test
|
||||
filledItem.applyOverlayToDataset(
|
||||
appInfo = appInfo,
|
||||
datasetBuilder = datasetBuilder,
|
||||
remoteViews = remoteViews,
|
||||
)
|
||||
|
||||
// Verify
|
||||
verify(exactly = 1) {
|
||||
datasetBuilder.setValue(
|
||||
autofillId,
|
||||
null,
|
||||
remoteViews,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `applyOverlayToDataset should use setField to set Presentation on or after Tiramisu`() {
|
||||
// Setup
|
||||
val appInfo = AutofillAppInfo(
|
||||
context = context,
|
||||
packageName = PACKAGE_NAME,
|
||||
sdkInt = 34,
|
||||
)
|
||||
mockBuilder<Presentations.Builder> { it.setDialogPresentation(remoteViews) }
|
||||
mockBuilder<Field.Builder> { it.setPresentations(presentations) }
|
||||
every {
|
||||
datasetBuilder.setField(
|
||||
autofillId,
|
||||
field,
|
||||
)
|
||||
} returns datasetBuilder
|
||||
|
||||
// Test
|
||||
filledItem.applyOverlayToDataset(
|
||||
appInfo = appInfo,
|
||||
datasetBuilder = datasetBuilder,
|
||||
remoteViews = remoteViews,
|
||||
)
|
||||
|
||||
// Verify
|
||||
verify(exactly = 1) {
|
||||
anyConstructed<Presentations.Builder>().setDialogPresentation(remoteViews)
|
||||
anyConstructed<Field.Builder>().setPresentations(presentations)
|
||||
datasetBuilder.setField(
|
||||
autofillId,
|
||||
field,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val PACKAGE_NAME: String = "com.x8bit.bitwarden"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,239 @@
|
||||
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
|
||||
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 ViewNodeExtensionsTest {
|
||||
private val expectedAutofillId: AutofillId = mockk()
|
||||
private val expectedIsFocused = true
|
||||
private val viewNode: AssistStructure.ViewNode = mockk {
|
||||
every { this@mockk.autofillId } returns expectedAutofillId
|
||||
every { this@mockk.childCount } returns 0
|
||||
every { this@mockk.isFocused } returns expectedIsFocused
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `toAutofillView should return AutofillView Card ExpirationMonth when hint matches`() {
|
||||
// Setup
|
||||
val autofillHint = View.AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_MONTH
|
||||
val expected = AutofillView.Card.ExpirationMonth(
|
||||
autofillId = expectedAutofillId,
|
||||
isFocused = expectedIsFocused,
|
||||
)
|
||||
every { viewNode.autofillHints } returns arrayOf(autofillHint)
|
||||
|
||||
// Test
|
||||
val actual = viewNode.toAutofillView()
|
||||
|
||||
// Verify
|
||||
assertEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `toAutofillView should return AutofillView Card ExpirationYear when hint matches`() {
|
||||
// Setup
|
||||
val autofillHint = View.AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_YEAR
|
||||
val expected = AutofillView.Card.ExpirationYear(
|
||||
autofillId = expectedAutofillId,
|
||||
isFocused = expectedIsFocused,
|
||||
)
|
||||
every { viewNode.autofillHints } returns arrayOf(autofillHint)
|
||||
|
||||
// Test
|
||||
val actual = viewNode.toAutofillView()
|
||||
|
||||
// Verify
|
||||
assertEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `toAutofillView should return AutofillView Card Number when hint matches`() {
|
||||
// Setup
|
||||
val autofillHint = View.AUTOFILL_HINT_CREDIT_CARD_NUMBER
|
||||
val expected = AutofillView.Card.Number(
|
||||
autofillId = expectedAutofillId,
|
||||
isFocused = expectedIsFocused,
|
||||
)
|
||||
every { viewNode.autofillHints } returns arrayOf(autofillHint)
|
||||
|
||||
// Test
|
||||
val actual = viewNode.toAutofillView()
|
||||
|
||||
// Verify
|
||||
assertEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `toAutofillView should return AutofillView Card SecurityCode when hint matches`() {
|
||||
// Setup
|
||||
val autofillHint = View.AUTOFILL_HINT_CREDIT_CARD_SECURITY_CODE
|
||||
val expected = AutofillView.Card.SecurityCode(
|
||||
autofillId = expectedAutofillId,
|
||||
isFocused = expectedIsFocused,
|
||||
)
|
||||
every { viewNode.autofillHints } returns arrayOf(autofillHint)
|
||||
|
||||
// Test
|
||||
val actual = viewNode.toAutofillView()
|
||||
|
||||
// Verify
|
||||
assertEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `toAutofillView should return AutofillView Login EmailAddress when hint matches`() {
|
||||
// Setup
|
||||
val autofillHint = View.AUTOFILL_HINT_EMAIL_ADDRESS
|
||||
val expected = AutofillView.Login.EmailAddress(
|
||||
autofillId = expectedAutofillId,
|
||||
isFocused = expectedIsFocused,
|
||||
)
|
||||
every { viewNode.autofillHints } returns arrayOf(autofillHint)
|
||||
|
||||
// Test
|
||||
val actual = viewNode.toAutofillView()
|
||||
|
||||
// Verify
|
||||
assertEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `toAutofillView should return AutofillView Identity Name when hint matches`() {
|
||||
// Setup
|
||||
val autofillHint = View.AUTOFILL_HINT_NAME
|
||||
val expected = AutofillView.Identity.Name(
|
||||
autofillId = expectedAutofillId,
|
||||
isFocused = expectedIsFocused,
|
||||
)
|
||||
every { viewNode.autofillHints } returns arrayOf(autofillHint)
|
||||
|
||||
// Test
|
||||
val actual = viewNode.toAutofillView()
|
||||
|
||||
// Verify
|
||||
assertEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `toAutofillView should return AutofillView Login Password when hint matches`() {
|
||||
// Setup
|
||||
val autofillHint = View.AUTOFILL_HINT_PASSWORD
|
||||
val expected = AutofillView.Login.Password(
|
||||
autofillId = expectedAutofillId,
|
||||
isFocused = expectedIsFocused,
|
||||
)
|
||||
every { viewNode.autofillHints } returns arrayOf(autofillHint)
|
||||
|
||||
// Test
|
||||
val actual = viewNode.toAutofillView()
|
||||
|
||||
// Verify
|
||||
assertEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `toAutofillView should return AutofillView Identity PhoneNumber when hint matches`() {
|
||||
// Setup
|
||||
val autofillHint = View.AUTOFILL_HINT_PHONE
|
||||
val expected = AutofillView.Identity.PhoneNumber(
|
||||
autofillId = expectedAutofillId,
|
||||
isFocused = expectedIsFocused,
|
||||
)
|
||||
every { viewNode.autofillHints } returns arrayOf(autofillHint)
|
||||
|
||||
// Test
|
||||
val actual = viewNode.toAutofillView()
|
||||
|
||||
// Verify
|
||||
assertEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `toAutofillView should return AutofillView Identity PostalAddress when hint matches`() {
|
||||
// Setup
|
||||
val autofillHint = View.AUTOFILL_HINT_POSTAL_ADDRESS
|
||||
val expected = AutofillView.Identity.PostalAddress(
|
||||
autofillId = expectedAutofillId,
|
||||
isFocused = expectedIsFocused,
|
||||
)
|
||||
every { viewNode.autofillHints } returns arrayOf(autofillHint)
|
||||
|
||||
// Test
|
||||
val actual = viewNode.toAutofillView()
|
||||
|
||||
// Verify
|
||||
assertEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `toAutofillView should return AutofillView Identity PostalCOde when hint matches`() {
|
||||
// Setup
|
||||
val autofillHint = View.AUTOFILL_HINT_POSTAL_CODE
|
||||
val expected = AutofillView.Identity.PostalCode(
|
||||
autofillId = expectedAutofillId,
|
||||
isFocused = expectedIsFocused,
|
||||
)
|
||||
every { viewNode.autofillHints } returns arrayOf(autofillHint)
|
||||
|
||||
// Test
|
||||
val actual = viewNode.toAutofillView()
|
||||
|
||||
// Verify
|
||||
assertEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `toAutofillView should return AutofillView Login Username when hint matches`() {
|
||||
// Setup
|
||||
val autofillHint = View.AUTOFILL_HINT_USERNAME
|
||||
val expected = AutofillView.Login.Username(
|
||||
autofillId = expectedAutofillId,
|
||||
isFocused = expectedIsFocused,
|
||||
)
|
||||
every { viewNode.autofillHints } returns arrayOf(autofillHint)
|
||||
|
||||
// Test
|
||||
val actual = viewNode.toAutofillView()
|
||||
|
||||
// Verify
|
||||
assertEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `toAutofillView should return null when hint is not supported`() {
|
||||
// Setup
|
||||
val autofillHint = "Shenanigans"
|
||||
every { viewNode.autofillHints } returns arrayOf(autofillHint)
|
||||
|
||||
// Test
|
||||
val actual = viewNode.toAutofillView()
|
||||
|
||||
// Verify
|
||||
assertNull(actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `toAutofillView should skip unsupported hint and return supported hint mapping`() {
|
||||
// Setup
|
||||
val autofillHintOne = "Shenanigans"
|
||||
val autofillHintTwo = View.AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_YEAR
|
||||
val expected = AutofillView.Card.ExpirationYear(
|
||||
autofillId = expectedAutofillId,
|
||||
isFocused = expectedIsFocused,
|
||||
)
|
||||
every { viewNode.autofillHints } returns arrayOf(autofillHintOne, autofillHintTwo)
|
||||
|
||||
// Test
|
||||
val actual = viewNode.toAutofillView()
|
||||
|
||||
// Verify
|
||||
assertEquals(expected, actual)
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
package com.x8bit.bitwarden.data.util
|
||||
|
||||
import com.x8bit.bitwarden.data.platform.datasource.network.di.PlatformNetworkModule
|
||||
import io.mockk.MockKMatcherScope
|
||||
import io.mockk.every
|
||||
import kotlinx.serialization.json.Json
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
|
||||
@@ -17,3 +19,31 @@ fun assertJsonEquals(
|
||||
json.parseToJsonElement(actual),
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method for mocking pipeline operations within the builder pattern. This saves a lot of
|
||||
* boiler plate. In order to use this, the builder's constructor must be mockked.
|
||||
*
|
||||
* Example:
|
||||
* ```
|
||||
* // Setup
|
||||
* mockkConstructor(FillResponse.Builder::class)
|
||||
* mockBuilder<FillResponse.Builder> { it.setIgnoredIds() }
|
||||
* every { anyConstructed<FillResponse.Builder>().build() } returns mockk()
|
||||
*
|
||||
* // Test
|
||||
* ...
|
||||
*
|
||||
* // Verify
|
||||
* verify(exactly = 1) {
|
||||
* anyConstructed<FillResponse.Builder>().setIgnoredIds()
|
||||
* anyConstructed<FillResponse.Builder>().build()
|
||||
* }
|
||||
* unmockkConstructor(FillResponse.Builder::class)
|
||||
* ```
|
||||
*/
|
||||
inline fun <reified T : Any> mockBuilder(crossinline block: MockKMatcherScope.(T) -> T) {
|
||||
every { block(anyConstructed<T>()) } answers {
|
||||
this.self as T
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
package com.x8bit.bitwarden.ui.autofill
|
||||
|
||||
import android.content.Context
|
||||
import android.content.res.Resources
|
||||
import android.widget.RemoteViews
|
||||
import com.x8bit.bitwarden.R
|
||||
import io.mockk.confirmVerified
|
||||
import io.mockk.every
|
||||
import io.mockk.just
|
||||
import io.mockk.mockk
|
||||
import io.mockk.mockkConstructor
|
||||
import io.mockk.runs
|
||||
import io.mockk.unmockkConstructor
|
||||
import io.mockk.verify
|
||||
import org.junit.jupiter.api.AfterEach
|
||||
import org.junit.jupiter.api.BeforeEach
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
class BitwardenRemoteViewsTest {
|
||||
private val testResources: Resources = mockk()
|
||||
private val context: Context = mockk {
|
||||
every { this@mockk.resources } returns testResources
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
fun setup() {
|
||||
mockkConstructor(RemoteViews::class)
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
fun teardown() {
|
||||
unmockkConstructor(RemoteViews::class)
|
||||
confirmVerified(
|
||||
context,
|
||||
testResources,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `buildAutofillRemoteViews should set text`() {
|
||||
// Setup
|
||||
val appName = "Bitwarden"
|
||||
every { testResources.getText(R.string.app_name) } returns appName
|
||||
every {
|
||||
anyConstructed<RemoteViews>()
|
||||
.setTextViewText(
|
||||
R.id.text,
|
||||
appName,
|
||||
)
|
||||
} just runs
|
||||
|
||||
// Test
|
||||
buildAutofillRemoteViews(
|
||||
context = context,
|
||||
packageName = PACKAGE_NAME,
|
||||
)
|
||||
|
||||
// Note: impossible to do a useful test of the returned RemoteViews due to mockking
|
||||
// constraints of the [RemoteViews] constructor. Our best bet is to make sure the correct
|
||||
// operations are performed on the constructed [RemoteViews].
|
||||
|
||||
// Verify
|
||||
verify(exactly = 1) {
|
||||
context.resources
|
||||
testResources.getText(R.string.app_name)
|
||||
anyConstructed<RemoteViews>()
|
||||
.setTextViewText(
|
||||
R.id.text,
|
||||
"Bitwarden",
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val PACKAGE_NAME: String = "com.x8bit.bitwarden"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user