From 72e6f7f5674ba3a670a70192914875f3304690dd Mon Sep 17 00:00:00 2001 From: Bruno Leroux Date: Fri, 19 May 2023 05:46:05 +0200 Subject: [PATCH] [Android] Fix BasicMessageChannel.resizeChannelBuffer (flutter/engine#41982) ## Description This PR updates `BasicMessageChannel.resizeChannelBuffer` implementation. Previous implementation builds a wrong `ByteBuffer` and was not tested so it is difficult to know when it stopped working. ## Related Issue Fixes https://github.com/flutter/flutter/issues/126632 ## Tests Adds 1 test. --- .../plugin/common/BasicMessageChannel.java | 6 ++- .../plugin/common/MethodChannelTest.java | 44 +++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 engine/src/flutter/shell/platform/android/test/io/flutter/plugin/common/MethodChannelTest.java diff --git a/engine/src/flutter/shell/platform/android/io/flutter/plugin/common/BasicMessageChannel.java b/engine/src/flutter/shell/platform/android/io/flutter/plugin/common/BasicMessageChannel.java index 284df4f03f..af56137c8f 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/plugin/common/BasicMessageChannel.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/plugin/common/BasicMessageChannel.java @@ -147,8 +147,10 @@ public final class BasicMessageChannel { @NonNull BinaryMessenger messenger, @NonNull String channel, int newSize) { Charset charset = Charset.forName("UTF-8"); String messageString = String.format(Locale.US, "resize\r%s\r%d", channel, newSize); - ByteBuffer message = ByteBuffer.wrap(messageString.getBytes(charset)); - messenger.send(CHANNEL_BUFFERS_CHANNEL, message); + final byte[] bytes = messageString.getBytes(charset); + ByteBuffer packet = ByteBuffer.allocateDirect(bytes.length); + packet.put(bytes); + messenger.send(BasicMessageChannel.CHANNEL_BUFFERS_CHANNEL, packet); } /** A handler of incoming messages. */ diff --git a/engine/src/flutter/shell/platform/android/test/io/flutter/plugin/common/MethodChannelTest.java b/engine/src/flutter/shell/platform/android/test/io/flutter/plugin/common/MethodChannelTest.java new file mode 100644 index 0000000000..c4ab750c2e --- /dev/null +++ b/engine/src/flutter/shell/platform/android/test/io/flutter/plugin/common/MethodChannelTest.java @@ -0,0 +1,44 @@ +package io.flutter.plugin.common; + +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import android.content.res.AssetManager; +import androidx.test.ext.junit.runners.AndroidJUnit4; +import io.flutter.embedding.engine.FlutterJNI; +import io.flutter.embedding.engine.dart.DartExecutor; +import java.nio.ByteBuffer; +import java.nio.charset.Charset; +import java.util.Locale; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.annotation.Config; + +@Config(manifest = Config.NONE) +@RunWith(AndroidJUnit4.class) +public class MethodChannelTest { + @Test + public void methodChannel_resizeChannelBuffer() { + FlutterJNI mockFlutterJNI = mock(FlutterJNI.class); + DartExecutor dartExecutor = new DartExecutor(mockFlutterJNI, mock(AssetManager.class)); + String channel = "flutter/test"; + MethodChannel rawChannel = new MethodChannel(dartExecutor, channel); + + int newSize = 3; + rawChannel.resizeChannelBuffer(newSize); + + Charset charset = Charset.forName("UTF-8"); + String messageString = String.format(Locale.US, "resize\r%s\r%d", channel, newSize); + final byte[] bytes = messageString.getBytes(charset); + ByteBuffer packet = ByteBuffer.allocateDirect(bytes.length); + packet.put(bytes); + + // Verify that DartExecutor sent the correct message to FlutterJNI. + verify(mockFlutterJNI, times(1)) + .dispatchPlatformMessage( + eq(BasicMessageChannel.CHANNEL_BUFFERS_CHANNEL), eq(packet), anyInt(), anyInt()); + } +}