276 {
277 final FlutterJNI fakeFlutterJni = mock(FlutterJNI.class);
278 final DartMessenger messenger =
280 final BinaryMessenger.TaskQueue taskQueue = messenger.makeBackgroundTaskQueue();
281 final String channel = "foobar";
282 final ByteBuffer
message = ByteBuffer.allocateDirect(4 * 2);
283 final int replyId = 1;
284 final long messageData = 1234;
285
286 messenger.enableBufferingIncomingMessages();
287 messenger.handleMessageFromDart(channel,
message, replyId, messageData);
288
289 shadowOf(getMainLooper()).idle();
290 verify(fakeFlutterJni, never()).invokePlatformMessageEmptyResponseCallback(
eq(replyId));
291
292 final BinaryMessenger.BinaryMessageHandler handler =
293 (ByteBuffer msg, BinaryMessenger.BinaryReply reply) -> {
294 reply.reply(ByteBuffer.wrap("done".getBytes()));
295 };
296 messenger.setMessageHandler(channel, handler, taskQueue);
297
298 shadowOf(getMainLooper()).idle();
299 verify(fakeFlutterJni, never()).invokePlatformMessageEmptyResponseCallback(
eq(replyId));
300
301 final ArgumentCaptor<ByteBuffer> response = ArgumentCaptor.forClass(ByteBuffer.class);
302 verify(fakeFlutterJni)
303 .invokePlatformMessageResponseCallback(anyInt(), response.capture(), anyInt());
304 assertArrayEquals("done".getBytes(), response.getValue().array());
305
306 messenger.disableBufferingIncomingMessages();
307 messenger.setMessageHandler(channel, null, null);
308
309 messenger.handleMessageFromDart(channel,
message, replyId, messageData);
310 shadowOf(getMainLooper()).idle();
311 verify(fakeFlutterJni).invokePlatformMessageEmptyResponseCallback(replyId);
312 }