uiTest function
void
uiTest(
- String description, {
- required WidgetTesterCallback verify,
- ProvidersContext? context,
- UI<ViewModel> builder()?,
- AppRouter<Enum>? router,
- FutureOr<void> setup()?,
- FutureOr<void> postFrame(
- WidgetTester
)?,
- bool wrapWithMaterialApp = true,
- Duration? pumpDuration,
- bool? skip,
- Timeout? timeout,
- bool semanticsEnabled = true,
- TestVariant<Object?> variant = const DefaultTestVariant(),
- dynamic tags,
- Size? screenSize,
- Iterable<LocalizationsDelegate>? localizationDelegates,
- Widget parentBuilder(
- 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,
);
}