1/*
2 * Copyright (C) 2013 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.view.inputmethod.BaseInputConnection;
21
22import com.android.inputmethod.latin.common.Constants;
23
24@LargeTest
25public class InputLogicTestsLanguageWithoutSpaces extends InputTestsBase {
26    public void testAutoCorrectForLanguageWithoutSpaces() {
27        final String STRING_TO_TYPE = "tgis is";
28        final String EXPECTED_RESULT = "thisis";
29        changeKeyboardLocaleAndDictLocale("th", "en_US");
30        type(STRING_TO_TYPE);
31        assertEquals("simple auto-correct for language without spaces", EXPECTED_RESULT,
32                mEditText.getText().toString());
33    }
34
35    public void testRevertAutoCorrectForLanguageWithoutSpaces() {
36        final String STRING_TO_TYPE = "tgis ";
37        final String EXPECTED_INTERMEDIATE_RESULT = "this";
38        final String EXPECTED_FINAL_RESULT = "tgis";
39        changeKeyboardLocaleAndDictLocale("th", "en_US");
40        type(STRING_TO_TYPE);
41        assertEquals("simple auto-correct for language without spaces",
42                EXPECTED_INTERMEDIATE_RESULT, mEditText.getText().toString());
43        type(Constants.CODE_DELETE);
44        assertEquals("simple auto-correct for language without spaces",
45                EXPECTED_FINAL_RESULT, mEditText.getText().toString());
46        // Check we are back to composing the word
47        assertEquals("don't resume suggestion on backspace", 0,
48                BaseInputConnection.getComposingSpanStart(mEditText.getText()));
49        assertEquals("don't resume suggestion on backspace", 4,
50                BaseInputConnection.getComposingSpanEnd(mEditText.getText()));
51    }
52
53    public void testDontResumeSuggestionOnBackspace() {
54        final String WORD_TO_TYPE = "and this ";
55        changeKeyboardLocaleAndDictLocale("th", "en_US");
56        type(WORD_TO_TYPE);
57        assertEquals("don't resume suggestion on backspace", -1,
58                BaseInputConnection.getComposingSpanStart(mEditText.getText()));
59        assertEquals("don't resume suggestion on backspace", -1,
60                BaseInputConnection.getComposingSpanEnd(mEditText.getText()));
61        type(" ");
62        type(Constants.CODE_DELETE);
63        assertEquals("don't resume suggestion on backspace", -1,
64                BaseInputConnection.getComposingSpanStart(mEditText.getText()));
65        assertEquals("don't resume suggestion on backspace", -1,
66                BaseInputConnection.getComposingSpanEnd(mEditText.getText()));
67    }
68
69    public void testStartComposingInsideText() {
70        final String WORD_TO_TYPE = "abcdefgh ";
71        final int typedLength = WORD_TO_TYPE.length() - 1; // -1 because space gets eaten
72        final int CURSOR_POS = 4;
73        changeKeyboardLocaleAndDictLocale("th", "en_US");
74        type(WORD_TO_TYPE);
75        mLatinIME.onUpdateSelection(0, 0, typedLength, typedLength, -1, -1);
76        mInputConnection.setSelection(CURSOR_POS, CURSOR_POS);
77        mLatinIME.onUpdateSelection(typedLength, typedLength,
78                CURSOR_POS, CURSOR_POS, -1, -1);
79        sleep(DELAY_TO_WAIT_FOR_PREDICTIONS_MILLIS);
80        runMessages();
81        assertEquals("start composing inside text", -1,
82                BaseInputConnection.getComposingSpanStart(mEditText.getText()));
83        assertEquals("start composing inside text", -1,
84                BaseInputConnection.getComposingSpanEnd(mEditText.getText()));
85        type("xxxx");
86        assertEquals("start composing inside text", 4,
87                BaseInputConnection.getComposingSpanStart(mEditText.getText()));
88        assertEquals("start composing inside text", 8,
89                BaseInputConnection.getComposingSpanEnd(mEditText.getText()));
90    }
91
92    public void testMovingCursorInsideWordAndType() {
93        final String WORD_TO_TYPE = "abcdefgh";
94        final int typedLength = WORD_TO_TYPE.length();
95        final int CURSOR_POS = 4;
96        changeKeyboardLocaleAndDictLocale("th", "en_US");
97        type(WORD_TO_TYPE);
98        mLatinIME.onUpdateSelection(0, 0, typedLength, typedLength, 0, typedLength);
99        sleep(DELAY_TO_WAIT_FOR_PREDICTIONS_MILLIS);
100        runMessages();
101        mInputConnection.setSelection(CURSOR_POS, CURSOR_POS);
102        mLatinIME.onUpdateSelection(typedLength, typedLength,
103                CURSOR_POS, CURSOR_POS, 0, typedLength);
104        sleep(DELAY_TO_WAIT_FOR_PREDICTIONS_MILLIS);
105        runMessages();
106        assertEquals("move cursor inside text", 0,
107                BaseInputConnection.getComposingSpanStart(mEditText.getText()));
108        assertEquals("move cursor inside text", typedLength,
109                BaseInputConnection.getComposingSpanEnd(mEditText.getText()));
110        type("x");
111        sleep(DELAY_TO_WAIT_FOR_PREDICTIONS_MILLIS);
112        sleep(DELAY_TO_WAIT_FOR_PREDICTIONS_MILLIS);
113        runMessages();
114        assertEquals("start typing while cursor inside composition", CURSOR_POS,
115                BaseInputConnection.getComposingSpanStart(mEditText.getText()));
116        assertEquals("start typing while cursor inside composition", CURSOR_POS + 1,
117                BaseInputConnection.getComposingSpanEnd(mEditText.getText()));
118    }
119
120    public void testPredictions() {
121        final String WORD_TO_TYPE = "Barack ";
122        changeKeyboardLocaleAndDictLocale("th", "en_US");
123        type(WORD_TO_TYPE);
124        sleep(DELAY_TO_WAIT_FOR_PREDICTIONS_MILLIS);
125        runMessages();
126        // Make sure there is no space
127        assertEquals("predictions in lang without spaces", "Barack",
128                mEditText.getText().toString());
129        // Test the first prediction is displayed
130        final SuggestedWords suggestedWords = mLatinIME.getSuggestedWordsForTest();
131        assertEquals("predictions in lang without spaces", "Obama",
132                suggestedWords.size() > 0 ? suggestedWords.getWord(0) : null);
133    }
134}
135