Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- Target `net9.0` on Sentry.Google.Cloud.Functions to avoid conflict with Sentry.AspNetCore ([#4039](https://github.com/getsentry/sentry-dotnet/pull/4039))
- Changed default value for `SentryOptions.EnableAppHangTrackingV2` to `false` ([#4042](https://github.com/getsentry/sentry-dotnet/pull/4042))
- Missing MAUI `Shell` navigation breadcrumbs on iOS ([#4006](https://github.com/getsentry/sentry-dotnet/pull/4006))
- Prevent application crashes when capturing screenshots on iOS ([#4069](https://github.com/getsentry/sentry-dotnet/pull/4069))

### Features

Expand Down
16 changes: 16 additions & 0 deletions src/Sentry.Maui/Internal/ScreenshotAttachment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,22 @@ public ScreenshotAttachmentContent(SentryMauiOptions options)
}

public Stream GetStream()
{
try
{
return GetStreamInternal();
}
catch (Exception ex)
{
// See https://github.com/getsentry/sentry-dotnet/issues/3880#issuecomment-2640159466
_options.LogError("Failed to capture screenshot", ex);
// Return empty stream since calling code may assume a non-null return value
// E.g. https://github.com/getsentry/sentry-dotnet/blob/db5606833a4b0662c6bea0663cca10cb05fb5157/src/Sentry/Protocol/Envelopes/EnvelopeItem.cs#L332-L333
return new MemoryStream();
}
}

private Stream GetStreamInternal()
{
var stream = Stream.Null;
// Not including this on Windows specific build because on WinUI this can deadlock.
Expand Down
16 changes: 16 additions & 0 deletions test/Sentry.Maui.Tests/SentryMauiScreenshotTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,20 @@ public Fixture()
private readonly Fixture _fixture = new();

#if __MOBILE__
// TODO: This test doesn't work on Android or iOS, so isn't much use. We should consider replacing it with some
// more granular tests that test SentryMauiOptionsSetup, SentryMauiScreenshotProcessor and ScreenshotAttachment
// in isolation. It's generally hard to test any of this functionality since ScreenshotImplementation relies of
// various static members like ActivityStateManager.Default:
// https://github.com/dotnet/maui/blob/3c7b65264d2f341a48db32263a271fd8718cfd23/src/Essentials/src/Screenshot/Screenshot.android.cs#L28
[SkippableFact]
public async Task CaptureException_WhenAttachScreenshots_ContainsScreenshotAttachmentAsync()
{
#if __IOS__
Skip.If(true, "Flaky on iOS");
#endif
#if ANDROID
Skip.If(true, "Doesn't work on Android");
#endif

// Arrange
var builder = _fixture.Builder.UseSentry();
Expand Down Expand Up @@ -133,12 +141,20 @@ public async Task CaptureException_BeforeCaptureScreenshot_DisableCaptureAsync()
envelopeItem.Should().BeNull();
}

// TODO: This test doesn't work on Android or iOS, so isn't much use. We should consider replacing it with some
// more granular tests that test SentryMauiOptionsSetup, SentryMauiScreenshotProcessor and ScreenshotAttachment
// in isolation. It's generally hard to test any of this functionality since ScreenshotImplementation relies of
// various static members like ActivityStateManager.Default:
// https://github.com/dotnet/maui/blob/3c7b65264d2f341a48db32263a271fd8718cfd23/src/Essentials/src/Screenshot/Screenshot.android.cs#L28
[SkippableFact]
public async Task CaptureException_BeforeCaptureScreenshot_DefaultAsync()
{
#if __IOS__
Skip.If(true, "Flaky on iOS");
#endif
#if ANDROID
Skip.If(true, "Doesn't work on Android");
#endif

// Arrange
var builder = _fixture.Builder.UseSentry(options => options.SetBeforeScreenshotCapture((e, hint) =>
Expand Down
Loading