Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions doc/marketing_goldens/shadcn_test_tools.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ class ShadcnSingleShotSceneLayout implements SceneLayout {
Widget build(
WidgetTester tester,
BuildContext context,
Map<GoldenSceneScreenshot, GlobalKey<State<StatefulWidget>>> goldens,
SceneLayoutContent content,
) {
final golden = goldens.entries.first;
final golden = content.goldens.entries.first;

return DefaultTextStyle(
style: GoldenSceneTheme.current.defaultTextStyle.copyWith(
Expand Down Expand Up @@ -129,9 +129,9 @@ class ShadcnGalleryLayout implements SceneLayout {
Widget build(
WidgetTester tester,
BuildContext context,
Map<GoldenSceneScreenshot, GlobalKey<State<StatefulWidget>>> goldens,
SceneLayoutContent content,
) {
final entries = goldens.entries.toList();
final entries = content.goldens.entries.toList();

return DefaultTextStyle(
style: GoldenSceneTheme.current.defaultTextStyle.copyWith(
Expand Down
2 changes: 1 addition & 1 deletion doc/website/source/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ how we solve them.

* **Failure Files:** Flutter spreads a single test failure across four different files.
It's frustrating to have to open up multiple files to cross reference. With
`flutter_test_goldes`, your failure output is painted to a single file for easy review.
`flutter_test_goldens`, your failure output is painted to a single file for easy review.
* **Widget Galleries:** Flutter developers often want to verify multiple configurations
of a single widget, or multiple related widgets, at the same time. With
`flutter_test_goldens`, you can easily paint a variety of widgets into a gallery,
Expand Down
16 changes: 9 additions & 7 deletions doc/website/source/styles/docs_page_layout.scss
Original file line number Diff line number Diff line change
Expand Up @@ -265,13 +265,15 @@ main.page-content {
margin-bottom: 1.5em;
}

code {
padding: 3px 6px;
background: #7f00a6;
border: 1px solid #a218cc;
border-radius: 4px;

color: WHITE;
p, li > {
code {
padding: 3px 6px;
background: #7f00a6;
border: 1px solid #a218cc;
border-radius: 4px;

color: WHITE;
}
}

li {
Expand Down
12 changes: 1 addition & 11 deletions lib/src/flutter/flutter_camera.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,7 @@ class FlutterCamera {
);
}

final pictureRecorder = PictureRecorder();
final canvas = Canvas(pictureRecorder);
final screenSize = fullscreenRenderObject.size;

final paintingContext = TestRecordingPaintingContext(canvas);
fullscreenRenderObject.paint(paintingContext, Offset.zero);

final fullscreenPhoto = await pictureRecorder.endRecording().toImage(
screenSize.width.round(),
screenSize.height.round(),
);
final fullscreenPhoto = fullscreenRenderObject.toImageSync();

final contentFinder = finder ?? find.byType(GoldenImageBounds);
expect(finder, findsOne);
Expand Down
1 change: 0 additions & 1 deletion lib/src/flutter/flutter_golden_matcher.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'dart:io';
import 'dart:typed_data';

import 'package:flutter/foundation.dart';
import 'package:flutter_test/flutter_test.dart';
Expand Down
2 changes: 1 addition & 1 deletion lib/src/scenes/failure_scene.dart
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ Future<(Image, FailureSceneMetadata)> _layoutFailureScene(
return layout.build(
tester,
context,
renderablePhotos,
SceneLayoutContent(goldens: renderablePhotos),
);
},
),
Expand Down
43 changes: 31 additions & 12 deletions lib/src/scenes/gallery.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ class Gallery {
BoxConstraints? itemConstraints,
Finder? itemBoundsFinder,
required SceneLayout layout,
GoldenSetup? itemSetup,
}) : _fileName = fileName,
_sceneDescription = sceneDescription,
_itemScaffold = itemScaffold,
_itemConstraints = itemConstraints,
_itemBoundsFinder = itemBoundsFinder,
_layout = layout {
_layout = layout,
_itemSetup = itemSetup {
_directory = directory ?? GoldenSceneTheme.current.directory;
}

Expand Down Expand Up @@ -84,6 +86,12 @@ class Gallery {
/// 3. `find.byType(GoldenImageBounds)`.
final Finder? _itemBoundsFinder;

/// An optional setup method that runs after pumping an item's tree, and just before the
/// item is screenshotted.
///
/// This setup runs for every item in the scene unless an individual item overrides it.
final GoldenSetup? _itemSetup;

/// Requests for all screenshots within this scene, by their ID.
final _requests = <String, GalleryGoldenRequest>{};

Expand Down Expand Up @@ -329,6 +337,14 @@ class Gallery {
final previousPlatform = debugDefaultTargetPlatformOverride;
debugDefaultTargetPlatformOverride = item.platform ?? previousPlatform;

if (itemConstraints != null && itemConstraints.hasBoundedWidth && itemConstraints.hasBoundedHeight) {
// Some tests may want to control the size of the window. If we're given bounded
// constraints, make the window the biggest allowable size.
final previousSize = tester.view.physicalSize;
tester.view.physicalSize = itemConstraints.biggest;
addTearDown(() => tester.view.physicalSize = previousSize);
}

if (item.pumper != null) {
// Defer to the `pumper` to pump the entire widget tree for this gallery item.
await item.pumper!.call(tester, itemScaffold, item.description);
Expand All @@ -353,7 +369,7 @@ class Gallery {
}

// Run the item's setup function, if there is one.
await item.setup?.call(tester);
await (item.setup ?? _itemSetup)?.call(tester);

// Take a screenshot.
expect(item.boundsFinder, findsOne);
Expand Down Expand Up @@ -476,8 +492,11 @@ Image.memory(
SceneLayout layout,
Map<String, GoldenSceneScreenshot> goldenScreenshots,
) async {
final goldensAndGlobalKeys = Map<GoldenSceneScreenshot, GlobalKey>.fromEntries(
goldenScreenshots.entries.map((entry) => MapEntry(entry.value, GlobalKey())),
final content = SceneLayoutContent(
description: _sceneDescription,
goldens: Map<GoldenSceneScreenshot, GlobalKey>.fromEntries(
goldenScreenshots.entries.map((entry) => MapEntry(entry.value, GlobalKey())),
),
);

// Layout the gallery scene with the new goldens, check the intrinsic size of the
Expand All @@ -487,13 +506,13 @@ Image.memory(
// a corresponding `GlobalKey` already in the tree. Therefore, this layout pass inserts a
// `GlobalKey` for every golden screenshot that we want to render.
await tester.pumpWidgetAndAdjustWindow(
_buildGalleryLayout(tester, goldensAndGlobalKeys),
_buildGalleryLayout(tester, content),
);

// Use Flutter's `precacheImage()` mechanism to get each golden screenshot bitmap to
// render in this widget test.
await tester.runAsync(() async {
for (final entry in goldensAndGlobalKeys.entries) {
for (final entry in content.goldens.entries) {
await precacheImage(
MemoryImage(entry.key.pngBytes),
tester.element(find.byKey(entry.value)),
Expand All @@ -506,21 +525,21 @@ Image.memory(
return GoldenSceneMetadata(
description: _sceneDescription,
images: [
for (final golden in goldensAndGlobalKeys.keys)
for (final golden in content.goldens.keys)
GoldenImageMetadata(
id: golden.id,
metadata: golden.metadata,
topLeft: (goldensAndGlobalKeys[golden]!.currentContext!.findRenderObject() as RenderBox)
.localToGlobal(Offset.zero),
size: goldensAndGlobalKeys[golden]!.currentContext!.size!,
topLeft:
(content.goldens[golden]!.currentContext!.findRenderObject() as RenderBox).localToGlobal(Offset.zero),
size: content.goldens[golden]!.currentContext!.size!,
),
],
);
}

Widget _buildGalleryLayout(WidgetTester tester, Map<GoldenSceneScreenshot, GlobalKey> candidatesAndGlobalKeys) {
Widget _buildGalleryLayout(WidgetTester tester, SceneLayoutContent content) {
return Builder(builder: (context) {
return _layout.build(tester, context, candidatesAndGlobalKeys);
return _layout.build(tester, context, content);
});
}

Expand Down
Loading