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"); you may not
      5  * use this file except in compliance with the License. You may obtain a copy of
      6  * 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, WITHOUT
     12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
     13  * License for the specific language governing permissions and limitations under
     14  * the License.
     15  */
     16 
     17 package com.android.inputmethod.latin;
     18 
     19 import com.android.inputmethod.keyboard.Keyboard;
     20 
     21 public class InputLogicTests extends InputTestsBase {
     22 
     23     public void testTypeWord() {
     24         final String WORD_TO_TYPE = "abcd";
     25         type(WORD_TO_TYPE);
     26         assertEquals("type word", WORD_TO_TYPE, mTextView.getText().toString());
     27     }
     28 
     29     public void testPickSuggestionThenBackspace() {
     30         final String WORD_TO_TYPE = "this";
     31         final String EXPECTED_RESULT = "thi";
     32         type(WORD_TO_TYPE);
     33         pickSuggestionManually(0, WORD_TO_TYPE);
     34         mLatinIME.onUpdateSelection(0, 0, WORD_TO_TYPE.length(), WORD_TO_TYPE.length(), -1, -1);
     35         type(Keyboard.CODE_DELETE);
     36         assertEquals("press suggestion then backspace", EXPECTED_RESULT,
     37                 mTextView.getText().toString());
     38     }
     39 
     40     public void testPickAutoCorrectionThenBackspace() {
     41         final String WORD_TO_TYPE = "tgis";
     42         final String WORD_TO_PICK = "this";
     43         final String EXPECTED_RESULT = "thi";
     44         type(WORD_TO_TYPE);
     45         // Choose the auto-correction, which is always in position 0. For "tgis", the
     46         // auto-correction should be "this".
     47         pickSuggestionManually(0, WORD_TO_PICK);
     48         mLatinIME.onUpdateSelection(0, 0, WORD_TO_TYPE.length(), WORD_TO_TYPE.length(), -1, -1);
     49         assertEquals("pick typed word over auto-correction then backspace", WORD_TO_PICK,
     50                 mTextView.getText().toString());
     51         type(Keyboard.CODE_DELETE);
     52         assertEquals("pick typed word over auto-correction then backspace", EXPECTED_RESULT,
     53                 mTextView.getText().toString());
     54     }
     55 
     56     public void testPickTypedWordOverAutoCorrectionThenBackspace() {
     57         final String WORD_TO_TYPE = "tgis";
     58         final String EXPECTED_RESULT = "tgi";
     59         type(WORD_TO_TYPE);
     60         // Choose the typed word, which should be in position 1 (because position 0 should
     61         // be occupied by the "this" auto-correction, as checked by testAutoCorrect())
     62         pickSuggestionManually(1, WORD_TO_TYPE);
     63         mLatinIME.onUpdateSelection(0, 0, WORD_TO_TYPE.length(), WORD_TO_TYPE.length(), -1, -1);
     64         assertEquals("pick typed word over auto-correction then backspace", WORD_TO_TYPE,
     65                 mTextView.getText().toString());
     66         type(Keyboard.CODE_DELETE);
     67         assertEquals("pick typed word over auto-correction then backspace", EXPECTED_RESULT,
     68                 mTextView.getText().toString());
     69     }
     70 
     71     public void testPickDifferentSuggestionThenBackspace() {
     72         final String WORD_TO_TYPE = "tgis";
     73         final String WORD_TO_PICK = "thus";
     74         final String EXPECTED_RESULT = "thu";
     75         type(WORD_TO_TYPE);
     76         // Choose the second suggestion, which should be in position 2 and should be "thus"
     77         // when "tgis is typed.
     78         pickSuggestionManually(2, WORD_TO_PICK);
     79         mLatinIME.onUpdateSelection(0, 0, WORD_TO_TYPE.length(), WORD_TO_TYPE.length(), -1, -1);
     80         assertEquals("pick different suggestion then backspace", WORD_TO_PICK,
     81                 mTextView.getText().toString());
     82         type(Keyboard.CODE_DELETE);
     83         assertEquals("pick different suggestion then backspace", EXPECTED_RESULT,
     84                 mTextView.getText().toString());
     85     }
     86 
     87     public void testDeleteSelection() {
     88         final String STRING_TO_TYPE = "some text delete me some text";
     89         final int SELECTION_START = 10;
     90         final int SELECTION_END = 19;
     91         final String EXPECTED_RESULT = "some text  some text";
     92         type(STRING_TO_TYPE);
     93         // There is no IMF to call onUpdateSelection for us so we must do it by hand.
     94         // Send once to simulate the cursor actually responding to the move caused by typing.
     95         // This is necessary because LatinIME is bookkeeping to avoid confusing a real cursor
     96         // move with a move triggered by LatinIME inputting stuff.
     97         mLatinIME.onUpdateSelection(0, 0, STRING_TO_TYPE.length(), STRING_TO_TYPE.length(), -1, -1);
     98         mInputConnection.setSelection(SELECTION_START, SELECTION_END);
     99         // And now we simulate the user actually selecting some text.
    100         mLatinIME.onUpdateSelection(0, 0, SELECTION_START, SELECTION_END, -1, -1);
    101         type(Keyboard.CODE_DELETE);
    102         assertEquals("delete selection", EXPECTED_RESULT, mTextView.getText().toString());
    103     }
    104 
    105     public void testAutoCorrect() {
    106         final String STRING_TO_TYPE = "tgis ";
    107         final String EXPECTED_RESULT = "this ";
    108         type(STRING_TO_TYPE);
    109         assertEquals("simple auto-correct", EXPECTED_RESULT, mTextView.getText().toString());
    110     }
    111 
    112     public void testAutoCorrectWithPeriod() {
    113         final String STRING_TO_TYPE = "tgis.";
    114         final String EXPECTED_RESULT = "this.";
    115         type(STRING_TO_TYPE);
    116         assertEquals("auto-correct with period", EXPECTED_RESULT, mTextView.getText().toString());
    117     }
    118 
    119     public void testAutoCorrectWithPeriodThenRevert() {
    120         final String STRING_TO_TYPE = "tgis.";
    121         final String EXPECTED_RESULT = "tgis.";
    122         type(STRING_TO_TYPE);
    123         mLatinIME.onUpdateSelection(0, 0, STRING_TO_TYPE.length(), STRING_TO_TYPE.length(), -1, -1);
    124         type(Keyboard.CODE_DELETE);
    125         assertEquals("auto-correct with period then revert", EXPECTED_RESULT,
    126                 mTextView.getText().toString());
    127     }
    128 
    129     public void testDoubleSpace() {
    130         final String STRING_TO_TYPE = "this  ";
    131         final String EXPECTED_RESULT = "this. ";
    132         type(STRING_TO_TYPE);
    133         assertEquals("double space make a period", EXPECTED_RESULT, mTextView.getText().toString());
    134     }
    135 
    136     public void testCancelDoubleSpace() {
    137         final String STRING_TO_TYPE = "this  ";
    138         final String EXPECTED_RESULT = "this  ";
    139         type(STRING_TO_TYPE);
    140         type(Keyboard.CODE_DELETE);
    141         assertEquals("double space make a period", EXPECTED_RESULT, mTextView.getText().toString());
    142     }
    143 
    144     public void testBackspaceAtStartAfterAutocorrect() {
    145         final String STRING_TO_TYPE = "tgis ";
    146         final String EXPECTED_RESULT = "this ";
    147         final int NEW_CURSOR_POSITION = 0;
    148         type(STRING_TO_TYPE);
    149         mLatinIME.onUpdateSelection(0, 0, STRING_TO_TYPE.length(), STRING_TO_TYPE.length(), -1, -1);
    150         mInputConnection.setSelection(NEW_CURSOR_POSITION, NEW_CURSOR_POSITION);
    151         mLatinIME.onUpdateSelection(0, 0, NEW_CURSOR_POSITION, NEW_CURSOR_POSITION, -1, -1);
    152         type(Keyboard.CODE_DELETE);
    153         assertEquals("auto correct then move cursor to start of line then backspace",
    154                 EXPECTED_RESULT, mTextView.getText().toString());
    155     }
    156 
    157     public void testAutoCorrectThenMoveCursorThenBackspace() {
    158         final String STRING_TO_TYPE = "and tgis ";
    159         final String EXPECTED_RESULT = "andthis ";
    160         final int NEW_CURSOR_POSITION = STRING_TO_TYPE.indexOf('t');
    161         type(STRING_TO_TYPE);
    162         mLatinIME.onUpdateSelection(0, 0, STRING_TO_TYPE.length(), STRING_TO_TYPE.length(), -1, -1);
    163         mInputConnection.setSelection(NEW_CURSOR_POSITION, NEW_CURSOR_POSITION);
    164         mLatinIME.onUpdateSelection(0, 0, NEW_CURSOR_POSITION, NEW_CURSOR_POSITION, -1, -1);
    165         type(Keyboard.CODE_DELETE);
    166         assertEquals("auto correct then move cursor then backspace",
    167                 EXPECTED_RESULT, mTextView.getText().toString());
    168     }
    169 
    170     public void testNoSpaceAfterManualPick() {
    171         final String WORD_TO_TYPE = "this";
    172         final String EXPECTED_RESULT = WORD_TO_TYPE;
    173         type(WORD_TO_TYPE);
    174         pickSuggestionManually(0, WORD_TO_TYPE);
    175         assertEquals("no space after manual pick", EXPECTED_RESULT,
    176                 mTextView.getText().toString());
    177     }
    178 
    179     public void testManualPickThenType() {
    180         final String WORD1_TO_TYPE = "this";
    181         final String WORD2_TO_TYPE = "is";
    182         final String EXPECTED_RESULT = "this is";
    183         type(WORD1_TO_TYPE);
    184         pickSuggestionManually(0, WORD1_TO_TYPE);
    185         type(WORD2_TO_TYPE);
    186         assertEquals("manual pick then type", EXPECTED_RESULT, mTextView.getText().toString());
    187     }
    188 
    189     public void testManualPickThenSeparator() {
    190         final String WORD1_TO_TYPE = "this";
    191         final String WORD2_TO_TYPE = "!";
    192         final String EXPECTED_RESULT = "this!";
    193         type(WORD1_TO_TYPE);
    194         pickSuggestionManually(0, WORD1_TO_TYPE);
    195         type(WORD2_TO_TYPE);
    196         assertEquals("manual pick then separator", EXPECTED_RESULT, mTextView.getText().toString());
    197     }
    198 
    199     public void testManualPickThenSpaceThenType() {
    200         final String WORD1_TO_TYPE = "this";
    201         final String WORD2_TO_TYPE = " is";
    202         final String EXPECTED_RESULT = "this is";
    203         type(WORD1_TO_TYPE);
    204         pickSuggestionManually(0, WORD1_TO_TYPE);
    205         type(WORD2_TO_TYPE);
    206         assertEquals("manual pick then space then type", EXPECTED_RESULT,
    207                 mTextView.getText().toString());
    208     }
    209 
    210     public void testManualPickThenManualPick() {
    211         final String WORD1_TO_TYPE = "this";
    212         final String WORD2_TO_PICK = "is";
    213         final String EXPECTED_RESULT = "this is";
    214         type(WORD1_TO_TYPE);
    215         pickSuggestionManually(0, WORD1_TO_TYPE);
    216         // Here we fake picking a word through bigram prediction. This test is taking
    217         // advantage of the fact that Latin IME blindly trusts the caller of #pickSuggestionManually
    218         // to actually pass the right string.
    219         pickSuggestionManually(1, WORD2_TO_PICK);
    220         assertEquals("manual pick then manual pick", EXPECTED_RESULT,
    221                 mTextView.getText().toString());
    222     }
    223 
    224     public void testDeleteWholeComposingWord() {
    225         final String WORD_TO_TYPE = "this";
    226         type(WORD_TO_TYPE);
    227         for (int i = 0; i < WORD_TO_TYPE.length(); ++i) {
    228             type(Keyboard.CODE_DELETE);
    229         }
    230         assertEquals("delete whole composing word", "", mTextView.getText().toString());
    231     }
    232     // TODO: Add some tests for non-BMP characters
    233 }
    234