receiveAction method

Future<void> receiveAction(
  1. TextInputAction action
)

Simulates the user pressing one of the TextInputAction buttons. Does not check that the TextInputAction performed is an acceptable one based on the inputAction setClientArgs.

This can be called even if the TestTextInput has not been registered.

If this is used to inject an action when there is a real IME connection, for example when using the integration_test library, there is a risk that the real IME will become confused as to the current state of input.

Implementation

Future<void> receiveAction(TextInputAction action) async {
  return TestAsyncUtils.guard(() {
    final Completer<void> completer = Completer<void>();
    TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger.handlePlatformMessage(
      SystemChannels.textInput.name,
      SystemChannels.textInput.codec.encodeMethodCall(
        MethodCall(
          'TextInputClient.performAction',
          <dynamic>[_client ?? -1, action.toString()],
        ),
      ),
      (ByteData? data) {
        assert(data != null);
        try {
          // Decoding throws a PlatformException if the data represents an
          // error, and that's all we care about here.
          SystemChannels.textInput.codec.decodeEnvelope(data!);
          // If we reach here then no error was found. Complete without issue.
          completer.complete();
        } catch (error) {
          // An exception occurred as a result of receiveAction()'ing. Report
          // that error.
          completer.completeError(error);
        }
      },
    );
    return completer.future;
  });
}