Forward Error objects to uncaught exception handler if there is one. (flutter/engine#21806)
This commit is contained in:
committed by
GitHub
parent
8f61a2ab97
commit
681ffacfe6
@@ -459,6 +459,7 @@ action("robolectric_tests") {
|
||||
"test/io/flutter/embedding/engine/PluginComponentTest.java",
|
||||
"test/io/flutter/embedding/engine/RenderingComponentTest.java",
|
||||
"test/io/flutter/embedding/engine/dart/DartExecutorTest.java",
|
||||
"test/io/flutter/embedding/engine/dart/DartMessengerTest.java",
|
||||
"test/io/flutter/embedding/engine/loader/ApplicationInfoLoaderTest.java",
|
||||
"test/io/flutter/embedding/engine/loader/FlutterLoaderTest.java",
|
||||
"test/io/flutter/embedding/engine/mutatorsstack/FlutterMutatorViewTest.java",
|
||||
|
||||
@@ -86,6 +86,8 @@ class DartMessenger implements BinaryMessenger, PlatformMessageHandler {
|
||||
} catch (Exception ex) {
|
||||
Log.e(TAG, "Uncaught exception in binary message listener", ex);
|
||||
flutterJNI.invokePlatformMessageEmptyResponseCallback(replyId);
|
||||
} catch (Error err) {
|
||||
handleError(err);
|
||||
}
|
||||
} else {
|
||||
Log.v(TAG, "No registered handler for message. Responding to Dart with empty reply message.");
|
||||
@@ -103,6 +105,8 @@ class DartMessenger implements BinaryMessenger, PlatformMessageHandler {
|
||||
callback.reply(reply == null ? null : ByteBuffer.wrap(reply));
|
||||
} catch (Exception ex) {
|
||||
Log.e(TAG, "Uncaught exception in binary message reply handler", ex);
|
||||
} catch (Error err) {
|
||||
handleError(err);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -123,7 +127,19 @@ class DartMessenger implements BinaryMessenger, PlatformMessageHandler {
|
||||
return pendingReplies.size();
|
||||
}
|
||||
|
||||
private static class Reply implements BinaryMessenger.BinaryReply {
|
||||
// Handles `Error` objects which are not supposed to be caught.
|
||||
//
|
||||
// We forward them to the thread's uncaught exception handler if there is one. If not, they
|
||||
// are rethrown.
|
||||
private static void handleError(Error err) {
|
||||
Thread currentThread = Thread.currentThread();
|
||||
if (currentThread.getUncaughtExceptionHandler() == null) {
|
||||
throw err;
|
||||
}
|
||||
currentThread.getUncaughtExceptionHandler().uncaughtException(currentThread, err);
|
||||
}
|
||||
|
||||
static class Reply implements BinaryMessenger.BinaryReply {
|
||||
@NonNull private final FlutterJNI flutterJNI;
|
||||
private final int replyId;
|
||||
private final AtomicBoolean done = new AtomicBoolean(false);
|
||||
|
||||
@@ -16,6 +16,8 @@ import io.flutter.embedding.engine.FlutterEngineConnectionRegistryTest;
|
||||
import io.flutter.embedding.engine.FlutterJNITest;
|
||||
import io.flutter.embedding.engine.LocalizationPluginTest;
|
||||
import io.flutter.embedding.engine.RenderingComponentTest;
|
||||
import io.flutter.embedding.engine.dart.DartExecutorTest;
|
||||
import io.flutter.embedding.engine.dart.DartMessengerTest;
|
||||
import io.flutter.embedding.engine.loader.ApplicationInfoLoaderTest;
|
||||
import io.flutter.embedding.engine.loader.FlutterLoaderTest;
|
||||
import io.flutter.embedding.engine.mutatorsstack.FlutterMutatorViewTest;
|
||||
@@ -41,7 +43,6 @@ import org.junit.runners.Suite.SuiteClasses;
|
||||
import test.io.flutter.embedding.engine.FlutterEngineTest;
|
||||
import test.io.flutter.embedding.engine.FlutterShellArgsTest;
|
||||
import test.io.flutter.embedding.engine.PluginComponentTest;
|
||||
import test.io.flutter.embedding.engine.dart.DartExecutorTest;
|
||||
|
||||
@RunWith(Suite.class)
|
||||
@SuiteClasses({
|
||||
@@ -49,6 +50,7 @@ import test.io.flutter.embedding.engine.dart.DartExecutorTest;
|
||||
AndroidKeyProcessorTest.class,
|
||||
ApplicationInfoLoaderTest.class,
|
||||
DartExecutorTest.class,
|
||||
DartMessengerTest.class,
|
||||
FlutterActivityAndFragmentDelegateTest.class,
|
||||
FlutterActivityTest.class,
|
||||
FlutterAndroidComponentTest.class,
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package test.io.flutter.embedding.engine.dart;
|
||||
package io.flutter.embedding.engine.dart;
|
||||
|
||||
import static junit.framework.TestCase.assertNotNull;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
package io.flutter.embedding.engine.dart;
|
||||
|
||||
import static junit.framework.TestCase.assertNotNull;
|
||||
import static junit.framework.TestCase.assertTrue;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
import io.flutter.embedding.engine.FlutterJNI;
|
||||
import io.flutter.plugin.common.BinaryMessenger.BinaryMessageHandler;
|
||||
import java.nio.ByteBuffer;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mockito;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.annotation.Config;
|
||||
|
||||
@Config(manifest = Config.NONE)
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class DartMessengerTest {
|
||||
private static class ReportingUncaughtExceptionHandler
|
||||
implements Thread.UncaughtExceptionHandler {
|
||||
public Throwable latestException;
|
||||
|
||||
@Override
|
||||
public void uncaughtException(Thread t, Throwable e) {
|
||||
latestException = e;
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void itHandlesErrors() {
|
||||
// Setup test.
|
||||
final FlutterJNI fakeFlutterJni = mock(FlutterJNI.class);
|
||||
final Thread currentThread = Thread.currentThread();
|
||||
final Thread.UncaughtExceptionHandler savedHandler =
|
||||
currentThread.getUncaughtExceptionHandler();
|
||||
final ReportingUncaughtExceptionHandler reportingHandler =
|
||||
new ReportingUncaughtExceptionHandler();
|
||||
currentThread.setUncaughtExceptionHandler(reportingHandler);
|
||||
|
||||
// Create object under test.
|
||||
final DartMessenger messenger = new DartMessenger(fakeFlutterJni);
|
||||
final BinaryMessageHandler throwingHandler = mock(BinaryMessageHandler.class);
|
||||
Mockito.doThrow(AssertionError.class)
|
||||
.when(throwingHandler)
|
||||
.onMessage(any(ByteBuffer.class), any(DartMessenger.Reply.class));
|
||||
|
||||
messenger.setMessageHandler("test", throwingHandler);
|
||||
messenger.handleMessageFromDart("test", new byte[] {}, 0);
|
||||
assertNotNull(reportingHandler.latestException);
|
||||
assertTrue(reportingHandler.latestException instanceof AssertionError);
|
||||
currentThread.setUncaughtExceptionHandler(savedHandler);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user