|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:flutter_map/flutter_map.dart'; |
| 3 | +import 'package:flutter_test/flutter_test.dart'; |
| 4 | +import 'package:latlong2/latlong.dart'; |
| 5 | + |
| 6 | +void main() { |
| 7 | + testWidgets('test fit bounds methods', (tester) async { |
| 8 | + final controller = MapController(); |
| 9 | + final bounds = LatLngBounds( |
| 10 | + LatLng(51, 0), |
| 11 | + LatLng(52, 1), |
| 12 | + ); |
| 13 | + final expectedCenter = LatLng(51.50274289405741, 0.49999999999999833); |
| 14 | + |
| 15 | + await tester.pumpWidget(TestApp(controller: controller)); |
| 16 | + |
| 17 | + { |
| 18 | + const fitOptions = FitBoundsOptions(); |
| 19 | + |
| 20 | + final expectedBounds = LatLngBounds( |
| 21 | + LatLng(51.00145915187144, -0.3079873797085076), |
| 22 | + LatLng(52.001427481787005, 1.298485398623206), |
| 23 | + ); |
| 24 | + const expectedZoom = 7.451812751543818; |
| 25 | + |
| 26 | + final fit = controller.centerZoomFitBounds(bounds, options: fitOptions); |
| 27 | + controller.move(fit.center, fit.zoom); |
| 28 | + await tester.pump(); |
| 29 | + expect(controller.bounds, equals(expectedBounds)); |
| 30 | + expect(controller.center, equals(expectedCenter)); |
| 31 | + expect(controller.zoom, equals(expectedZoom)); |
| 32 | + |
| 33 | + controller.fitBounds(bounds, options: fitOptions); |
| 34 | + await tester.pump(); |
| 35 | + expect(controller.bounds, equals(expectedBounds)); |
| 36 | + expect(controller.center, equals(expectedCenter)); |
| 37 | + expect(controller.zoom, equals(expectedZoom)); |
| 38 | + } |
| 39 | + |
| 40 | + { |
| 41 | + const fitOptions = FitBoundsOptions( |
| 42 | + forceIntegerZoomLevel: true, |
| 43 | + ); |
| 44 | + |
| 45 | + final expectedBounds = LatLngBounds( |
| 46 | + LatLng(50.819818262156545, -0.6042480468750001), |
| 47 | + LatLng(52.1874047455997, 1.5930175781250002), |
| 48 | + ); |
| 49 | + const expectedZoom = 7; |
| 50 | + |
| 51 | + final fit = controller.centerZoomFitBounds(bounds, options: fitOptions); |
| 52 | + controller.move(fit.center, fit.zoom); |
| 53 | + await tester.pump(); |
| 54 | + expect(controller.bounds, equals(expectedBounds)); |
| 55 | + expect(controller.center, equals(expectedCenter)); |
| 56 | + expect(controller.zoom, equals(expectedZoom)); |
| 57 | + |
| 58 | + controller.fitBounds(bounds, options: fitOptions); |
| 59 | + await tester.pump(); |
| 60 | + expect(controller.bounds, equals(expectedBounds)); |
| 61 | + expect(controller.center, equals(expectedCenter)); |
| 62 | + expect(controller.zoom, equals(expectedZoom)); |
| 63 | + } |
| 64 | + |
| 65 | + { |
| 66 | + const fitOptions = FitBoundsOptions( |
| 67 | + inside: true, |
| 68 | + ); |
| 69 | + |
| 70 | + final expectedBounds = LatLngBounds( |
| 71 | + LatLng(51.19148727133182, -6.195044477408375e-13), |
| 72 | + LatLng(51.8139520195805, 0.999999999999397), |
| 73 | + ); |
| 74 | + const expectedZoom = 8.135709286104404; |
| 75 | + |
| 76 | + final fit = controller.centerZoomFitBounds(bounds, options: fitOptions); |
| 77 | + controller.move(fit.center, fit.zoom); |
| 78 | + await tester.pump(); |
| 79 | + |
| 80 | + expect(controller.bounds, equals(expectedBounds)); |
| 81 | + expect(controller.center, equals(expectedCenter)); |
| 82 | + expect(controller.zoom, equals(expectedZoom)); |
| 83 | + |
| 84 | + controller.fitBounds(bounds, options: fitOptions); |
| 85 | + await tester.pump(); |
| 86 | + expect(controller.bounds, equals(expectedBounds)); |
| 87 | + expect(controller.center, equals(expectedCenter)); |
| 88 | + expect(controller.zoom, equals(expectedZoom)); |
| 89 | + } |
| 90 | + |
| 91 | + { |
| 92 | + const fitOptions = FitBoundsOptions( |
| 93 | + inside: true, |
| 94 | + forceIntegerZoomLevel: true, |
| 95 | + ); |
| 96 | + |
| 97 | + final expectedBounds = LatLngBounds( |
| 98 | + LatLng(51.33232774035881, 0.22521972656250003), |
| 99 | + LatLng(51.67425842259517, 0.7745361328125), |
| 100 | + ); |
| 101 | + const expectedZoom = 9; |
| 102 | + |
| 103 | + final fit = controller.centerZoomFitBounds(bounds, options: fitOptions); |
| 104 | + controller.move(fit.center, fit.zoom); |
| 105 | + await tester.pump(); |
| 106 | + expect(controller.bounds, equals(expectedBounds)); |
| 107 | + expect(controller.center, equals(expectedCenter)); |
| 108 | + expect(controller.zoom, equals(expectedZoom)); |
| 109 | + |
| 110 | + controller.fitBounds(bounds, options: fitOptions); |
| 111 | + await tester.pump(); |
| 112 | + expect(controller.bounds, equals(expectedBounds)); |
| 113 | + expect(controller.center, equals(expectedCenter)); |
| 114 | + expect(controller.zoom, equals(expectedZoom)); |
| 115 | + } |
| 116 | + }); |
| 117 | +} |
| 118 | + |
| 119 | +class TestApp extends StatelessWidget { |
| 120 | + final MapController controller; |
| 121 | + |
| 122 | + const TestApp({ |
| 123 | + required this.controller, |
| 124 | + Key? key, |
| 125 | + }) : super(key: key); |
| 126 | + |
| 127 | + @override |
| 128 | + Widget build(BuildContext context) { |
| 129 | + return MaterialApp( |
| 130 | + home: Scaffold( |
| 131 | + body: Center( |
| 132 | + // ensure that map is always of the same size |
| 133 | + child: SizedBox( |
| 134 | + width: 200, |
| 135 | + height: 200, |
| 136 | + child: FlutterMap( |
| 137 | + mapController: controller, |
| 138 | + options: MapOptions(), |
| 139 | + ), |
| 140 | + ), |
| 141 | + ), |
| 142 | + ), |
| 143 | + ); |
| 144 | + } |
| 145 | +} |
0 commit comments