1 /*
2  * Copyright (C) 2009 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 android.view.inputmethod.cts;
18 
19 
20 import android.os.Bundle;
21 import android.os.Handler;
22 import android.test.AndroidTestCase;
23 import android.text.TextUtils;
24 import android.view.KeyEvent;
25 import android.view.inputmethod.CompletionInfo;
26 import android.view.inputmethod.CorrectionInfo;
27 import android.view.inputmethod.EditorInfo;
28 import android.view.inputmethod.ExtractedText;
29 import android.view.inputmethod.ExtractedTextRequest;
30 import android.view.inputmethod.InputConnection;
31 import android.view.inputmethod.InputConnectionWrapper;
32 
33 public class InputConnectionWrapperTest extends AndroidTestCase {
34 
testInputConnectionWrapper()35     public void testInputConnectionWrapper() {
36         MockInputConnection inputConnection = new MockInputConnection();
37         InputConnectionWrapper wrapper = new InputConnectionWrapper(null, true);
38         try {
39             wrapper.beginBatchEdit();
40             fail("Failed to throw NullPointerException!");
41         } catch (NullPointerException e) {
42             // expected
43         }
44         wrapper.setTarget(inputConnection);
45 
46         wrapper.beginBatchEdit();
47         assertTrue(inputConnection.isBeginBatchEditCalled);
48         wrapper.clearMetaKeyStates(KeyEvent.META_ALT_ON);
49         assertTrue(inputConnection.isClearMetaKeyStatesCalled);
50         wrapper.commitCompletion(new CompletionInfo(1, 1, "testText"));
51         assertTrue(inputConnection.isCommitCompletionCalled);
52         wrapper.commitCorrection(new CorrectionInfo(0, "oldText", "newText"));
53         assertTrue(inputConnection.isCommitCorrectionCalled);
54         wrapper.commitText("Text", 1);
55         assertTrue(inputConnection.isCommitTextCalled);
56         wrapper.deleteSurroundingText(10, 100);
57         assertTrue(inputConnection.isDeleteSurroundingTextCalled);
58         wrapper.deleteSurroundingTextInCodePoints(10, 100);
59         assertTrue(inputConnection.isDeleteSurroundingTextInCodePointsCalled);
60         wrapper.endBatchEdit();
61         assertTrue(inputConnection.isEndBatchEditCalled);
62         wrapper.finishComposingText();
63         assertTrue(inputConnection.isFinishComposingTextCalled);
64         wrapper.getCursorCapsMode(TextUtils.CAP_MODE_CHARACTERS);
65         assertTrue(inputConnection.isGetCursorCapsModeCalled);
66         wrapper.getExtractedText(new ExtractedTextRequest(), 0);
67         assertTrue(inputConnection.isGetExtractedTextCalled);
68         wrapper.getTextAfterCursor(5, 0);
69         assertTrue(inputConnection.isGetTextAfterCursorCalled);
70         wrapper.getTextBeforeCursor(3, 0);
71         assertTrue(inputConnection.isGetTextBeforeCursorCalled);
72         wrapper.performContextMenuAction(1);
73         assertTrue(inputConnection.isPerformContextMenuActionCalled);
74         wrapper.performEditorAction(EditorInfo.IME_ACTION_GO);
75         assertTrue(inputConnection.isPerformEditorActionCalled);
76         wrapper.performPrivateCommand("com.android.action.MAIN", new Bundle());
77         assertTrue(inputConnection.isPerformPrivateCommandCalled);
78         wrapper.reportFullscreenMode(true);
79         assertTrue(inputConnection.isReportFullscreenModeCalled);
80         wrapper.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_0));
81         assertTrue(inputConnection.isSendKeyEventCalled);
82         wrapper.setComposingText("Text", 1);
83         assertTrue(inputConnection.isSetComposingTextCalled);
84         wrapper.setSelection(0, 10);
85         assertTrue(inputConnection.isSetSelectionCalled);
86         wrapper.getSelectedText(0);
87         assertTrue(inputConnection.isGetSelectedTextCalled);
88         wrapper.setComposingRegion(0, 3);
89         assertTrue(inputConnection.isSetComposingRegionCalled);
90         wrapper.requestCursorUpdates(InputConnection.CURSOR_UPDATE_IMMEDIATE);
91         assertTrue(inputConnection.isRequestCursorUpdatesCalled);
92         wrapper.closeConnection();
93         assertTrue(inputConnection.isCloseConnectionCalled);
94         assertFalse(inputConnection.isGetHandlerCalled);
95         assertNull(inputConnection.getHandler());
96         assertTrue(inputConnection.isGetHandlerCalled);
97     }
98 
99     private class MockInputConnection implements InputConnection {
100         public boolean isBeginBatchEditCalled;
101         public boolean isClearMetaKeyStatesCalled;
102         public boolean isCommitCompletionCalled;
103         public boolean isCommitCorrectionCalled;
104         public boolean isCommitTextCalled;
105         public boolean isDeleteSurroundingTextCalled;
106         public boolean isDeleteSurroundingTextInCodePointsCalled;
107         public boolean isEndBatchEditCalled;
108         public boolean isFinishComposingTextCalled;
109         public boolean isGetCursorCapsModeCalled;
110         public boolean isGetExtractedTextCalled;
111         public boolean isGetTextAfterCursorCalled;
112         public boolean isGetTextBeforeCursorCalled;
113         public boolean isGetSelectedTextCalled;
114         public boolean isPerformContextMenuActionCalled;
115         public boolean isPerformEditorActionCalled;
116         public boolean isPerformPrivateCommandCalled;
117         public boolean isReportFullscreenModeCalled;
118         public boolean isSendKeyEventCalled;
119         public boolean isSetComposingTextCalled;
120         public boolean isSetComposingRegionCalled;
121         public boolean isSetSelectionCalled;
122         public boolean isRequestCursorUpdatesCalled;
123         public boolean isGetHandlerCalled;
124         public boolean isCloseConnectionCalled;
125 
beginBatchEdit()126         public boolean beginBatchEdit() {
127             isBeginBatchEditCalled = true;
128             return false;
129         }
130 
clearMetaKeyStates(int states)131         public boolean clearMetaKeyStates(int states) {
132             isClearMetaKeyStatesCalled = true;
133             return false;
134         }
135 
commitCompletion(CompletionInfo text)136         public boolean commitCompletion(CompletionInfo text) {
137             isCommitCompletionCalled = true;
138             return false;
139         }
140 
commitCorrection(CorrectionInfo info)141         public boolean commitCorrection(CorrectionInfo info) {
142             isCommitCorrectionCalled = true;
143             return false;
144         }
145 
commitText(CharSequence text, int newCursorPosition)146         public boolean commitText(CharSequence text, int newCursorPosition) {
147             isCommitTextCalled = true;
148             return false;
149         }
150 
deleteSurroundingText(int beforeLength, int afterLength)151         public boolean deleteSurroundingText(int beforeLength, int afterLength) {
152             isDeleteSurroundingTextCalled = true;
153             return false;
154         }
155 
deleteSurroundingTextInCodePoints(int beforeLength, int afterLength)156         public boolean deleteSurroundingTextInCodePoints(int beforeLength, int afterLength) {
157             isDeleteSurroundingTextInCodePointsCalled = true;
158             return false;
159         }
160 
endBatchEdit()161         public boolean endBatchEdit() {
162             isEndBatchEditCalled = true;
163             return false;
164         }
165 
finishComposingText()166         public boolean finishComposingText() {
167             isFinishComposingTextCalled = true;
168             return false;
169         }
170 
getCursorCapsMode(int reqModes)171         public int getCursorCapsMode(int reqModes) {
172             isGetCursorCapsModeCalled = true;
173             return 0;
174         }
175 
getExtractedText(ExtractedTextRequest request, int flags)176         public ExtractedText getExtractedText(ExtractedTextRequest request, int flags) {
177             isGetExtractedTextCalled = true;
178             return null;
179         }
180 
getTextAfterCursor(int n, int flags)181         public CharSequence getTextAfterCursor(int n, int flags) {
182             isGetTextAfterCursorCalled = true;
183             return null;
184         }
185 
getTextBeforeCursor(int n, int flags)186         public CharSequence getTextBeforeCursor(int n, int flags) {
187             isGetTextBeforeCursorCalled = true;
188             return null;
189         }
190 
getSelectedText(int flags)191         public CharSequence getSelectedText(int flags) {
192             isGetSelectedTextCalled = true;
193             return null;
194         }
195 
performContextMenuAction(int id)196         public boolean performContextMenuAction(int id) {
197             isPerformContextMenuActionCalled = true;
198             return false;
199         }
200 
performEditorAction(int editorAction)201         public boolean performEditorAction(int editorAction) {
202             isPerformEditorActionCalled = true;
203             return false;
204         }
205 
performPrivateCommand(String action, Bundle data)206         public boolean performPrivateCommand(String action, Bundle data) {
207             isPerformPrivateCommandCalled = true;
208             return false;
209         }
210 
reportFullscreenMode(boolean enabled)211         public boolean reportFullscreenMode(boolean enabled) {
212             isReportFullscreenModeCalled = true;
213             return false;
214         }
215 
sendKeyEvent(KeyEvent event)216         public boolean sendKeyEvent(KeyEvent event) {
217             isSendKeyEventCalled = true;
218             return false;
219         }
220 
setComposingText(CharSequence text, int newCursorPosition)221         public boolean setComposingText(CharSequence text, int newCursorPosition) {
222             isSetComposingTextCalled = true;
223             return false;
224         }
225 
setComposingRegion(int start, int end)226         public boolean setComposingRegion(int start, int end) {
227             isSetComposingRegionCalled = true;
228             return false;
229         }
230 
setSelection(int start, int end)231         public boolean setSelection(int start, int end) {
232             isSetSelectionCalled = true;
233             return false;
234         }
235 
requestCursorUpdates(int cursorUpdateMode)236         public boolean requestCursorUpdates(int cursorUpdateMode) {
237             isRequestCursorUpdatesCalled = true;
238             return false;
239         }
240 
getHandler()241         public Handler getHandler() {
242             isGetHandlerCalled = true;
243             return null;
244         }
245 
closeConnection()246         public void closeConnection() {
247             isCloseConnectionCalled = true;
248         }
249     }
250 }
251