|
| 1 | +import 'dart:math' as math; |
| 2 | + |
| 3 | +import 'package:flutter/widgets.dart'; |
| 4 | +import 'package:flutter_map/flutter_map.dart'; |
| 5 | +import 'package:flutter_test/flutter_test.dart'; |
| 6 | +import 'package:latlong2/latlong.dart'; |
| 7 | + |
| 8 | +/// Two fingers around [c], [scale] times their initial 100 px apart, the |
| 9 | +/// line between them rotated by [degrees]. At 0° the line runs along the x |
| 10 | +/// axis with the first finger to the left: `atan2` of the start line is 180°, |
| 11 | +/// so a small rotation crosses ±180°. |
| 12 | +List<Offset> _fingers(Offset c, double scale, double degrees) { |
| 13 | + final a = degrees * math.pi / 180; |
| 14 | + final v = Offset(math.cos(a), math.sin(a)) * 50 * scale; |
| 15 | + return [c - v, c + v]; |
| 16 | +} |
| 17 | + |
| 18 | +void main() { |
| 19 | + testWidgets('a small rotation across ±180° does not win the gesture race', |
| 20 | + (tester) async { |
| 21 | + final controller = MapController(); |
| 22 | + await tester.pumpWidget( |
| 23 | + Directionality( |
| 24 | + textDirection: TextDirection.ltr, |
| 25 | + child: Center( |
| 26 | + child: SizedBox( |
| 27 | + width: 400, |
| 28 | + height: 400, |
| 29 | + child: FlutterMap( |
| 30 | + mapController: controller, |
| 31 | + options: const MapOptions( |
| 32 | + initialCenter: LatLng(46.5, 6.6), |
| 33 | + initialZoom: 10, |
| 34 | + interactionOptions: InteractionOptions( |
| 35 | + enableMultiFingerGestureRace: true, |
| 36 | + ), |
| 37 | + ), |
| 38 | + children: const [], |
| 39 | + ), |
| 40 | + ), |
| 41 | + ), |
| 42 | + ), |
| 43 | + ); |
| 44 | + |
| 45 | + final c = tester.getCenter(find.byType(FlutterMap)); |
| 46 | + final start = _fingers(c, 1, 0); |
| 47 | + final g1 = await tester.startGesture(start[0], pointer: 1); |
| 48 | + final g2 = await tester.startGesture(start[1], pointer: 2); |
| 49 | + await tester.pump(); |
| 50 | + // Spread the fingers too, so the gesture passes the touch slop. |
| 51 | + for (final (scale, degrees) in [(1.2, 2.0), (1.4, 4.0)]) { |
| 52 | + final p = _fingers(c, scale, degrees); |
| 53 | + await g1.moveTo(p[0]); |
| 54 | + await g2.moveTo(p[1]); |
| 55 | + await tester.pump(); |
| 56 | + } |
| 57 | + await g1.up(); |
| 58 | + await g2.up(); |
| 59 | + await tester.pumpAndSettle(); |
| 60 | + // Let the double tap timer expire. |
| 61 | + await tester.pump(const Duration(seconds: 1)); |
| 62 | + |
| 63 | + // 4° is below the default rotationThreshold (20°): rotation must not win |
| 64 | + // the race, which the raw -356° value made it do. |
| 65 | + expect(controller.camera.rotation, 0); |
| 66 | + }); |
| 67 | +} |
0 commit comments