1 /* 2 * Copyright (C) 2022 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.accessibilityservice.cts.utils; 18 19 import android.os.Bundle; 20 import android.os.Handler; 21 import android.view.KeyEvent; 22 import android.view.inputmethod.CompletionInfo; 23 import android.view.inputmethod.CorrectionInfo; 24 import android.view.inputmethod.ExtractedText; 25 import android.view.inputmethod.ExtractedTextRequest; 26 import android.view.inputmethod.InputConnection; 27 import android.view.inputmethod.InputConnectionWrapper; 28 import android.view.inputmethod.InputContentInfo; 29 import android.view.inputmethod.SurroundingText; 30 import android.view.inputmethod.TextAttribute; 31 32 /** 33 * A special version of {@link InputConnectionWrapper} so that you can forward method invocations 34 * to another {@link InputConnection}. 35 * 36 * <p>Useful for mocking/spying.</p> 37 */ 38 public final class InputConnectionSplitter extends InputConnectionWrapper { 39 private final InputConnection mForNotify; 40 InputConnectionSplitter(InputConnection target, InputConnection forNotify)41 public InputConnectionSplitter(InputConnection target, InputConnection forNotify) { 42 super(target, false); 43 mForNotify = forNotify; 44 } 45 46 @Override getTextBeforeCursor(int n, int flags)47 public CharSequence getTextBeforeCursor(int n, int flags) { 48 mForNotify.getTextBeforeCursor(n, flags); 49 return super.getTextBeforeCursor(n, flags); 50 } 51 52 @Override getTextAfterCursor(int n, int flags)53 public CharSequence getTextAfterCursor(int n, int flags) { 54 mForNotify.getTextBeforeCursor(n, flags); 55 return super.getTextBeforeCursor(n, flags); 56 } 57 58 @Override getSelectedText(int flags)59 public CharSequence getSelectedText(int flags) { 60 mForNotify.getSelectedText(flags); 61 return super.getSelectedText(flags); 62 } 63 64 @Override getSurroundingText(int beforeLength, int afterLength, int flags)65 public SurroundingText getSurroundingText(int beforeLength, int afterLength, int flags) { 66 mForNotify.getSurroundingText(beforeLength, afterLength, flags); 67 return super.getSurroundingText(beforeLength, afterLength, flags); 68 } 69 70 @Override getCursorCapsMode(int reqModes)71 public int getCursorCapsMode(int reqModes) { 72 mForNotify.getCursorCapsMode(reqModes); 73 return super.getCursorCapsMode(reqModes); 74 } 75 76 @Override getExtractedText(ExtractedTextRequest request, int flags)77 public ExtractedText getExtractedText(ExtractedTextRequest request, int flags) { 78 mForNotify.getExtractedText(request, flags); 79 return super.getExtractedText(request, flags); 80 } 81 82 @Override deleteSurroundingTextInCodePoints(int beforeLength, int afterLength)83 public boolean deleteSurroundingTextInCodePoints(int beforeLength, int afterLength) { 84 mForNotify.deleteSurroundingTextInCodePoints(beforeLength, afterLength); 85 return super.deleteSurroundingTextInCodePoints(beforeLength, afterLength); 86 } 87 88 @Override deleteSurroundingText(int beforeLength, int afterLength)89 public boolean deleteSurroundingText(int beforeLength, int afterLength) { 90 mForNotify.deleteSurroundingText(beforeLength, afterLength); 91 return super.deleteSurroundingText(beforeLength, afterLength); 92 } 93 94 @Override setComposingText(CharSequence text, int newCursorPosition)95 public boolean setComposingText(CharSequence text, int newCursorPosition) { 96 mForNotify.setComposingText(text, newCursorPosition); 97 return super.setComposingText(text, newCursorPosition); 98 } 99 100 @Override setComposingText(CharSequence text, int newCursorPosition, TextAttribute textAttribute)101 public boolean setComposingText(CharSequence text, 102 int newCursorPosition, TextAttribute textAttribute) { 103 mForNotify.setComposingText(text, newCursorPosition, textAttribute); 104 return super.setComposingText(text, newCursorPosition, textAttribute); 105 } 106 107 @Override setComposingRegion(int start, int end)108 public boolean setComposingRegion(int start, int end) { 109 mForNotify.setComposingRegion(start, end); 110 return super.setComposingRegion(start, end); 111 } 112 113 @Override setComposingRegion(int start, int end, TextAttribute textAttribute)114 public boolean setComposingRegion(int start, int end, TextAttribute textAttribute) { 115 mForNotify.setComposingRegion(start, end, textAttribute); 116 return super.setComposingRegion(start, end, textAttribute); 117 } 118 119 @Override finishComposingText()120 public boolean finishComposingText() { 121 mForNotify.finishComposingText(); 122 return super.finishComposingText(); 123 } 124 125 @Override commitText(CharSequence text, int newCursorPosition)126 public boolean commitText(CharSequence text, int newCursorPosition) { 127 mForNotify.commitText(text, newCursorPosition); 128 return super.commitText(text, newCursorPosition); 129 } 130 131 @Override commitText(CharSequence text, int newCursorPosition, TextAttribute textAttribute)132 public boolean commitText(CharSequence text, int newCursorPosition, 133 TextAttribute textAttribute) { 134 mForNotify.commitText(text, newCursorPosition, textAttribute); 135 return super.commitText(text, newCursorPosition, textAttribute); 136 } 137 138 @Override commitCompletion(CompletionInfo text)139 public boolean commitCompletion(CompletionInfo text) { 140 mForNotify.commitCompletion(text); 141 return super.commitCompletion(text); 142 } 143 144 @Override commitCorrection(CorrectionInfo correctionInfo)145 public boolean commitCorrection(CorrectionInfo correctionInfo) { 146 mForNotify.commitCorrection(correctionInfo); 147 return super.commitCorrection(correctionInfo); 148 } 149 150 @Override setSelection(int start, int end)151 public boolean setSelection(int start, int end) { 152 mForNotify.setSelection(start, end); 153 return super.setSelection(start, end); 154 } 155 156 @Override performEditorAction(int editorAction)157 public boolean performEditorAction(int editorAction) { 158 mForNotify.performEditorAction(editorAction); 159 return super.performEditorAction(editorAction); 160 } 161 162 @Override performContextMenuAction(int id)163 public boolean performContextMenuAction(int id) { 164 mForNotify.performContextMenuAction(id); 165 return super.performContextMenuAction(id); 166 } 167 168 @Override beginBatchEdit()169 public boolean beginBatchEdit() { 170 mForNotify.beginBatchEdit(); 171 return super.beginBatchEdit(); 172 } 173 174 @Override endBatchEdit()175 public boolean endBatchEdit() { 176 mForNotify.endBatchEdit(); 177 return super.endBatchEdit(); 178 } 179 180 @Override sendKeyEvent(KeyEvent event)181 public boolean sendKeyEvent(KeyEvent event) { 182 mForNotify.sendKeyEvent(event); 183 return super.sendKeyEvent(event); 184 185 } 186 187 @Override clearMetaKeyStates(int states)188 public boolean clearMetaKeyStates(int states) { 189 mForNotify.clearMetaKeyStates(states); 190 return super.clearMetaKeyStates(states); 191 } 192 193 @Override reportFullscreenMode(boolean enabled)194 public boolean reportFullscreenMode(boolean enabled) { 195 mForNotify.reportFullscreenMode(enabled); 196 return super.reportFullscreenMode(enabled); 197 } 198 199 @Override performSpellCheck()200 public boolean performSpellCheck() { 201 mForNotify.performSpellCheck(); 202 return super.performSpellCheck(); 203 } 204 205 @Override performPrivateCommand(String action, Bundle data)206 public boolean performPrivateCommand(String action, Bundle data) { 207 mForNotify.performPrivateCommand(action, data); 208 return super.performPrivateCommand(action, data); 209 } 210 211 @Override requestCursorUpdates(int cursorUpdateMode)212 public boolean requestCursorUpdates(int cursorUpdateMode) { 213 mForNotify.requestCursorUpdates(cursorUpdateMode); 214 return super.requestCursorUpdates(cursorUpdateMode); 215 } 216 217 @Override getHandler()218 public Handler getHandler() { 219 mForNotify.getHandler(); 220 return super.getHandler(); 221 } 222 223 @Override closeConnection()224 public void closeConnection() { 225 mForNotify.closeConnection(); 226 super.closeConnection(); 227 } 228 229 @Override commitContent(InputContentInfo inputContentInfo, int flags, Bundle opts)230 public boolean commitContent(InputContentInfo inputContentInfo, int flags, Bundle opts) { 231 mForNotify.commitContent(inputContentInfo, flags, opts); 232 return super.commitContent(inputContentInfo, flags, opts); 233 } 234 235 @Override setImeConsumesInput(boolean imeConsumesInput)236 public boolean setImeConsumesInput(boolean imeConsumesInput) { 237 mForNotify.setImeConsumesInput(imeConsumesInput); 238 return super.setImeConsumesInput(imeConsumesInput); 239 } 240 } 241