From f6095ba9daf321629df18434f092e979969235ed Mon Sep 17 00:00:00 2001 From: jordyhers Date: Mon, 10 Jun 2024 13:20:09 +0200 Subject: [PATCH 1/2] chore: clampX axis limit --- test/src/flutter_floaty_test.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/src/flutter_floaty_test.dart b/test/src/flutter_floaty_test.dart index e341597..6199921 100644 --- a/test/src/flutter_floaty_test.dart +++ b/test/src/flutter_floaty_test.dart @@ -114,7 +114,7 @@ void main() { ), ); - final initialPosition = tester.getTopLeft(find.text('Floaty')); + tester.getTopLeft(find.text('Floaty')); await tester.drag(find.text('Floaty'), const Offset(300, 300)); await tester.pumpAndSettle(); final newPosition = tester.getTopLeft(find.text('Floaty')); From 3cb5a0f02c9f2e060f6e336dbd355ac27896c3c3 Mon Sep 17 00:00:00 2001 From: jordyhers Date: Mon, 10 Jun 2024 13:51:42 +0200 Subject: [PATCH 2/2] chore: fix widget tests --- test/src/flutter_floaty_test.dart | 187 +++++++++++++++++------------ test/src/generator_app_floaty.dart | 25 ++++ 2 files changed, 135 insertions(+), 77 deletions(-) create mode 100644 test/src/generator_app_floaty.dart diff --git a/test/src/flutter_floaty_test.dart b/test/src/flutter_floaty_test.dart index 6199921..95ef844 100644 --- a/test/src/flutter_floaty_test.dart +++ b/test/src/flutter_floaty_test.dart @@ -2,33 +2,17 @@ import 'package:flutter/material.dart'; import 'package:flutter_floaty/flutter_floaty.dart'; import 'package:flutter_test/flutter_test.dart'; +import 'generator_app_floaty.dart'; + void main() { TestWidgetsFlutterBinding.ensureInitialized(); - testWidgets('FlutterFloaty is created successfully', - (WidgetTester tester) async { - await tester.pumpWidget( - MaterialApp( - home: Scaffold( - body: Stack( - children: [ - FlutterFloaty( - initialX: 50, - initialY: 50, - width: 200, - height: 200, - builder: (context) => const Text('Floaty'), - ), - ], - ), - ), - ), - ); + testWidgets('FlutterFloaty is created successfully', ( + WidgetTester tester, + ) async { + await Generator.createApp(tester); - // Check if the widget is present expect(find.text('Floaty'), findsOneWidget); - - // Check if the widget has the correct initial size final floaty = tester.firstWidget(find.byType(Container)) as Container; expect(floaty.constraints!.maxWidth, 200); expect(floaty.constraints!.maxHeight, 200); @@ -37,27 +21,17 @@ void main() { testWidgets('FlutterFloaty responds to tap gestures', (WidgetTester tester) async { var tapped = false; - - await tester.pumpWidget( - MaterialApp( - home: Scaffold( - body: Stack( - children: [ - FlutterFloaty( - initialX: 50, - initialY: 50, - width: 200, - height: 200, - builder: (context) => const Text('Floaty'), - onTap: () { - tapped = true; - }, - ), - ], - ), - ), - ), + final child = FlutterFloaty( + initialX: 50, + initialY: 50, + width: 200, + height: 200, + builder: (context) => const Text('Floaty'), + onTap: () { + tapped = true; + }, ); + await Generator.createApp(tester, child: child); await tester.tap(find.text('Floaty')); await tester.pump(); @@ -65,23 +39,7 @@ void main() { }); testWidgets('FlutterFloaty can be dragged', (WidgetTester tester) async { - await tester.pumpWidget( - MaterialApp( - home: Scaffold( - body: Stack( - children: [ - FlutterFloaty( - initialX: 50, - initialY: 50, - width: 200, - height: 200, - builder: (context) => const Text('Floaty'), - ), - ], - ), - ), - ), - ); + await Generator.createApp(tester); final initialPosition = tester.getTopLeft(find.text('Floaty')); await tester.drag(find.text('Floaty'), const Offset(200, 200)); @@ -95,32 +53,107 @@ void main() { testWidgets('FlutterFloaty respects intrinsic boundaries', (WidgetTester tester) async { - await tester.pumpWidget( - MaterialApp( - home: Scaffold( - body: Stack( - children: [ - FlutterFloaty( - initialX: 50, - initialY: 50, - width: 200, - height: 200, - intrinsicBoundaries: const Rect.fromLTWH(0, 0, 300, 300), - builder: (context) => const Text('Floaty'), - ), - ], - ), - ), - ), + final child = FlutterFloaty( + initialX: 50, + initialY: 50, + width: 200, + height: 200, + intrinsicBoundaries: const Rect.fromLTWH(0, 0, 300, 300), + builder: (context) => const Text('Floaty'), ); + await Generator.createApp(tester, child: child); tester.getTopLeft(find.text('Floaty')); await tester.drag(find.text('Floaty'), const Offset(300, 300)); await tester.pumpAndSettle(); final newPosition = tester.getTopLeft(find.text('Floaty')); - // Ensure the widget doesn't go out of the boundaries (300, 300 is the max) expect(newPosition.dx, lessThanOrEqualTo(200)); // 300 - 200 (width) expect(newPosition.dy, lessThanOrEqualTo(200)); // 300 - 200 (height) }); + + testWidgets('FlutterFloaty returns to initial position when pinned', + (WidgetTester tester) async { + final child = FlutterFloaty( + initialX: 50, + initialY: 50, + width: 200, + height: 200, + pinned: true, + builder: (context) => const Text('Floaty'), + ); + await Generator.createApp(tester, child: child); + + await tester.drag(find.text('Floaty'), const Offset(100, 100)); + await tester.pumpAndSettle(); + + final movedPosition = tester.getTopLeft(find.text('Floaty')); + expect(movedPosition, isNot(const Offset(50, 50))); + + await tester.tapAt(const Offset(1, 1)); // Tap outside to end dragging + await tester.pumpAndSettle(); + + final resetPosition = tester.getTopLeft(find.text('Floaty')); + expect(resetPosition.dx, 127.25); + expect(resetPosition.dy, 160); + }); + + testWidgets('FlutterFloaty respects growing factor constraints', + (WidgetTester tester) async { + final child = FlutterFloaty( + initialX: 50, + initialY: 50, + growingFactor: 2, + builder: (context) => const Text('Floaty'), + ); + await Generator.createApp(tester, child: child); + + await tester.drag(find.text('Floaty'), const Offset(1, 1)); + final floaty = tester.firstWidget(find.byType(Container)) as Container; + await tester.pumpAndSettle(); + + expect(floaty.constraints!.maxWidth, 100); + expect(floaty.constraints!.maxHeight, 100); + }); + + testWidgets('FlutterFloaty is not visible when isVisible is false', + (WidgetTester tester) async { + final child = FlutterFloaty( + initialX: 50, + initialY: 50, + width: 200, + height: 200, + isVisible: false, + builder: (context) => const Text('Floaty'), + ); + await Generator.createApp(tester, child: child); + + final offstageWidget = find.byType(Offstage).last; + expect(offstageWidget, findsOneWidget); + expect((tester.firstWidget(offstageWidget) as Offstage).offstage, isTrue); + }); + + testWidgets('FlutterFloaty handles animations correctly', + (WidgetTester tester) async { + final child = FlutterFloaty( + initialX: 50, + initialY: 50, + width: 200, + height: 200, + enableAnimation: false, + builder: (context) => const Text('Floaty'), + ); + await Generator.createApp(tester, child: child); + + final floaty = tester.firstWidget(find.byType(Container)) as Container; + expect(floaty.constraints!.maxWidth, 200); + expect(floaty.constraints!.maxHeight, 200); + + await tester.drag(find.text('Floaty'), const Offset(1, 1)); + await tester.pumpAndSettle(); + + final newSize = tester.firstWidget(find.byType(Container)) as Container; + expect(newSize.constraints!.maxWidth, 200); + expect(newSize.constraints!.maxHeight, 200); + }); } diff --git a/test/src/generator_app_floaty.dart b/test/src/generator_app_floaty.dart new file mode 100644 index 0000000..7cffb17 --- /dev/null +++ b/test/src/generator_app_floaty.dart @@ -0,0 +1,25 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_floaty/flutter_floaty.dart'; +import 'package:flutter_test/flutter_test.dart'; + +class Generator { + static Future createApp(WidgetTester tester, {Widget? child}) async => + tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: Stack( + children: [ + child ?? + FlutterFloaty( + initialX: 50, + initialY: 50, + width: 200, + height: 200, + builder: (context) => const Text('Floaty'), + ), + ], + ), + ), + ), + ); +}