1 /*
2  * Copyright (C) 2014 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.text.TextUtils;
20 import android.view.inputmethod.EditorInfo;
21 
22 import androidx.test.filters.LargeTest;
23 
24 import com.android.inputmethod.latin.common.Constants;
25 
26 @LargeTest
27 public class ShiftModeTests extends InputTestsBase {
28 
29     @Override
enrichEditorInfo(final EditorInfo ei)30     protected EditorInfo enrichEditorInfo(final EditorInfo ei) {
31         ei.inputType |= TextUtils.CAP_MODE_SENTENCES;
32         ei.initialCapsMode = TextUtils.CAP_MODE_SENTENCES;
33         return ei;
34     }
35 
isCapsModeAutoShifted()36     private boolean isCapsModeAutoShifted() {
37         return mLatinIME.mKeyboardSwitcher.getKeyboardShiftMode()
38                 == WordComposer.CAPS_MODE_AUTO_SHIFTED;
39     }
40 
testTypicalSentence()41     public void testTypicalSentence() {
42         assertTrue("Initial auto caps state", isCapsModeAutoShifted());
43         type("Test");
44         assertFalse("Caps after letter", isCapsModeAutoShifted());
45         type(" ");
46         assertFalse("Caps after space", isCapsModeAutoShifted());
47         type("some,");
48         assertFalse("Caps after comma", isCapsModeAutoShifted());
49         type(" ");
50         assertFalse("Caps after comma space", isCapsModeAutoShifted());
51         type("words.");
52         assertFalse("Caps directly after period", isCapsModeAutoShifted());
53         type(" ");
54         assertTrue("Caps after period space", isCapsModeAutoShifted());
55     }
56 
testBackspace()57     public void testBackspace() {
58         assertTrue("Initial auto caps state", isCapsModeAutoShifted());
59         type("A");
60         assertFalse("Caps state after one letter", isCapsModeAutoShifted());
61         type(Constants.CODE_DELETE);
62         assertTrue("Auto caps state at start after delete", isCapsModeAutoShifted());
63     }
64 
testRepeatingBackspace()65     public void testRepeatingBackspace() {
66         final String SENTENCE_TO_TYPE = "Test sentence. Another.";
67         final int BACKSPACE_COUNT =
68                 SENTENCE_TO_TYPE.length() - SENTENCE_TO_TYPE.lastIndexOf(' ') - 1;
69 
70         type(SENTENCE_TO_TYPE);
71         assertFalse("Caps after typing \"" + SENTENCE_TO_TYPE + "\"", isCapsModeAutoShifted());
72         type(Constants.CODE_DELETE);
73         for (int i = 1; i < BACKSPACE_COUNT; ++i) {
74             repeatKey(Constants.CODE_DELETE);
75         }
76         assertFalse("Caps immediately after repeating Backspace a lot", isCapsModeAutoShifted());
77         sleep(DELAY_TO_WAIT_FOR_PREDICTIONS_MILLIS);
78         runMessages();
79         assertTrue("Caps after a while after repeating Backspace a lot", isCapsModeAutoShifted());
80     }
81 
testAutoCapsAfterDigitsPeriod()82     public void testAutoCapsAfterDigitsPeriod() {
83         changeLanguage("en");
84         type("On 22.11.");
85         assertFalse("(English) Auto caps after digits-period", isCapsModeAutoShifted());
86         type(" ");
87         assertTrue("(English) Auto caps after digits-period-whitespace", isCapsModeAutoShifted());
88         mEditText.setText("");
89         changeLanguage("fr");
90         type("Le 22.");
91         assertFalse("(French) Auto caps after digits-period", isCapsModeAutoShifted());
92         type(" ");
93         assertTrue("(French) Auto caps after digits-period-whitespace", isCapsModeAutoShifted());
94         mEditText.setText("");
95         changeLanguage("de");
96         type("Am 22.");
97         assertFalse("(German) Auto caps after digits-period", isCapsModeAutoShifted());
98         type(" ");
99         // For German, no auto-caps in this case
100         assertFalse("(German) Auto caps after digits-period-whitespace", isCapsModeAutoShifted());
101     }
102 
testAutoCapsAfterInvertedMarks()103     public void testAutoCapsAfterInvertedMarks() {
104         changeLanguage("es");
105         assertTrue("(Spanish) Auto caps at start", isCapsModeAutoShifted());
106         type("Hey. ¿");
107         assertTrue("(Spanish) Auto caps after inverted what", isCapsModeAutoShifted());
108         mEditText.setText("");
109         type("¡");
110         assertTrue("(Spanish) Auto caps after inverted bang", isCapsModeAutoShifted());
111     }
112 
testOtherSentenceSeparators()113     public void testOtherSentenceSeparators() {
114         changeLanguage("hy_AM");
115         assertTrue("(Armenian) Auto caps at start", isCapsModeAutoShifted());
116         type("Hey. ");
117         assertFalse("(Armenian) No auto-caps after latin period", isCapsModeAutoShifted());
118         type("Hey\u0589");
119         assertFalse("(Armenian) No auto-caps directly after armenian period",
120                 isCapsModeAutoShifted());
121         type(" ");
122         assertTrue("(Armenian) Auto-caps after armenian period-whitespace",
123                 isCapsModeAutoShifted());
124     }
125 }
126