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