screenTestGoldens function

void screenTestGoldens(
  1. String title, {
  2. required Widget widget,
  3. Future<void> initScreen(
    1. WidgetTester
    )?,
  4. Future<void> afterPumpAction(
    1. WidgetTester
    )?,
  5. int? waitDurationForAnimationInMs,
  6. bool onlyDark = false,
  7. bool onlyLight = false,
  8. List<Device> additionalDevices = const [],
  9. List<Device>? overrideDevices,
  10. Iterable<LocalizationsDelegate>? localizations,
})

Full-screen golden test of a widget on multiple Devices.

Generates one golden per device named <title>_<mode>.<device> for light mode (smallPhone, smallPhoneWithZoom2) and dark mode (Device.iphone11). Use onlyLight / onlyDark to restrict to a single mode, overrideDevices or additionalDevices to customize the simulated devices, and waitDurationForAnimationInMs to let animations complete before capture.

Implementation

void screenTestGoldens(
  String title, {
  required Widget widget,
  Future<void> Function(WidgetTester)? initScreen,
  Future<void> Function(WidgetTester)? afterPumpAction,
  int? waitDurationForAnimationInMs,
  bool onlyDark = false,
  bool onlyLight = false,
  List<Device> additionalDevices = const [],
  List<Device>? overrideDevices,
  Iterable<LocalizationsDelegate<dynamic>>? localizations,
}) {
  assert(
    !onlyDark || !onlyLight,
    'Only one of onlyDark or onlyLight can be true',
  );

  void executeTest(ThemeMode mode, List<Device> devices) => _runTestGoldens(
    title: title,
    widget: widget,
    mode: mode,
    afterPumpAction: afterPumpAction,
    localizations: localizations,
    test: (tester) async {
      await initScreen?.call(tester);
      await multiScreenGolden(
        tester,
        '${title}_${mode.name}',
        autoHeight: true,
        devices: devices,
        customPump: waitDurationForAnimationInMs != null
            ? (tester) async => tester.pump(
                // We need to provide a duration to make the fake timer
                // advance, otherwise it remains on the 0 time slot and
                // raises an exception.
                Duration(milliseconds: waitDurationForAnimationInMs),
              )
            : null,
      );
    },
  );

  if (onlyDark || onlyLight) {
    executeTest(onlyDark ? ThemeMode.dark : ThemeMode.light, [
      ...(overrideDevices ??
          [Device.iphone11, smallPhone, smallPhoneWithZoom2]),
      ...additionalDevices,
    ]);
  } else {
    executeTest(ThemeMode.light, [
      ...(overrideDevices ?? [smallPhone, smallPhoneWithZoom2]),
      ...additionalDevices,
    ]);
    executeTest(ThemeMode.dark, [Device.iphone11]);
  }
}