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