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.provider.Settings.Secure;
20import android.test.suitebuilder.annotation.LargeTest;
21
22import com.android.inputmethod.latin.R;
23
24@LargeTest
25public class PunctuationTests extends InputTestsBase {
26
27    final String NEXT_WORD_PREDICTION_OPTION = "next_word_prediction";
28
29    public void testWordThenSpaceThenPunctuationFromStripTwice() {
30        final String WORD_TO_TYPE = "this ";
31        final String PUNCTUATION_FROM_STRIP = "!";
32        final String EXPECTED_RESULT = "this!! ";
33        final boolean defaultNextWordPredictionOption =
34                mLatinIME.getResources().getBoolean(R.bool.config_default_next_word_prediction);
35        final boolean previousNextWordPredictionOption =
36                setBooleanPreference(NEXT_WORD_PREDICTION_OPTION, false,
37                        defaultNextWordPredictionOption);
38        try {
39            mLatinIME.loadSettings();
40            type(WORD_TO_TYPE);
41            sleep(DELAY_TO_WAIT_FOR_UNDERLINE_MILLIS);
42            runMessages();
43            assertTrue("type word then type space should display punctuation strip",
44                    mLatinIME.getSuggestedWordsForTest().isPunctuationSuggestions());
45            pickSuggestionManually(PUNCTUATION_FROM_STRIP);
46            pickSuggestionManually(PUNCTUATION_FROM_STRIP);
47            assertEquals("type word then type space then punctuation from strip twice",
48                    EXPECTED_RESULT, mEditText.getText().toString());
49        } finally {
50            setBooleanPreference(NEXT_WORD_PREDICTION_OPTION, previousNextWordPredictionOption,
51                    defaultNextWordPredictionOption);
52        }
53    }
54
55    public void testWordThenSpaceThenPunctuationFromKeyboardTwice() {
56        final String WORD_TO_TYPE = "this !!";
57        final String EXPECTED_RESULT = "this !!";
58        type(WORD_TO_TYPE);
59        assertEquals("manual pick then space then punctuation from keyboard twice", EXPECTED_RESULT,
60                mEditText.getText().toString());
61    }
62
63    public void testManualPickThenPunctuationFromStripTwiceThenType() {
64        final String WORD1_TO_TYPE = "this";
65        final String WORD2_TO_TYPE = "is";
66        final String PUNCTUATION_FROM_STRIP = "!";
67        final String EXPECTED_RESULT = "this!! is";
68        type(WORD1_TO_TYPE);
69        pickSuggestionManually(WORD1_TO_TYPE);
70        pickSuggestionManually(PUNCTUATION_FROM_STRIP);
71        pickSuggestionManually(PUNCTUATION_FROM_STRIP);
72        type(WORD2_TO_TYPE);
73        assertEquals("pick word then pick punctuation twice then type", EXPECTED_RESULT,
74                mEditText.getText().toString());
75    }
76
77    public void testManualPickThenManualPickWithPunctAtStart() {
78        final String WORD1_TO_TYPE = "this";
79        final String WORD2_TO_PICK = "!is";
80        final String EXPECTED_RESULT = "this!is";
81        type(WORD1_TO_TYPE);
82        pickSuggestionManually(WORD1_TO_TYPE);
83        pickSuggestionManually(WORD2_TO_PICK);
84        assertEquals("manual pick then manual pick a word with punct at start", EXPECTED_RESULT,
85                mEditText.getText().toString());
86    }
87
88    public void testManuallyPickedWordThenColon() {
89        final String WORD_TO_TYPE = "this";
90        final String PUNCTUATION = ":";
91        final String EXPECTED_RESULT = "this:";
92        type(WORD_TO_TYPE);
93        pickSuggestionManually(WORD_TO_TYPE);
94        type(PUNCTUATION);
95        assertEquals("manually pick word then colon",
96                EXPECTED_RESULT, mEditText.getText().toString());
97    }
98
99    public void testManuallyPickedWordThenOpenParen() {
100        final String WORD_TO_TYPE = "this";
101        final String PUNCTUATION = "(";
102        final String EXPECTED_RESULT = "this (";
103        type(WORD_TO_TYPE);
104        pickSuggestionManually(WORD_TO_TYPE);
105        type(PUNCTUATION);
106        assertEquals("manually pick word then open paren",
107                EXPECTED_RESULT, mEditText.getText().toString());
108    }
109
110    public void testManuallyPickedWordThenCloseParen() {
111        final String WORD_TO_TYPE = "this";
112        final String PUNCTUATION = ")";
113        final String EXPECTED_RESULT = "this)";
114        type(WORD_TO_TYPE);
115        pickSuggestionManually(WORD_TO_TYPE);
116        type(PUNCTUATION);
117        assertEquals("manually pick word then close paren",
118                EXPECTED_RESULT, mEditText.getText().toString());
119    }
120
121    public void testManuallyPickedWordThenSmiley() {
122        final String WORD_TO_TYPE = "this";
123        final String SPECIAL_KEY = ":-)";
124        final String EXPECTED_RESULT = "this :-)";
125        type(WORD_TO_TYPE);
126        pickSuggestionManually(WORD_TO_TYPE);
127        mLatinIME.onTextInput(SPECIAL_KEY);
128        assertEquals("manually pick word then press the smiley key",
129                EXPECTED_RESULT, mEditText.getText().toString());
130    }
131
132    public void testManuallyPickedWordThenDotCom() {
133        final String WORD_TO_TYPE = "this";
134        final String SPECIAL_KEY = ".com";
135        final String EXPECTED_RESULT = "this.com";
136        type(WORD_TO_TYPE);
137        pickSuggestionManually(WORD_TO_TYPE);
138        mLatinIME.onTextInput(SPECIAL_KEY);
139        assertEquals("manually pick word then press the .com key",
140                EXPECTED_RESULT, mEditText.getText().toString());
141    }
142
143    public void testTypeWordTypeDotThenPressDotCom() {
144        final String WORD_TO_TYPE = "this.";
145        final String SPECIAL_KEY = ".com";
146        final String EXPECTED_RESULT = "this.com";
147        type(WORD_TO_TYPE);
148        mLatinIME.onTextInput(SPECIAL_KEY);
149        assertEquals("type word type dot then press the .com key",
150                EXPECTED_RESULT, mEditText.getText().toString());
151    }
152
153    public void testAutoCorrectionWithSingleQuoteInside() {
154        final String WORD_TO_TYPE = "you'f ";
155        final String EXPECTED_RESULT = "you'd ";
156        type(WORD_TO_TYPE);
157        assertEquals("auto-correction with single quote inside. ID = "
158                + Secure.getString(getContext().getContentResolver(), Secure.ANDROID_ID)
159                + " ; Suggestions = " + mLatinIME.getSuggestedWordsForTest(),
160                EXPECTED_RESULT, mEditText.getText().toString());
161    }
162
163    public void testAutoCorrectionWithSingleQuotesAround() {
164        final String WORD_TO_TYPE = "'tgis' ";
165        final String EXPECTED_RESULT = "'this' ";
166        type(WORD_TO_TYPE);
167        assertEquals("auto-correction with single quotes around. ID = "
168                + Secure.getString(getContext().getContentResolver(), Secure.ANDROID_ID)
169                + " ; Suggestions = " + mLatinIME.getSuggestedWordsForTest(),
170                EXPECTED_RESULT, mEditText.getText().toString());
171    }
172
173    public void testAutoSpaceWithDoubleQuotes() {
174        final String STRING_TO_TYPE = "He said\"hello\"to me. I replied,\"hi\"."
175                + "Then, 5\"passed. He said\"bye\"and left.";
176        final String EXPECTED_RESULT = "He said \"hello\" to me. I replied, \"hi\". "
177                + "Then, 5\" passed. He said \"bye\" and left. \"";
178        // Split by double quote, so that we can type the double quotes individually.
179        for (final String partToType : STRING_TO_TYPE.split("\"")) {
180            // Split at word boundaries. This regexp means "anywhere that is preceded
181            // by a word character but not followed by a word character, OR that is not
182            // preceded by a word character but followed by a word character".
183            // We need to input word by word because auto-spaces are only active when
184            // manually picking or gesturing (which we can't simulate yet), but only words
185            // can be picked.
186            final String[] wordsToType = partToType.split("(?<=\\w)(?!\\w)|(?<!\\w)(?=\\w)");
187            for (final String wordToType : wordsToType) {
188                type(wordToType);
189                if (wordToType.matches("^\\w+$")) {
190                    // Only pick selection if that was a word, because if that was not a word,
191                    // then we don't have a composition.
192                    pickSuggestionManually(wordToType);
193                }
194            }
195            type("\"");
196        }
197        assertEquals("auto-space with double quotes",
198                EXPECTED_RESULT, mEditText.getText().toString());
199    }
200}
201