BlueUnderlineTests.java revision cf03ff02b18be101f031eaadc4f7e44e4056be10
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(mEditText.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(mEditText.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(mEditText.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 typedLength = STRING_TO_TYPE.length();
65        final int EXPECTED_SUGGESTION_SPAN_START = -1;
66        final int EXPECTED_UNDERLINE_SPAN_START = 0;
67        final int EXPECTED_UNDERLINE_SPAN_END = 4;
68        type(STRING_TO_TYPE);
69        sleep(DELAY_TO_WAIT_FOR_UNDERLINE);
70        runMessages();
71        type(Constants.CODE_SPACE);
72        // typedLength + 1 because we also typed a space
73        mLatinIME.onUpdateSelection(0, 0, typedLength + 1, typedLength + 1, -1, -1);
74        sleep(DELAY_TO_WAIT_FOR_UNDERLINE);
75        runMessages();
76        type(Constants.CODE_DELETE);
77        sleep(DELAY_TO_WAIT_FOR_UNDERLINE);
78        runMessages();
79        type(Constants.CODE_DELETE);
80        sleep(DELAY_TO_WAIT_FOR_UNDERLINE);
81        runMessages();
82        final SpanGetter suggestionSpan = new SpanGetter(mEditText.getText(), SuggestionSpan.class);
83        assertFalse("show no blue underline after backspace, span start should be -1",
84                suggestionSpan.isAutoCorrectionIndicator());
85        final SpanGetter underlineSpan = new SpanGetter(mEditText.getText(), UnderlineSpan.class);
86        assertEquals("should be composing, so should have an underline span",
87                EXPECTED_UNDERLINE_SPAN_START, underlineSpan.mStart);
88        assertEquals("should be composing, so should have an underline span",
89                EXPECTED_UNDERLINE_SPAN_END, underlineSpan.mEnd);
90    }
91
92    public void testBlueUnderlineDisappearsWhenCursorMoved() {
93        final String STRING_TO_TYPE = "tgis";
94        final int typedLength = STRING_TO_TYPE.length();
95        final int NEW_CURSOR_POSITION = 0;
96        type(STRING_TO_TYPE);
97        sleep(DELAY_TO_WAIT_FOR_UNDERLINE);
98        // Simulate the onUpdateSelection() event
99        mLatinIME.onUpdateSelection(0, 0, typedLength, typedLength, -1, -1);
100        runMessages();
101        // Here the blue underline has been set. testBlueUnderline() is testing for this already,
102        // so let's not test it here again.
103        // Now simulate the user moving the cursor.
104        mInputConnection.setSelection(NEW_CURSOR_POSITION, NEW_CURSOR_POSITION);
105        mLatinIME.onUpdateSelection(typedLength, typedLength,
106                NEW_CURSOR_POSITION, NEW_CURSOR_POSITION, -1, -1);
107        sleep(DELAY_TO_WAIT_FOR_UNDERLINE);
108        runMessages();
109        final SpanGetter span = new SpanGetter(mEditText.getText(), SuggestionSpan.class);
110        assertNull("blue underline removed when cursor is moved", span.mSpan);
111    }
112
113    public void testComposingStopsOnSpace() {
114        final String STRING_TO_TYPE = "this ";
115        type(STRING_TO_TYPE);
116        sleep(DELAY_TO_WAIT_FOR_UNDERLINE);
117        // Simulate the onUpdateSelection() event
118        mLatinIME.onUpdateSelection(0, 0, STRING_TO_TYPE.length(), STRING_TO_TYPE.length(), -1, -1);
119        runMessages();
120        // Here the blue underline has been set. testBlueUnderline() is testing for this already,
121        // so let's not test it here again.
122        // Now simulate the user moving the cursor.
123        SpanGetter span = new SpanGetter(mEditText.getText(), UnderlineSpan.class);
124        assertNull("should not be composing, so should not have an underline span", span.mSpan);
125    }
126}
127