Skip to content

Commit 4e7a78b

Browse files
authored
Merge pull request #35 from eBay/add-dark-mode-and-safe-area-support
Add dark mode and safe area support
2 parents b43fc0a + f9516f5 commit 4e7a78b

File tree

7 files changed

+110
-17
lines changed

7 files changed

+110
-17
lines changed

packages/golden_toolkit/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 0.3.1
4+
5+
Resolve an issue where configuration performed on WidgetTester during multiScreenGolden could bleed over to other tests in the same file. Add additional convenience helpers for the Device class.
6+
37
## 0.3.0
48

59
Add support for configuring safe area (to simulate a device notch) and platform brightness (light/dark mode) on a multiScreenGolden device.

packages/golden_toolkit/lib/src/device.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ class Device {
2525
/// [phone] one of the smallest phone screens
2626
static const Device phone = Device(name: 'phone', size: Size(375, 667));
2727

28+
/// [iphone11] matches specs of iphone11, but with lower DPI for performance
29+
static const Device iphone11 = Device(
30+
name: 'iphone11',
31+
size: Size(414, 896),
32+
devicePixelRatio: 1.0,
33+
safeArea: EdgeInsets.only(top: 44, bottom: 34),
34+
);
35+
2836
/// [tabletLandscape] example of tablet that in landscape mode
2937
static const Device tabletLandscape =
3038
Device(name: 'tablet_landscape', size: Size(1366, 1024));
@@ -70,4 +78,16 @@ class Device {
7078
safeArea: safeArea ?? this.safeArea,
7179
);
7280
}
81+
82+
/// [dark] convenience method to copy the current device and apply dark theme
83+
Device dark() {
84+
return Device(
85+
size: size,
86+
devicePixelRatio: devicePixelRatio,
87+
textScale: textScale,
88+
brightness: Brightness.dark,
89+
safeArea: safeArea,
90+
name: '$name\_dark',
91+
);
92+
}
7393
}

packages/golden_toolkit/lib/src/multi_screen_golden.dart

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -55,27 +55,50 @@ Future<void> multiScreenGolden(
5555
bool skip = false,
5656
}) async {
5757
for (final device in devices) {
58-
final size =
59-
Size(device.size.width, overrideGoldenHeight ?? device.size.height);
60-
await tester.binding.setSurfaceSize(size);
61-
tester.binding.window.physicalSizeTestValue = device.size;
62-
tester.binding.window.devicePixelRatioTestValue = device.devicePixelRatio;
63-
tester.binding.window.textScaleFactorTestValue = device.textScale;
64-
tester.binding.window.paddingTestValue = _FakeWindowPadding(
58+
await tester._applyDeviceOverrides(
59+
device,
60+
overriddenHeight: overrideGoldenHeight,
61+
operation: () async {
62+
await deviceSetup(device, tester);
63+
await screenMatchesGolden(
64+
tester,
65+
'$goldenFileName.${device.name}',
66+
customPump: customPump,
67+
skip: skip,
68+
finder: finder,
69+
);
70+
},
71+
);
72+
}
73+
}
74+
75+
extension on WidgetTester {
76+
Future<void> _applyDeviceOverrides(
77+
Device device, {
78+
double overriddenHeight,
79+
Future<void> Function() operation,
80+
}) async {
81+
await binding.setSurfaceSize(
82+
Size(device.size.width, overriddenHeight ?? device.size.height));
83+
binding.window.physicalSizeTestValue = device.size;
84+
binding.window.devicePixelRatioTestValue = device.devicePixelRatio;
85+
binding.window.textScaleFactorTestValue = device.textScale;
86+
binding.window.paddingTestValue = _FakeWindowPadding(
6587
bottom: device.safeArea.bottom,
6688
left: device.safeArea.left,
6789
right: device.safeArea.right,
6890
top: device.safeArea.top,
6991
);
70-
tester.binding.window.platformBrightnessTestValue = device.brightness;
71-
await deviceSetup(device, tester);
72-
await screenMatchesGolden(
73-
tester,
74-
'$goldenFileName.${device.name}',
75-
customPump: customPump,
76-
skip: skip,
77-
finder: finder,
78-
);
92+
binding.window.platformBrightnessTestValue = device.brightness;
93+
94+
await operation();
95+
96+
binding.window.clearDevicePixelRatioTestValue();
97+
binding.window.clearPlatformBrightnessTestValue();
98+
binding.window.clearPaddingTestValue();
99+
binding.window.clearTextScaleFactorTestValue();
100+
binding.window.clearPhysicalSizeTestValue();
101+
await binding.setSurfaceSize(null);
79102
}
80103
}
81104

packages/golden_toolkit/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: golden_toolkit
22
description: Common patterns for screenshot-based widget testing using Goldens.
3-
version: 0.3.0
3+
version: 0.3.1
44
homepage: https://github.com/eBay/flutter_glove_box/
55
repository: https://github.com/eBay/flutter_glove_box/tree/master/packages/golden_toolkit
66
issue_tracker: https://github.com/eBay/flutter_glove_box/issues

packages/golden_toolkit/test/device_test.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,15 @@ void main() {
4141
expect(copied.brightness, equals(Brightness.dark));
4242
expect(copied.safeArea, equals(const EdgeInsets.symmetric(vertical: 16)));
4343
});
44+
45+
test('dark() helper', () {
46+
final dark = Device.iphone11.dark();
47+
expect(dark.devicePixelRatio, equals(Device.iphone11.devicePixelRatio));
48+
expect(dark.name, equals('iphone11_dark'));
49+
expect(dark.size, equals(Device.iphone11.size));
50+
expect(dark.textScale, equals(Device.iphone11.textScale));
51+
expect(dark.brightness, equals(Brightness.dark));
52+
expect(dark.safeArea, equals(Device.iphone11.safeArea));
53+
});
4454
});
4555
}
456 Bytes
Loading

packages/golden_toolkit/test/multi_screen_golden_test.dart

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,42 @@ Future<void> main() async {
108108
skip: !Platform.isMacOS,
109109
);
110110
});
111+
112+
testGoldens('Should restore window binding settings', (tester) async {
113+
final size = tester.binding.createViewConfiguration().size;
114+
final initialSize = tester.binding.window.physicalSize;
115+
final initialBrightness = tester.binding.window.platformBrightness;
116+
final initialDevicePixelRatio = tester.binding.window.devicePixelRatio;
117+
final initialTextScaleFactor = tester.binding.window.textScaleFactor;
118+
final initialViewInsets = tester.binding.window.padding;
119+
120+
await tester.pumpWidgetBuilder(Container());
121+
await multiScreenGolden(
122+
tester,
123+
'empty',
124+
devices: [
125+
const Device(
126+
name: 'anything',
127+
size: Size(50, 75),
128+
brightness: Brightness.light,
129+
safeArea: EdgeInsets.all(4),
130+
devicePixelRatio: 2.0,
131+
textScale: 1.5,
132+
)
133+
],
134+
skip: !Platform.isMacOS,
135+
);
136+
137+
expect(tester.binding.createViewConfiguration().size, equals(size));
138+
expect(tester.binding.window.physicalSize, equals(initialSize));
139+
expect(tester.binding.window.platformBrightness,
140+
equals(initialBrightness));
141+
expect(tester.binding.window.devicePixelRatio,
142+
equals(initialDevicePixelRatio));
143+
expect(tester.binding.window.textScaleFactor,
144+
equals(initialTextScaleFactor));
145+
expect(tester.binding.window.padding, equals(initialViewInsets));
146+
});
111147
});
112148
});
113149
}

0 commit comments

Comments
 (0)