Skip to content

Commit bc1c8ab

Browse files
Generalised and added for ImageButton as well
1 parent d89400b commit bc1c8ab

3 files changed

Lines changed: 197 additions & 14 deletions

File tree

‎src/Sentry.Maui/Internal/MauiEventsBinder.cs‎

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,17 @@ internal void OnApplicationOnDescendantAdded(object? _, ElementEventArgs e)
142142
}
143143

144144
if (_options.EnableAutoTransactions
145-
&& _options.EnableUserInteractionTracing
146-
&& e.Element is Button button)
145+
&& _options.EnableUserInteractionTracing)
147146
{
148-
button.Pressed += OnButtonPressedForTransaction;
147+
switch (e.Element)
148+
{
149+
case Button button:
150+
button.Pressed += OnElementPressedForTransaction;
151+
break;
152+
case ImageButton imageButton:
153+
imageButton.Pressed += OnElementPressedForTransaction;
154+
break;
155+
}
149156
}
150157
}
151158

@@ -201,9 +208,14 @@ internal void OnApplicationOnDescendantRemoved(object? _, ElementEventArgs e)
201208
break;
202209
}
203210

204-
if (e.Element is Button button)
211+
switch (e.Element)
205212
{
206-
button.Pressed -= OnButtonPressedForTransaction;
213+
case Button button:
214+
button.Pressed -= OnElementPressedForTransaction;
215+
break;
216+
case ImageButton imageButton:
217+
imageButton.Pressed -= OnElementPressedForTransaction;
218+
break;
207219
}
208220
}
209221

@@ -412,32 +424,32 @@ internal void HandlePageEvents(Page page, bool bind = true)
412424
return transaction;
413425
}
414426

415-
private void OnButtonPressedForTransaction(object? sender, EventArgs _)
427+
private void OnElementPressedForTransaction(object? sender, EventArgs _)
416428
{
417-
if (sender is not Button button)
429+
if (sender is not Element element)
418430
{
419431
return;
420432
}
421433

422434
string? identifier = null;
423-
if (!string.IsNullOrEmpty(button.AutomationId))
435+
if (!string.IsNullOrEmpty(element.AutomationId))
424436
{
425-
identifier = button.AutomationId;
437+
identifier = element.AutomationId;
426438
}
427-
else if (!string.IsNullOrEmpty(button.StyleId))
439+
else if (!string.IsNullOrEmpty(element.StyleId))
428440
{
429-
identifier = button.StyleId;
441+
identifier = element.StyleId;
430442
}
431443

432444
if (identifier is null)
433445
{
434446
_options.DiagnosticLogger?.Log(
435447
SentryLevel.Warning,
436-
"Button click transaction skipped: element has no AutomationId or StyleId");
448+
"Click transaction skipped: element has no AutomationId or StyleId");
437449
return;
438450
}
439451

440-
var pageName = button.FindContainingPage()?.GetType().Name;
452+
var pageName = element.FindContainingPage()?.GetType().Name;
441453
var name = pageName != null ? $"{pageName}.{identifier}" : identifier;
442454
StartUserInteractionTransaction(name);
443455
}

‎test/Sentry.Maui.Tests/MauiEventsBinderTests.Button.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public void Button_Pressed_NoAutomationIdOrStyleId_DoesNotStartTransaction()
7474
_fixture.Hub.DidNotReceive().StartTransaction(Arg.Any<ITransactionContext>(), Arg.Any<TimeSpan?>());
7575
_fixture.Options.DiagnosticLogger!.Received(1).Log(
7676
SentryLevel.Warning,
77-
Arg.Is<string>(m => m.Contains("AutomationId") && m.Contains("StyleId")),
77+
Arg.Is<string>(m => m.Contains("AutomationId") && m.Contains("StyleId") && m.Contains("Click transaction skipped")),
7878
Arg.Any<Exception>(),
7979
Arg.Any<object[]>());
8080
}
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
using Sentry.Internal;
2+
using Sentry.Maui.Internal;
3+
4+
namespace Sentry.Maui.Tests;
5+
6+
public partial class MauiEventsBinderTests
7+
{
8+
[Fact]
9+
public void ImageButton_Pressed_StartsTransactionWithClickOp()
10+
{
11+
// Arrange
12+
var button = new ImageButton { AutomationId = "my-img-btn" };
13+
_fixture.Binder.OnApplicationOnDescendantAdded(null, new ElementEventArgs(button));
14+
_fixture.Hub.StartTransaction(Arg.Any<ITransactionContext>(), Arg.Any<TimeSpan?>())
15+
.Returns(Substitute.For<ITransactionTracer>());
16+
17+
// Act
18+
button.RaiseEvent(nameof(ImageButton.Pressed), EventArgs.Empty);
19+
20+
// Assert
21+
_fixture.Hub.Received(1).StartTransaction(
22+
Arg.Is<ITransactionContext>(c => c.Operation == MauiEventsBinder.UserInteractionClickOp),
23+
Arg.Any<TimeSpan?>());
24+
}
25+
26+
[Fact]
27+
public void ImageButton_Pressed_UsesAutomationIdAsIdentifier()
28+
{
29+
// Arrange
30+
var button = new ImageButton { AutomationId = "my-img-btn", StyleId = "styleId-ignored" };
31+
_fixture.Binder.OnApplicationOnDescendantAdded(null, new ElementEventArgs(button));
32+
_fixture.Hub.StartTransaction(Arg.Any<ITransactionContext>(), Arg.Any<TimeSpan?>())
33+
.Returns(Substitute.For<ITransactionTracer>());
34+
35+
// Act
36+
button.RaiseEvent(nameof(ImageButton.Pressed), EventArgs.Empty);
37+
38+
// Assert - AutomationId wins over StyleId
39+
_fixture.Hub.Received(1).StartTransaction(
40+
Arg.Is<ITransactionContext>(c => c.Name == "my-img-btn"),
41+
Arg.Any<TimeSpan?>());
42+
}
43+
44+
[Fact]
45+
public void ImageButton_Pressed_FallsBackToStyleIdWhenNoAutomationId()
46+
{
47+
// Arrange
48+
var button = new ImageButton { StyleId = "my-img-btn" };
49+
_fixture.Binder.OnApplicationOnDescendantAdded(null, new ElementEventArgs(button));
50+
_fixture.Hub.StartTransaction(Arg.Any<ITransactionContext>(), Arg.Any<TimeSpan?>())
51+
.Returns(Substitute.For<ITransactionTracer>());
52+
53+
// Act
54+
button.RaiseEvent(nameof(ImageButton.Pressed), EventArgs.Empty);
55+
56+
// Assert
57+
_fixture.Hub.Received(1).StartTransaction(
58+
Arg.Is<ITransactionContext>(c => c.Name == "my-img-btn"),
59+
Arg.Any<TimeSpan?>());
60+
}
61+
62+
[Fact]
63+
public void ImageButton_Pressed_NoAutomationIdOrStyleId_DoesNotStartTransaction()
64+
{
65+
// Arrange
66+
var button = new ImageButton();
67+
_fixture.Binder.OnApplicationOnDescendantAdded(null, new ElementEventArgs(button));
68+
69+
// Act
70+
button.RaiseEvent(nameof(ImageButton.Pressed), EventArgs.Empty);
71+
72+
// Assert
73+
_fixture.Hub.DidNotReceive().StartTransaction(Arg.Any<ITransactionContext>(), Arg.Any<TimeSpan?>());
74+
_fixture.Options.DiagnosticLogger!.Received(1).Log(
75+
SentryLevel.Warning,
76+
Arg.Is<string>(m => m.Contains("AutomationId") && m.Contains("StyleId") && m.Contains("Click transaction skipped")),
77+
Arg.Any<Exception>(),
78+
Arg.Any<object[]>());
79+
}
80+
81+
[Fact]
82+
public void ImageButton_Pressed_UsesPageTypeNameInTransactionName()
83+
{
84+
// Arrange
85+
var button = new ImageButton { AutomationId = "my-img-btn" };
86+
_ = new ContentPage { Content = new VerticalStackLayout { button } };
87+
_fixture.Binder.OnApplicationOnDescendantAdded(null, new ElementEventArgs(button));
88+
_fixture.Hub.StartTransaction(Arg.Any<ITransactionContext>(), Arg.Any<TimeSpan?>())
89+
.Returns(Substitute.For<ITransactionTracer>());
90+
91+
// Act
92+
button.RaiseEvent(nameof(ImageButton.Pressed), EventArgs.Empty);
93+
94+
// Assert
95+
_fixture.Hub.Received(1).StartTransaction(
96+
Arg.Is<ITransactionContext>(c => c.Name == $"{nameof(ContentPage)}.my-img-btn"),
97+
Arg.Any<TimeSpan?>());
98+
}
99+
100+
[Fact]
101+
public void ImageButton_Pressed_EnableUserInteractionTracingFalse_DoesNotStart()
102+
{
103+
// Arrange
104+
_fixture.Options.EnableUserInteractionTracing = false;
105+
var button = new ImageButton { AutomationId = "my-img-btn" };
106+
_fixture.Binder.OnApplicationOnDescendantAdded(null, new ElementEventArgs(button));
107+
108+
// Act
109+
button.RaiseEvent(nameof(ImageButton.Pressed), EventArgs.Empty);
110+
111+
// Assert
112+
_fixture.Hub.DidNotReceive().StartTransaction(Arg.Any<ITransactionContext>(), Arg.Any<TimeSpan?>());
113+
}
114+
115+
[Fact]
116+
public void ImageButton_Pressed_EnableAutoTransactionsFalse_DoesNotStart()
117+
{
118+
// Arrange
119+
_fixture.Options.EnableAutoTransactions = false;
120+
var button = new ImageButton { AutomationId = "my-img-btn" };
121+
_fixture.Binder.OnApplicationOnDescendantAdded(null, new ElementEventArgs(button));
122+
123+
// Act
124+
button.RaiseEvent(nameof(ImageButton.Pressed), EventArgs.Empty);
125+
126+
// Assert
127+
_fixture.Hub.DidNotReceive().StartTransaction(Arg.Any<ITransactionContext>(), Arg.Any<TimeSpan?>());
128+
}
129+
130+
[Fact]
131+
public void ImageButton_Pressed_ThenShellNavigating_NavigationIsChildSpanOfClick()
132+
{
133+
// Arrange
134+
var shell = new Shell { StyleId = "shell" };
135+
_fixture.Binder.HandleShellEvents(shell);
136+
var button = new ImageButton { AutomationId = "my-img-btn" };
137+
_fixture.Binder.OnApplicationOnDescendantAdded(null, new ElementEventArgs(button));
138+
139+
var navSpan = Substitute.For<ISpan>();
140+
var clickTransaction = Substitute.For<ITransactionTracer>();
141+
clickTransaction.IsFinished.Returns(false);
142+
clickTransaction.StartChild(Arg.Any<string>()).Returns(navSpan);
143+
_fixture.Hub.StartTransaction(Arg.Any<ITransactionContext>(), Arg.Any<TimeSpan?>())
144+
.Returns(clickTransaction);
145+
146+
// Act - press button, then navigate
147+
button.RaiseEvent(nameof(ImageButton.Pressed), EventArgs.Empty);
148+
shell.RaiseEvent(nameof(Shell.Navigating),
149+
new ShellNavigatingEventArgs(new ShellNavigationState("foo"), new ShellNavigationState("bar"), ShellNavigationSource.Push, false));
150+
151+
// Assert - only one transaction created (click), navigation is a child span
152+
_fixture.Hub.Received(1).StartTransaction(Arg.Any<ITransactionContext>(), Arg.Any<TimeSpan?>());
153+
clickTransaction.Received(1).StartChild("ui.load");
154+
}
155+
156+
[Fact]
157+
public void ImageButton_Unbind_StopsStartingTransactions()
158+
{
159+
// Arrange
160+
var button = new ImageButton { AutomationId = "my-img-btn" };
161+
var el = new ElementEventArgs(button);
162+
_fixture.Binder.OnApplicationOnDescendantAdded(null, el);
163+
164+
// Act - unbind, then press
165+
_fixture.Binder.OnApplicationOnDescendantRemoved(null, el);
166+
button.RaiseEvent(nameof(ImageButton.Pressed), EventArgs.Empty);
167+
168+
// Assert
169+
_fixture.Hub.DidNotReceive().StartTransaction(Arg.Any<ITransactionContext>(), Arg.Any<TimeSpan?>());
170+
}
171+
}

0 commit comments

Comments
 (0)