|
| 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