From 6f7e34fbb805664efb3d4efad9e3d9ef0c8edfc6 Mon Sep 17 00:00:00 2001 From: adazh <46544665+adazh@users.noreply.github.com> Date: Tue, 23 Jul 2019 13:11:03 -0700 Subject: [PATCH] Added a DartExecutor API for querying # of pending channel callbacks (flutter/engine#10021) --- .../embedding/engine/dart/DartExecutor.java | 20 +++++++++++++++++++ .../embedding/engine/dart/DartMessenger.java | 18 ++++++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) 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 7e74f4de1a..2b8653c49d 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 @@ -196,6 +196,26 @@ public class DartExecutor implements BinaryMessenger { public void setMessageHandler(@NonNull String channel, @Nullable BinaryMessenger.BinaryMessageHandler handler) { messenger.setMessageHandler(channel, handler); } + + /** + * Returns the number of pending channel callback replies. + * + *

When sending messages to the Flutter application using {@link BinaryMessenger#send(String, + * ByteBuffer, io.flutter.plugin.common.BinaryMessenger.BinaryReply)}, developers can optionally + * specify a reply callback if they expect a reply from the Flutter application. + * + *

This method tracks all the pending callbacks that are waiting for response, and is supposed + * to be called from the main thread (as other methods). Calling from a different thread could + * possibly capture an indeterministic internal state, so don't do it. + * + *

Currently, it's mainly useful for a testing framework like Espresso to determine whether all + * the async channel callbacks are handled and the app is idle. + */ + @UiThread + public int getPendingChannelResponseCount() { + return messenger.getPendingChannelResponseCount(); + } + //------ END BinaryMessenger ----- /** diff --git a/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/dart/DartMessenger.java b/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/dart/DartMessenger.java index 3c0dac121b..3d23b4fd09 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/dart/DartMessenger.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/dart/DartMessenger.java @@ -118,6 +118,22 @@ class DartMessenger implements BinaryMessenger, PlatformMessageHandler { } } + /** + * Returns the number of pending channel callback replies. + * + *

When sending messages to the Flutter application using {@link BinaryMessenger#send(String, + * ByteBuffer, io.flutter.plugin.common.BinaryMessenger.BinaryReply)}, developers can optionally + * specify a reply callback if they expect a reply from the Flutter application. + * + *

This method tracks all the pending callbacks that are waiting for response, and is supposed + * to be called from the main thread (as other methods). Calling from a different thread could + * possibly capture an indeterministic internal state, so don't do it. + */ + @UiThread + public int getPendingChannelResponseCount() { + return pendingReplies.size(); + } + private static class Reply implements BinaryMessenger.BinaryReply { @NonNull private final FlutterJNI flutterJNI; @@ -141,4 +157,4 @@ class DartMessenger implements BinaryMessenger, PlatformMessageHandler { } } } -} \ No newline at end of file +}