testAdaptiveWidget function

void testAdaptiveWidget(
  1. String description,
  2. Future<void> body(
    1. WidgetTester tester,
    2. TargetPlatform platform
    ), {
  3. List<TargetPlatform> platforms = kAdaptivePlatforms,
  4. bool overrideFrameworkPlatform = true,
})

Runs body once per entry in platforms, with the platform forced.

Ensures both Cupertino and Material branches are exercised in unit and widget tests. The forced platform is automatically reset in a finally block to prevent test contamination.

Implementation

void testAdaptiveWidget(
  String description,
  Future<void> Function(WidgetTester tester, TargetPlatform platform) body, {
  List<TargetPlatform> platforms = kAdaptivePlatforms,
  bool overrideFrameworkPlatform = true,
}) {
  for (final TargetPlatform platform in platforms) {
    testWidgets('$description · ${platform.name}', (WidgetTester tester) async {
      PlatformUtils.debugOverridePlatform = platform;
      if (overrideFrameworkPlatform) {
        debugDefaultTargetPlatformOverride = platform;
      }
      try {
        await body(tester, platform);
      } finally {
        PlatformUtils.debugOverridePlatform = null;
        debugDefaultTargetPlatformOverride = null;
      }
    });
  }
}