Skip to content

Commit 0de9b9b

Browse files
Separate metadata extraction
1 parent 831e127 commit 0de9b9b

File tree

3 files changed

+33
-9
lines changed

3 files changed

+33
-9
lines changed

lib/src/goldens/golden_scenes.dart

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'dart:convert';
22
import 'dart:io';
3+
import 'dart:typed_data';
34
import 'dart:ui' as ui;
45

56
import 'package:flutter/rendering.dart';
@@ -15,20 +16,17 @@ import 'package:image/image.dart';
1516
///
1617
/// This function loads the scene image from the [file], extracts each individual golden
1718
/// image from the scene, and then returns all of those golden images as a [GoldenCollection].
18-
(GoldenCollection, GoldenSceneMetadata) extractGoldenCollectionFromSceneFile(File file) {
19+
GoldenCollection extractGoldenCollectionFromSceneFile(File file) {
1920
FtgLog.pipeline.fine("Extracting golden collection from golden image.");
2021

2122
// Read the scene PNG data into memory.
2223
final scenePngBytes = file.readAsBytesSync();
2324

2425
// Extract scene metadata from PNG.
25-
final pngText = scenePngBytes.readTextMetadata();
26-
final sceneJsonText = pngText["flutter_test_goldens"];
27-
if (sceneJsonText == null) {
26+
final sceneMetadata = _extractGoldenSceneMetadataFromBytes(scenePngBytes);
27+
if (sceneMetadata == null) {
2828
throw Exception("Golden image is missing scene metadata: ${file.path}");
2929
}
30-
final sceneJson = JsonDecoder().convert(sceneJsonText);
31-
final sceneMetadata = GoldenSceneMetadata.fromJson(sceneJson);
3230

3331
// Decode PNG data to an image.
3432
final sceneImage = decodePng(scenePngBytes);
@@ -38,7 +36,7 @@ import 'package:image/image.dart';
3836
}
3937

4038
// Extract the golden images from the scene image.
41-
return (_extractCollectionFromScene(sceneMetadata, sceneImage), sceneMetadata);
39+
return _extractCollectionFromScene(sceneMetadata, sceneImage);
4240
}
4341

4442
/// Extracts a [GoldenCollection] from a golden scene within the current widget tree.
@@ -84,6 +82,31 @@ Future<GoldenCollection> extractGoldenCollectionFromSceneWidgetTree(
8482
return _extractCollectionFromScene(sceneMetadata, treeImage);
8583
}
8684

85+
/// Extracts then golden scene metadata within the given image [file].
86+
GoldenSceneMetadata extractGoldenSceneMetadataFromFile(File file) {
87+
// Read the scene PNG data into memory.
88+
final scenePngBytes = file.readAsBytesSync();
89+
90+
// Extract scene metadata from PNG.
91+
final sceneMetadata = _extractGoldenSceneMetadataFromBytes(scenePngBytes);
92+
if (sceneMetadata == null) {
93+
throw Exception("Golden image is missing scene metadata: ${file.path}");
94+
}
95+
96+
return sceneMetadata;
97+
}
98+
99+
GoldenSceneMetadata? _extractGoldenSceneMetadataFromBytes(Uint8List pngBytes) {
100+
// Extract scene metadata from PNG.
101+
final pngText = pngBytes.readTextMetadata();
102+
final sceneJsonText = pngText["flutter_test_goldens"];
103+
if (sceneJsonText == null) {
104+
return null;
105+
}
106+
final sceneJson = JsonDecoder().convert(sceneJsonText);
107+
return GoldenSceneMetadata.fromJson(sceneJson);
108+
}
109+
87110
GoldenCollection _extractCollectionFromScene(GoldenSceneMetadata sceneMetadata, Image sceneImage) {
88111
// Cut each golden image out of the scene.
89112
final goldenImages = <String, GoldenImage>{};

lib/src/scenes/film_strip.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ class FilmStrip {
340340
// TODO: report error in structured way.
341341
throw Exception("Can't compare goldens. Golden file doesn't exist: ${goldenFile.path}");
342342
}
343-
final (goldenCollection, metadata) = extractGoldenCollectionFromSceneFile(goldenFile);
343+
final goldenCollection = extractGoldenCollectionFromSceneFile(goldenFile);
344344

345345
FtgLog.pipeline.fine("Extracting golden collection from current widget tree (screenshots).");
346346
late final GoldenCollection screenshotCollection;

lib/src/scenes/gallery.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,8 @@ class Gallery {
460460
// TODO: report error in structured way.
461461
throw Exception("Can't compare goldens. Golden file doesn't exist: ${goldenFile.path}");
462462
}
463-
final (goldenCollection, metadata) = extractGoldenCollectionFromSceneFile(goldenFile);
463+
final goldenCollection = extractGoldenCollectionFromSceneFile(goldenFile);
464+
final metadata = extractGoldenSceneMetadataFromFile(goldenFile);
464465

465466
// Extract scene metadata from the current widget tree.
466467
FtgLog.pipeline.fine("Extracting golden collection from current widget tree (screenshots).");

0 commit comments

Comments
 (0)