Skip to content

Commit

Permalink
Bug 1794933 - Remove autocomplete when not applicable anymore
Browse files Browse the repository at this point in the history
Immediately after the user modified it's input text to something that doesn't
match the current autocompleted text the autocompletion will be removed.
  • Loading branch information
Mugurell authored and mergify[bot] committed Nov 3, 2022
1 parent 4d0d666 commit fe733d3
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
Expand Up @@ -575,9 +575,24 @@ open class InlineAutocompleteEditText @JvmOverloads constructor(
return super.deleteSurroundingText(beforeLength, afterLength)
}

/**
* Optionally remove the current autocompletion depending on the new [text].
*
* Cases in which the autocompletion will be removed:
* - if the user pressed the backspace to remove the autocompletion or
* - if the user modified their input such that the autocompletion does not apply anymore.
*
* @return `true` if this method consumed the user input, `false` otherwise.
*/
@Suppress("ComplexCondition")
private fun removeAutocompleteOnComposing(text: CharSequence): Boolean {
val editable = getText()

// Remove the autocomplete text as soon as possible if not applicable anymore.
if (!editableText.startsWith(text) && removeAutocomplete(editable)) {
return false // If the user modified their input then allow the new text to be set.
}

val composingStart = BaseInputConnection.getComposingSpanStart(editable)
val composingEnd = BaseInputConnection.getComposingSpanEnd(editable)
// We only delete the autocomplete text when the user is backspacing,
Expand Down
Expand Up @@ -351,6 +351,26 @@ class InlineAutocompleteEditTextTest {
assertEquals(-1, BaseInputConnection.getComposingSpanEnd(et.text))
}

@Test
fun `GIVEN the current text contains an autocompletion WHEN a new character does not match the autocompletion THEN remove the autocompletion`() {
val et = InlineAutocompleteEditText(testContext, attributes)
val ic = et.onCreateInputConnection(mock(EditorInfo::class.java))

ic?.setComposingText("mo", 1)
assertEquals("mo", et.text.toString())

et.applyAutocompleteResult(AutocompleteResult("mozilla", "source", 1))
assertEquals("mozilla", et.text.toString())

// Simulating the user entering a new character which makes the current autocomplete invalid
ic?.setComposingText("mod", 1)
assertEquals("mod", et.text.toString())

// Verify that autocompletion works for the new text
et.applyAutocompleteResult(AutocompleteResult("moderator", "source", 1))
assertEquals("moderator", et.text.toString())
}

@Test
fun `GIVEN empty edit field WHEN text 'g' added THEN autocomplete to google`() {
val et = InlineAutocompleteEditText(testContext, attributes)
Expand Down
3 changes: 3 additions & 0 deletions docs/changelog.md
Expand Up @@ -10,6 +10,9 @@ permalink: /changelog/
* [Gecko](https://github.com/mozilla-mobile/android-components/blob/main/buildSrc/src/main/java/Gecko.kt)
* [Configuration](https://github.com/mozilla-mobile/android-components/blob/main/.config.yml)

* **ui-autocomplete**
* 🚒 Bug fixed [bug #1794933](https://bugzilla.mozilla.org/show_bug.cgi?id=1794933) Immediately remove autocomplete when not applicable anymore.

* **concept-engine**
* [bug #1798359](https://bugzilla.mozilla.org/show_bug.cgi?id=1798359) Set Total Cookie Protection as the default cookie policy for all Tracking Protection modes. Read more about Total Cookie Protection [here](https://blog.mozilla.org/en/mozilla/firefox-rolls-out-total-cookie-protection-by-default-to-all-users-worldwide/).

Expand Down

0 comments on commit fe733d3

Please sign in to comment.