uiTest function

  1. @isTest
void uiTest(
  1. String description, {
  2. required WidgetTesterCallback verify,
  3. ProvidersContext? context,
  4. UI<ViewModel> builder()?,
  5. AppRouter<Enum>? router,
  6. FutureOr<void> setup()?,
  7. FutureOr<void> postFrame(
    1. WidgetTester
    )?,
  8. bool wrapWithMaterialApp = true,
  9. Duration? pumpDuration,
  10. bool? skip,
  11. Timeout? timeout,
  12. bool semanticsEnabled = true,
  13. TestVariant<Object?> variant = const DefaultTestVariant(),
  14. dynamic tags,
  15. Size? screenSize,
  16. Iterable<LocalizationsDelegate>? localizationDelegates,
  17. Widget parentBuilder(
    1. Widget
    )?,
})

Implementation

@isTest
void uiTest(
  String description, {
  required WidgetTesterCallback verify,
  ProvidersContext? context,
  UI Function()? builder,
  AppRouter? router,
  FutureOr<void> Function()? setup,
  FutureOr<void> Function(WidgetTester)? postFrame,
  bool wrapWithMaterialApp = true,
  Duration? pumpDuration,
  bool? skip,
  Timeout? timeout,
  bool semanticsEnabled = true,
  TestVariant<Object?> variant = const DefaultTestVariant(),
  dynamic tags,
  Size? screenSize,
  Iterable<LocalizationsDelegate<dynamic>>? localizationDelegates,
  Widget Function(Widget)? parentBuilder,
}) {
  assert(
    () {
      return localizationDelegates == null || wrapWithMaterialApp;
    }(),
    'Need to wrap with MaterialApp '
    'if overriding localization delegates is required',
  );

  final resolvedRouter = router ?? _uiTestConfig?.router;
  final resolvedContext = context ?? _uiTestConfig?.context;

  assert(
    () {
      return builder != null || resolvedRouter != null;
    }(),
    'Provide either "builder" or "router".',
  );
  assert(
    () {
      return resolvedRouter == null || wrapWithMaterialApp;
    }(),
    '"router" should not be passed when wrapWithMaterialApp is false',
  );
  assert(
    () {
      return resolvedContext != null;
    }(),
    'Either pass "context" or call "setupUITest()" before test block.',
  );

  testWidgets(
    description,
    (tester) async {
      final view = tester.binding.platformDispatcher.implicitView;
      if (view != null && screenSize != null) {
        view.physicalSize = screenSize * view.devicePixelRatio;
      }

      await setup?.call();

      Widget scopedChild(Widget child) {
        return uiTestWidgetBuilder(
          UncontrolledProviderScope(
            container: resolvedContext!(),
            child: child,
          ),
        );
      }

      Widget child;
      if (wrapWithMaterialApp) {
        if (builder == null) {
          child = AppRouterScope(
            create: () => resolvedRouter!,
            builder: (context) {
              return MaterialApp.router(
                routerConfig: context.router.config,
                localizationsDelegates: localizationDelegates,
                builder: (context, child) => scopedChild(child!),
              );
            },
          );
        } else {
          child = MaterialApp(
            home: scopedChild(builder()),
            localizationsDelegates: localizationDelegates,
          );
        }
      } else {
        child = scopedChild(builder!());
      }

      await tester.pumpWidget(
        parentBuilder == null ? child : parentBuilder(child),
        pumpDuration,
      );

      if (postFrame == null) {
        await tester.pumpAndSettle();
      } else {
        await postFrame(tester);
      }

      await verify(tester);

      if (view != null && screenSize != null) view.resetPhysicalSize();
    },
    skip: skip,
    timeout: timeout,
    semanticsEnabled: semanticsEnabled,
    variant: variant,
    tags: tags,
  );
}