Skip to content

Commit b43fc0a

Browse files
authored
Merge pull request #33 from eBay/add-dark-mode-and-safe-area-support
Add dark mode and safe area support
2 parents 5f3b9c8 + 7fa8d14 commit b43fc0a

File tree

10 files changed

+108
-1
lines changed

10 files changed

+108
-1
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.0
4+
5+
Add support for configuring safe area (to simulate a device notch) and platform brightness (light/dark mode) on a multiScreenGolden device.
6+
37
## 0.2.3
48

59
Breaking: Removed Future return type from `testGoldens`.

packages/golden_toolkit/lib/src/device.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
/// ***************************************************
88
99
import 'dart:ui';
10+
import 'package:flutter/widgets.dart';
1011
import 'package:meta/meta.dart';
1112

1213
/// This [Device] is a configuration for golden test. Can be provided for [multiScreenGolden]
@@ -17,6 +18,8 @@ class Device {
1718
this.devicePixelRatio = 1.0,
1819
@required this.name,
1920
this.textScale = 1.0,
21+
this.brightness = Brightness.light,
22+
this.safeArea = const EdgeInsets.all(0),
2023
});
2124

2225
/// [phone] one of the smallest phone screens
@@ -43,18 +46,28 @@ class Device {
4346
/// [textScale] specify custom text scale
4447
final double textScale;
4548

49+
/// [brightness] specify platform brightness
50+
final Brightness brightness;
51+
52+
/// [safeArea] specify insets to define a safe area
53+
final EdgeInsets safeArea;
54+
4655
/// [copyWith] convenience function for [Device] modification
4756
Device copyWith({
4857
Size size,
4958
double devicePixelRatio,
5059
String name,
5160
double textScale,
61+
Brightness brightness,
62+
EdgeInsets safeArea,
5263
}) {
5364
return Device(
5465
size: size ?? this.size,
5566
devicePixelRatio: devicePixelRatio ?? this.devicePixelRatio,
5667
name: name ?? this.name,
5768
textScale: textScale ?? this.textScale,
69+
brightness: brightness ?? this.brightness,
70+
safeArea: safeArea ?? this.safeArea,
5871
);
5972
}
6073
}

packages/golden_toolkit/lib/src/multi_screen_golden.dart

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ Future<void> multiScreenGolden(
6161
tester.binding.window.physicalSizeTestValue = device.size;
6262
tester.binding.window.devicePixelRatioTestValue = device.devicePixelRatio;
6363
tester.binding.window.textScaleFactorTestValue = device.textScale;
64+
tester.binding.window.paddingTestValue = _FakeWindowPadding(
65+
bottom: device.safeArea.bottom,
66+
left: device.safeArea.left,
67+
right: device.safeArea.right,
68+
top: device.safeArea.top,
69+
);
70+
tester.binding.window.platformBrightnessTestValue = device.brightness;
6471
await deviceSetup(device, tester);
6572
await screenMatchesGolden(
6673
tester,
@@ -71,3 +78,24 @@ Future<void> multiScreenGolden(
7178
);
7279
}
7380
}
81+
82+
class _FakeWindowPadding implements WindowPadding {
83+
const _FakeWindowPadding({
84+
this.bottom = 0,
85+
this.left = 0,
86+
this.right = 0,
87+
this.top = 0,
88+
});
89+
90+
@override
91+
final double bottom;
92+
93+
@override
94+
final double left;
95+
96+
@override
97+
final double right;
98+
99+
@override
100+
final double top;
101+
}

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.2.3
3+
version: 0.3.0
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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
///
99
1010
import 'dart:ui';
11+
import 'package:flutter/widgets.dart';
1112
import 'package:flutter_test/flutter_test.dart';
1213
import 'package:golden_toolkit/golden_toolkit.dart';
1314

@@ -20,6 +21,8 @@ void main() {
2021
expect(copied.name, equals(Device.phone.name));
2122
expect(copied.size, equals(Device.phone.size));
2223
expect(copied.textScale, equals(Device.phone.textScale));
24+
expect(copied.brightness, equals(Device.phone.brightness));
25+
expect(copied.safeArea, equals(Device.phone.safeArea));
2326
});
2427

2528
test('copy with parameters', () {
@@ -28,11 +31,15 @@ void main() {
2831
devicePixelRatio: 2.0,
2932
name: 'foo',
3033
size: const Size(100, 100),
34+
brightness: Brightness.dark,
35+
safeArea: const EdgeInsets.symmetric(vertical: 16),
3136
);
3237
expect(copied.devicePixelRatio, equals(2.0));
3338
expect(copied.name, equals('foo'));
3439
expect(copied.size, equals(const Size(100, 100)));
3540
expect(copied.textScale, equals(3.0));
41+
expect(copied.brightness, equals(Brightness.dark));
42+
expect(copied.safeArea, equals(const EdgeInsets.symmetric(vertical: 16)));
3643
});
3744
});
3845
}
2.69 KB
Loading
2.6 KB
Loading
623 Bytes
Loading
651 Bytes
Loading

packages/golden_toolkit/test/multi_screen_golden_test.dart

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,61 @@ Future<void> main() async {
5353
skip: !Platform.isMacOS,
5454
);
5555
});
56+
57+
testGoldens('Safe Area test', (tester) async {
58+
await tester.pumpWidgetBuilder(
59+
Container(
60+
color: Colors.white,
61+
child: SafeArea(child: Container(color: Colors.blue))),
62+
);
63+
await multiScreenGolden(
64+
tester,
65+
'safe_area',
66+
devices: [
67+
const Device(
68+
name: 'no_safe_area',
69+
size: Size(200, 200),
70+
),
71+
const Device(
72+
name: 'safe_area',
73+
size: Size(200, 200),
74+
safeArea: EdgeInsets.fromLTRB(5, 10, 15, 20),
75+
)
76+
],
77+
skip: !Platform.isMacOS,
78+
);
79+
});
80+
81+
testGoldens('Platform Brightness Test', (tester) async {
82+
await tester.pumpWidgetBuilder(
83+
Builder(
84+
builder: (context) => Container(
85+
color:
86+
MediaQuery.of(context).platformBrightness == Brightness.dark
87+
? Colors.grey
88+
: Colors.white,
89+
child: Text(MediaQuery.of(context).platformBrightness.toString()),
90+
),
91+
),
92+
);
93+
await multiScreenGolden(
94+
tester,
95+
'brightness',
96+
devices: [
97+
const Device(
98+
name: 'light',
99+
size: Size(200, 200),
100+
brightness: Brightness.light,
101+
),
102+
const Device(
103+
name: 'dark',
104+
size: Size(200, 200),
105+
brightness: Brightness.dark,
106+
)
107+
],
108+
skip: !Platform.isMacOS,
109+
);
110+
});
56111
});
57112
});
58113
}

0 commit comments

Comments
 (0)