Skip to content

Commit

Permalink
Merge pull request #34 from JordyHers/dev
Browse files Browse the repository at this point in the history
dev
  • Loading branch information
JordyHers authored Jun 10, 2024
2 parents ab8d784 + 3cb5a0f commit c05c11e
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 78 deletions.
189 changes: 111 additions & 78 deletions test/src/flutter_floaty_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -37,51 +21,25 @@ 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();
expect(tapped, isTrue);
});

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));
Expand All @@ -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);

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'));

// 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);
});
}
25 changes: 25 additions & 0 deletions test/src/generator_app_floaty.dart
Original file line number Diff line number Diff line change
@@ -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<void> 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'),
),
],
),
),
),
);
}

0 comments on commit c05c11e

Please sign in to comment.