|
1 | | -namespace Sentry.Maui.Device.IntegrationTestApp; |
| 1 | +#if ANDROID |
| 2 | +using Android.OS; |
| 3 | +#endif |
| 4 | +using System.Collections.Concurrent; |
| 5 | + |
| 6 | +namespace Sentry.Maui.Device.IntegrationTestApp; |
2 | 7 |
|
3 | 8 | public partial class App : Application |
4 | 9 | { |
| 10 | + private static readonly ConcurrentDictionary<string, Dictionary<string, string>> systemBreadcrumbs = new(); |
| 11 | + private static string? testArg; |
| 12 | + |
5 | 13 | public App() |
6 | 14 | { |
7 | 15 | InitializeComponent(); |
8 | 16 | } |
9 | 17 |
|
| 18 | + private static bool HasTestArg(string arg) |
| 19 | + { |
| 20 | + return string.Equals(testArg, arg, StringComparison.OrdinalIgnoreCase); |
| 21 | + } |
| 22 | + |
| 23 | + public static void ReceiveSystemBreadcrumb(Breadcrumb breadcrumb) |
| 24 | + { |
| 25 | + if (breadcrumb.Type != "system" || |
| 26 | + breadcrumb.Data?.TryGetValue("action", out var action) != true || |
| 27 | + string.IsNullOrEmpty(action)) |
| 28 | + { |
| 29 | + return; |
| 30 | + } |
| 31 | + |
| 32 | + systemBreadcrumbs[action] = new Dictionary<string, string>() |
| 33 | + { |
| 34 | + ["action"] = action, |
| 35 | + ["category"] = breadcrumb.Category ?? string.Empty, |
| 36 | + ["thread_id"] = Thread.CurrentThread.ManagedThreadId.ToString(), |
| 37 | + ["type"] = breadcrumb.Type ?? string.Empty, |
| 38 | + }; |
| 39 | + |
| 40 | + if (HasTestArg(action)) |
| 41 | + { |
| 42 | + // received after OnAppearing |
| 43 | + CaptureSystemBreadcrumb(action, systemBreadcrumbs[action]!); |
| 44 | + Kill(); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + public static void CaptureSystemBreadcrumb(string action, Dictionary<string, string> data) |
| 49 | + { |
| 50 | + SentrySdk.CaptureMessage(action, scope => |
| 51 | + { |
| 52 | + foreach (var kvp in data) |
| 53 | + { |
| 54 | + scope.SetExtra(kvp.Key, kvp.Value); |
| 55 | + } |
| 56 | + }); |
| 57 | + } |
| 58 | + |
10 | 59 | protected override Window CreateWindow(IActivationState? activationState) |
11 | 60 | { |
12 | 61 | return new Window(new AppShell()); |
13 | 62 | } |
| 63 | + |
| 64 | + public static void OnAppearing() |
| 65 | + { |
| 66 | + testArg = System.Environment.GetEnvironmentVariable("SENTRY_TEST_ARG"); |
| 67 | + |
| 68 | +#pragma warning disable CS0618 |
| 69 | + if (Enum.TryParse<CrashType>(testArg, ignoreCase: true, out var crashType)) |
| 70 | + { |
| 71 | + SentrySdk.CauseCrash(crashType); |
| 72 | + } |
| 73 | +#pragma warning restore CS0618 |
| 74 | + |
| 75 | + if (HasTestArg("NullReferenceException")) |
| 76 | + { |
| 77 | + try |
| 78 | + { |
| 79 | + object? obj = null; |
| 80 | + _ = obj!.ToString(); |
| 81 | + } |
| 82 | + catch (NullReferenceException ex) |
| 83 | + { |
| 84 | + SentrySdk.CaptureException(ex); |
| 85 | + } |
| 86 | + Kill(); |
| 87 | + } |
| 88 | + else if (!string.IsNullOrEmpty(testArg) && systemBreadcrumbs.TryGetValue(testArg, out var breadcrumb)) |
| 89 | + { |
| 90 | + // received before OnAppearing |
| 91 | + CaptureSystemBreadcrumb(testArg, breadcrumb); |
| 92 | + Kill(); |
| 93 | + } |
| 94 | + else if (HasTestArg("None")) |
| 95 | + { |
| 96 | + Kill(); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + public static void Kill() |
| 101 | + { |
| 102 | + SentrySdk.Close(); |
| 103 | + |
| 104 | +#if ANDROID |
| 105 | + // prevent auto-restart |
| 106 | + Platform.CurrentActivity?.FinishAffinity(); |
| 107 | + Process.KillProcess(Process.MyPid()); |
| 108 | +#elif IOS |
| 109 | + System.Environment.Exit(0); |
| 110 | +#endif |
| 111 | + } |
14 | 112 | } |
0 commit comments