|
4 | 4 | import android.content.res.AssetManager; |
5 | 5 | import android.provider.Settings; |
6 | 6 | import android.util.SparseIntArray; |
| 7 | +import android.view.KeyEvent; |
7 | 8 | import android.view.View; |
| 9 | +import android.view.inputmethod.EditorInfo; |
| 10 | +import android.view.inputmethod.InputConnection; |
8 | 11 | import android.view.inputmethod.InputMethodManager; |
9 | 12 | import android.view.inputmethod.InputMethodSubtype; |
10 | 13 |
|
11 | 14 | import java.nio.ByteBuffer; |
| 15 | +import java.util.Arrays; |
12 | 16 |
|
13 | 17 | import org.junit.Test; |
14 | 18 | import org.junit.runner.RunWith; |
|
21 | 25 | import org.robolectric.shadows.ShadowBuild; |
22 | 26 | import org.robolectric.shadows.ShadowInputMethodManager; |
23 | 27 |
|
| 28 | +import io.flutter.Log; |
24 | 29 | import io.flutter.embedding.engine.FlutterJNI; |
25 | 30 | import io.flutter.embedding.engine.dart.DartExecutor; |
26 | 31 | import io.flutter.embedding.engine.systemchannels.TextInputChannel; |
@@ -155,6 +160,33 @@ public void setTextInputEditingState_nullInputMethodSubtype() { |
155 | 160 | assertEquals(1, testImm.getRestartCount(testView)); |
156 | 161 | } |
157 | 162 |
|
| 163 | + @Test |
| 164 | + public void inputConnection_createsActionFromEnter() { |
| 165 | + Log.setLogLevel(android.util.Log.VERBOSE); |
| 166 | + TestImm testImm = Shadow.extract(RuntimeEnvironment.application.getSystemService(Context.INPUT_METHOD_SERVICE)); |
| 167 | + FlutterJNI mockFlutterJni = mock(FlutterJNI.class); |
| 168 | + View testView = new View(RuntimeEnvironment.application); |
| 169 | + DartExecutor dartExecutor = spy(new DartExecutor(mockFlutterJni, mock(AssetManager.class))); |
| 170 | + TextInputPlugin textInputPlugin = new TextInputPlugin(testView, dartExecutor, mock(PlatformViewsController.class)); |
| 171 | + textInputPlugin.setTextInputClient(0, new TextInputChannel.Configuration(false, false, true, TextInputChannel.TextCapitalization.NONE, new TextInputChannel.InputType(TextInputChannel.TextInputType.TEXT, false, false), null, null)); |
| 172 | + // There's a pending restart since we initialized the text input client. Flush that now. |
| 173 | + textInputPlugin.setTextInputEditingState(testView, new TextInputChannel.TextEditState("", 0, 0)); |
| 174 | + |
| 175 | + EditorInfo editorInfo = new EditorInfo(); |
| 176 | + InputConnection connection = textInputPlugin.createInputConnection(testView, editorInfo); |
| 177 | + |
| 178 | + ByteBuffer message = JSONMethodCodec.INSTANCE.encodeMethodCall( |
| 179 | + new MethodCall("TextInputClient.performAction", Arrays.asList(0, "TextInputAction.done"))); |
| 180 | + connection.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER)); |
| 181 | + verify(dartExecutor, times(2)).send("flutter/textinput", message, null); |
| 182 | + connection.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_ENTER)); |
| 183 | + |
| 184 | + connection.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_NUMPAD_ENTER)); |
| 185 | + verify(dartExecutor, times(3)).send("flutter/textinput", message, null); |
| 186 | + connection.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_NUMPAD_ENTER)); |
| 187 | + } |
| 188 | + |
| 189 | + |
158 | 190 | @Implements(InputMethodManager.class) |
159 | 191 | public static class TestImm extends ShadowInputMethodManager { |
160 | 192 | private InputMethodSubtype currentInputMethodSubtype; |
|
0 commit comments