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