1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.inputmethod.latin.spellcheck;
18 
19 import android.text.style.SuggestionSpan;
20 
21 import androidx.test.filters.LargeTest;
22 
23 import com.android.inputmethod.latin.InputTestsBase;
24 
25 @LargeTest
26 public class AndroidSpellCheckerServiceTest extends InputTestsBase {
testSpellchecker()27     public void testSpellchecker() {
28         changeLanguage("en_US");
29         mEditText.setText("tgis ");
30         mEditText.setSelection(mEditText.getText().length());
31         mEditText.onAttachedToWindow();
32         sleep(1000);
33         runMessages();
34         sleep(1000);
35 
36         final SpanGetter span = new SpanGetter(mEditText.getText(), SuggestionSpan.class);
37         // If no span, the following will crash
38         final String[] suggestions = span.getSuggestions();
39         // For this test we consider "tgis" should yield at least 2 suggestions (at this moment
40         // it yields 5).
41         assertTrue(suggestions.length >= 2);
42         // We also assume the top suggestion should be "this".
43         assertEquals("Test basic spell checking", "this", suggestions[0]);
44     }
45 
testRussianSpellchecker()46     public void testRussianSpellchecker() {
47         changeLanguage("ru");
48         mEditText.onAttachedToWindow();
49         mEditText.setText("годп ");
50         mEditText.setSelection(mEditText.getText().length());
51         mEditText.onAttachedToWindow();
52         sleep(1000);
53         runMessages();
54         sleep(1000);
55 
56         final SpanGetter span = new SpanGetter(mEditText.getText(), SuggestionSpan.class);
57         // We don't ship with Russian LM
58         assertNull(span.getSpan());
59     }
60 
testSpellcheckWithPeriods()61     public void testSpellcheckWithPeriods() {
62         changeLanguage("en_US");
63         mEditText.setText("I'm.sure ");
64         mEditText.setSelection(mEditText.getText().length());
65         mEditText.onAttachedToWindow();
66         sleep(1000);
67         runMessages();
68         sleep(1000);
69 
70         final SpanGetter span = new SpanGetter(mEditText.getText(), SuggestionSpan.class);
71         // If no span, the following will crash
72         final String[] suggestions = span.getSuggestions();
73         // The first suggestion should be "I'm sure".
74         assertEquals("Test spell checking of mistyped period for space", "I'm sure",
75                 suggestions[0]);
76     }
77 }
78