Add spell checker test for invalid suggestions.

Fix: 185786752
Test: atest CtsInputMethodTestCases:SpellCheckerTest
Change-Id: I1982e4428a46bfbb83c7a6af41cc19f723038d3c
diff --git a/tests/inputmethod/src/android/view/inputmethod/cts/SpellCheckerTest.kt b/tests/inputmethod/src/android/view/inputmethod/cts/SpellCheckerTest.kt
index 2f179aa..990fc95 100644
--- a/tests/inputmethod/src/android/view/inputmethod/cts/SpellCheckerTest.kt
+++ b/tests/inputmethod/src/android/view/inputmethod/cts/SpellCheckerTest.kt
@@ -73,6 +73,7 @@
 import java.util.Locale
 import java.util.concurrent.Executor
 import java.util.concurrent.TimeUnit
+import java.util.concurrent.TimeoutException
 import kotlin.collections.ArrayList
 
 @MediumTest
@@ -520,6 +521,40 @@
         }
     }
 
+    @Test
+    fun ignoreInvalidSuggestions() {
+        // Set up a wrong rule:
+        // - Matches the sentence "Context word" and marks "word" as grammar error.
+        val configuration = MockSpellCheckerConfiguration.newBuilder()
+                .setMatchSentence(true)
+                .addSuggestionRules(
+                        MockSpellCheckerProto.SuggestionRule.newBuilder()
+                                .setMatch("Context word")
+                                .addSuggestions("suggestion")
+                                .setStartOffset(8)
+                                .setLength(5) // Should be 4
+                                .setAttributes(RESULT_ATTR_LOOKS_LIKE_TYPO)
+                ).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("Context word", 1)
+                session.callPerformSpellCheck()
+                try {
+                    waitOnMainUntil({
+                        findSuggestionSpanWithFlags(editText, RESULT_ATTR_LOOKS_LIKE_TYPO) != null
+                    }, TIMEOUT)
+                    fail("Invalid suggestions should be ignored")
+                } catch (e: TimeoutException) {
+                    // Expected.
+                }
+            }
+        }
+    }
+
     private fun findSuggestionSpanWithFlags(editText: EditText, flags: Int): SuggestionSpan? =
             getSuggestionSpans(editText).find { (it.flags and flags) == flags }