extractGoldenCollectionFromSceneWidgetTree function

Future<ScreenshotCollection> extractGoldenCollectionFromSceneWidgetTree(
  1. WidgetTester tester,
  2. GoldenSceneMetadata sceneMetadata, [
  3. Finder? sceneBounds
])

Extracts a ScreenshotCollection from a golden scene within the current widget tree.

A golden scene is an image that contains some number of individual golden screenshots within it. In other words, a single image contains (possibly) many different golden images.

This function screenshots the part of the Flutter UI within the given sceneBounds, extracts each individual golden image from the scene, and then returns all of those golden images as a ScreenshotCollection.

WARNING: When running this method within a test suite, this method must be run with tester.runAsync() because this method moves image data from the Flutter engine to the framework, which can only happen with real async support, which is otherwise disabled in tests. If your test hangs, this is probably why.

  late final GoldenCollection collection;
  await tester.runAsync(() async {
    collection = await extractGoldenCollectionFromSceneWidgetTree(tester);
  });

Implementation

Future<ScreenshotCollection> extractGoldenCollectionFromSceneWidgetTree(
  WidgetTester tester,
  GoldenSceneMetadata sceneMetadata, [
  Finder? sceneBounds,
]) async {
  FtgLog.pipeline.fine("Extracting golden collection from widget tree.");
  final renderRepaintBoundary = _findNearestRepaintBoundary(sceneBounds ?? find.byType(GoldenSceneBounds));
  if (renderRepaintBoundary == null) {
    // TODO: use structured error
    throw Exception(
      "Can't create golden collection from widget tree because we couldn't find a repaint boundary at or above the given bounds finder: $sceneBounds",
    );
  }

  // Screenshot the widget tree.
  final treeFlutterImage = renderRepaintBoundary.toImageSync();
  final treeRawImageData = (await treeFlutterImage.toByteData(format: ui.ImageByteFormat.png))!.buffer.asUint8List();
  final treeImage = decodePng(treeRawImageData)!;

  // Extract the golden images from the scene image.
  return _extractCollectionFromSceneBitmap(sceneMetadata, treeImage);
}