Commit d709f18c authored by Ian Hickson's avatar Ian Hickson Committed by GitHub

Do not swallow exceptions in guarded functions. (#9064)

parent a94e995f
...@@ -88,7 +88,7 @@ void main() { ...@@ -88,7 +88,7 @@ void main() {
), ),
); );
await tester.fling(find.text('X'), const Offset(0.0, -300.0), 1000.0); await tester.fling(find.text('X'), const Offset(0.0, 300.0), 1000.0);
await tester.pump(); await tester.pump();
await tester.pump(const Duration(seconds: 1)); await tester.pump(const Duration(seconds: 1));
await tester.pump(const Duration(seconds: 1)); await tester.pump(const Duration(seconds: 1));
......
...@@ -380,6 +380,7 @@ class WidgetController { ...@@ -380,6 +380,7 @@ class WidgetController {
Future<Null> dragFrom(Point startLocation, Offset offset, { int pointer }) { Future<Null> dragFrom(Point startLocation, Offset offset, { int pointer }) {
return TestAsyncUtils.guard(() async { return TestAsyncUtils.guard(() async {
final TestGesture gesture = await startGesture(startLocation, pointer: pointer); final TestGesture gesture = await startGesture(startLocation, pointer: pointer);
assert(gesture != null);
await gesture.moveBy(offset); await gesture.moveBy(offset);
await gesture.up(); await gesture.up();
return null; return null;
......
...@@ -67,7 +67,7 @@ class TestAsyncUtils { ...@@ -67,7 +67,7 @@ class TestAsyncUtils {
final _AsyncScope scope = new _AsyncScope(StackTrace.current, zone); final _AsyncScope scope = new _AsyncScope(StackTrace.current, zone);
_scopeStack.add(scope); _scopeStack.add(scope);
final Future<Null> result = scope.zone.run(body); final Future<Null> result = scope.zone.run(body);
void completionHandler(dynamic error, StackTrace stack) { Future<Null> completionHandler(dynamic error, StackTrace stack) {
assert(_scopeStack.isNotEmpty); assert(_scopeStack.isNotEmpty);
assert(_scopeStack.contains(scope)); assert(_scopeStack.contains(scope));
bool leaked = false; bool leaked = false;
...@@ -101,10 +101,13 @@ class TestAsyncUtils { ...@@ -101,10 +101,13 @@ class TestAsyncUtils {
} }
throw new FlutterError(message.toString().trimRight()); throw new FlutterError(message.toString().trimRight());
} }
if (error != null)
return new Future<Null>.error(error, stack);
return new Future<Null>.value(null);
} }
return result.then<Null>( return result.then<Null>(
(Null value) { (Null value) {
completionHandler(null, null); return completionHandler(null, null);
}, },
onError: completionHandler onError: completionHandler
); );
......
...@@ -140,9 +140,8 @@ class TestGesture { ...@@ -140,9 +140,8 @@ class TestGesture {
}) async { }) async {
assert(hitTester != null); assert(hitTester != null);
assert(dispatcher != null); assert(dispatcher != null);
final Completer<TestGesture> completer = new Completer<TestGesture>();
TestGesture result; TestGesture result;
TestAsyncUtils.guard(() async { return TestAsyncUtils.guard(() async {
// dispatch down event // dispatch down event
final HitTestResult hitTestResult = hitTester(downLocation); final HitTestResult hitTestResult = hitTester(downLocation);
final TestPointer testPointer = new TestPointer(pointer); final TestPointer testPointer = new TestPointer(pointer);
...@@ -151,10 +150,11 @@ class TestGesture { ...@@ -151,10 +150,11 @@ class TestGesture {
// create a TestGesture // create a TestGesture
result = new TestGesture._(dispatcher, hitTestResult, testPointer); result = new TestGesture._(dispatcher, hitTestResult, testPointer);
return null; return null;
}).whenComplete(() { }).then<TestGesture>((Null value) {
completer.complete(result); return result;
}, onError: (dynamic error, StackTrace stack) {
return new Future<TestGesture>.error(error, stack);
}); });
return completer.future;
} }
final EventDispatcher _dispatcher; final EventDispatcher _dispatcher;
......
...@@ -27,13 +27,9 @@ class TestAPISubclass extends TestAPI { ...@@ -27,13 +27,9 @@ class TestAPISubclass extends TestAPI {
} }
} }
Future<Null> helperFunction(WidgetTester tester) async { Future<Null> _guardedThrower() {
await tester.pump();
}
Future<Null> guardedHelper(WidgetTester tester) {
return TestAsyncUtils.guard(() async { return TestAsyncUtils.guard(() async {
await tester.pumpWidget(new Text('Hello')); throw 'Hello';
}); });
} }
...@@ -174,5 +170,14 @@ void main() { ...@@ -174,5 +170,14 @@ void main() {
await f1; await f1;
}); });
testWidgets('TestAsyncUtils - guard body can throw', (WidgetTester tester) async {
try {
await _guardedThrower();
expect(false, true); // _guardedThrower should throw and we shouldn't reach here
} on String catch (s) {
expect(s, 'Hello');
}
});
// see also dev/manual_tests/test_data which contains tests run by the flutter_tools tests for 'flutter test' // see also dev/manual_tests/test_data which contains tests run by the flutter_tools tests for 'flutter test'
} }
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