import 'dart:math' as math; import 'package:cutdex_native/cutdex_native.dart'; import 'package:flutter/widgets.dart'; import 'package:flutter_test/flutter_test.dart'; void main() { testWidgets('moving a cached layer updates native bounds', (tester) async { final offset = ValueNotifier(Offset.zero); addTearDown(offset.dispose); final reports = []; final content = RepaintBoundary( child: NativeBoundsReporter( onBounds: reports.add, child: const SizedBox(width: 100, height: 40), ), ); await tester.pumpWidget( Directionality( textDirection: TextDirection.ltr, child: Align( alignment: Alignment.topLeft, child: ValueListenableBuilder( valueListenable: offset, child: content, builder: (_, position, child) => Transform.translate(offset: position, child: child), ), ), ), ); expect(reports.last, const Rect.fromLTWH(0, 0, 100, 40)); offset.value = const Offset(50, 25); await tester.pump(); expect(reports.last, const Rect.fromLTWH(50, 25, 100, 40)); final count = reports.length; await tester.pump(); expect(reports, hasLength(count)); expect(tester.binding.hasScheduledFrame, isFalse); await tester.pumpWidget(const SizedBox()); await tester.pump(); expect(reports, hasLength(count)); }); for (final rotated in [false, true]) { testWidgets('native bounds include ${rotated ? 'rotation' : 'scaling'}', ( tester, ) async { Rect? bounds; final reporter = NativeBoundsReporter( onBounds: (rect) => bounds = rect, child: const SizedBox(width: 100, height: 40), ); await tester.pumpWidget( Directionality( textDirection: TextDirection.ltr, child: Align( alignment: Alignment.topLeft, child: Padding( padding: const EdgeInsets.only(left: 120, top: 100), child: rotated ? Transform.rotate( angle: math.pi / 2, alignment: Alignment.topLeft, child: reporter, ) : Transform.scale( scale: 2, alignment: Alignment.topLeft, child: reporter, ), ), ), ), ); expect(bounds, isNotNull); final expected = rotated ? const Rect.fromLTWH(80, 100, 40, 100) : const Rect.fromLTWH(120, 100, 200, 80); expect(bounds!.left, closeTo(expected.left, 1e-8)); expect(bounds!.top, closeTo(expected.top, 1e-8)); expect(bounds!.width, closeTo(expected.width, 1e-8)); expect(bounds!.height, closeTo(expected.height, 1e-8)); }); } }