Minor fix compendium (#107874)

This commit is contained in:
Ian Hickson
2022-08-10 19:33:07 -07:00
committed by GitHub
parent da88c953c7
commit 9b2668a451
34 changed files with 524 additions and 56 deletions

View File

@@ -56,6 +56,7 @@ library flutter_test;
export 'dart:async' show Future;
export 'src/_goldens_io.dart' if (dart.library.html) 'src/_goldens_web.dart';
export 'src/_matchers_io.dart' if (dart.library.html) 'src/_matchers_web.dart';
export 'src/accessibility.dart';
export 'src/all_elements.dart';
export 'src/animation_sheet.dart';

View File

@@ -30,6 +30,15 @@ Future<ui.Image> captureImage(Element element) {
return layer.toImage(renderObject.paintBounds);
}
/// Whether or not [captureImage] is supported.
///
/// This can be used to skip tests on platforms that don't support
/// capturing images.
///
/// Currently this is true except when tests are running in the context of a web
/// browser (`flutter test --platform chrome`).
const bool canCaptureImage = true;
/// The matcher created by [matchesGoldenFile]. This class is enabled when the
/// test is running on a VM using conditional import.
class MatchesGoldenFile extends AsyncMatcher {
@@ -84,7 +93,7 @@ class MatchesGoldenFile extends AsyncMatcher {
throw AssertionError('must provide a Finder, Image, Future<Image>, List<int>, or Future<List<int>>');
}
final TestWidgetsFlutterBinding binding = TestWidgetsFlutterBinding.ensureInitialized();
final TestWidgetsFlutterBinding binding = TestWidgetsFlutterBinding.instance;
return binding.runAsync<String?>(() async {
final ui.Image? image = await imageFuture;
if (image == null) {

View File

@@ -18,6 +18,15 @@ Future<ui.Image> captureImage(Element element) {
throw UnsupportedError('captureImage is not supported on the web.');
}
/// Whether or not [captureImage] is supported.
///
/// This can be used to skip tests on platforms that don't support
/// capturing images.
///
/// Currently this is true except when tests are running in the context of a web
/// browser (`flutter test --platform chrome`).
const bool canCaptureImage = false;
/// The matcher created by [matchesGoldenFile]. This class is enabled when the
/// test is running in a web browser using conditional import.
class MatchesGoldenFile extends AsyncMatcher {
@@ -47,7 +56,7 @@ class MatchesGoldenFile extends AsyncMatcher {
final Element element = elements.single;
final RenderObject renderObject = _findRepaintBoundary(element);
final Size size = renderObject.paintBounds.size;
final TestWidgetsFlutterBinding binding = TestWidgetsFlutterBinding.ensureInitialized();
final TestWidgetsFlutterBinding binding = TestWidgetsFlutterBinding.instance;
final Element e = binding.renderViewElement!;
// Unlike `flutter_tester`, we don't have the ability to render an element

View File

@@ -1131,28 +1131,45 @@ class AutomatedTestWidgetsFlutterBinding extends TestWidgetsFlutterBinding {
addTime(additionalTime);
return realAsyncZone.run<Future<T?>>(() async {
return realAsyncZone.run<Future<T?>>(() {
final Completer<T?> result = Completer<T?>();
_pendingAsyncTasks = Completer<void>();
T? result;
try {
result = await callback();
callback().then(result.complete).catchError(
(Object exception, StackTrace stack) {
FlutterError.reportError(FlutterErrorDetails(
exception: exception,
stack: stack,
library: 'Flutter test framework',
context: ErrorDescription('while running async test code'),
informationCollector: () {
return <DiagnosticsNode>[
ErrorHint('The exception was caught asynchronously.'),
];
},
));
result.complete(null);
},
);
} catch (exception, stack) {
FlutterError.reportError(FlutterErrorDetails(
exception: exception,
stack: stack,
library: 'Flutter test framework',
context: ErrorDescription('while running async test code'),
informationCollector: () {
return <DiagnosticsNode>[
ErrorHint('The exception was caught synchronously.'),
];
},
));
} 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,
// completing the future with an error would trigger an unhandled
// exception due to zone error boundaries.
result.complete(null);
}
result.future.whenComplete(() {
_pendingAsyncTasks!.complete();
_pendingAsyncTasks = null;
}
return result;
});
return result.future;
});
}

View File

@@ -60,11 +60,11 @@ class _BufferGoldenMatcher extends AsyncMatcher {
/// golden files. This parameter is optional.
///
/// {@tool snippet}
/// Sample invocations of [matchesGoldenFile].
/// Sample invocations of [bufferMatchesGoldenFile].
///
/// ```dart
/// await expectLater(
/// const <int>[],
/// const <int>[ /* bytes... */ ],
/// bufferMatchesGoldenFile('sample.png'),
/// );
/// ```

View File

@@ -2030,7 +2030,7 @@ class _MatchesReferenceImage extends AsyncMatcher {
imageFuture = captureImage(elements.single);
}
final TestWidgetsFlutterBinding binding = TestWidgetsFlutterBinding.ensureInitialized();
final TestWidgetsFlutterBinding binding = TestWidgetsFlutterBinding.instance;
return binding.runAsync<String?>(() async {
final ui.Image image = await imageFuture;
final ByteData? bytes = await image.toByteData();

View File

@@ -630,6 +630,23 @@ void main() {
key: 'abczed',
});
});
testWidgets('control test (return value)', (WidgetTester tester) async {
final String? result = await tester.binding.runAsync<String>(() async => 'Judy Turner');
expect(result, 'Judy Turner');
});
testWidgets('async throw', (WidgetTester tester) async {
final String? result = await tester.binding.runAsync<Never>(() async => throw Exception('Lois Dilettente'));
expect(result, isNull);
expect(tester.takeException(), isNotNull);
});
testWidgets('sync throw', (WidgetTester tester) async {
final String? result = await tester.binding.runAsync<Never>(() => throw Exception('Butch Barton'));
expect(result, isNull);
expect(tester.takeException(), isNotNull);
});
});
group('showKeyboard', () {