diff --git a/shell/platform/android/io/flutter/embedding/engine/dart/DartMessenger.java b/shell/platform/android/io/flutter/embedding/engine/dart/DartMessenger.java index 8a5e44f637a1a..2822b4060fbcd 100644 --- a/shell/platform/android/io/flutter/embedding/engine/dart/DartMessenger.java +++ b/shell/platform/android/io/flutter/embedding/engine/dart/DartMessenger.java @@ -61,9 +61,8 @@ public void send( @Nullable ByteBuffer message, @Nullable BinaryMessenger.BinaryReply callback) { Log.v(TAG, "Sending message with callback over channel '" + channel + "'"); - int replyId = 0; + int replyId = nextReplyId++; if (callback != null) { - replyId = nextReplyId++; pendingReplies.put(replyId, callback); } if (message == null) { diff --git a/shell/platform/android/platform_view_android.cc b/shell/platform/android/platform_view_android.cc index 557c87b021f5d..50b9d55c6ca4a 100644 --- a/shell/platform/android/platform_view_android.cc +++ b/shell/platform/android/platform_view_android.cc @@ -227,9 +227,8 @@ void PlatformViewAndroid::InvokePlatformMessageEmptyResponseCallback( // |PlatformView| void PlatformViewAndroid::HandlePlatformMessage( std::unique_ptr message) { - int response_id = 0; + int response_id = next_response_id_++; if (auto response = message->response()) { - response_id = next_response_id_++; pending_responses_[response_id] = response; } // This call can re-enter in InvokePlatformMessageXxxResponseCallback. diff --git a/shell/platform/android/test/io/flutter/embedding/engine/dart/DartMessengerTest.java b/shell/platform/android/test/io/flutter/embedding/engine/dart/DartMessengerTest.java index af4008e98c72e..5e1922103afbf 100644 --- a/shell/platform/android/test/io/flutter/embedding/engine/dart/DartMessengerTest.java +++ b/shell/platform/android/test/io/flutter/embedding/engine/dart/DartMessengerTest.java @@ -4,7 +4,10 @@ import static junit.framework.TestCase.assertNotNull; import static junit.framework.TestCase.assertTrue; import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; import io.flutter.embedding.engine.FlutterJNI; import io.flutter.plugin.common.BinaryMessenger; @@ -124,4 +127,16 @@ public void directByteBufferLimitZeroAfterReply() { messenger.handlePlatformMessageResponse(1, message); assertEquals(0, byteBuffers[0].limit()); } + + @Test + public void replyIdIncrementsOnNullReply() { + /// Setup test. + final FlutterJNI fakeFlutterJni = mock(FlutterJNI.class); + final DartMessenger messenger = new DartMessenger(fakeFlutterJni); + final String channel = "foobar"; + messenger.send(channel, null, null); + verify(fakeFlutterJni, times(1)).dispatchEmptyPlatformMessage(eq("foobar"), eq(1)); + messenger.send(channel, null, null); + verify(fakeFlutterJni, times(1)).dispatchEmptyPlatformMessage(eq("foobar"), eq(2)); + } }