未验证 提交 2c0eee43 编写于 作者: G Gary Qian 提交者: GitHub

Always set mEditable values when different in TextPlugin (#13951)

上级 c139def5
...@@ -102,6 +102,10 @@ public class TextInputPlugin { ...@@ -102,6 +102,10 @@ public class TextInputPlugin {
return mImm; return mImm;
} }
@VisibleForTesting Editable getEditable() {
return mEditable;
}
/*** /***
* Use the current platform view input connection until unlockPlatformViewInputConnection is called. * Use the current platform view input connection until unlockPlatformViewInputConnection is called.
* *
...@@ -306,15 +310,21 @@ public class TextInputPlugin { ...@@ -306,15 +310,21 @@ public class TextInputPlugin {
} }
@VisibleForTesting void setTextInputEditingState(View view, TextInputChannel.TextEditState state) { @VisibleForTesting void setTextInputEditingState(View view, TextInputChannel.TextEditState state) {
if (!restartAlwaysRequired && !mRestartInputPending && state.text.equals(mEditable.toString())) { // Always replace the contents of mEditable if the text differs
if (!state.text.equals(mEditable.toString())) {
mEditable.replace(0, mEditable.length(), state.text);
}
// Always apply state to selection which handles updating the selection if needed.
applyStateToSelection(state); applyStateToSelection(state);
// Use updateSelection to update imm on selection if it is not neccessary to restart.
if (!restartAlwaysRequired && !mRestartInputPending) {
mImm.updateSelection(mView, Math.max(Selection.getSelectionStart(mEditable), 0), mImm.updateSelection(mView, Math.max(Selection.getSelectionStart(mEditable), 0),
Math.max(Selection.getSelectionEnd(mEditable), 0), Math.max(Selection.getSelectionEnd(mEditable), 0),
BaseInputConnection.getComposingSpanStart(mEditable), BaseInputConnection.getComposingSpanStart(mEditable),
BaseInputConnection.getComposingSpanEnd(mEditable)); BaseInputConnection.getComposingSpanEnd(mEditable));
// Restart if there is a pending restart or the device requires a force restart
// (see isRestartAlwaysRequired). Restarting will also update the selection.
} else { } else {
mEditable.replace(0, mEditable.length(), state.text);
applyStateToSelection(state);
mImm.restartInput(view); mImm.restartInput(view);
mRestartInputPending = false; mRestartInputPending = false;
} }
......
...@@ -29,6 +29,7 @@ import io.flutter.plugin.common.MethodCall; ...@@ -29,6 +29,7 @@ import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.platform.PlatformViewsController; import io.flutter.plugin.platform.PlatformViewsController;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy; import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.times;
...@@ -73,6 +74,27 @@ public class TextInputPluginTest { ...@@ -73,6 +74,27 @@ public class TextInputPluginTest {
assertEquals(1, testImm.getRestartCount(testView)); assertEquals(1, testImm.getRestartCount(testView));
} }
@Test
public void setTextInputEditingState_alwaysSetEditableWhenDifferent() {
// Initialize a general TextInputPlugin.
InputMethodSubtype inputMethodSubtype = mock(InputMethodSubtype.class);
TestImm testImm = Shadow.extract(RuntimeEnvironment.application.getSystemService(Context.INPUT_METHOD_SERVICE));
testImm.setCurrentInputMethodSubtype(inputMethodSubtype);
View testView = new View(RuntimeEnvironment.application);
TextInputPlugin textInputPlugin = new TextInputPlugin(testView, mock(DartExecutor.class), mock(PlatformViewsController.class));
textInputPlugin.setTextInputClient(0, new TextInputChannel.Configuration(false, false, true, TextInputChannel.TextCapitalization.NONE, null, null, null));
// There's a pending restart since we initialized the text input client. Flush that now. With changed text, we should
// always set the Editable contents.
textInputPlugin.setTextInputEditingState(testView, new TextInputChannel.TextEditState("hello", 0, 0));
assertEquals(1, testImm.getRestartCount(testView));
assertTrue(textInputPlugin.getEditable().toString().equals("hello"));
// No pending restart, set Editable contents anyways.
textInputPlugin.setTextInputEditingState(testView, new TextInputChannel.TextEditState("Shibuyawoo", 0, 0));
assertEquals(1, testImm.getRestartCount(testView));
assertTrue(textInputPlugin.getEditable().toString().equals("Shibuyawoo"));
}
// See https://github.com/flutter/flutter/issues/29341 and https://github.com/flutter/flutter/issues/31512 // See https://github.com/flutter/flutter/issues/29341 and https://github.com/flutter/flutter/issues/31512
// All modern Samsung keybords are affected including non-korean languages and thus // All modern Samsung keybords are affected including non-korean languages and thus
// need the restart. // need the restart.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册