|
| 1 | +using Avalonia.Controls; |
| 2 | +using Avalonia.Headless.XUnit; |
| 3 | +using Avalonia.Threading; |
| 4 | +using Mapsui.Layers; |
| 5 | +using Mapsui.Nts; |
| 6 | +using Mapsui.Styles; |
| 7 | +using MissionPlanner.ArduPilot; |
| 8 | +using MissionPlanner.Controls; |
| 9 | + |
| 10 | +namespace MissionPlanner.Tests; |
| 11 | + |
| 12 | +public class FlightPlannerViewportTests { |
| 13 | + [AvaloniaFact] |
| 14 | + public void Waypoint_redraw_preserves_an_initialized_viewport() { |
| 15 | + var map = new FlightPlannerMap(); |
| 16 | + var window = new Window { Width = 1200, Height = 800, Content = map }; |
| 17 | + try { |
| 18 | + window.Show(); |
| 19 | + Dispatcher.UIThread.RunJobs(); |
| 20 | + |
| 21 | + map.CenterOnAndZoom(50.344, 30.88, 14); |
| 22 | + map.SetHome(37.619373, -122.376637, 5.28); |
| 23 | + map.SetWaypoints(KyivWaypoints(2), 50, 80, Firmwares.ArduPlane); |
| 24 | + Dispatcher.UIThread.RunJobs(); |
| 25 | + Mapsui.Viewport expected = map.Map.Navigator.Viewport; |
| 26 | + |
| 27 | + WritableLayer route = RouteLayer(map); |
| 28 | + route.DataChanged += (_, _) => { |
| 29 | + if (route.GetFeatures().OfType<GeometryFeature>().Count() == 2) { |
| 30 | + Dispatcher.UIThread.Post(() => map.Map.Navigator.ZoomToBox(route.Extent)); |
| 31 | + } |
| 32 | + }; |
| 33 | + |
| 34 | + map.SetWaypoints(KyivWaypoints(3), 50, 80, Firmwares.ArduPlane); |
| 35 | + Dispatcher.UIThread.RunJobs(); |
| 36 | + Mapsui.Viewport actual = map.Map.Navigator.Viewport; |
| 37 | + |
| 38 | + Assert.Equal(expected.CenterX, actual.CenterX, 3); |
| 39 | + Assert.Equal(expected.CenterY, actual.CenterY, 3); |
| 40 | + Assert.Equal(expected.Resolution, actual.Resolution, 6); |
| 41 | + } finally { |
| 42 | + window.Close(); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + [AvaloniaFact] |
| 47 | + public void Distant_home_closing_route_remains_solid() { |
| 48 | + var map = new FlightPlannerMap(); |
| 49 | + map.SetHome(37.619373, -122.376637, 5.28); |
| 50 | + map.SetWaypoints(KyivWaypoints(3), 50, 80, Firmwares.ArduPlane); |
| 51 | + |
| 52 | + GeometryFeature homeRoute = HomeRoute(map); |
| 53 | + Assert.True(Assert.IsType<Mapsui.MRect>(homeRoute.Extent).Width > 10_000_000); |
| 54 | + Assert.Equal(PenStyle.Solid, LineStyle(homeRoute)); |
| 55 | + } |
| 56 | + |
| 57 | + [AvaloniaFact] |
| 58 | + public void Nearby_home_closing_route_remains_dashed() { |
| 59 | + var map = new FlightPlannerMap(); |
| 60 | + map.SetHome(50.3440, 30.8940, 100); |
| 61 | + map.SetWaypoints(KyivWaypoints(3), 50, 80, Firmwares.ArduPlane); |
| 62 | + |
| 63 | + Assert.Equal(PenStyle.Dash, LineStyle(HomeRoute(map))); |
| 64 | + } |
| 65 | + |
| 66 | + private static GeometryFeature HomeRoute(FlightPlannerMap map) { |
| 67 | + GeometryFeature[] features = RouteLayer(map).GetFeatures().OfType<GeometryFeature>().ToArray(); |
| 68 | + Assert.Equal(2, features.Length); |
| 69 | + return Assert.Single(features, |
| 70 | + feature => Assert.Single(feature.Styles.OfType<VectorStyle>()).Line?.Width == 2); |
| 71 | + } |
| 72 | + |
| 73 | + private static PenStyle LineStyle(GeometryFeature feature) => |
| 74 | + Assert.IsType<Pen>(Assert.Single(feature.Styles.OfType<VectorStyle>()).Line).PenStyle; |
| 75 | + |
| 76 | + private static WritableLayer RouteLayer(FlightPlannerMap map) => |
| 77 | + Assert.IsAssignableFrom<WritableLayer>( |
| 78 | + Assert.Single(map.Map.Layers, candidate => candidate.Name == "Route")); |
| 79 | + |
| 80 | + private static IReadOnlyList<( |
| 81 | + int Seq, double Lat, double Lng, ushort Cmd, double P1, double P2, double P3, double P4)> |
| 82 | + KyivWaypoints(int count) => new[] { |
| 83 | + Waypoint(0, 50.3443012, 30.8947693), |
| 84 | + Waypoint(1, 50.3475125, 30.8815415), |
| 85 | + Waypoint(2, 50.3468588, 30.8318016), |
| 86 | + }.Take(count).ToArray(); |
| 87 | + |
| 88 | + private static ( |
| 89 | + int Seq, double Lat, double Lng, ushort Cmd, double P1, double P2, double P3, double P4) |
| 90 | + Waypoint(int seq, double lat, double lng) => |
| 91 | + (seq, lat, lng, (ushort)MAVLink.MAV_CMD.WAYPOINT, 0, 0, 0, 0); |
| 92 | +} |
0 commit comments