Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose a method to override rendering of the final generated image #165

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
19 changes: 15 additions & 4 deletions packages/golden_toolkit/lib/src/configuration.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:async';
import 'dart:ui';

import 'package:flutter/foundation.dart';

Expand All @@ -20,8 +21,7 @@ import '../golden_toolkit.dart';
class GoldenToolkit {
GoldenToolkit._();

static GoldenToolkitConfiguration _configuration =
GoldenToolkitConfiguration();
static GoldenToolkitConfiguration _configuration = GoldenToolkitConfiguration();

/// Applies a GoldenToolkitConfiguration to a block of code to effectively provide a scoped
/// singleton. The configuration will apply to just the injected body function.
Expand Down Expand Up @@ -53,6 +53,10 @@ class GoldenToolkit {
/// A func that will be evaluated at runtime to determine if the golden assertion should be skipped
typedef SkipGoldenAssertion = bool Function();

/// a function that returns a Future<ui.Image> given a Finder & Tester.
/// A typical example is to draw boxes instead of paragraphs when rendering the UI to get consistent results across platforms
typedef RenderModification = Future<Image> Function({required Finder finder, required WidgetTester tester});

/// A factory to determine an actual file name/path from a given name.
///
/// See also:
Expand Down Expand Up @@ -88,6 +92,9 @@ class GoldenToolkitConfiguration {
///
/// [primeAssets] a func that is used to ensure that all images have been decoded before trying to render
///
/// [renderModification] a func that returns a Future<ui.Image> given a Finder & Tester.
/// A typical example is to draw boxes instead of paragraphs when rendering the UI to get consistent results across platforms
///
/// [enableRealShadows] a flag indicating that we want the goldens to have real shadows (instead of opaque shadows)
///
/// [tags] a string or iterable of strings used to tag golden tests with
Expand All @@ -99,11 +106,16 @@ class GoldenToolkitConfiguration {
this.defaultDevices = const [Device.phone, Device.tabletLandscape],
this.enableRealShadows = false,
this.tags = const ['golden'],
this.renderModification,
}) : assert(defaultDevices.isNotEmpty);

/// a function indicating whether a golden assertion should be skipped
final SkipGoldenAssertion skipGoldenAssertion;

/// a function that returns a Future<ui.Image> given a Finder & Tester.
/// A typical example is to draw boxes instead of paragraphs when rendering the UI to get consistent results across platforms
final RenderModification? renderModification;

/// A function to determine the file name/path [screenMatchesGolden] uses to call [matchesGoldenFile].
final FileNameFactory fileNameFactory;

Expand Down Expand Up @@ -138,8 +150,7 @@ class GoldenToolkitConfiguration {
return GoldenToolkitConfiguration(
skipGoldenAssertion: skipGoldenAssertion ?? this.skipGoldenAssertion,
fileNameFactory: fileNameFactory ?? this.fileNameFactory,
deviceFileNameFactory:
deviceFileNameFactory ?? this.deviceFileNameFactory,
deviceFileNameFactory: deviceFileNameFactory ?? this.deviceFileNameFactory,
primeAssets: primeAssets ?? this.primeAssets,
defaultDevices: defaultDevices ?? this.defaultDevices,
enableRealShadows: enableRealShadows ?? this.enableRealShadows,
Expand Down
19 changes: 14 additions & 5 deletions packages/golden_toolkit/lib/src/testing_tools.dart
Original file line number Diff line number Diff line change
Expand Up @@ -292,11 +292,20 @@ Future<void> compareWithGolden(
await tester.pump();
}

await expectLater(
actualFinder,
matchesGoldenFile(fileName),
skip: shouldSkipGoldenGeneration,
);
if (GoldenToolkit.configuration.renderModification != null) {
final ciImage = GoldenToolkit.configuration.renderModification!(finder: actualFinder, tester: tester);
await expectLater(
ciImage,
matchesGoldenFile(fileName),
skip: shouldSkipGoldenGeneration,
);
} else {
await expectLater(
actualFinder,
matchesGoldenFile(fileName),
skip: shouldSkipGoldenGeneration,
);
}

if (autoHeight == true) {
// Here we reset the window size to its original value to be clean
Expand Down