Skip to content

Commit

Permalink
Add test for draggable.0.dart API example. (#147941)
Browse files Browse the repository at this point in the history
This PR contributes to #130459

### Description
- Adds test for `examples/api/lib/widgets/drag_target/draggable.0.dart`
  • Loading branch information
ksokolovskyi committed May 10, 2024
1 parent 3223f1c commit da9712f
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 1 deletion.
1 change: 0 additions & 1 deletion dev/bots/check_code_samples.dart
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,6 @@ final Set<String> _knownMissingTests = <String>{
'examples/api/test/widgets/inherited_theme/inherited_theme.0_test.dart',
'examples/api/test/widgets/sliver/decorated_sliver.0_test.dart',
'examples/api/test/widgets/autofill/autofill_group.0_test.dart',
'examples/api/test/widgets/drag_target/draggable.0_test.dart',
'examples/api/test/widgets/shared_app_data/shared_app_data.1_test.dart',
'examples/api/test/widgets/shared_app_data/shared_app_data.0_test.dart',
'examples/api/test/widgets/nested_scroll_view/nested_scroll_view_state.0_test.dart',
Expand Down
122 changes: 122 additions & 0 deletions examples/api/test/widgets/drag_target/draggable.0_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/material.dart';
import 'package:flutter_api_samples/widgets/drag_target/draggable.0.dart'
as example;
import 'package:flutter_test/flutter_test.dart';

void main() {
Finder findContainerWith({
required Finder child,
required Color color,
}) {
return find.ancestor(
of: child,
matching: find.byWidgetPredicate(
(Widget widget) => widget is Container && widget.color == color,
),
);
}

testWidgets('Verify initial state', (WidgetTester tester) async {
await tester.pumpWidget(
const example.DraggableExampleApp(),
);

expect(find.text('Draggable Sample'), findsOneWidget);

expect(
findContainerWith(
color: Colors.lightGreenAccent,
child: find.text('Draggable'),
),
findsOneWidget,
);

expect(
findContainerWith(
color: Colors.cyan,
child: find.text('Value is updated to: 0'),
),
findsOneWidget,
);
});

testWidgets('Verify correct containers are displayed while dragging', (WidgetTester tester) async {
await tester.pumpWidget(
const example.DraggableExampleApp(),
);

final Finder idleContainer = findContainerWith(
color: Colors.lightGreenAccent,
child: find.text('Draggable'),
);
final Finder draggingContainer = findContainerWith(
color: Colors.pinkAccent,
child: find.text('Child When Dragging'),
);
final Finder feedbackContainer = findContainerWith(
color: Colors.deepOrange,
child: find.byIcon(Icons.directions_run),
);

expect(idleContainer, findsOneWidget);
expect(draggingContainer, findsNothing);
expect(feedbackContainer, findsNothing);

final TestGesture gesture = await tester.startGesture(
tester.getCenter(idleContainer),
);
await tester.pump();

expect(idleContainer, findsNothing);
expect(draggingContainer, findsOneWidget);
expect(feedbackContainer, findsOneWidget);

await gesture.moveBy(const Offset(200, 0));
await tester.pump();

expect(idleContainer, findsNothing);
expect(draggingContainer, findsOneWidget);
expect(feedbackContainer, findsOneWidget);

await gesture.up();
await tester.pump();

expect(idleContainer, findsOneWidget);
expect(draggingContainer, findsNothing);
expect(feedbackContainer, findsNothing);
});

testWidgets('Dropping Draggable over DragTarget updates the counter', (WidgetTester tester) async {
await tester.pumpWidget(
const example.DraggableExampleApp(),
);

final Finder draggable = find.byType(Draggable<int>);
final Finder target = find.byType(DragTarget<int>);

int counter = 0;

for (int i = 0; i < 5; i++) {
final TestGesture gesture = await tester.startGesture(
tester.getCenter(draggable),
);
await gesture.moveTo(tester.getCenter(target));
await gesture.up();
await tester.pump();

counter += 10;

expect(
findContainerWith(
color: Colors.cyan,
child: find.text('Value is updated to: $counter'),
),
findsOneWidget,
);
}
});
}

0 comments on commit da9712f

Please sign in to comment.