From ba5e4b3c4fdf1819ee5f95bb3aa5ca7d500e4b58 Mon Sep 17 00:00:00 2001 From: Koen Van Looveren Date: Thu, 14 Nov 2024 21:02:50 +0100 Subject: [PATCH] fix: added configurable padding (#133) * fix: added configurable padding * fix: added test for padding * fix: padding * fix: tests --- lib/src/golden_test_adapter.dart | 5 +- lib/src/golden_test_scenario.dart | 41 +++++++++-------- lib/src/golden_test_theme.dart | 9 ++++ test/src/golden_test_adapter_test.dart | 63 ++++++++++++++++++++++++++ 4 files changed, 98 insertions(+), 20 deletions(-) diff --git a/lib/src/golden_test_adapter.dart b/lib/src/golden_test_adapter.dart index 1b8453b..00d5157 100644 --- a/lib/src/golden_test_adapter.dart +++ b/lib/src/golden_test_adapter.dart @@ -264,7 +264,10 @@ class FlutterGoldenTestAdapter extends GoldenTestAdapter { maxHeight: constraints.maxHeight, child: Center( key: childKey, - child: widget, + child: Padding( + padding: goldenTestTheme.padding, + child: widget, + ), ), ), ); diff --git a/lib/src/golden_test_scenario.dart b/lib/src/golden_test_scenario.dart index 12c74ac..65a8532 100644 --- a/lib/src/golden_test_scenario.dart +++ b/lib/src/golden_test_scenario.dart @@ -73,27 +73,30 @@ class GoldenTestScenario extends StatelessWidget { final testTheme = Theme.of(context).extension() ?? 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, + ), ), - ), - ], + ], + ), ); } } diff --git a/lib/src/golden_test_theme.dart b/lib/src/golden_test_theme.dart index dd21ae9..2ef1d70 100644 --- a/lib/src/golden_test_theme.dart +++ b/lib/src/golden_test_theme.dart @@ -14,6 +14,7 @@ class GoldenTestTheme extends ThemeExtension { 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. @@ -33,6 +34,11 @@ class GoldenTestTheme extends ThemeExtension { /// 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; @@ -40,11 +46,13 @@ class GoldenTestTheme extends ThemeExtension { ThemeExtension 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, ); } @@ -61,6 +69,7 @@ class GoldenTestTheme extends ThemeExtension { 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, diff --git a/test/src/golden_test_adapter_test.dart b/test/src/golden_test_adapter_test.dart index f1bcfa1..07bbcd8 100644 --- a/test/src/golden_test_adapter_test.dart +++ b/test/src/golden_test_adapter_test.dart @@ -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'; @@ -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(box.at(0)).padding, + EdgeInsets.zero, + ); + expect( + tester.widget(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(box.at(0)).padding, + const EdgeInsets.all(16), + ); + expect( + tester.widget(box.at(1)).padding, + const EdgeInsets.all(16), + ); + }); + }); + testWidgets( 'renders FlutterGoldenTestWrapper with provided ' 'obscure font, themes and child arguments',