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