|
| 1 | +// Copyright 2014 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:flutter/foundation.dart'; |
| 6 | +import 'package:flutter/material.dart'; |
| 7 | +import 'package:flutter_test/flutter_test.dart'; |
| 8 | + |
| 9 | +void main() { |
| 10 | + testWidgets('ErrorWidget displays actual error when throwing during build', (WidgetTester tester) async { |
| 11 | + final Key container = UniqueKey(); |
| 12 | + const String errorText = 'Oh no, there was a crash!!1'; |
| 13 | + |
| 14 | + await tester.pumpWidget( |
| 15 | + Container( |
| 16 | + key: container, |
| 17 | + color: Colors.red, |
| 18 | + padding: const EdgeInsets.all(10), |
| 19 | + child: Builder( |
| 20 | + builder: (BuildContext context) { |
| 21 | + throw UnsupportedError(errorText); |
| 22 | + }, |
| 23 | + ), |
| 24 | + ), |
| 25 | + ); |
| 26 | + |
| 27 | + expect( |
| 28 | + tester.takeException(), |
| 29 | + isA<UnsupportedError>().having((UnsupportedError error) => error.message, 'message', contains(errorText)), |
| 30 | + ); |
| 31 | + |
| 32 | + final ErrorWidget errorWidget = tester.widget(find.byType(ErrorWidget)); |
| 33 | + expect(errorWidget.message, contains(errorText)); |
| 34 | + |
| 35 | + // Failure in one widget shouldn't ripple through the entire tree and effect |
| 36 | + // ancestors. Those should still be in the tree. |
| 37 | + expect(find.byKey(container), findsOneWidget); |
| 38 | + }); |
| 39 | + |
| 40 | + testWidgets('when constructing an ErrorWidget due to a build failure throws an error, fail gracefully', (WidgetTester tester) async { |
| 41 | + final Key container = UniqueKey(); |
| 42 | + await tester.pumpWidget( |
| 43 | + Container( |
| 44 | + key: container, |
| 45 | + color: Colors.red, |
| 46 | + padding: const EdgeInsets.all(10), |
| 47 | + // This widget throws during build, which causes the construction of an |
| 48 | + // ErrorWidget with the build error. However, during construction of |
| 49 | + // that ErrorWidget, another error is thrown. |
| 50 | + child: const MyDoubleThrowingWidget(), |
| 51 | + ), |
| 52 | + ); |
| 53 | + |
| 54 | + expect( |
| 55 | + tester.takeException(), |
| 56 | + isA<UnsupportedError>().having((UnsupportedError error) => error.message, 'message', contains(MyThrowingElement.debugFillPropertiesErrorMessage)), |
| 57 | + ); |
| 58 | + |
| 59 | + final ErrorWidget errorWidget = tester.widget(find.byType(ErrorWidget)); |
| 60 | + expect(errorWidget.message, contains(MyThrowingElement.debugFillPropertiesErrorMessage)); |
| 61 | + |
| 62 | + // Failure in one widget shouldn't ripple through the entire tree and effect |
| 63 | + // ancestors. Those should still be in the tree. |
| 64 | + expect(find.byKey(container), findsOneWidget); |
| 65 | + }); |
| 66 | +} |
| 67 | + |
| 68 | +// This widget throws during its regular build and then again when the |
| 69 | +// ErrorWidget is constructed, which calls MyThrowingElement.debugFillProperties. |
| 70 | +class MyDoubleThrowingWidget extends StatelessWidget { |
| 71 | + const MyDoubleThrowingWidget({super.key}); |
| 72 | + |
| 73 | + @override |
| 74 | + StatelessElement createElement() => MyThrowingElement(this); |
| 75 | + |
| 76 | + @override |
| 77 | + Widget build(BuildContext context) { |
| 78 | + throw UnsupportedError('You cannot build me!'); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +class MyThrowingElement extends StatelessElement { |
| 83 | + MyThrowingElement(super.widget); |
| 84 | + |
| 85 | + static const String debugFillPropertiesErrorMessage = 'Crash during debugFillProperties'; |
| 86 | + |
| 87 | + @override |
| 88 | + void debugFillProperties(DiagnosticPropertiesBuilder properties) { |
| 89 | + super.debugFillProperties(properties); |
| 90 | + throw UnsupportedError(debugFillPropertiesErrorMessage); |
| 91 | + } |
| 92 | +} |
0 commit comments