Skip to content

Commit

Permalink
feat: GoldenTestTheme (Betterment#124)
Browse files Browse the repository at this point in the history
* wip: introduce GoldenTestTheme

* chore: regenerate goldens and add some tests

* chore: Updating Goldens

* Re-run CI after updating goldens
  • Loading branch information
btrautmann authored Sep 16, 2024
1 parent 7b386af commit 83740a6
Show file tree
Hide file tree
Showing 22 changed files with 212 additions and 4 deletions.
14 changes: 14 additions & 0 deletions lib/src/alchemist_config.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:async';

import 'package:alchemist/alchemist.dart';
import 'package:alchemist/src/golden_test_theme.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter/material.dart';

Expand Down Expand Up @@ -67,11 +68,13 @@ class AlchemistConfig extends Equatable {
/// {@macro alchemist_config}
const AlchemistConfig({
bool? forceUpdateGoldenFiles,
GoldenTestTheme? goldenTestTheme,
ThemeData? theme,
PlatformGoldensConfig? platformGoldensConfig,
CiGoldensConfig? ciGoldensConfig,
}) : _forceUpdateGoldenFiles = forceUpdateGoldenFiles,
_theme = theme,
_goldenTestTheme = goldenTestTheme,
_platformGoldensConfig = platformGoldensConfig,
_ciGoldensConfig = ciGoldensConfig;

Expand Down Expand Up @@ -181,6 +184,13 @@ class AlchemistConfig extends Equatable {
ThemeData? get theme => _theme;
final ThemeData? _theme;

/// The [GoldenTestTheme] to use when generating golden tests.
///
/// If no [GoldenTestTheme] is provided, the default
/// [GoldenTestTheme.standard] will be used.
GoldenTestTheme? get goldenTestTheme => _goldenTestTheme;
final GoldenTestTheme? _goldenTestTheme;

/// The configuration for human-readable golden tests running in non-CI
/// environments.
///
Expand All @@ -202,12 +212,14 @@ class AlchemistConfig extends Equatable {
AlchemistConfig copyWith({
bool? forceUpdateGoldenFiles,
ThemeData? theme,
GoldenTestTheme? goldenTestTheme,
PlatformGoldensConfig? platformGoldensConfig,
CiGoldensConfig? ciGoldensConfig,
}) {
return AlchemistConfig(
forceUpdateGoldenFiles: forceUpdateGoldenFiles ?? _forceUpdateGoldenFiles,
theme: theme ?? _theme,
goldenTestTheme: goldenTestTheme ?? _goldenTestTheme,
platformGoldensConfig: platformGoldensConfig ?? _platformGoldensConfig,
ciGoldensConfig: ciGoldensConfig ?? _ciGoldensConfig,
);
Expand All @@ -224,6 +236,7 @@ class AlchemistConfig extends Equatable {
return copyWith(
forceUpdateGoldenFiles: other?._forceUpdateGoldenFiles,
theme: other?._theme,
goldenTestTheme: other?._goldenTestTheme,
platformGoldensConfig:
platformGoldensConfig.merge(other?._platformGoldensConfig),
ciGoldensConfig: ciGoldensConfig.merge(other?._ciGoldensConfig),
Expand All @@ -234,6 +247,7 @@ class AlchemistConfig extends Equatable {
List<Object?> get props => [
forceUpdateGoldenFiles,
theme,
goldenTestTheme,
platformGoldensConfig,
ciGoldensConfig,
];
Expand Down
7 changes: 6 additions & 1 deletion lib/src/golden_test_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:async';
import 'dart:ui' as ui;

import 'package:alchemist/alchemist.dart';
import 'package:alchemist/src/golden_test_theme.dart';
import 'package:alchemist/src/utilities.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
Expand Down Expand Up @@ -164,6 +165,7 @@ abstract class GoldenTestAdapter {
required bool obscureFont,
required ThemeData? globalConfigTheme,
required ThemeData? variantConfigTheme,
required GoldenTestTheme? goldenTestTheme,
required PumpAction pumpBeforeTest,
required PumpWidget pumpWidget,
required Widget widget,
Expand Down Expand Up @@ -227,13 +229,16 @@ class FlutterGoldenTestAdapter extends GoldenTestAdapter {
required bool obscureFont,
required ThemeData? globalConfigTheme,
required ThemeData? variantConfigTheme,
required GoldenTestTheme? goldenTestTheme,
required PumpAction pumpBeforeTest,
required PumpWidget pumpWidget,
required Widget widget,
}) async {
tester.view.devicePixelRatio = 1.0;
tester.platformDispatcher.textScaleFactorTestValue = textScaleFactor;

goldenTestTheme ??= GoldenTestTheme.standard();

await pumpWidget(
tester,
FlutterGoldenTestWrapper(
Expand All @@ -250,7 +255,7 @@ class FlutterGoldenTestAdapter extends GoldenTestAdapter {
child: Builder(
builder: (context) {
return ColoredBox(
color: Theme.of(context).colorScheme.background,
color: goldenTestTheme!.backgroundColor,
child: OverflowBox(
alignment: Alignment.topLeft,
minWidth: constraints.minWidth,
Expand Down
9 changes: 6 additions & 3 deletions lib/src/golden_test_group.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:math';

import 'package:alchemist/alchemist.dart';
import 'package:alchemist/src/golden_test_scenario_constraints.dart';
import 'package:alchemist/src/golden_test_theme.dart';
import 'package:flutter/material.dart';

/// A function that receives the index of a column in a table and returns the
Expand Down Expand Up @@ -112,15 +113,17 @@ class GoldenTestGroup extends StatelessWidget {
}
}

final alchemistConfig = AlchemistConfig.current();
final testTheme =
alchemistConfig.goldenTestTheme ?? GoldenTestTheme.standard();

return GoldenTestScenarioConstraints(
constraints: scenarioConstraints,
child: Table(
defaultColumnWidth: const IntrinsicColumnWidth(),
columnWidths: columnWidths,
border: TableBorder.symmetric(
inside: BorderSide(
color: Colors.black.withOpacity(0.3),
),
inside: BorderSide(color: testTheme.borderColor),
),
children: [
for (int i = 0; i < _effectiveRows; i++)
Expand Down
3 changes: 3 additions & 0 deletions lib/src/golden_test_runner.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:alchemist/src/golden_test_adapter.dart';
import 'package:alchemist/src/golden_test_theme.dart';
import 'package:alchemist/src/interactions.dart';
import 'package:alchemist/src/pumps.dart';
import 'package:flutter/foundation.dart';
Expand Down Expand Up @@ -55,6 +56,7 @@ class FlutterGoldenTestRunner extends GoldenTestRunner {
required Widget widget,
ThemeData? globalConfigTheme,
ThemeData? variantConfigTheme,
GoldenTestTheme? goldenTestTheme,
bool forceUpdate = false,
bool obscureText = false,
bool renderShadows = false,
Expand Down Expand Up @@ -83,6 +85,7 @@ class FlutterGoldenTestRunner extends GoldenTestRunner {
obscureFont: obscureText,
globalConfigTheme: globalConfigTheme,
variantConfigTheme: variantConfigTheme,
goldenTestTheme: goldenTestTheme,
pumpBeforeTest: pumpBeforeTest,
pumpWidget: pumpWidget,
widget: widget,
Expand Down
32 changes: 32 additions & 0 deletions lib/src/golden_test_theme.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import 'package:alchemist/src/golden_test_group.dart';
import 'package:flutter/material.dart';

/// {@template golden_test_theme}
/// A theme that dictates the behavior and appearance of elements created
/// by Alchemist during golden testing. This theme is used to ensure that
/// parts of golden tests controlled by Alchemist are consistent across
/// Flutter SDK versions.
/// {@endtemplate}
class GoldenTestTheme {
/// {@macro golden_test_theme}
GoldenTestTheme({
required this.backgroundColor,
required this.borderColor,
});

/// The standard theme for golden tests, used when no other theme is provided.
factory GoldenTestTheme.standard() {
return GoldenTestTheme(
// These colors are not tied to any implementation so they won't
// change out from under us, which would cause golden tests to fail.
backgroundColor: const Color(0xFF2b54a1),
borderColor: const Color(0xFF3d394a),
);
}

/// The background color of the golden test.
final Color backgroundColor;

/// The border color used to separate scenarios in a [GoldenTestGroup].
final Color borderColor;
}
Binary file modified test/smoke_tests/goldens/ci/asset_image_smoke_test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/smoke_tests/goldens/ci/back_button_smoke_test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/smoke_tests/goldens/ci/composited_transform_smoke_test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/smoke_tests/goldens/ci/constrained_big_smoke_test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/smoke_tests/goldens/ci/dropdown_smoke_test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/smoke_tests/goldens/ci/error_message_smoke_test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/smoke_tests/goldens/ci/interactions_smoke_test_pressed.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/smoke_tests/goldens/ci/interactions_smoke_test_regular.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/smoke_tests/goldens/ci/network_image_smoke_test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/smoke_tests/goldens/ci/render_object_text_smoke_test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/smoke_tests/goldens/ci/timer_button_smoke_test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/smoke_tests/goldens/ci/unconstrained_smoke_test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions test/src/golden_test_adapter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ void main() {
obscureFont: false,
globalConfigTheme: null,
variantConfigTheme: null,
goldenTestTheme: null,
pumpBeforeTest: onlyPumpAndSettle,
pumpWidget: onlyPumpWidget,
widget: buildGroup(),
Expand All @@ -286,6 +287,7 @@ void main() {
obscureFont: false,
globalConfigTheme: null,
variantConfigTheme: null,
goldenTestTheme: null,
pumpBeforeTest: onlyPumpAndSettle,
pumpWidget: onlyPumpWidget,
widget: buildGroup(),
Expand All @@ -305,6 +307,7 @@ void main() {
obscureFont: false,
globalConfigTheme: null,
variantConfigTheme: null,
goldenTestTheme: null,
pumpBeforeTest: onlyPumpAndSettle,
pumpWidget: onlyPumpWidget,
widget: buildGroup(),
Expand All @@ -324,6 +327,7 @@ void main() {
obscureFont: true,
globalConfigTheme: globalConfigTheme,
variantConfigTheme: variantConfigTheme,
goldenTestTheme: null,
pumpBeforeTest: onlyPumpAndSettle,
pumpWidget: onlyPumpWidget,
widget: buildGroup(),
Expand Down Expand Up @@ -365,6 +369,7 @@ void main() {
obscureFont: false,
globalConfigTheme: null,
variantConfigTheme: null,
goldenTestTheme: null,
pumpBeforeTest: (_) async => pumpBeforeTestCalled = true,
pumpWidget: onlyPumpWidget,
widget: buildGroup(),
Expand All @@ -382,6 +387,7 @@ void main() {
obscureFont: false,
globalConfigTheme: null,
variantConfigTheme: null,
goldenTestTheme: null,
pumpBeforeTest: onlyPumpAndSettle,
pumpWidget: (tester, widget) async {
await onlyPumpWidget(tester, widget);
Expand Down
2 changes: 2 additions & 0 deletions test/src/golden_test_runner_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ void main() {
obscureFont: any(named: 'obscureFont'),
variantConfigTheme: any(named: 'variantConfigTheme'),
globalConfigTheme: any(named: 'globalConfigTheme'),
goldenTestTheme: any(named: 'goldenTestTheme'),
pumpBeforeTest: any(named: 'pumpBeforeTest'),
pumpWidget: any(named: 'pumpWidget'),
widget: any(named: 'widget'),
Expand Down Expand Up @@ -166,6 +167,7 @@ void main() {
obscureFont: any(named: 'obscureFont'),
globalConfigTheme: any(named: 'globalConfigTheme'),
variantConfigTheme: any(named: 'variantConfigTheme'),
goldenTestTheme: any(named: 'goldenTestTheme'),
),
).thenAnswer((_) async {
tester.view.physicalSize = Size.zero;
Expand Down
2 changes: 2 additions & 0 deletions test/src/golden_test_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:ui' as ui;
import 'package:alchemist/alchemist.dart';
import 'package:alchemist/src/golden_test_adapter.dart';
import 'package:alchemist/src/golden_test_runner.dart';
import 'package:alchemist/src/golden_test_theme.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';
Expand Down Expand Up @@ -41,6 +42,7 @@ class FakeGoldenTestAdapter extends Mock implements GoldenTestAdapter {
required bool obscureFont,
required ThemeData? globalConfigTheme,
required ThemeData? variantConfigTheme,
required GoldenTestTheme? goldenTestTheme,
required PumpAction pumpBeforeTest,
required PumpWidget pumpWidget,
required Widget widget,
Expand Down
Loading

0 comments on commit 83740a6

Please sign in to comment.