|
| 1 | +using Microsoft.Extensions.Options; |
| 2 | +#if ANDROID |
| 3 | +using Sentry.Android; |
| 4 | +#endif |
| 5 | + |
| 6 | +namespace Sentry.Maui.Tests; |
| 7 | + |
| 8 | +public class SentryMauiLogcatsTests |
| 9 | +{ |
| 10 | + private class Fixture |
| 11 | + { |
| 12 | + public MauiAppBuilder Builder { get; } |
| 13 | + public FakeTransport Transport { get; private set; } = new FakeTransport(); |
| 14 | + public InMemoryDiagnosticLogger Logger { get; private set; } = new InMemoryDiagnosticLogger(); |
| 15 | + |
| 16 | + public Fixture() |
| 17 | + { |
| 18 | + var builder = MauiApp.CreateBuilder(); |
| 19 | + builder.Services.AddSingleton(Substitute.For<IApplication>()); |
| 20 | + |
| 21 | + builder.Services.Configure<SentryMauiOptions>(options => |
| 22 | + { |
| 23 | + options.Transport = Transport; |
| 24 | + options.Dsn = ValidDsn; |
| 25 | + options.AttachScreenshot = false; //Disable the screenshot attachment to have the logcat as primary attachment |
| 26 | + options.Debug = true; |
| 27 | + options.DiagnosticLogger = Logger; |
| 28 | + options.AutoSessionTracking = false; //Get rid of session envelope for easier Assert |
| 29 | + options.CacheDirectoryPath = null; //Do not wrap our FakeTransport with a caching transport |
| 30 | + options.FlushTimeout = TimeSpan.FromSeconds(10); |
| 31 | + }); |
| 32 | + Builder = builder; |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + private readonly Fixture _fixture = new(); |
| 37 | + |
| 38 | +#if ANDROID |
| 39 | + [Fact] |
| 40 | + public void CaptureException_CheckLogcatType() |
| 41 | + { |
| 42 | + var builder = _fixture.Builder.UseSentry(options => |
| 43 | + { |
| 44 | + options.Android.LogCatIntegration = Android.LogCatIntegrationType.All; |
| 45 | + }); |
| 46 | + |
| 47 | + // Arrange |
| 48 | + var processor = Substitute.For<ISentryEventProcessorWithHint>(); |
| 49 | + using var app = builder.Build(); |
| 50 | + SentryHint hint = null; |
| 51 | + var options = app.Services.GetRequiredService<IOptions<SentryMauiOptions>>().Value; |
| 52 | + |
| 53 | + var scope = new Scope(options); |
| 54 | + |
| 55 | + // Act |
| 56 | + processor.Process(Arg.Any<SentryEvent>(), Arg.Do<SentryHint>(h => hint = h)).Returns(new SentryEvent()); |
| 57 | + options.AddEventProcessor(processor); |
| 58 | + |
| 59 | + _ = new SentryClient(options).CaptureEvent(new SentryEvent(), scope); |
| 60 | + |
| 61 | + |
| 62 | + // Assert |
| 63 | + hint.Should().NotBeNull(); |
| 64 | + hint.Attachments.First().ContentType.Should().Be("text/plain", hint.Attachments.First().ContentType); |
| 65 | + } |
| 66 | +#endif |
| 67 | +} |
0 commit comments