From e3b0a0c7788dbee1d4fcdfab1d07eb9dae0063dc Mon Sep 17 00:00:00 2001 From: Corey Sprague Date: Mon, 13 Apr 2020 16:11:27 -0700 Subject: [PATCH 1/6] revert device settings back to normal upon completion of multiScreenGolden --- .../lib/src/multi_screen_golden.dart | 56 +++++++++++++----- .../test/goldens/empty.anything.png | Bin 0 -> 456 bytes .../test/multi_screen_golden_test.dart | 31 ++++++++++ 3 files changed, 71 insertions(+), 16 deletions(-) create mode 100644 packages/golden_toolkit/test/goldens/empty.anything.png diff --git a/packages/golden_toolkit/lib/src/multi_screen_golden.dart b/packages/golden_toolkit/lib/src/multi_screen_golden.dart index 79b251c..7b34807 100644 --- a/packages/golden_toolkit/lib/src/multi_screen_golden.dart +++ b/packages/golden_toolkit/lib/src/multi_screen_golden.dart @@ -55,27 +55,51 @@ Future multiScreenGolden( bool skip = false, }) async { for (final device in devices) { - final size = - Size(device.size.width, overrideGoldenHeight ?? device.size.height); - await tester.binding.setSurfaceSize(size); - tester.binding.window.physicalSizeTestValue = device.size; - tester.binding.window.devicePixelRatioTestValue = device.devicePixelRatio; - tester.binding.window.textScaleFactorTestValue = device.textScale; - tester.binding.window.paddingTestValue = _FakeWindowPadding( + await tester._applyDeviceOverrides( + device, + overriddenHeight: overrideGoldenHeight, + operation: () async { + await deviceSetup(device, tester); + await screenMatchesGolden( + tester, + '$goldenFileName.${device.name}', + customPump: customPump, + skip: skip, + finder: finder, + ); + }, + ); + await tester.pump(); + } +} + +extension on WidgetTester { + Future _applyDeviceOverrides( + Device device, { + double overriddenHeight, + Future Function() operation, + }) async { + await binding.setSurfaceSize( + Size(device.size.width, overriddenHeight ?? device.size.height)); + binding.window.physicalSizeTestValue = device.size; + binding.window.devicePixelRatioTestValue = device.devicePixelRatio; + binding.window.textScaleFactorTestValue = device.textScale; + binding.window.paddingTestValue = _FakeWindowPadding( bottom: device.safeArea.bottom, left: device.safeArea.left, right: device.safeArea.right, top: device.safeArea.top, ); - tester.binding.window.platformBrightnessTestValue = device.brightness; - await deviceSetup(device, tester); - await screenMatchesGolden( - tester, - '$goldenFileName.${device.name}', - customPump: customPump, - skip: skip, - finder: finder, - ); + binding.window.platformBrightnessTestValue = device.brightness; + + await operation(); + + binding.window.clearDevicePixelRatioTestValue(); + binding.window.clearPlatformBrightnessTestValue(); + binding.window.clearPaddingTestValue(); + binding.window.clearTextScaleFactorTestValue(); + binding.window.clearPhysicalSizeTestValue(); + await binding.setSurfaceSize(null); } } diff --git a/packages/golden_toolkit/test/goldens/empty.anything.png b/packages/golden_toolkit/test/goldens/empty.anything.png new file mode 100644 index 0000000000000000000000000000000000000000..9dc4fe913e8de784ebbb16416d12ee6f6fe64b43 GIT binary patch literal 456 zcmeAS@N?(olHy`uVBq!ia0vp^DL_1pgAGV(?9g=sQjEnx?oJHr&dIz4a#)I;JVQ8u zpoSx*11MMI>EaktG3V{Yjl2ws0tYrc|33eR)QXu6BHvghZc0n5Px-T+d55G>vPX}i hVcQXjQ3l0 main() async { skip: !Platform.isMacOS, ); }); + + testGoldens('Should restore window binding settings', (tester) async { + final size = tester.binding.createViewConfiguration().size; + final initialSize = tester.binding.window.physicalSize; + final initialBrightness = tester.binding.window.platformBrightness; + final initialDevicePixelRatio = tester.binding.window.devicePixelRatio; + final initialTextScaleFactor = tester.binding.window.textScaleFactor; + final initialViewInsets = tester.binding.window.padding; + + await tester.pumpWidgetBuilder(Container()); + await multiScreenGolden(tester, 'empty', devices: [ + const Device( + name: 'anything', + size: Size(50, 75), + brightness: Brightness.light, + safeArea: EdgeInsets.all(4), + devicePixelRatio: 2.0, + textScale: 1.5, + ) + ]); + + expect(tester.binding.createViewConfiguration().size, equals(size)); + expect(tester.binding.window.physicalSize, equals(initialSize)); + expect(tester.binding.window.platformBrightness, + equals(initialBrightness)); + expect(tester.binding.window.devicePixelRatio, + equals(initialDevicePixelRatio)); + expect(tester.binding.window.textScaleFactor, + equals(initialTextScaleFactor)); + expect(tester.binding.window.padding, equals(initialViewInsets)); + }); }); }); } From 9c4a8eea5019ff88da9c1a80597648ac09fb85a0 Mon Sep 17 00:00:00 2001 From: Corey Sprague Date: Mon, 13 Apr 2020 16:11:47 -0700 Subject: [PATCH 2/6] add a default device for iphone11, and a convenience for converting to dark mode --- packages/golden_toolkit/lib/src/device.dart | 20 +++++++++++++++++++ packages/golden_toolkit/test/device_test.dart | 10 ++++++++++ 2 files changed, 30 insertions(+) diff --git a/packages/golden_toolkit/lib/src/device.dart b/packages/golden_toolkit/lib/src/device.dart index 826482f..e87fa6a 100644 --- a/packages/golden_toolkit/lib/src/device.dart +++ b/packages/golden_toolkit/lib/src/device.dart @@ -25,6 +25,14 @@ class Device { /// [phone] one of the smallest phone screens static const Device phone = Device(name: 'phone', size: Size(375, 667)); + /// [iphone11] matches specs of iphone11, but with lower DPI for performance + static const Device iphone11 = Device( + name: 'iphone11', + size: Size(414, 896), + devicePixelRatio: 1.0, + safeArea: EdgeInsets.only(top: 44, bottom: 34), + ); + /// [tabletLandscape] example of tablet that in landscape mode static const Device tabletLandscape = Device(name: 'tablet_landscape', size: Size(1366, 1024)); @@ -70,4 +78,16 @@ class Device { safeArea: safeArea ?? this.safeArea, ); } + + /// [dark] convenience method to copy the current device and apply dark theme + Device dark() { + return Device( + size: size, + devicePixelRatio: devicePixelRatio, + textScale: textScale, + brightness: Brightness.dark, + safeArea: safeArea, + name: '$name\_dark', + ); + } } diff --git a/packages/golden_toolkit/test/device_test.dart b/packages/golden_toolkit/test/device_test.dart index a2836c8..9bd4fb7 100644 --- a/packages/golden_toolkit/test/device_test.dart +++ b/packages/golden_toolkit/test/device_test.dart @@ -41,5 +41,15 @@ void main() { expect(copied.brightness, equals(Brightness.dark)); expect(copied.safeArea, equals(const EdgeInsets.symmetric(vertical: 16))); }); + + test('dark() helper', () { + final dark = Device.iphone11.dark(); + expect(dark.devicePixelRatio, equals(Device.iphone11.devicePixelRatio)); + expect(dark.name, equals('iphone11_dark')); + expect(dark.size, equals(Device.iphone11.size)); + expect(dark.textScale, equals(Device.iphone11.textScale)); + expect(dark.brightness, equals(Brightness.dark)); + expect(dark.safeArea, equals(Device.iphone11.safeArea)); + }); }); } From 02bc1a2954005489d0ab8879bdc18b76637a2db8 Mon Sep 17 00:00:00 2001 From: Corey Sprague Date: Mon, 13 Apr 2020 16:14:08 -0700 Subject: [PATCH 3/6] nuke unnecessary pump --- packages/golden_toolkit/lib/src/multi_screen_golden.dart | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/golden_toolkit/lib/src/multi_screen_golden.dart b/packages/golden_toolkit/lib/src/multi_screen_golden.dart index 7b34807..0727ee7 100644 --- a/packages/golden_toolkit/lib/src/multi_screen_golden.dart +++ b/packages/golden_toolkit/lib/src/multi_screen_golden.dart @@ -69,7 +69,6 @@ Future multiScreenGolden( ); }, ); - await tester.pump(); } } From 62700ea9711fba72a891e74a3c844011de8a1628 Mon Sep 17 00:00:00 2001 From: Corey Sprague Date: Mon, 13 Apr 2020 16:18:55 -0700 Subject: [PATCH 4/6] update changelog --- packages/golden_toolkit/CHANGELOG.md | 4 ++++ packages/golden_toolkit/pubspec.yaml | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/golden_toolkit/CHANGELOG.md b/packages/golden_toolkit/CHANGELOG.md index a81c600..f84cb55 100644 --- a/packages/golden_toolkit/CHANGELOG.md +++ b/packages/golden_toolkit/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 0.3.1 + +Resolve an issue where configuration performed on WidgetTester during multiScreenGolden could bleed over to other tests in the same file + ## 0.3.0 Add support for configuring safe area (to simulate a device notch) and platform brightness (light/dark mode) on a multiScreenGolden device. diff --git a/packages/golden_toolkit/pubspec.yaml b/packages/golden_toolkit/pubspec.yaml index f67c603..e3a1589 100644 --- a/packages/golden_toolkit/pubspec.yaml +++ b/packages/golden_toolkit/pubspec.yaml @@ -1,6 +1,6 @@ name: golden_toolkit description: Common patterns for screenshot-based widget testing using Goldens. -version: 0.3.0 +version: 0.3.1 homepage: https://github.com/eBay/flutter_glove_box/ repository: https://github.com/eBay/flutter_glove_box/tree/master/packages/golden_toolkit issue_tracker: https://github.com/eBay/flutter_glove_box/issues From 9f5b5cb5f17e750b600afccc64367b14049d8aa9 Mon Sep 17 00:00:00 2001 From: Corey Sprague Date: Mon, 13 Apr 2020 16:21:16 -0700 Subject: [PATCH 5/6] clarify changelog --- packages/golden_toolkit/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/golden_toolkit/CHANGELOG.md b/packages/golden_toolkit/CHANGELOG.md index f84cb55..7595a94 100644 --- a/packages/golden_toolkit/CHANGELOG.md +++ b/packages/golden_toolkit/CHANGELOG.md @@ -2,7 +2,7 @@ ## 0.3.1 -Resolve an issue where configuration performed on WidgetTester during multiScreenGolden could bleed over to other tests in the same file +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. ## 0.3.0 From f9516f57cf9522e73d0a116e54a7d7ca56879f06 Mon Sep 17 00:00:00 2001 From: Corey Sprague Date: Mon, 13 Apr 2020 16:22:11 -0700 Subject: [PATCH 6/6] skip golden on linux :( --- .../test/multi_screen_golden_test.dart | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/packages/golden_toolkit/test/multi_screen_golden_test.dart b/packages/golden_toolkit/test/multi_screen_golden_test.dart index 35c423b..96849f8 100644 --- a/packages/golden_toolkit/test/multi_screen_golden_test.dart +++ b/packages/golden_toolkit/test/multi_screen_golden_test.dart @@ -118,16 +118,21 @@ Future main() async { final initialViewInsets = tester.binding.window.padding; await tester.pumpWidgetBuilder(Container()); - await multiScreenGolden(tester, 'empty', devices: [ - const Device( - name: 'anything', - size: Size(50, 75), - brightness: Brightness.light, - safeArea: EdgeInsets.all(4), - devicePixelRatio: 2.0, - textScale: 1.5, - ) - ]); + await multiScreenGolden( + tester, + 'empty', + devices: [ + const Device( + name: 'anything', + size: Size(50, 75), + brightness: Brightness.light, + safeArea: EdgeInsets.all(4), + devicePixelRatio: 2.0, + textScale: 1.5, + ) + ], + skip: !Platform.isMacOS, + ); expect(tester.binding.createViewConfiguration().size, equals(size)); expect(tester.binding.window.physicalSize, equals(initialSize));