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
[Android text input] fix android autofill on focused text field #24463
Merged
fluttergithubbot
merged 2 commits into
flutter:master
from
LongCatIsLooong:fix-android-autofill
Feb 18, 2021
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,6 +27,7 @@ | |
| import android.provider.Settings; | ||
| import android.text.InputType; | ||
| import android.text.Selection; | ||
| import android.util.SparseArray; | ||
| import android.util.SparseIntArray; | ||
| import android.view.KeyEvent; | ||
| import android.view.View; | ||
|
|
@@ -55,6 +56,7 @@ | |
| import io.flutter.plugin.platform.PlatformViewsController; | ||
| import java.nio.ByteBuffer; | ||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import org.json.JSONArray; | ||
| import org.json.JSONException; | ||
|
|
@@ -927,6 +929,92 @@ public void autofill_testLifeCycle() { | |
| assertEquals("1".hashCode(), testAfm.exitId); | ||
| } | ||
|
|
||
| @Test | ||
| public void autofill_testAutofillUpdatesTheFramework() { | ||
| if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { | ||
| return; | ||
| } | ||
|
|
||
| TestAfm testAfm = | ||
| Shadow.extract(RuntimeEnvironment.application.getSystemService(AutofillManager.class)); | ||
| FlutterView testView = new FlutterView(RuntimeEnvironment.application); | ||
| TextInputChannel textInputChannel = spy(new TextInputChannel(mock(DartExecutor.class))); | ||
| TextInputPlugin textInputPlugin = | ||
| new TextInputPlugin(testView, textInputChannel, mock(PlatformViewsController.class)); | ||
|
|
||
| // Set up an autofill scenario with 2 fields. | ||
| final TextInputChannel.Configuration.Autofill autofill1 = | ||
| new TextInputChannel.Configuration.Autofill( | ||
| "1", | ||
| new String[] {"HINT1"}, | ||
| new TextInputChannel.TextEditState("field 1", 0, 0, -1, -1)); | ||
| final TextInputChannel.Configuration.Autofill autofill2 = | ||
| new TextInputChannel.Configuration.Autofill( | ||
| "2", | ||
| new String[] {"HINT2", "EXTRA"}, | ||
| new TextInputChannel.TextEditState("field 2", 0, 0, -1, -1)); | ||
|
|
||
| final TextInputChannel.Configuration config1 = | ||
| new TextInputChannel.Configuration( | ||
| false, | ||
| false, | ||
| true, | ||
| TextInputChannel.TextCapitalization.NONE, | ||
| null, | ||
| null, | ||
| null, | ||
| autofill1, | ||
| null); | ||
| final TextInputChannel.Configuration config2 = | ||
| new TextInputChannel.Configuration( | ||
| false, | ||
| false, | ||
| true, | ||
| TextInputChannel.TextCapitalization.NONE, | ||
| null, | ||
| null, | ||
| null, | ||
| autofill2, | ||
| null); | ||
|
|
||
| final TextInputChannel.Configuration autofillConfiguration = | ||
| new TextInputChannel.Configuration( | ||
| false, | ||
| false, | ||
| true, | ||
| TextInputChannel.TextCapitalization.NONE, | ||
| null, | ||
| null, | ||
| null, | ||
| autofill1, | ||
| new TextInputChannel.Configuration[] {config1, config2}); | ||
|
|
||
| textInputPlugin.setTextInputClient(0, autofillConfiguration); | ||
| textInputPlugin.setTextInputEditingState( | ||
| testView, new TextInputChannel.TextEditState("", 0, 0, -1, -1)); | ||
|
|
||
| final SparseArray<AutofillValue> autofillValues = new SparseArray(); | ||
| autofillValues.append("1".hashCode(), AutofillValue.forText("focused field")); | ||
| autofillValues.append("2".hashCode(), AutofillValue.forText("unfocused field")); | ||
|
|
||
| // Autofill both fields. | ||
| textInputPlugin.autofill(autofillValues); | ||
|
|
||
| // Verify the Editable has been updated. | ||
| assertTrue(textInputPlugin.getEditable().toString().equals("focused field")); | ||
|
|
||
| // The autofill value of the focused field is sent via updateEditingState. | ||
| verify(textInputChannel, times(1)) | ||
| .updateEditingState(anyInt(), eq("focused field"), eq(13), eq(13), eq(-1), eq(-1)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where do the 13s come from?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| final ArgumentCaptor<HashMap> mapCaptor = ArgumentCaptor.forClass(HashMap.class); | ||
|
|
||
| verify(textInputChannel, times(1)).updateEditingStateWithTag(anyInt(), mapCaptor.capture()); | ||
| final TextInputChannel.TextEditState editState = | ||
| (TextInputChannel.TextEditState) mapCaptor.getValue().get("2"); | ||
| assertEquals(editState.text, "unfocused field"); | ||
| } | ||
|
|
||
| @Test | ||
| public void autofill_testSetTextIpnutClientUpdatesSideFields() { | ||
| if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { | ||
|
|
||
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.
Nit: TextEditState => TextEditingState
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 class that this method takes as a parameter is called
TextEditState.