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