From c31a16cefcdb2d20b6f3f2bc84c1fde81a660664 Mon Sep 17 00:00:00 2001 From: Ahamed-Ali <102580874+Ahamed-Ali@users.noreply.github.com> Date: Tue, 29 Apr 2025 10:34:43 +0530 Subject: [PATCH 1/7] Committed the test case and sample --- .../TestCases.HostApp/Issues/Issue20612.cs | 85 +++++++++++++++++++ .../Tests/Issues/Issue20612.cs | 27 ++++++ 2 files changed, 112 insertions(+) create mode 100644 src/Controls/tests/TestCases.HostApp/Issues/Issue20612.cs create mode 100644 src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue20612.cs diff --git a/src/Controls/tests/TestCases.HostApp/Issues/Issue20612.cs b/src/Controls/tests/TestCases.HostApp/Issues/Issue20612.cs new file mode 100644 index 000000000000..32d6312a9066 --- /dev/null +++ b/src/Controls/tests/TestCases.HostApp/Issues/Issue20612.cs @@ -0,0 +1,85 @@ +using Microsoft.Maui.Maps; + +namespace Maui.Controls.Sample.Issues; + + +[Issue(IssueTracker.Github, 20612, "Disconnecting Map Handler causes Map to crash on second page entrance and moving to region.", PlatformAffected.Android)] +public class Issue20612 : TestNavigationPage +{ + protected override void Init() + { + PushAsync(new Issue20612page1()); + } +} + +public class Issue20612page1 : ContentPage +{ + public Issue20612page1() + { + var openMapButton = new Button + { + Text = "Navigate to Map page", + HorizontalOptions = LayoutOptions.Center, + VerticalOptions = LayoutOptions.Center, + AutomationId = "MapButton" + }; + + openMapButton.Clicked += (s, e) => + { + Navigation.PushAsync(new Issue20612page2()); + }; + Content = openMapButton; + } +} + +public class Issue20612page2 : ContentPage +{ + private Microsoft.Maui.Controls.Maps.Map _map; + + public Issue20612page2() + { + _map = new Microsoft.Maui.Controls.Maps.Map + { + HorizontalOptions = LayoutOptions.Fill, + VerticalOptions = LayoutOptions.Fill, + BackgroundColor = Colors.Red + }; + + var goBackButton = new Button + { + Text = "Go back", + HorizontalOptions = LayoutOptions.Center, + VerticalOptions = LayoutOptions.Center, + AutomationId = "GoBackButton" + }; + goBackButton.Clicked += GoBack; + + var grid = new Grid(); + grid.Children.Add(_map); + grid.Children.Add(goBackButton); + + Content = grid; + + MoveMap(); + } + + private async void MoveMap() + { + await Task.Delay(1000).ConfigureAwait(false); + + MainThread.BeginInvokeOnMainThread(() => + { + var mapSpan = MapSpan.FromCenterAndRadius( + new Location(5, 5), + Distance.FromMeters(10000)); + + _map.MoveToRegion(mapSpan); + }); + } + + void GoBack(object sender, EventArgs e) + { + _map.Handler?.DisconnectHandler(); + Navigation.PopAsync(); + } +} \ No newline at end of file diff --git a/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue20612.cs b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue20612.cs new file mode 100644 index 000000000000..00dac80b213b --- /dev/null +++ b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue20612.cs @@ -0,0 +1,27 @@ +using NUnit.Framework; +using UITest.Appium; +using UITest.Core; + +namespace Microsoft.Maui.TestCases.Tests.Issues +{ + public class Issue20612 : _IssuesUITest + { + public Issue20612(TestDevice device) : base(device) { } + + public override string Issue => "Disconnecting Map Handler causes Map to crash on second page entrance and moving to region."; + + [Test] + [Category(UITestCategories.Maps)] + public void MapsShouldNotCrashWhenNavigationOccurs() + { + App.WaitForElement("MapButton"); + App.Tap("MapButton"); + App.WaitForElement("GoBackButton"); + App.Tap("GoBackButton"); + // second Navigation to Map page + App.WaitForElement("MapButton"); + App.Tap("MapButton"); + //App.WaitForElement("GoBackButton"); + } + } +} \ No newline at end of file From 62e382fd0c30a55ae96d6a569f5224d76af1b2cf Mon Sep 17 00:00:00 2001 From: Ahamed-Ali <102580874+Ahamed-Ali@users.noreply.github.com> Date: Tue, 29 Apr 2025 12:54:35 +0530 Subject: [PATCH 2/7] committed the fix --- .../TestCases.HostApp/Issues/Issue20612.cs | 2 +- .../Tests/Issues/Issue20612.cs | 2 +- .../maps/src/Handlers/Map/MapHandler.iOS.cs | 2 +- src/Core/maps/src/Platform/iOS/MapPool.cs | 13 +++++++++++- .../maps/src/Platform/iOS/MauiMKMapView.cs | 20 +++++++++++++++++-- 5 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/Controls/tests/TestCases.HostApp/Issues/Issue20612.cs b/src/Controls/tests/TestCases.HostApp/Issues/Issue20612.cs index 32d6312a9066..7087d632f207 100644 --- a/src/Controls/tests/TestCases.HostApp/Issues/Issue20612.cs +++ b/src/Controls/tests/TestCases.HostApp/Issues/Issue20612.cs @@ -3,7 +3,7 @@ namespace Maui.Controls.Sample.Issues; -[Issue(IssueTracker.Github, 20612, "Disconnecting Map Handler causes Map to crash on second page entrance and moving to region.", PlatformAffected.Android)] +[Issue(IssueTracker.Github, 20612, "Disconnecting Map Handler causes Map to crash on second page entrance and moving to region.", PlatformAffected.iOS | PlatformAffected.macOS)] public class Issue20612 : TestNavigationPage { protected override void Init() diff --git a/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue20612.cs b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue20612.cs index 00dac80b213b..6165b075b67e 100644 --- a/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue20612.cs +++ b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue20612.cs @@ -21,7 +21,7 @@ public void MapsShouldNotCrashWhenNavigationOccurs() // second Navigation to Map page App.WaitForElement("MapButton"); App.Tap("MapButton"); - //App.WaitForElement("GoBackButton"); + } } } \ No newline at end of file diff --git a/src/Core/maps/src/Handlers/Map/MapHandler.iOS.cs b/src/Core/maps/src/Handlers/Map/MapHandler.iOS.cs index 7fa40cfc68b0..181105ff4f0c 100644 --- a/src/Core/maps/src/Handlers/Map/MapHandler.iOS.cs +++ b/src/Core/maps/src/Handlers/Map/MapHandler.iOS.cs @@ -13,7 +13,7 @@ public partial class MapHandler : ViewHandler protected override MauiMKMapView CreatePlatformView() { - return MapPool.Get() ?? new MauiMKMapView(this); + return MapPool.Get(this) ?? new MauiMKMapView(this); } protected override void ConnectHandler(MauiMKMapView platformView) diff --git a/src/Core/maps/src/Platform/iOS/MapPool.cs b/src/Core/maps/src/Platform/iOS/MapPool.cs index 801229429a1b..cf9456364ec7 100644 --- a/src/Core/maps/src/Platform/iOS/MapPool.cs +++ b/src/Core/maps/src/Platform/iOS/MapPool.cs @@ -1,4 +1,5 @@ using System.Collections.Concurrent; +using Microsoft.Maui.Maps.Handlers; namespace Microsoft.Maui.Maps.Platform { @@ -11,6 +12,16 @@ internal class MapPool public static void Add(MauiMKMapView mapView) => Instance.Maps.Enqueue(mapView); - public static MauiMKMapView? Get() => Instance.Maps.TryDequeue(out MauiMKMapView? mapView) ? mapView : null; + public static MauiMKMapView? Get(IMapHandler mapHandler) + { + var hasInstance = Instance.Maps.TryDequeue(out MauiMKMapView? mapView); + + if (hasInstance && mapView != null) + { + mapView.Handler = mapHandler; + } + + return hasInstance ? mapView : null; + } } } diff --git a/src/Core/maps/src/Platform/iOS/MauiMKMapView.cs b/src/Core/maps/src/Platform/iOS/MauiMKMapView.cs index 2f5be1447c52..ecef819070c0 100644 --- a/src/Core/maps/src/Platform/iOS/MauiMKMapView.cs +++ b/src/Core/maps/src/Platform/iOS/MauiMKMapView.cs @@ -12,16 +12,32 @@ namespace Microsoft.Maui.Maps.Platform { public class MauiMKMapView : MKMapView { - WeakReference _handlerRef; + WeakReference _handlerRef = new WeakReference(null!); object? _lastTouchedView; UITapGestureRecognizer? _mapClickedGestureRecognizer; public MauiMKMapView(IMapHandler handler) { - _handlerRef = new WeakReference(handler); + Handler = handler; OverlayRenderer = GetViewForOverlayDelegate; } + internal IMapHandler? Handler + { + get + { + _handlerRef.TryGetTarget(out var handler); + return handler; + } + set + { + if (value != null) + { + _handlerRef = new WeakReference(value); + } + } + } + public override void MovedToWindow() { base.MovedToWindow(); From df491f171f5a4c7c0e4ac1e24ded83a5900c69b1 Mon Sep 17 00:00:00 2001 From: Ahamed-Ali <102580874+Ahamed-Ali@users.noreply.github.com> Date: Tue, 29 Apr 2025 13:32:49 +0530 Subject: [PATCH 3/7] Modified the test sample --- .../tests/TestCases.HostApp/Issues/Issue20612.cs | 14 +++++++++++++- .../Tests/Issues/Issue20612.cs | 1 - 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/Controls/tests/TestCases.HostApp/Issues/Issue20612.cs b/src/Controls/tests/TestCases.HostApp/Issues/Issue20612.cs index 7087d632f207..02fe9852bd6b 100644 --- a/src/Controls/tests/TestCases.HostApp/Issues/Issue20612.cs +++ b/src/Controls/tests/TestCases.HostApp/Issues/Issue20612.cs @@ -54,9 +54,21 @@ public Issue20612page2() }; goBackButton.Clicked += GoBack; - var grid = new Grid(); + var grid = new Grid + { + RowDefinitions = + { + new RowDefinition { Height = new GridLength(1, GridUnitType.Star) }, // Row 0: for map + new RowDefinition { Height = GridLength.Auto } // Row 1: for button + } + }; + grid.Children.Add(_map); + Grid.SetRow(_map, 0); + grid.Children.Add(goBackButton); + Grid.SetRow(goBackButton, 1); + Content = grid; diff --git a/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue20612.cs b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue20612.cs index 6165b075b67e..7e8f53a3e90e 100644 --- a/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue20612.cs +++ b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue20612.cs @@ -21,7 +21,6 @@ public void MapsShouldNotCrashWhenNavigationOccurs() // second Navigation to Map page App.WaitForElement("MapButton"); App.Tap("MapButton"); - } } } \ No newline at end of file From e7e2bc6698afa3b917b0a746b869448c1603ffa2 Mon Sep 17 00:00:00 2001 From: Ahamed-Ali <102580874+Ahamed-Ali@users.noreply.github.com> Date: Tue, 29 Apr 2025 15:08:05 +0530 Subject: [PATCH 4/7] Updated the Test condition for windows --- .../tests/TestCases.Shared.Tests/Tests/Issues/Issue20612.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue20612.cs b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue20612.cs index 7e8f53a3e90e..a6f03d68e338 100644 --- a/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue20612.cs +++ b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue20612.cs @@ -1,3 +1,5 @@ +#if TEST_FAILS_ON_WINDOWS +//No Map control support in windows https://learn.microsoft.com/en-us/dotnet/maui/user-interface/controls/map?view=net-maui-9.0 using NUnit.Framework; using UITest.Appium; using UITest.Core; @@ -23,4 +25,5 @@ public void MapsShouldNotCrashWhenNavigationOccurs() App.Tap("MapButton"); } } -} \ No newline at end of file +} +#endif \ No newline at end of file From de5f19580247c4d3d81868f26b8d901ebc4e0bd6 Mon Sep 17 00:00:00 2001 From: Ahamed-Ali <102580874+Ahamed-Ali@users.noreply.github.com> Date: Wed, 7 May 2025 13:38:06 +0530 Subject: [PATCH 5/7] Removed unwanted spaces --- .../TestCases.HostApp/Issues/Issue20612.cs | 3 -- .../Tests/Issues/Issue20612.cs | 32 +++++++++---------- 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/src/Controls/tests/TestCases.HostApp/Issues/Issue20612.cs b/src/Controls/tests/TestCases.HostApp/Issues/Issue20612.cs index 02fe9852bd6b..a513d8aaf0b8 100644 --- a/src/Controls/tests/TestCases.HostApp/Issues/Issue20612.cs +++ b/src/Controls/tests/TestCases.HostApp/Issues/Issue20612.cs @@ -2,7 +2,6 @@ namespace Maui.Controls.Sample.Issues; - [Issue(IssueTracker.Github, 20612, "Disconnecting Map Handler causes Map to crash on second page entrance and moving to region.", PlatformAffected.iOS | PlatformAffected.macOS)] public class Issue20612 : TestNavigationPage { @@ -68,8 +67,6 @@ public Issue20612page2() grid.Children.Add(goBackButton); Grid.SetRow(goBackButton, 1); - - Content = grid; MoveMap(); diff --git a/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue20612.cs b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue20612.cs index a6f03d68e338..edc96e423c10 100644 --- a/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue20612.cs +++ b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue20612.cs @@ -4,26 +4,24 @@ using UITest.Appium; using UITest.Core; -namespace Microsoft.Maui.TestCases.Tests.Issues +namespace Microsoft.Maui.TestCases.Tests.Issues; +public class Issue20612 : _IssuesUITest { - public class Issue20612 : _IssuesUITest - { - public Issue20612(TestDevice device) : base(device) { } + public Issue20612(TestDevice device) : base(device) { } - public override string Issue => "Disconnecting Map Handler causes Map to crash on second page entrance and moving to region."; + public override string Issue => "Disconnecting Map Handler causes Map to crash on second page entrance and moving to region."; - [Test] - [Category(UITestCategories.Maps)] - public void MapsShouldNotCrashWhenNavigationOccurs() - { - App.WaitForElement("MapButton"); - App.Tap("MapButton"); - App.WaitForElement("GoBackButton"); - App.Tap("GoBackButton"); - // second Navigation to Map page - App.WaitForElement("MapButton"); - App.Tap("MapButton"); - } + [Test] + [Category(UITestCategories.Maps)] + public void MapsShouldNotCrashWhenNavigationOccurs() + { + App.WaitForElement("MapButton"); + App.Tap("MapButton"); + App.WaitForElement("GoBackButton"); + App.Tap("GoBackButton"); + // second Navigation to Map page + App.WaitForElement("MapButton"); + App.Tap("MapButton"); } } #endif \ No newline at end of file From be0feefb2cf43383ad41a3399e311d6d9bc61dd7 Mon Sep 17 00:00:00 2001 From: Ahamed-Ali <102580874+Ahamed-Ali@users.noreply.github.com> Date: Wed, 7 May 2025 16:23:34 +0530 Subject: [PATCH 6/7] Optimized the fix --- src/Core/maps/src/Platform/iOS/MapPool.cs | 8 +++----- src/Core/maps/src/Platform/iOS/MauiMKMapView.cs | 4 ++-- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/Core/maps/src/Platform/iOS/MapPool.cs b/src/Core/maps/src/Platform/iOS/MapPool.cs index cf9456364ec7..dd2fe8258f88 100644 --- a/src/Core/maps/src/Platform/iOS/MapPool.cs +++ b/src/Core/maps/src/Platform/iOS/MapPool.cs @@ -14,14 +14,12 @@ internal class MapPool public static MauiMKMapView? Get(IMapHandler mapHandler) { - var hasInstance = Instance.Maps.TryDequeue(out MauiMKMapView? mapView); - - if (hasInstance && mapView != null) + if (Instance.Maps.TryDequeue(out MauiMKMapView? mapView) && mapView is not null) { mapView.Handler = mapHandler; + return mapView; } - - return hasInstance ? mapView : null; + return null; } } } diff --git a/src/Core/maps/src/Platform/iOS/MauiMKMapView.cs b/src/Core/maps/src/Platform/iOS/MauiMKMapView.cs index ecef819070c0..949354fa00f5 100644 --- a/src/Core/maps/src/Platform/iOS/MauiMKMapView.cs +++ b/src/Core/maps/src/Platform/iOS/MauiMKMapView.cs @@ -12,13 +12,13 @@ namespace Microsoft.Maui.Maps.Platform { public class MauiMKMapView : MKMapView { - WeakReference _handlerRef = new WeakReference(null!); + WeakReference _handlerRef; object? _lastTouchedView; UITapGestureRecognizer? _mapClickedGestureRecognizer; public MauiMKMapView(IMapHandler handler) { - Handler = handler; + _handlerRef = new WeakReference(handler); OverlayRenderer = GetViewForOverlayDelegate; } From 16c13c87018ceeebfb7b256ccda75af12dfef103 Mon Sep 17 00:00:00 2001 From: Ahamed-Ali <102580874+Ahamed-Ali@users.noreply.github.com> Date: Wed, 7 May 2025 17:30:52 +0530 Subject: [PATCH 7/7] Change the code into modern c# --- src/Core/maps/src/Platform/iOS/MauiMKMapView.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Core/maps/src/Platform/iOS/MauiMKMapView.cs b/src/Core/maps/src/Platform/iOS/MauiMKMapView.cs index 949354fa00f5..e52025abeb5b 100644 --- a/src/Core/maps/src/Platform/iOS/MauiMKMapView.cs +++ b/src/Core/maps/src/Platform/iOS/MauiMKMapView.cs @@ -31,7 +31,7 @@ internal IMapHandler? Handler } set { - if (value != null) + if (value is not null) { _handlerRef = new WeakReference(value); }