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;
18 
19 import android.text.style.SuggestionSpan;
20 import android.text.style.UnderlineSpan;
21 
22 import androidx.test.filters.LargeTest;
23 
24 import com.android.inputmethod.latin.common.Constants;
25 
26 @LargeTest
27 public class BlueUnderlineTests extends InputTestsBase {
28 
testBlueUnderline()29     public void testBlueUnderline() {
30         final String STRING_TO_TYPE = "tgis";
31         final int EXPECTED_SPAN_START = 0;
32         final int EXPECTED_SPAN_END = 4;
33         type(STRING_TO_TYPE);
34         sleep(DELAY_TO_WAIT_FOR_UNDERLINE_MILLIS);
35         runMessages();
36         final SpanGetter span = new SpanGetter(mEditText.getText(), SuggestionSpan.class);
37         assertEquals("show blue underline, span start", EXPECTED_SPAN_START, span.mStart);
38         assertEquals("show blue underline, span end", EXPECTED_SPAN_END, span.mEnd);
39         assertEquals("show blue underline, span color", true, span.isAutoCorrectionIndicator());
40     }
41 
testBlueUnderlineDisappears()42     public void testBlueUnderlineDisappears() {
43         final String STRING_1_TO_TYPE = "tqis";
44         final String STRING_2_TO_TYPE = "g";
45         final int EXPECTED_SPAN_START = 0;
46         final int EXPECTED_SPAN_END = 5;
47         type(STRING_1_TO_TYPE);
48         sleep(DELAY_TO_WAIT_FOR_UNDERLINE_MILLIS);
49         runMessages();
50         type(STRING_2_TO_TYPE);
51         // We haven't have time to look into the dictionary yet, so the line should still be
52         // blue to avoid any flicker.
53         final SpanGetter spanBefore = new SpanGetter(mEditText.getText(), SuggestionSpan.class);
54         assertEquals("extend blue underline, span start", EXPECTED_SPAN_START, spanBefore.mStart);
55         assertEquals("extend blue underline, span end", EXPECTED_SPAN_END, spanBefore.mEnd);
56         assertTrue("extend blue underline, span color", spanBefore.isAutoCorrectionIndicator());
57         sleep(DELAY_TO_WAIT_FOR_UNDERLINE_MILLIS);
58         runMessages();
59         // Now we have been able to re-evaluate the word, there shouldn't be an auto-correction span
60         final SpanGetter spanAfter = new SpanGetter(mEditText.getText(), SuggestionSpan.class);
61         assertNull("hide blue underline", spanAfter.mSpan);
62     }
63 
testBlueUnderlineOnBackspace()64     public void testBlueUnderlineOnBackspace() {
65         final String STRING_TO_TYPE = "tgis";
66         final int typedLength = STRING_TO_TYPE.length();
67         final int EXPECTED_UNDERLINE_SPAN_START = 0;
68         final int EXPECTED_UNDERLINE_SPAN_END = 3;
69         type(STRING_TO_TYPE);
70         sleep(DELAY_TO_WAIT_FOR_UNDERLINE_MILLIS);
71         runMessages();
72         type(Constants.CODE_SPACE);
73         // typedLength + 1 because we also typed a space
74         mLatinIME.onUpdateSelection(0, 0, typedLength + 1, typedLength + 1, -1, -1);
75         sleep(DELAY_TO_WAIT_FOR_UNDERLINE_MILLIS);
76         runMessages();
77         type(Constants.CODE_DELETE);
78         sleep(DELAY_TO_WAIT_FOR_UNDERLINE_MILLIS);
79         runMessages();
80         type(Constants.CODE_DELETE);
81         sleep(DELAY_TO_WAIT_FOR_UNDERLINE_MILLIS);
82         runMessages();
83         final SpanGetter suggestionSpan = new SpanGetter(mEditText.getText(), SuggestionSpan.class);
84         assertFalse("show no blue underline after backspace, span should not be the auto-"
85                 + "correction indicator", suggestionSpan.isAutoCorrectionIndicator());
86         final SpanGetter underlineSpan = new SpanGetter(mEditText.getText(), UnderlineSpan.class);
87         assertEquals("should be composing, so should have an underline span",
88                 EXPECTED_UNDERLINE_SPAN_START, underlineSpan.mStart);
89         assertEquals("should be composing, so should have an underline span",
90                 EXPECTED_UNDERLINE_SPAN_END, underlineSpan.mEnd);
91     }
92 
testBlueUnderlineDisappearsWhenCursorMoved()93     public void testBlueUnderlineDisappearsWhenCursorMoved() {
94         final String STRING_TO_TYPE = "tgis";
95         final int typedLength = STRING_TO_TYPE.length();
96         final int NEW_CURSOR_POSITION = 0;
97         type(STRING_TO_TYPE);
98         sleep(DELAY_TO_WAIT_FOR_UNDERLINE_MILLIS);
99         // Simulate the onUpdateSelection() event
100         mLatinIME.onUpdateSelection(0, 0, typedLength, typedLength, -1, -1);
101         runMessages();
102         // Here the blue underline has been set. testBlueUnderline() is testing for this already,
103         // so let's not test it here again.
104         // Now simulate the user moving the cursor.
105         mInputConnection.setSelection(NEW_CURSOR_POSITION, NEW_CURSOR_POSITION);
106         mLatinIME.onUpdateSelection(typedLength, typedLength,
107                 NEW_CURSOR_POSITION, NEW_CURSOR_POSITION, -1, -1);
108         sleep(DELAY_TO_WAIT_FOR_UNDERLINE_MILLIS);
109         runMessages();
110         final SpanGetter span = new SpanGetter(mEditText.getText(), SuggestionSpan.class);
111         assertFalse("blue underline removed when cursor is moved",
112                 span.isAutoCorrectionIndicator());
113     }
114 
testComposingStopsOnSpace()115     public void testComposingStopsOnSpace() {
116         final String STRING_TO_TYPE = "this ";
117         type(STRING_TO_TYPE);
118         sleep(DELAY_TO_WAIT_FOR_UNDERLINE_MILLIS);
119         // Simulate the onUpdateSelection() event
120         mLatinIME.onUpdateSelection(0, 0, STRING_TO_TYPE.length(), STRING_TO_TYPE.length(), -1, -1);
121         runMessages();
122         // Here the blue underline has been set. testBlueUnderline() is testing for this already,
123         // so let's not test it here again.
124         // Now simulate the user moving the cursor.
125         SpanGetter span = new SpanGetter(mEditText.getText(), UnderlineSpan.class);
126         assertNull("should not be composing, so should not have an underline span", span.mSpan);
127     }
128 }
129