Skip to content

Commit da9712f

Browse files
authored
Add test for draggable.0.dart API example. (#147941)
This PR contributes to flutter/flutter#130459 ### Description - Adds test for `examples/api/lib/widgets/drag_target/draggable.0.dart`
1 parent 3223f1c commit da9712f

File tree

2 files changed

+122
-1
lines changed

2 files changed

+122
-1
lines changed

dev/bots/check_code_samples.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,6 @@ final Set<String> _knownMissingTests = <String>{
396396
'examples/api/test/widgets/inherited_theme/inherited_theme.0_test.dart',
397397
'examples/api/test/widgets/sliver/decorated_sliver.0_test.dart',
398398
'examples/api/test/widgets/autofill/autofill_group.0_test.dart',
399-
'examples/api/test/widgets/drag_target/draggable.0_test.dart',
400399
'examples/api/test/widgets/shared_app_data/shared_app_data.1_test.dart',
401400
'examples/api/test/widgets/shared_app_data/shared_app_data.0_test.dart',
402401
'examples/api/test/widgets/nested_scroll_view/nested_scroll_view_state.0_test.dart',
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter/material.dart';
6+
import 'package:flutter_api_samples/widgets/drag_target/draggable.0.dart'
7+
as example;
8+
import 'package:flutter_test/flutter_test.dart';
9+
10+
void main() {
11+
Finder findContainerWith({
12+
required Finder child,
13+
required Color color,
14+
}) {
15+
return find.ancestor(
16+
of: child,
17+
matching: find.byWidgetPredicate(
18+
(Widget widget) => widget is Container && widget.color == color,
19+
),
20+
);
21+
}
22+
23+
testWidgets('Verify initial state', (WidgetTester tester) async {
24+
await tester.pumpWidget(
25+
const example.DraggableExampleApp(),
26+
);
27+
28+
expect(find.text('Draggable Sample'), findsOneWidget);
29+
30+
expect(
31+
findContainerWith(
32+
color: Colors.lightGreenAccent,
33+
child: find.text('Draggable'),
34+
),
35+
findsOneWidget,
36+
);
37+
38+
expect(
39+
findContainerWith(
40+
color: Colors.cyan,
41+
child: find.text('Value is updated to: 0'),
42+
),
43+
findsOneWidget,
44+
);
45+
});
46+
47+
testWidgets('Verify correct containers are displayed while dragging', (WidgetTester tester) async {
48+
await tester.pumpWidget(
49+
const example.DraggableExampleApp(),
50+
);
51+
52+
final Finder idleContainer = findContainerWith(
53+
color: Colors.lightGreenAccent,
54+
child: find.text('Draggable'),
55+
);
56+
final Finder draggingContainer = findContainerWith(
57+
color: Colors.pinkAccent,
58+
child: find.text('Child When Dragging'),
59+
);
60+
final Finder feedbackContainer = findContainerWith(
61+
color: Colors.deepOrange,
62+
child: find.byIcon(Icons.directions_run),
63+
);
64+
65+
expect(idleContainer, findsOneWidget);
66+
expect(draggingContainer, findsNothing);
67+
expect(feedbackContainer, findsNothing);
68+
69+
final TestGesture gesture = await tester.startGesture(
70+
tester.getCenter(idleContainer),
71+
);
72+
await tester.pump();
73+
74+
expect(idleContainer, findsNothing);
75+
expect(draggingContainer, findsOneWidget);
76+
expect(feedbackContainer, findsOneWidget);
77+
78+
await gesture.moveBy(const Offset(200, 0));
79+
await tester.pump();
80+
81+
expect(idleContainer, findsNothing);
82+
expect(draggingContainer, findsOneWidget);
83+
expect(feedbackContainer, findsOneWidget);
84+
85+
await gesture.up();
86+
await tester.pump();
87+
88+
expect(idleContainer, findsOneWidget);
89+
expect(draggingContainer, findsNothing);
90+
expect(feedbackContainer, findsNothing);
91+
});
92+
93+
testWidgets('Dropping Draggable over DragTarget updates the counter', (WidgetTester tester) async {
94+
await tester.pumpWidget(
95+
const example.DraggableExampleApp(),
96+
);
97+
98+
final Finder draggable = find.byType(Draggable<int>);
99+
final Finder target = find.byType(DragTarget<int>);
100+
101+
int counter = 0;
102+
103+
for (int i = 0; i < 5; i++) {
104+
final TestGesture gesture = await tester.startGesture(
105+
tester.getCenter(draggable),
106+
);
107+
await gesture.moveTo(tester.getCenter(target));
108+
await gesture.up();
109+
await tester.pump();
110+
111+
counter += 10;
112+
113+
expect(
114+
findContainerWith(
115+
color: Colors.cyan,
116+
child: find.text('Value is updated to: $counter'),
117+
),
118+
findsOneWidget,
119+
);
120+
}
121+
});
122+
}

0 commit comments

Comments
 (0)