From 454b97d69172c5fe3a0ae85568f7504c169b4dfc Mon Sep 17 00:00:00 2001 From: Matt Carroll Date: Fri, 25 Oct 2019 20:07:13 -0700 Subject: [PATCH] Deprecated DartExecutor as BinaryMessenger and added a getBinaryMessenger() method. (#43202) (flutter/engine#13349) --- .../flutter/shell/platform/android/BUILD.gn | 1 + .../embedding/engine/dart/DartExecutor.java | 112 +++++++++++++----- .../test/io/flutter/FlutterTestSuite.java | 2 + .../engine/dart/DartExecutorTest.java | 48 ++++++++ 4 files changed, 131 insertions(+), 32 deletions(-) create mode 100644 engine/src/flutter/shell/platform/android/test/io/flutter/embedding/engine/dart/DartExecutorTest.java diff --git a/engine/src/flutter/shell/platform/android/BUILD.gn b/engine/src/flutter/shell/platform/android/BUILD.gn index 7cc9c05902..7ba8969d5b 100644 --- a/engine/src/flutter/shell/platform/android/BUILD.gn +++ b/engine/src/flutter/shell/platform/android/BUILD.gn @@ -419,6 +419,7 @@ action("robolectric_tests") { "test/io/flutter/embedding/android/FlutterAndroidComponentTest.java", "test/io/flutter/embedding/android/FlutterFragmentTest.java", "test/io/flutter/embedding/android/FlutterViewTest.java", + "test/io/flutter/embedding/engine/dart/DartExecutorTest.java", "test/io/flutter/embedding/engine/FlutterEngineCacheTest.java", "test/io/flutter/embedding/engine/FlutterJNITest.java", "test/io/flutter/embedding/engine/RenderingComponentTest.java", diff --git a/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/dart/DartExecutor.java b/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/dart/DartExecutor.java index d525357c87..4659bf4f91 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/dart/DartExecutor.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/dart/DartExecutor.java @@ -21,9 +21,6 @@ import io.flutter.view.FlutterMain; /** * Configures, bootstraps, and starts executing Dart code. *

- * WARNING: THIS CLASS IS EXPERIMENTAL. DO NOT SHIP A DEPENDENCY ON THIS CODE. - * IF YOU USE IT, WE WILL BREAK YOU. - *

* To specify a top-level Dart function to execute, use a {@link DartEntrypoint} to tell * {@link DartExecutor} where to find the Dart code to execute, and which Dart function to use as * the entrypoint. To execute the entrypoint, pass the {@link DartEntrypoint} to @@ -46,7 +43,9 @@ public class DartExecutor implements BinaryMessenger { @NonNull private final AssetManager assetManager; @NonNull - private final DartMessenger messenger; + private final DartMessenger dartMessenger; + @NonNull + private final BinaryMessenger binaryMessenger; private boolean isApplicationRunning = false; @Nullable private String isolateServiceId; @@ -67,8 +66,9 @@ public class DartExecutor implements BinaryMessenger { public DartExecutor(@NonNull FlutterJNI flutterJNI, @NonNull AssetManager assetManager) { this.flutterJNI = flutterJNI; this.assetManager = assetManager; - this.messenger = new DartMessenger(flutterJNI); - messenger.setMessageHandler("flutter/isolate", isolateChannelMessageHandler); + this.dartMessenger = new DartMessenger(flutterJNI); + dartMessenger.setMessageHandler("flutter/isolate", isolateChannelMessageHandler); + this.binaryMessenger = new DefaultBinaryMessenger(dartMessenger); } /** @@ -84,7 +84,7 @@ public class DartExecutor implements BinaryMessenger { */ public void onAttachedToJNI() { Log.v(TAG, "Attached to JNI. Registering the platform message handler for this Dart execution context."); - flutterJNI.setPlatformMessageHandler(messenger); + flutterJNI.setPlatformMessageHandler(dartMessenger); } /** @@ -158,48 +158,49 @@ public class DartExecutor implements BinaryMessenger { isApplicationRunning = true; } - //------ START BinaryMessenger ----- - /** - * Sends the given {@code message} from Android to Dart over the given {@code channel}. - * - * @param channel the name of the logical channel used for the message. - * @param message the message payload, a direct-allocated {@link ByteBuffer} with the message bytes + * Returns a {@link BinaryMessenger} that can be used to send messages to, and receive + * messages from, Dart code that this {@code DartExecutor} is executing. */ + @NonNull + public BinaryMessenger getBinaryMessenger() { + return binaryMessenger; + } + + //------ START BinaryMessenger (Deprecated: use getBinaryMessenger() instead) ----- + /** + * @deprecated + * Use {@link #getBinaryMessenger()} instead. + */ + @Deprecated @Override @UiThread public void send(@NonNull String channel, @Nullable ByteBuffer message) { - messenger.send(channel, message, null); + binaryMessenger.send(channel, message); } /** - * Sends the given {@code messages} from Android to Dart over the given {@code channel} and - * then has the provided {@code callback} invoked when the Dart side responds. - * - * @param channel the name of the logical channel used for the message. - * @param message the message payload, a direct-allocated {@link ByteBuffer} with the message bytes - * between position zero and current position, or null. - * @param callback a callback invoked when the Dart application responds to the message + * @deprecated + * Use {@link #getBinaryMessenger()} instead. */ + @Deprecated @Override @UiThread public void send(@NonNull String channel, @Nullable ByteBuffer message, @Nullable BinaryMessenger.BinaryReply callback) { - messenger.send(channel, message, callback); + binaryMessenger.send(channel, message, callback); } /** - * Sets the given {@link io.flutter.plugin.common.BinaryMessenger.BinaryMessageHandler} as the - * singular handler for all incoming messages received from the Dart side of this Dart execution - * context. - * - * @param channel the name of the channel. - * @param handler a {@link BinaryMessageHandler} to be invoked on incoming messages, or null. + * @deprecated + * Use {@link #getBinaryMessenger()} instead. */ + @Deprecated @Override @UiThread public void setMessageHandler(@NonNull String channel, @Nullable BinaryMessenger.BinaryMessageHandler handler) { - messenger.setMessageHandler(channel, handler); + binaryMessenger.setMessageHandler(channel, handler); } + //------ END BinaryMessenger ----- /** * Returns the number of pending channel callback replies. @@ -217,11 +218,9 @@ public class DartExecutor implements BinaryMessenger { */ @UiThread public int getPendingChannelResponseCount() { - return messenger.getPendingChannelResponseCount(); + return dartMessenger.getPendingChannelResponseCount(); } - //------ END BinaryMessenger ----- - /** * Returns an identifier for this executor's primary isolate. This identifier can be used * in queries to the Dart service protocol. @@ -345,4 +344,53 @@ public class DartExecutor implements BinaryMessenger { + ", function: " + callbackHandle.callbackName + " )"; } } + + private static class DefaultBinaryMessenger implements BinaryMessenger { + private final DartMessenger messenger; + + private DefaultBinaryMessenger(@NonNull DartMessenger messenger) { + this.messenger = messenger; + } + + /** + * Sends the given {@code message} from Android to Dart over the given {@code channel}. + * + * @param channel the name of the logical channel used for the message. + * @param message the message payload, a direct-allocated {@link ByteBuffer} with the message bytes + */ + @Override + @UiThread + public void send(@NonNull String channel, @Nullable ByteBuffer message) { + messenger.send(channel, message, null); + } + + /** + * Sends the given {@code messages} from Android to Dart over the given {@code channel} and + * then has the provided {@code callback} invoked when the Dart side responds. + * + * @param channel the name of the logical channel used for the message. + * @param message the message payload, a direct-allocated {@link ByteBuffer} with the message bytes + * between position zero and current position, or null. + * @param callback a callback invoked when the Dart application responds to the message + */ + @Override + @UiThread + public void send(@NonNull String channel, @Nullable ByteBuffer message, @Nullable BinaryMessenger.BinaryReply callback) { + messenger.send(channel, message, callback); + } + + /** + * Sets the given {@link io.flutter.plugin.common.BinaryMessenger.BinaryMessageHandler} as the + * singular handler for all incoming messages received from the Dart side of this Dart execution + * context. + * + * @param channel the name of the channel. + * @param handler a {@link BinaryMessageHandler} to be invoked on incoming messages, or null. + */ + @Override + @UiThread + public void setMessageHandler(@NonNull String channel, @Nullable BinaryMessenger.BinaryMessageHandler handler) { + messenger.setMessageHandler(channel, handler); + } + } } diff --git a/engine/src/flutter/shell/platform/android/test/io/flutter/FlutterTestSuite.java b/engine/src/flutter/shell/platform/android/test/io/flutter/FlutterTestSuite.java index 2eae27cb3a..645c8c9b0e 100644 --- a/engine/src/flutter/shell/platform/android/test/io/flutter/FlutterTestSuite.java +++ b/engine/src/flutter/shell/platform/android/test/io/flutter/FlutterTestSuite.java @@ -21,10 +21,12 @@ import io.flutter.plugin.common.StandardMessageCodecTest; import io.flutter.plugin.editing.TextInputPluginTest; import io.flutter.plugin.platform.SingleViewPresentationTest; import io.flutter.util.PreconditionsTest; +import test.io.flutter.embedding.engine.dart.DartExecutorTest; @RunWith(Suite.class) @SuiteClasses({ //FlutterActivityAndFragmentDelegateTest.class, //TODO(mklim): Fix and re-enable this + DartExecutorTest.class, FlutterActivityTest.class, FlutterAndroidComponentTest.class, FlutterEngineCacheTest.class, diff --git a/engine/src/flutter/shell/platform/android/test/io/flutter/embedding/engine/dart/DartExecutorTest.java b/engine/src/flutter/shell/platform/android/test/io/flutter/embedding/engine/dart/DartExecutorTest.java new file mode 100644 index 0000000000..01b9bb1cd5 --- /dev/null +++ b/engine/src/flutter/shell/platform/android/test/io/flutter/embedding/engine/dart/DartExecutorTest.java @@ -0,0 +1,48 @@ +package test.io.flutter.embedding.engine.dart; + +import android.content.res.AssetManager; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.annotation.Config; + +import java.nio.ByteBuffer; + +import io.flutter.embedding.engine.FlutterJNI; +import io.flutter.embedding.engine.dart.DartExecutor; + +import static junit.framework.TestCase.assertNotNull; +import static org.mockito.Matchers.anyInt; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +@Config(manifest=Config.NONE) +@RunWith(RobolectricTestRunner.class) +public class DartExecutorTest { + @Test + public void itSendsBinaryMessages() { + // Setup test. + FlutterJNI fakeFlutterJni = mock(FlutterJNI.class); + + // Create object under test. + DartExecutor dartExecutor = new DartExecutor(fakeFlutterJni, mock(AssetManager.class)); + + // Verify a BinaryMessenger exists. + assertNotNull(dartExecutor.getBinaryMessenger()); + + // Execute the behavior under test. + ByteBuffer fakeMessage = mock(ByteBuffer.class); + dartExecutor.getBinaryMessenger().send("fake_channel", fakeMessage); + + // Verify that DartExecutor sent our message to FlutterJNI. + verify(fakeFlutterJni, times(1)).dispatchPlatformMessage( + eq("fake_channel"), + eq(fakeMessage), + anyInt(), + anyInt() + ); + } +}