Sound null safety for framework and flutter_test (#68642)

This commit is contained in:
Michael Goderbauer
2020-10-21 10:09:11 -07:00
committed by GitHub
parent 955e07461b
commit 42f3709a5a
56 changed files with 400 additions and 381 deletions

View File

@@ -49,27 +49,31 @@ class MatchesGoldenFile extends AsyncMatcher {
@override
Future<String?> matchAsync(dynamic item) async {
Future<ui.Image> imageFuture;
if (item is Future<ui.Image>) {
Future<ui.Image?> imageFuture;
if (item is Future<ui.Image?>) {
imageFuture = item;
} else if (item is ui.Image) {
imageFuture = Future<ui.Image>.value(item);
} else {
final Finder finder = item as Finder;
final Iterable<Element> elements = finder.evaluate();
} else if (item is Finder) {
final Iterable<Element> elements = item.evaluate();
if (elements.isEmpty) {
return 'could not be rendered because no widget was found';
} else if (elements.length > 1) {
return 'matched too many widgets';
}
imageFuture = captureImage(elements.single);
} else {
throw 'must provide a Finder, Image, or Future<Image>';
}
final Uri testNameUri = goldenFileComparator.getTestUri(key, version);
final TestWidgetsFlutterBinding binding = TestWidgetsFlutterBinding.ensureInitialized() as TestWidgetsFlutterBinding;
return binding.runAsync<String?>(() async {
final ui.Image image = await imageFuture;
final ui.Image? image = await imageFuture;
if (image == null) {
throw 'Future<Image> completed to null';
}
final ByteData? bytes = await image.toByteData(format: ui.ImageByteFormat.png);
if (bytes == null)
return 'could not encode screenshot.';

View File

@@ -1011,17 +1011,19 @@ class AutomatedTestWidgetsFlutterBinding extends TestWidgetsFlutterBinding {
addTime(additionalTime);
return realAsyncZone.run<Future<T>>(() {
return realAsyncZone.run<Future<T?>>(() async {
_pendingAsyncTasks = Completer<void>();
return callback().catchError((Object exception, StackTrace stack) {
T? result;
try {
result = await callback();
} catch (exception, stack) {
FlutterError.reportError(FlutterErrorDetails(
exception: exception,
stack: stack,
library: 'Flutter test framework',
context: ErrorDescription('while running async test code'),
));
return null;
}).whenComplete(() {
} finally {
// We complete the _pendingAsyncTasks future successfully regardless of
// whether an exception occurred because in the case of an exception,
// we already reported the exception to FlutterError. Moreover,
@@ -1029,7 +1031,8 @@ class AutomatedTestWidgetsFlutterBinding extends TestWidgetsFlutterBinding {
// exception due to zone error boundaries.
_pendingAsyncTasks!.complete();
_pendingAsyncTasks = null;
});
}
return result;
});
}

View File

@@ -11,7 +11,7 @@ void testConfig(
String? expectedStringValue, {
Map<Type, dynamic> otherExpectedValues = const <Type, dynamic>{int: isNull},
}) {
final String actualStringValue = Zone.current[String] as String;
final String? actualStringValue = Zone.current[String] as String?;
final Map<Type, dynamic> otherActualValues = otherExpectedValues.map<Type, dynamic>(
(Type key, dynamic value) {
return MapEntry<Type, dynamic>(key, Zone.current[key]);