Add test for error_widget.0_test.dart (#153103)
Add test for error_widget.0_test.dart which is listed in this issue related to this issue https://github.com/flutter/flutter/issues/130459
This commit is contained in:
@@ -334,7 +334,6 @@ final Set<String> _knownMissingTests = <String>{
|
||||
'examples/api/test/widgets/navigator/navigator_state.restorable_push.0_test.dart',
|
||||
'examples/api/test/widgets/focus_manager/focus_node.unfocus.0_test.dart',
|
||||
'examples/api/test/widgets/framework/build_owner.0_test.dart',
|
||||
'examples/api/test/widgets/framework/error_widget.0_test.dart',
|
||||
'examples/api/test/widgets/autofill/autofill_group.0_test.dart',
|
||||
'examples/api/test/widgets/nested_scroll_view/nested_scroll_view_state.0_test.dart',
|
||||
'examples/api/test/widgets/scroll_position/scroll_metrics_notification.0_test.dart',
|
||||
|
||||
@@ -16,15 +16,7 @@ void main() {
|
||||
return ErrorWidget(details.exception);
|
||||
}
|
||||
// In release builds, show a yellow-on-blue message instead:
|
||||
return Container(
|
||||
alignment: Alignment.center,
|
||||
child: Text(
|
||||
'Error!\n${details.exception}',
|
||||
style: const TextStyle(color: Colors.yellow),
|
||||
textAlign: TextAlign.center,
|
||||
textDirection: TextDirection.ltr,
|
||||
),
|
||||
);
|
||||
return ReleaseModeErrorWidget(details: details);
|
||||
};
|
||||
|
||||
// Start the app.
|
||||
@@ -69,3 +61,21 @@ class _ErrorWidgetExampleAppState extends State<ErrorWidgetExampleApp> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class ReleaseModeErrorWidget extends StatelessWidget {
|
||||
const ReleaseModeErrorWidget({super.key, required this.details});
|
||||
|
||||
final FlutterErrorDetails details;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Center(
|
||||
child: Text(
|
||||
'Error!\n${details.exception}',
|
||||
style: const TextStyle(color: Colors.yellow),
|
||||
textAlign: TextAlign.center,
|
||||
textDirection: TextDirection.ltr,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
55
examples/api/test/widgets/framework/error_widget.0_test.dart
Normal file
55
examples/api/test/widgets/framework/error_widget.0_test.dart
Normal file
@@ -0,0 +1,55 @@
|
||||
// Copyright 2014 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_api_samples/widgets/framework/error_widget.0.dart' as example;
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
void main() {
|
||||
testWidgets('ErrorWidget is displayed in debug mode', (WidgetTester tester) async {
|
||||
final ErrorWidgetBuilder oldBuilder = ErrorWidget.builder;
|
||||
ErrorWidget.builder = (FlutterErrorDetails details) {
|
||||
return ErrorWidget(details.exception);
|
||||
};
|
||||
await tester.pumpWidget(const example.ErrorWidgetExampleApp());
|
||||
|
||||
expect(find.widgetWithText(AppBar, 'ErrorWidget Sample'), findsOne);
|
||||
|
||||
await tester.tap(find.widgetWithText(TextButton, 'Error Prone'));
|
||||
await tester.pump();
|
||||
|
||||
expectLater(tester.takeException(), isInstanceOf<Exception>());
|
||||
|
||||
final Finder errorWidget = find.byType(ErrorWidget);
|
||||
expect(errorWidget, findsOneWidget);
|
||||
final ErrorWidget error = tester.firstWidget(errorWidget);
|
||||
expect(error.message, 'Exception: oh no, an error');
|
||||
|
||||
// Restore the ErrorWidget to conclude the test.
|
||||
ErrorWidget.builder = oldBuilder;
|
||||
});
|
||||
|
||||
testWidgets('ErrorWidget is displayed in release mode', (WidgetTester tester) async {
|
||||
final ErrorWidgetBuilder oldBuilder = ErrorWidget.builder;
|
||||
ErrorWidget.builder = (FlutterErrorDetails details) {
|
||||
return example.ReleaseModeErrorWidget(details: details);
|
||||
};
|
||||
await tester.pumpWidget(const example.ErrorWidgetExampleApp());
|
||||
|
||||
expect(find.widgetWithText(AppBar, 'ErrorWidget Sample'), findsOne);
|
||||
|
||||
await tester.tap(find.widgetWithText(TextButton, 'Error Prone'));
|
||||
await tester.pump();
|
||||
|
||||
expectLater(tester.takeException(), isInstanceOf<Exception>());
|
||||
|
||||
final Finder errorTextFinder = find.textContaining('Error!\nException: oh no, an error');
|
||||
expect(errorTextFinder, findsOneWidget);
|
||||
final Text errorText = tester.firstWidget(errorTextFinder);
|
||||
expect(errorText.style?.color, Colors.yellow);
|
||||
|
||||
// Restore the ErrorWidget to conclude the test.
|
||||
ErrorWidget.builder = oldBuilder;
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user