Skip to content

Commit 76f3632

Browse files
committed
Fix missing iOS navigation events and removes duplicate page (dis)appearing events
1 parent f0a778b commit 76f3632

File tree

4 files changed

+59
-1
lines changed

4 files changed

+59
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
### Fixes
1010

1111
- Unknown stack frames in profiles on .NET 8+ ([#3967](https://github.com/getsentry/sentry-dotnet/pull/3967))
12+
- Missing MAUI `Shell` navigation breadcrumbs on iOS ([#4006](https://github.com/getsentry/sentry-dotnet/pull/4006))
1213

1314
## 5.3.0
1415

src/Sentry.Maui/Internal/MauiEventsBinder.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,16 @@ public void HandleApplicationEvents(Application application, bool bind = true)
3434
{
3535
if (bind)
3636
{
37-
// Attach element events to all descendents as they are added to the application.
37+
// Attach element events to all existing descendants (skip the application itself)
38+
foreach (var descendant in application.GetVisualTreeDescendants().Skip(1))
39+
{
40+
if (descendant is VisualElement element)
41+
{
42+
OnApplicationOnDescendantAdded(application, new ElementEventArgs(element));
43+
}
44+
}
45+
46+
// Attach element events to all descendants as they are added to the application.
3847
application.DescendantAdded += OnApplicationOnDescendantAdded;
3948
application.DescendantRemoved += OnApplicationOnDescendantRemoved;
4049

test/Sentry.Maui.Tests/MauiEventsBinderTests.Application.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,36 @@ public void Application_UnbindModalEvents_DoesNotAddBreadcrumb(string eventName,
145145
Assert.Single(_fixture.Scope.Breadcrumbs);
146146
}
147147

148+
[Fact]
149+
public void Application_HandleApplicationEvents_TracksExistingDescendants()
150+
{
151+
// Arrange
152+
var application = MockApplication.Create();
153+
var element = new MockVisualElement("element");
154+
var mainPage = new ContentPage
155+
{
156+
Content = new VerticalStackLayout
157+
{
158+
element
159+
}
160+
};
161+
162+
application.AddWindow(mainPage);
163+
164+
_fixture.Binder.HandleApplicationEvents(application);
165+
166+
// Act
167+
element.RaiseEvent(nameof(VisualElement.Focused), new FocusEventArgs(element, true));
168+
169+
// Assert
170+
var crumb = Assert.Single(_fixture.Scope.Breadcrumbs);
171+
Assert.Equal($"{nameof(MockVisualElement)}.{nameof(VisualElement.Focused)}", crumb.Message);
172+
Assert.Equal(BreadcrumbLevel.Info, crumb.Level);
173+
Assert.Equal(MauiEventsBinder.SystemType, crumb.Type);
174+
Assert.Equal(MauiEventsBinder.RenderingCategory, crumb.Category);
175+
crumb.Data.Should().Contain($"{nameof(MockVisualElement)}.Name", "element");
176+
}
177+
148178
public static IEnumerable<object[]> ApplicationModalEventsData
149179
{
150180
get

test/Sentry.Maui.Tests/Mocks/MockApplication.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace Sentry.Maui.Tests.Mocks;
55
public class MockApplication : Application
66
{
77
private static readonly object LockObj = new();
8+
private Page _mainPage;
89

910
static MockApplication()
1011
{
@@ -18,6 +19,23 @@ private MockApplication()
1819
{
1920
}
2021

22+
public void AddWindow(Page mainPage)
23+
{
24+
_mainPage = mainPage;
25+
((IApplication)this).CreateWindow(null);
26+
_mainPage = null;
27+
}
28+
29+
protected override Window CreateWindow(IActivationState activationState)
30+
{
31+
if (_mainPage != null)
32+
{
33+
return new Window(_mainPage);
34+
}
35+
36+
return base.CreateWindow(activationState);
37+
}
38+
2139
public static MockApplication Create()
2240
{
2341
// The base constructor will try to set the mock as the current application, which we don't want in tests.

0 commit comments

Comments
 (0)