Unverified Commit 2f0b4158 authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

Add option to silence driver extension errors (#19247)

parent e32e4479
...@@ -37,14 +37,15 @@ const String _extensionMethod = 'ext.flutter.$_extensionMethodName'; ...@@ -37,14 +37,15 @@ const String _extensionMethod = 'ext.flutter.$_extensionMethodName';
typedef Future<String> DataHandler(String message); typedef Future<String> DataHandler(String message);
class _DriverBinding extends BindingBase with ServicesBinding, SchedulerBinding, GestureBinding, PaintingBinding, RendererBinding, WidgetsBinding { class _DriverBinding extends BindingBase with ServicesBinding, SchedulerBinding, GestureBinding, PaintingBinding, RendererBinding, WidgetsBinding {
_DriverBinding(this._handler); _DriverBinding(this._handler, this._silenceErrors);
final DataHandler _handler; final DataHandler _handler;
final bool _silenceErrors;
@override @override
void initServiceExtensions() { void initServiceExtensions() {
super.initServiceExtensions(); super.initServiceExtensions();
final FlutterDriverExtension extension = new FlutterDriverExtension(_handler); final FlutterDriverExtension extension = new FlutterDriverExtension(_handler, _silenceErrors);
registerServiceExtension( registerServiceExtension(
name: _extensionMethodName, name: _extensionMethodName,
callback: extension.call, callback: extension.call,
...@@ -62,9 +63,14 @@ class _DriverBinding extends BindingBase with ServicesBinding, SchedulerBinding, ...@@ -62,9 +63,14 @@ class _DriverBinding extends BindingBase with ServicesBinding, SchedulerBinding,
/// ///
/// Optionally you can pass a [DataHandler] callback. It will be called if the /// Optionally you can pass a [DataHandler] callback. It will be called if the
/// test calls [FlutterDriver.requestData]. /// test calls [FlutterDriver.requestData].
void enableFlutterDriverExtension({ DataHandler handler }) { ///
/// `slienceErrors` will prevent exceptions from being logged. This is useful
/// for tests where exceptions are expected. Defaults to false. Any errors
/// will still be returned in the `response` field of the result json along
/// with an `isError` boolean.
void enableFlutterDriverExtension({ DataHandler handler, bool silenceErrors = false }) {
assert(WidgetsBinding.instance == null); assert(WidgetsBinding.instance == null);
new _DriverBinding(handler); new _DriverBinding(handler, silenceErrors);
assert(WidgetsBinding.instance is _DriverBinding); assert(WidgetsBinding.instance is _DriverBinding);
} }
...@@ -88,7 +94,7 @@ class FlutterDriverExtension { ...@@ -88,7 +94,7 @@ class FlutterDriverExtension {
final TestTextInput _testTextInput = new TestTextInput(); final TestTextInput _testTextInput = new TestTextInput();
/// Creates an object to manage a Flutter Driver connection. /// Creates an object to manage a Flutter Driver connection.
FlutterDriverExtension(this._requestDataHandler) { FlutterDriverExtension(this._requestDataHandler, this._silenceErrors) {
_testTextInput.register(); _testTextInput.register();
_commandHandlers.addAll(<String, CommandHandlerCallback>{ _commandHandlers.addAll(<String, CommandHandlerCallback>{
...@@ -136,6 +142,7 @@ class FlutterDriverExtension { ...@@ -136,6 +142,7 @@ class FlutterDriverExtension {
} }
final DataHandler _requestDataHandler; final DataHandler _requestDataHandler;
final bool _silenceErrors;
static final Logger _log = new Logger('FlutterDriverExtension'); static final Logger _log = new Logger('FlutterDriverExtension');
...@@ -176,7 +183,8 @@ class FlutterDriverExtension { ...@@ -176,7 +183,8 @@ class FlutterDriverExtension {
return _makeResponse(msg, isError: true); return _makeResponse(msg, isError: true);
} catch (error, stackTrace) { } catch (error, stackTrace) {
final String msg = 'Uncaught extension error while executing $commandKind: $error\n$stackTrace'; final String msg = 'Uncaught extension error while executing $commandKind: $error\n$stackTrace';
_log.error(msg); if (!_silenceErrors)
_log.error(msg);
return _makeResponse(msg, isError: true); return _makeResponse(msg, isError: true);
} }
} }
......
...@@ -19,7 +19,7 @@ void main() { ...@@ -19,7 +19,7 @@ void main() {
setUp(() { setUp(() {
result = null; result = null;
extension = new FlutterDriverExtension((String message) async { log.add(message); return (messageId += 1).toString(); }); extension = new FlutterDriverExtension((String message) async { log.add(message); return (messageId += 1).toString(); }, false);
}); });
testWidgets('returns immediately when transient callback queue is empty', (WidgetTester tester) async { testWidgets('returns immediately when transient callback queue is empty', (WidgetTester tester) async {
...@@ -74,7 +74,7 @@ void main() { ...@@ -74,7 +74,7 @@ void main() {
group('getSemanticsId', () { group('getSemanticsId', () {
FlutterDriverExtension extension; FlutterDriverExtension extension;
setUp(() { setUp(() {
extension = new FlutterDriverExtension((String arg) async {}); extension = new FlutterDriverExtension((String arg) async {}, true);
}); });
testWidgets('works when semantics are enabled', (WidgetTester tester) async { testWidgets('works when semantics are enabled', (WidgetTester tester) async {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment