1
1
import 'dart:convert' ;
2
2
import 'dart:io' ;
3
+ import 'dart:typed_data' ;
3
4
import 'dart:ui' as ui;
4
5
5
6
import 'package:flutter/rendering.dart' ;
@@ -15,20 +16,17 @@ import 'package:image/image.dart';
15
16
///
16
17
/// This function loads the scene image from the [file] , extracts each individual golden
17
18
/// 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) {
19
20
FtgLog .pipeline.fine ("Extracting golden collection from golden image." );
20
21
21
22
// Read the scene PNG data into memory.
22
23
final scenePngBytes = file.readAsBytesSync ();
23
24
24
25
// 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 ) {
28
28
throw Exception ("Golden image is missing scene metadata: ${file .path }" );
29
29
}
30
- final sceneJson = JsonDecoder ().convert (sceneJsonText);
31
- final sceneMetadata = GoldenSceneMetadata .fromJson (sceneJson);
32
30
33
31
// Decode PNG data to an image.
34
32
final sceneImage = decodePng (scenePngBytes);
@@ -38,7 +36,7 @@ import 'package:image/image.dart';
38
36
}
39
37
40
38
// Extract the golden images from the scene image.
41
- return ( _extractCollectionFromScene (sceneMetadata, sceneImage), sceneMetadata );
39
+ return _extractCollectionFromScene (sceneMetadata, sceneImage);
42
40
}
43
41
44
42
/// Extracts a [GoldenCollection] from a golden scene within the current widget tree.
@@ -84,6 +82,31 @@ Future<GoldenCollection> extractGoldenCollectionFromSceneWidgetTree(
84
82
return _extractCollectionFromScene (sceneMetadata, treeImage);
85
83
}
86
84
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
+
87
110
GoldenCollection _extractCollectionFromScene (GoldenSceneMetadata sceneMetadata, Image sceneImage) {
88
111
// Cut each golden image out of the scene.
89
112
final goldenImages = < String , GoldenImage > {};
0 commit comments