Skip to content

Commit

Permalink
Always increment response_id for Android platform messages (flutter#2…
Browse files Browse the repository at this point in the history
  • Loading branch information
GaryQian authored Oct 12, 2021
1 parent 00fc690 commit 295ba5c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 1 addition & 2 deletions shell/platform/android/platform_view_android.cc
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,8 @@ void PlatformViewAndroid::InvokePlatformMessageEmptyResponseCallback(
// |PlatformView|
void PlatformViewAndroid::HandlePlatformMessage(
std::unique_ptr<flutter::PlatformMessage> 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
}

0 comments on commit 295ba5c

Please sign in to comment.