|
| 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