|
1 | 1 | using System.Reflection; |
2 | 2 |
|
| 3 | +#if ENABLE_RUNTIME_DRIFT_CASE_ANALYTICS_SESSIONIDWITHCOMPLETION |
| 4 | +using Firebase.Analytics; |
| 5 | +using Foundation; |
| 6 | +using ObjCRuntime; |
| 7 | +#endif |
| 8 | + |
3 | 9 | #if ENABLE_RUNTIME_DRIFT_CASE_DATABASE_SERVERVALUE_INCREMENT |
4 | 10 | using Firebase.Database; |
5 | 11 | using Foundation; |
@@ -84,6 +90,100 @@ public static async Task<string> ExecuteConfiguredCaseAsync() |
84 | 90 | ?.Value; |
85 | 91 | } |
86 | 92 |
|
| 93 | +#if ENABLE_RUNTIME_DRIFT_CASE_ANALYTICS_SESSIONIDWITHCOMPLETION |
| 94 | + static async Task<string> VerifyAnalyticsSessionIdWithCompletionAsync() |
| 95 | + { |
| 96 | + const string selector = "sessionIDWithCompletion:"; |
| 97 | + |
| 98 | + var signature = typeof(Analytics).GetMethod( |
| 99 | + nameof(Analytics.SessionIdWithCompletion), |
| 100 | + BindingFlags.Static | BindingFlags.Public, |
| 101 | + binder: null, |
| 102 | + types: new[] { typeof(Action<long, NSError>) }, |
| 103 | + modifiers: null); |
| 104 | + if (signature is null) |
| 105 | + { |
| 106 | + throw new InvalidOperationException( |
| 107 | + $"Expected managed API '{nameof(Analytics.SessionIdWithCompletion)}({typeof(Action<long, NSError>).FullName})' was not found."); |
| 108 | + } |
| 109 | + |
| 110 | + Analytics.SetAnalyticsCollectionEnabled(true); |
| 111 | + Analytics.SetConsent(new Dictionary<ConsentType, ConsentStatus> |
| 112 | + { |
| 113 | + [ConsentType.AnalyticsStorage] = ConsentStatus.Granted, |
| 114 | + [ConsentType.AdStorage] = ConsentStatus.Denied, |
| 115 | + }); |
| 116 | + |
| 117 | + var callbackInvoked = false; |
| 118 | + long callbackSessionId = 0; |
| 119 | + NSError? callbackError = null; |
| 120 | + NSException? marshaledException = null; |
| 121 | + MarshalObjectiveCExceptionMode? marshaledExceptionMode = null; |
| 122 | + var completionSource = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously); |
| 123 | + |
| 124 | + void OnMarshalObjectiveCException(object? sender, MarshalObjectiveCExceptionEventArgs args) |
| 125 | + { |
| 126 | + marshaledException ??= args.Exception; |
| 127 | + marshaledExceptionMode ??= args.ExceptionMode; |
| 128 | + } |
| 129 | + |
| 130 | + Runtime.MarshalObjectiveCException += OnMarshalObjectiveCException; |
| 131 | + try |
| 132 | + { |
| 133 | + try |
| 134 | + { |
| 135 | + Analytics.SessionIdWithCompletion((sessionId, error) => |
| 136 | + { |
| 137 | + callbackInvoked = true; |
| 138 | + callbackSessionId = sessionId; |
| 139 | + callbackError = error; |
| 140 | + completionSource.TrySetResult(true); |
| 141 | + }); |
| 142 | + } |
| 143 | + catch (ObjCException ex) |
| 144 | + { |
| 145 | + throw new InvalidOperationException( |
| 146 | + $"Selector '{selector}' should not throw after the missing binding is added, but observed {ex.GetType().FullName}. " + |
| 147 | + $"Completion delegate type: {typeof(Action<long, NSError>).FullName}. " + |
| 148 | + $"NSException.Name: {FormatDetail(marshaledException?.Name?.ToString())}. " + |
| 149 | + $"NSException.Reason: {FormatDetail(marshaledException?.Reason)}. " + |
| 150 | + $"Marshal mode: {FormatDetail(marshaledExceptionMode?.ToString())}.", |
| 151 | + ex); |
| 152 | + } |
| 153 | + |
| 154 | + var completedTask = await Task.WhenAny(completionSource.Task, Task.Delay(AsyncTimeout)); |
| 155 | + if (completedTask != completionSource.Task) |
| 156 | + { |
| 157 | + throw new TimeoutException( |
| 158 | + $"Selector '{selector}' did not invoke its completion callback within {AsyncTimeout.TotalSeconds} seconds."); |
| 159 | + } |
| 160 | + |
| 161 | + if (!callbackInvoked) |
| 162 | + { |
| 163 | + throw new InvalidOperationException( |
| 164 | + $"Selector '{selector}' completed without throwing, but the completion callback was never marked as invoked."); |
| 165 | + } |
| 166 | + |
| 167 | + if (marshaledException is not null) |
| 168 | + { |
| 169 | + throw new InvalidOperationException( |
| 170 | + $"Selector '{selector}' completed, but Runtime.MarshalObjectiveCException captured unexpected NSException.Name '{marshaledException.Name}'. " + |
| 171 | + $"Reason: {FormatDetail(marshaledException.Reason)}. Marshal mode: {FormatDetail(marshaledExceptionMode?.ToString())}."); |
| 172 | + } |
| 173 | + |
| 174 | + return |
| 175 | + $"Selector '{selector}' invoked its completion callback without ObjC exception. " + |
| 176 | + $"Completion delegate type: {typeof(Action<long, NSError>).FullName}. " + |
| 177 | + $"SessionId: {callbackSessionId}. " + |
| 178 | + $"NSError: {callbackError?.LocalizedDescription ?? "<null>"}."; |
| 179 | + } |
| 180 | + finally |
| 181 | + { |
| 182 | + Runtime.MarshalObjectiveCException -= OnMarshalObjectiveCException; |
| 183 | + } |
| 184 | + } |
| 185 | +#endif |
| 186 | + |
87 | 187 | #if ENABLE_RUNTIME_DRIFT_CASE_DATABASE_SERVERVALUE_INCREMENT |
88 | 188 | static Task<string> VerifyDatabaseServerValueIncrementAsync() |
89 | 189 | { |
|
0 commit comments