/
my_golden_test_adapter.dart
133 lines (116 loc) · 3.8 KB
/
my_golden_test_adapter.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import 'dart:ui' as ui;
import 'package:alchemist/src/pumps.dart';
import 'package:alchemist/src/utilities.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:alchemist/src/golden_test_adapter.dart';
class MyGoldenTestAdapter extends GoldenTestAdapter {
/// Create a new [MyGoldenTestAdapter].
const MyGoldenTestAdapter() : super();
/// Key for the root of the golden test.
static final rootKey = UniqueKey();
/// Key for the child container in the golden test.
static final childKey = UniqueKey();
@override
Future<T> withForceUpdateGoldenFiles<T>({
bool forceUpdate = false,
required MatchesGoldenFileInvocation<T> callback,
}) async {
if (!forceUpdate) {
return await callback();
}
final originalValue = autoUpdateGoldenFiles;
autoUpdateGoldenFiles = true;
try {
return await callback();
} finally {
autoUpdateGoldenFiles = originalValue;
}
}
@override
TestLifecycleFn get setUp => setUpFn;
@override
TestLifecycleFn get tearDown => tearDownFn;
@override
TestWidgetsFn get testWidgets => testWidgetsFn;
@override
GoldenFileExpectation get goldenFileExpectation => goldenFileExpectationFn;
@override
Future<void> pumpGoldenTest({
Key? rootKey,
required WidgetTester tester,
required double textScaleFactor,
required BoxConstraints constraints,
required ThemeData theme,
required Widget widget,
required PumpAction pumpBeforeTest,
required PumpWidget pumpWidget,
}) async {
final initialSize = Size(
constraints.hasBoundedWidth ? constraints.maxWidth : 2000,
constraints.hasBoundedHeight ? constraints.maxHeight : 2000,
);
await tester.binding.setSurfaceSize(initialSize);
tester.binding.window.physicalSizeTestValue = initialSize;
tester.binding.window.devicePixelRatioTestValue = 1.0;
tester.binding.window.textScaleFactorTestValue = textScaleFactor;
await pumpWidget(
tester,
MaterialApp(
key: rootKey,
theme: theme.stripTextPackages(),
debugShowCheckedModeBanner: false,
supportedLocales: const [Locale('en')],
/// My Changes:
home: DefaultAssetBundle(
bundle: TestAssetBundle(),
child: Material(
type: MaterialType.transparency,
child: Align(
alignment: Alignment.topLeft,
child: ColoredBox(
color: theme.colorScheme.background,
child: Padding(
key: childKey,
padding: const EdgeInsets.all(8),
child: widget,
),
),
),
),
),
),
);
final shouldTryResize = !constraints.isTight;
await pumpBeforeTest(tester);
if (shouldTryResize) {
final childSize = tester.getSize(find.byKey(childKey));
final newSize = Size(
childSize.width.clamp(constraints.minWidth, constraints.maxWidth),
childSize.height.clamp(constraints.minHeight, constraints.maxHeight),
);
if (newSize != initialSize) {
await tester.binding.setSurfaceSize(newSize);
tester.binding.window.physicalSizeTestValue = newSize;
}
}
await tester.pump();
}
@override
Future<ui.Image> getBlockedTextImage({
required Finder finder,
required WidgetTester tester,
}) async {
var renderObject = tester.renderObject(finder);
while (!renderObject.isRepaintBoundary) {
renderObject = renderObject.parent! as RenderObject;
}
final layer = renderObject.debugLayer! as OffsetLayer;
paintingContextBuilder(
layer,
renderObject.paintBounds,
).paintSingleChild(renderObject);
return layer.toImage(renderObject.paintBounds);
}
}