[#322] Restore Wallet "greedy" autocompletion

- Changed order of conditions of automatic autocompletion.
- Added a test for validation of autocompletion functionality
- Changed type of test tag on ChipLayout component, as it seems its not necessary to have this one for proper component functionality. The one from the test package is needed here for the newly added test. And other screen components also use only those from the test package.
This commit is contained in:
Honza Rychnovsky 2022-04-11 15:19:04 +02:00 committed by GitHub
parent 482a48edb7
commit f091da51ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 5 deletions

View File

@ -10,6 +10,10 @@ import org.junit.Test
class ParseResultTest { class ParseResultTest {
companion object { companion object {
private val SAMPLE_WORD_LIST = setOf("bar", "baz", "foo") private val SAMPLE_WORD_LIST = setOf("bar", "baz", "foo")
private val SAMPLE_WORD_LIST_EXT = buildSet {
addAll(SAMPLE_WORD_LIST)
add("bazooka")
}
} }
@Test @Test
@ -69,6 +73,29 @@ class ParseResultTest {
assertEquals(ParseResult.Autocomplete(listOf("bar", "baz")), actual) assertEquals(ParseResult.Autocomplete(listOf("bar", "baz")), actual)
} }
@Test
@SmallTest
fun autocomplete_multiple_same_base() {
ParseResult.new(SAMPLE_WORD_LIST_EXT, "baz").also {
assertTrue(it is ParseResult.Autocomplete)
assertTrue((it as ParseResult.Autocomplete).suggestions.size == 2)
assertTrue((it).suggestions.contains("baz"))
assertTrue((it).suggestions.contains("bazooka"))
}
ParseResult.new(SAMPLE_WORD_LIST_EXT, "bazo").also {
assertTrue(it is ParseResult.Autocomplete)
assertTrue((it as ParseResult.Autocomplete).suggestions.size == 1)
assertTrue((it).suggestions.contains("bazooka"))
}
ParseResult.new(SAMPLE_WORD_LIST_EXT, "bazooka").also {
assertTrue(it is ParseResult.Add)
assertTrue((it as ParseResult.Add).words.size == 1)
assertTrue(it.words.contains("bazooka"))
}
}
@Test @Test
@SmallTest @SmallTest
fun autocomplete_security() { fun autocomplete_security() {

View File

@ -31,11 +31,13 @@ internal sealed class ParseResult {
return Continue return Continue
} }
if (completeWordList.contains(trimmed)) { val autocomplete = completeWordList.filter { it.startsWith(trimmed) }
// we accept the word only in case that there is no other available
if (completeWordList.contains(trimmed) && autocomplete.size == 1) {
return Add(listOf(trimmed)) return Add(listOf(trimmed))
} }
val autocomplete = completeWordList.filter { it.startsWith(trimmed) }
if (autocomplete.isNotEmpty()) { if (autocomplete.isNotEmpty()) {
return Autocomplete(autocomplete) return Autocomplete(autocomplete)
} }

View File

@ -53,7 +53,6 @@ import co.electriccoin.zcash.ui.design.MINIMAL_WEIGHT
import co.electriccoin.zcash.ui.design.component.Body import co.electriccoin.zcash.ui.design.component.Body
import co.electriccoin.zcash.ui.design.component.CHIP_GRID_ROW_SIZE import co.electriccoin.zcash.ui.design.component.CHIP_GRID_ROW_SIZE
import co.electriccoin.zcash.ui.design.component.Chip import co.electriccoin.zcash.ui.design.component.Chip
import co.electriccoin.zcash.ui.design.component.CommonTag
import co.electriccoin.zcash.ui.design.component.GradientSurface import co.electriccoin.zcash.ui.design.component.GradientSurface
import co.electriccoin.zcash.ui.design.component.Header import co.electriccoin.zcash.ui.design.component.Header
import co.electriccoin.zcash.ui.design.component.NavigationButton import co.electriccoin.zcash.ui.design.component.NavigationButton
@ -78,7 +77,9 @@ fun PreviewRestore() {
"above", "above",
"absent", "absent",
"absorb", "absorb",
"abstract" "abstract",
"rib",
"ribbon"
), ),
userWordList = WordList(listOf("abandon", "absorb")), userWordList = WordList(listOf("abandon", "absorb")),
onBack = {}, onBack = {},
@ -215,7 +216,7 @@ private fun ChipGridWithText(
Column( Column(
Modifier Modifier
.verticalScroll(scrollState) .verticalScroll(scrollState)
.testTag(CommonTag.CHIP_LAYOUT) .testTag(RestoreTag.CHIP_LAYOUT)
) { ) {
userWordList.chunked(CHIP_GRID_ROW_SIZE).forEachIndexed { chunkIndex, chunk -> userWordList.chunked(CHIP_GRID_ROW_SIZE).forEachIndexed { chunkIndex, chunk ->
Row(Modifier.fillMaxWidth()) { Row(Modifier.fillMaxWidth()) {