• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     }
88 
89     private class MockInputConnection implements InputConnection {
90         public boolean isBeginBatchEditCalled;
91         public boolean isClearMetaKeyStatesCalled;
92         public boolean isCommitCompletionCalled;
93         public boolean isCommitCorrectionCalled;
94         public boolean isCommitTextCalled;
95         public boolean isDeleteSurroundingTextCalled;
96         public boolean isEndBatchEditCalled;
97         public boolean isFinishComposingTextCalled;
98         public boolean isGetCursorCapsModeCalled;
99         public boolean isGetExtractedTextCalled;
100         public boolean isGetTextAfterCursorCalled;
101         public boolean isGetTextBeforeCursorCalled;
102         public boolean isGetSelectedTextCalled;
103         public boolean isPerformContextMenuActionCalled;
104         public boolean isPerformEditorActionCalled;
105         public boolean isPerformPrivateCommandCalled;
106         public boolean isReportFullscreenModeCalled;
107         public boolean isSendKeyEventCalled;
108         public boolean isSetComposingTextCalled;
109         public boolean isSetComposingRegionCalled;
110         public boolean isSetSelectionCalled;
111 
beginBatchEdit()112         public boolean beginBatchEdit() {
113             isBeginBatchEditCalled = true;
114             return false;
115         }
116 
clearMetaKeyStates(int states)117         public boolean clearMetaKeyStates(int states) {
118             isClearMetaKeyStatesCalled = true;
119             return false;
120         }
121 
commitCompletion(CompletionInfo text)122         public boolean commitCompletion(CompletionInfo text) {
123             isCommitCompletionCalled = true;
124             return false;
125         }
126 
commitCorrection(CorrectionInfo info)127         public boolean commitCorrection(CorrectionInfo info) {
128             isCommitCorrectionCalled = true;
129             return false;
130         }
131 
commitText(CharSequence text, int newCursorPosition)132         public boolean commitText(CharSequence text, int newCursorPosition) {
133             isCommitTextCalled = true;
134             return false;
135         }
136 
deleteSurroundingText(int beforeLength, int afterLength)137         public boolean deleteSurroundingText(int beforeLength, int afterLength) {
138             isDeleteSurroundingTextCalled = true;
139             return false;
140         }
141 
endBatchEdit()142         public boolean endBatchEdit() {
143             isEndBatchEditCalled = true;
144             return false;
145         }
146 
finishComposingText()147         public boolean finishComposingText() {
148             isFinishComposingTextCalled = true;
149             return false;
150         }
151 
getCursorCapsMode(int reqModes)152         public int getCursorCapsMode(int reqModes) {
153             isGetCursorCapsModeCalled = true;
154             return 0;
155         }
156 
getExtractedText(ExtractedTextRequest request, int flags)157         public ExtractedText getExtractedText(ExtractedTextRequest request, int flags) {
158             isGetExtractedTextCalled = true;
159             return null;
160         }
161 
getTextAfterCursor(int n, int flags)162         public CharSequence getTextAfterCursor(int n, int flags) {
163             isGetTextAfterCursorCalled = true;
164             return null;
165         }
166 
getTextBeforeCursor(int n, int flags)167         public CharSequence getTextBeforeCursor(int n, int flags) {
168             isGetTextBeforeCursorCalled = true;
169             return null;
170         }
171 
getSelectedText(int flags)172         public CharSequence getSelectedText(int flags) {
173             isGetSelectedTextCalled = true;
174             return null;
175         }
176 
performContextMenuAction(int id)177         public boolean performContextMenuAction(int id) {
178             isPerformContextMenuActionCalled = true;
179             return false;
180         }
181 
performEditorAction(int editorAction)182         public boolean performEditorAction(int editorAction) {
183             isPerformEditorActionCalled = true;
184             return false;
185         }
186 
performPrivateCommand(String action, Bundle data)187         public boolean performPrivateCommand(String action, Bundle data) {
188             isPerformPrivateCommandCalled = true;
189             return false;
190         }
191 
reportFullscreenMode(boolean enabled)192         public boolean reportFullscreenMode(boolean enabled) {
193             isReportFullscreenModeCalled = true;
194             return false;
195         }
196 
sendKeyEvent(KeyEvent event)197         public boolean sendKeyEvent(KeyEvent event) {
198             isSendKeyEventCalled = true;
199             return false;
200         }
201 
setComposingText(CharSequence text, int newCursorPosition)202         public boolean setComposingText(CharSequence text, int newCursorPosition) {
203             isSetComposingTextCalled = true;
204             return false;
205         }
206 
setComposingRegion(int start, int end)207         public boolean setComposingRegion(int start, int end) {
208             isSetComposingRegionCalled = true;
209             return false;
210         }
211 
setSelection(int start, int end)212         public boolean setSelection(int start, int end) {
213             isSetSelectionCalled = true;
214             return false;
215         }
216     }
217 }
218