@@ -4,9 +4,10 @@ import 'dart:math';
4
4
import 'dart:typed_data' ;
5
5
import 'dart:ui' as ui;
6
6
7
- import 'package:flutter/material.dart' hide Image;
8
7
import 'package:flutter/material.dart' as m;
8
+ import 'package:flutter/material.dart' hide Image;
9
9
import 'package:flutter_test/flutter_test.dart' ;
10
+ import 'package:flutter_test_goldens/golden_bricks.dart' ;
10
11
import 'package:flutter_test_goldens/src/flutter/flutter_test_extensions.dart' ;
11
12
import 'package:flutter_test_goldens/src/goldens/golden_camera.dart' ;
12
13
import 'package:flutter_test_goldens/src/goldens/golden_collections.dart' ;
@@ -15,9 +16,11 @@ import 'package:flutter_test_goldens/src/goldens/golden_rendering.dart';
15
16
import 'package:flutter_test_goldens/src/goldens/golden_scenes.dart' ;
16
17
import 'package:flutter_test_goldens/src/goldens/pixel_comparisons.dart' ;
17
18
import 'package:flutter_test_goldens/src/logging.dart' ;
19
+ import 'package:flutter_test_goldens/src/scenes/golden_files.dart' ;
18
20
import 'package:flutter_test_goldens/src/scenes/golden_scene.dart' ;
19
21
import 'package:flutter_test_goldens/src/scenes/scene_layout.dart' ;
20
22
import 'package:image/image.dart' ;
23
+ import 'package:path/path.dart' ;
21
24
import 'package:qr_bar_code/code/code.dart' ;
22
25
23
26
/// A golden builder that builds independent widget tree UIs and then either
@@ -26,20 +29,25 @@ import 'package:qr_bar_code/code/code.dart';
26
29
class Gallery {
27
30
Gallery (
28
31
this ._tester, {
29
- required String sceneName,
32
+ Directory ? directory,
33
+ required String fileName,
34
+ required String sceneDescription,
30
35
required SceneLayout layout,
31
36
GalleryItemScaffold itemScaffold = defaultGalleryItemScaffold,
32
37
GalleryItemDecorator ? itemDecorator,
33
38
Widget ? goldenBackground,
34
39
m.Color qrCodeColor = m.Colors .black,
35
40
m.Color qrCodeBackgroundColor = m.Colors .white,
36
- }) : _sceneName = sceneName,
41
+ }) : _fileName = fileName,
42
+ _sceneDescription = sceneDescription,
37
43
_layout = layout,
38
44
_itemScaffold = itemScaffold,
39
45
_itemDecorator = itemDecorator,
40
46
_goldenBackground = goldenBackground,
41
47
_qrCodeColor = qrCodeColor,
42
- _qrCodeBackgroundColor = qrCodeBackgroundColor;
48
+ _qrCodeBackgroundColor = qrCodeBackgroundColor {
49
+ _directory = directory ?? defaultGoldenDirectory;
50
+ }
43
51
44
52
final WidgetTester _tester;
45
53
@@ -54,9 +62,14 @@ class Gallery {
54
62
/// All screenshots within this scene.
55
63
final _items = < GalleryItem > [];
56
64
57
- /// The name of the overall golden scene, which may includes many individual
58
- /// goldens.
59
- final String _sceneName;
65
+ /// The directory where the golden scene file will be saved.
66
+ late final Directory _directory;
67
+
68
+ /// The file name for the golden scene file, which will be saved in [_directory] .
69
+ final String _fileName;
70
+
71
+ /// A human readable description of what's in this scene.
72
+ final String _sceneDescription;
60
73
61
74
/// The layout to use to position all the items in this scene.
62
75
final SceneLayout _layout;
@@ -78,6 +91,7 @@ class Gallery {
78
91
GalleryItem .withWidget (
79
92
id: id,
80
93
description: description,
94
+ boundsFinder: boundsFinder,
81
95
child: widget,
82
96
),
83
97
);
@@ -96,6 +110,7 @@ class Gallery {
96
110
GalleryItem .withBuilder (
97
111
id: id,
98
112
description: description,
113
+ boundsFinder: boundsFinder,
99
114
builder: builder,
100
115
),
101
116
);
@@ -130,6 +145,7 @@ class Gallery {
130
145
GalleryItem .withPumper (
131
146
id: id,
132
147
description: description,
148
+ boundsFinder: boundsFinder,
133
149
pumper: pumper,
134
150
),
135
151
);
@@ -140,7 +156,7 @@ class Gallery {
140
156
/// Either renders a new golden to a scene file, or compares new screenshots against an existing
141
157
/// golden scene file.
142
158
Future <void > renderOrCompareGolden () async {
143
- FtgLog .pipeline.info ("Rendering or comparing golden - $_sceneName " );
159
+ FtgLog .pipeline.info ("Rendering or comparing golden - $_sceneDescription " );
144
160
145
161
// Build each gallery item and screenshot it.
146
162
final camera = GoldenCamera ();
@@ -155,29 +171,25 @@ class Gallery {
155
171
await _tester.pumpWidget (
156
172
_itemScaffold (
157
173
_tester,
158
- GoldenImageBounds (
159
- child: _itemDecorator != null
160
- ? _itemDecorator.call (
161
- _tester,
162
- Builder (builder: item.builder! ),
163
- )
164
- : Builder (builder: item.builder! ),
165
- ),
174
+ _itemDecorator != null
175
+ ? _itemDecorator.call (
176
+ _tester,
177
+ Builder (builder: item.builder! ),
178
+ )
179
+ : Builder (builder: item.builder! ),
166
180
),
167
181
);
168
182
} else {
169
183
// Pump this gallery item, deferring to a `Widget` for the content.
170
184
await _tester.pumpWidget (
171
185
_itemScaffold (
172
186
_tester,
173
- GoldenImageBounds (
174
- child: _itemDecorator != null
175
- ? _itemDecorator.call (
176
- _tester,
177
- item.child! ,
178
- )
179
- : item.child! ,
180
- ),
187
+ _itemDecorator != null
188
+ ? _itemDecorator.call (
189
+ _tester,
190
+ item.child! ,
191
+ )
192
+ : item.child! ,
181
193
),
182
194
);
183
195
}
@@ -237,15 +249,15 @@ class Gallery {
237
249
238
250
await _tester.pumpAndSettle ();
239
251
240
- final goldenFileName = "$ _sceneName . png" ;
252
+ final goldenFilePath = File ( "${ _directory . path }$ separator $ _fileName . png") ;
241
253
if (autoUpdateGoldenFiles) {
242
254
// Generate new goldens.
243
255
FtgLog .pipeline.finer ("Doing golden generation - window height: ${_tester .view .physicalSize .height }" );
244
- await expectLater (find.byType (GoldenSceneBounds ), matchesGoldenFile (goldenFileName ));
256
+ await expectLater (find.byType (GoldenSceneBounds ), matchesGoldenFile (goldenFilePath.path ));
245
257
} else {
246
258
// Compare to existing goldens.
247
259
FtgLog .pipeline.finer ("Comparing existing goldens..." );
248
- await _compareGoldens (_tester, goldenFileName , find.byType (GoldenSceneBounds ));
260
+ await _compareGoldens (_tester, goldenFilePath , find.byType (GoldenSceneBounds ));
249
261
FtgLog .pipeline.finer ("Done comparing goldens for gallery" );
250
262
}
251
263
@@ -363,9 +375,9 @@ class Gallery {
363
375
);
364
376
}
365
377
366
- Future <void > _compareGoldens (WidgetTester tester, String existingGoldenFileName, Finder goldenBounds) async {
378
+ Future <void > _compareGoldens (WidgetTester tester, File existingGoldenFileName, Finder goldenBounds) async {
367
379
final testFileDirectory = (goldenFileComparator as LocalFileComparator ).basedir.path;
368
- final goldenFile = File ("$testFileDirectory $existingGoldenFileName " );
380
+ final goldenFile = File ("$testFileDirectory ${ existingGoldenFileName . path } " );
369
381
370
382
if (! goldenFile.existsSync ()) {
371
383
// TODO: report error in structured way.
@@ -568,10 +580,21 @@ class GalleryItem {
568
580
final Widget ? child;
569
581
}
570
582
583
+ /// The ancestor widget tree for every item in a gallery, unless overridden by
584
+ /// the gallery configuration.
571
585
Widget defaultGalleryItemScaffold (WidgetTester tester, Widget content) {
572
586
return MaterialApp (
573
587
home: Scaffold (
574
- body: content,
588
+ body: m.Builder (builder: (context) {
589
+ return DefaultTextStyle (
590
+ style: DefaultTextStyle .of (context).style.copyWith (
591
+ fontFamily: goldenBricks,
592
+ ),
593
+ child: Center (
594
+ child: GoldenImageBounds (child: content),
595
+ ),
596
+ );
597
+ }),
575
598
),
576
599
debugShowCheckedModeBanner: false ,
577
600
);
0 commit comments