Skip to content

Commit

Permalink
fix: added configurable padding (#133)
Browse files Browse the repository at this point in the history
* fix: added configurable padding

* fix: added test for padding

* fix: padding

* fix: tests
  • Loading branch information
vanlooverenkoen authored Nov 14, 2024
1 parent 4bc0fc5 commit ba5e4b3
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 20 deletions.
5 changes: 4 additions & 1 deletion lib/src/golden_test_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,10 @@ class FlutterGoldenTestAdapter extends GoldenTestAdapter {
maxHeight: constraints.maxHeight,
child: Center(
key: childKey,
child: widget,
child: Padding(
padding: goldenTestTheme.padding,
child: widget,
),
),
),
);
Expand Down
41 changes: 22 additions & 19 deletions lib/src/golden_test_scenario.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,27 +73,30 @@ class GoldenTestScenario extends StatelessWidget {
final testTheme = Theme.of(context).extension<GoldenTestTheme>() ??
AlchemistConfig.current().goldenTestTheme ??
GoldenTestTheme.standard();
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Text(
name,
style: testTheme.nameTextStyle,
textHeightBehavior: const TextHeightBehavior(
applyHeightToFirstAscent: false,
return Padding(
padding: testTheme.padding,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Text(
name,
style: testTheme.nameTextStyle,
textHeightBehavior: const TextHeightBehavior(
applyHeightToFirstAscent: false,
),
),
),
const SizedBox(height: 8),
ConstrainedBox(
constraints: constraints ??
GoldenTestScenarioConstraints.maybeOf(context) ??
const BoxConstraints(),
child: Builder(
builder: builder,
const SizedBox(height: 8),
ConstrainedBox(
constraints: constraints ??
GoldenTestScenarioConstraints.maybeOf(context) ??
const BoxConstraints(),
child: Builder(
builder: builder,
),
),
),
],
],
),
);
}
}
Expand Down
9 changes: 9 additions & 0 deletions lib/src/golden_test_theme.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class GoldenTestTheme extends ThemeExtension<GoldenTestTheme> {
required this.backgroundColor,
required this.borderColor,
required this.nameTextStyle,
this.padding = EdgeInsets.zero,
});

/// The standard theme for golden tests, used when no other theme is provided.
Expand All @@ -33,18 +34,25 @@ class GoldenTestTheme extends ThemeExtension<GoldenTestTheme> {
/// The border color used to separate scenarios in a [GoldenTestGroup].
final Color borderColor;

/// The padding that is used to wrap around:
/// - the whole image
/// - each individual [GoldenTestScenario]
final EdgeInsetsGeometry padding;

/// The text style that is used to show the name in a [GoldenTestScenario]
final TextStyle nameTextStyle;

@override
ThemeExtension<GoldenTestTheme> copyWith({
Color? backgroundColor,
Color? borderColor,
EdgeInsetsGeometry? padding,
TextStyle? nameTextStyle,
}) {
return GoldenTestTheme(
backgroundColor: backgroundColor ?? this.backgroundColor,
borderColor: borderColor ?? this.borderColor,
padding: padding ?? this.padding,
nameTextStyle: nameTextStyle ?? this.nameTextStyle,
);
}
Expand All @@ -61,6 +69,7 @@ class GoldenTestTheme extends ThemeExtension<GoldenTestTheme> {
backgroundColor: Color.lerp(backgroundColor, other.backgroundColor, t) ??
backgroundColor,
borderColor: Color.lerp(borderColor, other.borderColor, t) ?? borderColor,
padding: EdgeInsetsGeometry.lerp(padding, other.padding, t) ?? padding,
nameTextStyle: nameTextStyle.copyWith(
color: Color.lerp(nameTextStyle.color, other.nameTextStyle.color, t) ??
nameTextStyle.color,
Expand Down
63 changes: 63 additions & 0 deletions test/src/golden_test_adapter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:alchemist/src/blocked_text_image.dart';
import 'package:alchemist/src/golden_test_adapter.dart';
import 'package:alchemist/src/golden_test_group.dart';
import 'package:alchemist/src/golden_test_scenario.dart';
import 'package:alchemist/src/golden_test_theme.dart';
import 'package:alchemist/src/pumps.dart';
import 'package:alchemist/src/utilities.dart';
import 'package:flutter/cupertino.dart';
Expand Down Expand Up @@ -316,6 +317,68 @@ void main() {
expect(tester.platformDispatcher.textScaleFactor, 2.0);
});

group('padding', () {
testWidgets('default padding for `GoldenTestTheme`', (tester) async {
await adapter.pumpGoldenTest(
tester: tester,
textScaleFactor: 2,
constraints: const BoxConstraints(),
obscureFont: false,
globalConfigTheme: null,
variantConfigTheme: null,
goldenTestTheme: GoldenTestTheme.standard(),
pumpBeforeTest: onlyPumpAndSettle,
pumpWidget: onlyPumpWidget,
widget: buildGroup(),
);

final box = find.byType(Padding);

expect(box, findsNWidgets(2));
expect(
tester.widget<Padding>(box.at(0)).padding,
EdgeInsets.zero,
);
expect(
tester.widget<Padding>(box.at(1)).padding,
EdgeInsets.zero,
);
});
testWidgets('custom padding on `GoldenTestTheme`', (tester) async {
await adapter.pumpGoldenTest(
tester: tester,
textScaleFactor: 2,
constraints: const BoxConstraints(),
obscureFont: false,
globalConfigTheme: null,
variantConfigTheme: null,
goldenTestTheme: GoldenTestTheme(
backgroundColor: Colors.white,
borderColor: Colors.black,
padding: const EdgeInsets.all(16),
nameTextStyle: const TextStyle(
color: Colors.black,
),
),
pumpBeforeTest: onlyPumpAndSettle,
pumpWidget: onlyPumpWidget,
widget: buildGroup(),
);

final box = find.byType(Padding);

expect(box, findsNWidgets(2));
expect(
tester.widget<Padding>(box.at(0)).padding,
const EdgeInsets.all(16),
);
expect(
tester.widget<Padding>(box.at(1)).padding,
const EdgeInsets.all(16),
);
});
});

testWidgets(
'renders FlutterGoldenTestWrapper with provided '
'obscure font, themes and child arguments',
Expand Down

0 comments on commit ba5e4b3

Please sign in to comment.