Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result
textInputMethodHandler.clearClient();
result.success(null);
break;
case "TextInput.finishAutofillContext":
textInputMethodHandler.finishAutofillContext((boolean) args);
result.success(null);
break;
default:
result.notImplemented();
break;
Expand Down Expand Up @@ -284,6 +288,18 @@ public interface TextInputMethodHandler {
*/
void requestAutofill();

/**
* Requests that the {@link AutofillManager} cancel or commit the current autofill context.
*
* <p>The method calls {@link android.view.autofill.AutofillManager#commit()} when {@code
* shouldSave} is true, and calls {@link android.view.autofill.AutofillManager#cancel()}
* otherwise.
*
* @param shouldSave whether the active autofill service should save the current user input for
* future use.
*/
void finishAutofillContext(boolean shouldSave);

// TODO(mattcarroll): javadoc
void setClient(int textInputClientId, @NonNull Configuration configuration);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,18 @@ public void requestAutofill() {
notifyViewEntered();
}

@Override
public void finishAutofillContext(boolean shouldSave) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O || afm == null) {
return;
}
if (shouldSave) {
afm.commit();
} else {
afm.cancel();
}
}

@Override
public void setClient(
int textInputClientId, TextInputChannel.Configuration configuration) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ private void verifyMethodCall(ByteBuffer buffer, String methodName, String[] exp
}
}

private static void sendToBinaryMessageHandler(
BinaryMessenger.BinaryMessageHandler binaryMessageHandler, String method, Object args) {
MethodCall methodCall = new MethodCall(method, args);
ByteBuffer encodedMethodCall = JSONMethodCodec.INSTANCE.encodeMethodCall(methodCall);
binaryMessageHandler.onMessage(
(ByteBuffer) encodedMethodCall.flip(), mock(BinaryMessenger.BinaryReply.class));
}

@Test
public void textInputPlugin_RequestsReattachOnCreation() throws JSONException {
// Initialize a general TextInputPlugin.
Expand Down Expand Up @@ -531,6 +539,33 @@ public void autofill_onProvideVirtualViewStructure_single() {
verify(children[0]).setDimens(anyInt(), anyInt(), anyInt(), anyInt(), geq(0), geq(0));
}

@Test
public void respondsToInputChannelMessages() {
ArgumentCaptor<BinaryMessenger.BinaryMessageHandler> binaryMessageHandlerCaptor =
ArgumentCaptor.forClass(BinaryMessenger.BinaryMessageHandler.class);
DartExecutor mockBinaryMessenger = mock(DartExecutor.class);
TextInputChannel.TextInputMethodHandler mockHandler =
mock(TextInputChannel.TextInputMethodHandler.class);
TextInputChannel textInputChannel = new TextInputChannel(mockBinaryMessenger);

textInputChannel.setTextInputMethodHandler(mockHandler);

verify(mockBinaryMessenger, times(1))
.setMessageHandler(any(String.class), binaryMessageHandlerCaptor.capture());

BinaryMessenger.BinaryMessageHandler binaryMessageHandler =
binaryMessageHandlerCaptor.getValue();

sendToBinaryMessageHandler(binaryMessageHandler, "TextInput.requestAutofill", null);
verify(mockHandler, times(1)).requestAutofill();

sendToBinaryMessageHandler(binaryMessageHandler, "TextInput.finishAutofillContext", true);
verify(mockHandler, times(1)).finishAutofillContext(true);

sendToBinaryMessageHandler(binaryMessageHandler, "TextInput.finishAutofillContext", false);
verify(mockHandler, times(1)).finishAutofillContext(false);
}

@Implements(InputMethodManager.class)
public static class TestImm extends ShadowInputMethodManager {
private InputMethodSubtype currentInputMethodSubtype;
Expand Down
Loading