Merge "Update SpellChecker CTS." into sc-dev
diff --git a/tests/inputmethod/mockspellchecker/src/com/android/cts/mockspellchecker/MockSpellChecker.kt b/tests/inputmethod/mockspellchecker/src/com/android/cts/mockspellchecker/MockSpellChecker.kt
index acc030b..45730c5 100644
--- a/tests/inputmethod/mockspellchecker/src/com/android/cts/mockspellchecker/MockSpellChecker.kt
+++ b/tests/inputmethod/mockspellchecker/src/com/android/cts/mockspellchecker/MockSpellChecker.kt
@@ -77,8 +77,8 @@
                     ?.let {
                         SentenceSuggestionsInfo(
                                 arrayOf(suggestionsInfo(it, textInfo.cookie, textInfo.sequence)),
-                                intArrayOf(0),
-                                intArrayOf(textInfo.text.length))
+                                intArrayOf(if (it.hasStartOffset()) it.startOffset else 0),
+                                intArrayOf(if (it.hasLength()) it.length else textInfo.text.length))
                     }
                     ?: SentenceSuggestionsInfo(emptyArray(), intArrayOf(), intArrayOf())
         }
diff --git a/tests/inputmethod/mockspellchecker/src/com/android/cts/mockspellchecker/mockspellchecker.proto b/tests/inputmethod/mockspellchecker/src/com/android/cts/mockspellchecker/mockspellchecker.proto
index 35574ba..572b2de 100644
--- a/tests/inputmethod/mockspellchecker/src/com/android/cts/mockspellchecker/mockspellchecker.proto
+++ b/tests/inputmethod/mockspellchecker/src/com/android/cts/mockspellchecker/mockspellchecker.proto
@@ -26,6 +26,8 @@
   optional string match = 1;
   optional int32 attributes = 2;
   repeated string suggestions = 3;
+  optional int32 start_offset = 4;
+  optional int32 length = 5;
 }
 
 // Represents a MockSpellChecker configuration.
diff --git a/tests/inputmethod/src/android/view/inputmethod/cts/SpellCheckerTest.kt b/tests/inputmethod/src/android/view/inputmethod/cts/SpellCheckerTest.kt
index fe67b1e..40c2d03 100644
--- a/tests/inputmethod/src/android/view/inputmethod/cts/SpellCheckerTest.kt
+++ b/tests/inputmethod/src/android/view/inputmethod/cts/SpellCheckerTest.kt
@@ -443,6 +443,49 @@
         }
     }
 
+    @Test
+    fun removePreviousSuggestion() {
+        // Set up two rules:
+        // - Matches the sentence "Wrong context word?" and marks "word" as grammar error.
+        // - Matches the sentence "Correct context word?" and marks "word" as in-vocabulary.
+        val configuration = MockSpellCheckerConfiguration.newBuilder()
+                .setMatchSentence(true)
+                .addSuggestionRules(
+                        MockSpellCheckerProto.SuggestionRule.newBuilder()
+                                .setMatch("Wrong context word?")
+                                .addSuggestions("suggestion")
+                                .setStartOffset(14)
+                                .setLength(4)
+                                .setAttributes(RESULT_ATTR_LOOKS_LIKE_GRAMMAR_ERROR)
+                ).addSuggestionRules(
+                        MockSpellCheckerProto.SuggestionRule.newBuilder()
+                                .setMatch("Correct context word?")
+                                .setStartOffset(16)
+                                .setLength(4)
+                                .setAttributes(RESULT_ATTR_IN_THE_DICTIONARY)
+                ).build()
+        MockImeSession.create(context).use { session ->
+            MockSpellCheckerClient.create(context, configuration).use { client ->
+                val (_, editText) = startTestActivity()
+                CtsTouchUtils.emulateTapOnViewCenter(instrumentation, null, editText)
+                waitOnMainUntil({ editText.hasFocus() }, TIMEOUT)
+                InputMethodVisibilityVerifier.expectImeVisible(TIMEOUT)
+                session.callCommitText("Wrong context word", 1)
+                session.callCommitText("?", 1)
+                waitOnMainUntil({
+                    findSuggestionSpanWithFlags(editText, FLAG_GRAMMAR_ERROR) != null
+                }, TIMEOUT)
+                // Change "Wrong" to "Correct" and then trigger spell check.
+                session.callSetSelection(0, 5) // Select "Wrong"
+                session.callCommitText("Correct", 1)
+                session.callPerformSpellCheck()
+                waitOnMainUntil({
+                    findSuggestionSpanWithFlags(editText, FLAG_GRAMMAR_ERROR) == null
+                }, TIMEOUT)
+            }
+        }
+    }
+
     private fun findSuggestionSpanWithFlags(editText: EditText, flags: Int): SuggestionSpan? =
             getSuggestionSpans(editText).find { (it.flags and flags) == flags }