This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
Work around Samsung keyboard issue #12432
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
shell/platform/android/test/io/flutter/plugin/editing/TextInputPluginTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| package io.flutter.plugin.editing; | ||
|
|
||
| import android.content.Context; | ||
| import android.util.SparseIntArray; | ||
| import android.view.View; | ||
| import android.view.inputmethod.InputMethodManager; | ||
| import android.view.inputmethod.InputMethodSubtype; | ||
|
|
||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.robolectric.RobolectricTestRunner; | ||
| import org.robolectric.RuntimeEnvironment; | ||
| import org.robolectric.annotation.Config; | ||
| import org.robolectric.annotation.Implementation; | ||
| import org.robolectric.annotation.Implements; | ||
| import org.robolectric.shadow.api.Shadow; | ||
| import org.robolectric.shadows.ShadowBuild; | ||
| import org.robolectric.shadows.ShadowInputMethodManager; | ||
|
|
||
| import io.flutter.embedding.engine.dart.DartExecutor; | ||
| import io.flutter.embedding.engine.systemchannels.TextInputChannel; | ||
| import io.flutter.plugin.platform.PlatformViewsController; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.mockito.Mockito.mock; | ||
|
|
||
| @Config(manifest = Config.NONE, shadows = TextInputPluginTest.TestImm.class) | ||
| @RunWith(RobolectricTestRunner.class) | ||
| public class TextInputPluginTest { | ||
| @Test | ||
| public void setTextInputEditingState_doesNotRestartWhenTextIsIdentical() { | ||
| // 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, TextInputChannel.TextCapitalization.NONE, null, null, null)); | ||
| // There's a pending restart since we initialized the text input client. Flush that now. | ||
| textInputPlugin.setTextInputEditingState(testView, new TextInputChannel.TextEditState("", 0, 0)); | ||
|
|
||
| // Move the cursor. | ||
| assertEquals(1, testImm.getRestartCount(testView)); | ||
| textInputPlugin.setTextInputEditingState(testView, new TextInputChannel.TextEditState("", 0, 0)); | ||
|
|
||
| // Verify that we haven't restarted the input. | ||
| assertEquals(1, testImm.getRestartCount(testView)); | ||
| } | ||
|
|
||
| // See https://github.com/flutter/flutter/issues/29341 | ||
| @Test | ||
| public void setTextInputEditingState_alwaysRestartsOnAffectedDevices() { | ||
| // Initialize a TextInputPlugin that needs to be always restarted. | ||
| ShadowBuild.setManufacturer("samsung"); | ||
| InputMethodSubtype inputMethodSubtype = new InputMethodSubtype(0, 0, /*locale=*/"ko", "", "", false, false); | ||
| 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, TextInputChannel.TextCapitalization.NONE, null, null, null)); | ||
| // There's a pending restart since we initialized the text input client. Flush that now. | ||
| textInputPlugin.setTextInputEditingState(testView, new TextInputChannel.TextEditState("", 0, 0)); | ||
|
|
||
| // Move the cursor. | ||
| assertEquals(1, testImm.getRestartCount(testView)); | ||
| textInputPlugin.setTextInputEditingState(testView, new TextInputChannel.TextEditState("", 0, 0)); | ||
|
|
||
| // Verify that we've restarted the input. | ||
| assertEquals(2, testImm.getRestartCount(testView)); | ||
| } | ||
|
|
||
| @Implements(InputMethodManager.class) | ||
| public static class TestImm extends ShadowInputMethodManager { | ||
| private InputMethodSubtype currentInputMethodSubtype; | ||
| private SparseIntArray restartCounter = new SparseIntArray(); | ||
|
|
||
| public TestImm() { | ||
| } | ||
|
|
||
| @Implementation | ||
| public InputMethodSubtype getCurrentInputMethodSubtype() { | ||
| return currentInputMethodSubtype; | ||
| } | ||
|
|
||
| @Implementation | ||
| public void restartInput(View view) { | ||
| int count = restartCounter.get(view.hashCode(), /*defaultValue=*/0) + 1; | ||
| restartCounter.put(view.hashCode(), count); | ||
| } | ||
|
|
||
| public void setCurrentInputMethodSubtype(InputMethodSubtype inputMethodSubtype) { | ||
| this.currentInputMethodSubtype = inputMethodSubtype; | ||
| } | ||
|
|
||
| public int getRestartCount(View view) { | ||
| return restartCounter.get(view.hashCode(), /*defaultValue=*/0); | ||
| } | ||
| } | ||
| } | ||
|
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are there any cases where this will cause the IMM to restart besides tapping to move the cursor? I wonder if there are any optimizations we can make by not restarting in those cases if so. Not sure though, I'll have to play around with the device tomorrow.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question. From reading through the code and manually testing it out it looked like just tapping to move, but I'm not totally convinced that I'm seeing the whole situation here. @jason-simmons do you know?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The engine asks the IMM to restart input when the app switches to a new text editing client or when the app sends an update to editor state originating from the Dart side.
In the latter case it's possible that we could diff the current and new state and do something less expensive than a full restart if the diff is simple enough. But AFAIK this hasn't been noticeable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems fine to me as-is after playing around with it. I only see it restart when the user pastes into the input, but that may be necessary, and it shouldn't be a performance problem anyway.