|
12 | 12 | using ObjCRuntime; |
13 | 13 | #endif |
14 | 14 |
|
| 15 | +#if ENABLE_RUNTIME_DRIFT_CASE_REMOTECONFIG_REALTIME_CUSTOMSIGNALS |
| 16 | +using Firebase.RemoteConfig; |
| 17 | +using Foundation; |
| 18 | +using ObjCRuntime; |
| 19 | +#endif |
| 20 | + |
15 | 21 | #if ENABLE_RUNTIME_DRIFT_CASE_DATABASE_SERVERVALUE_INCREMENT |
16 | 22 | using Firebase.Database; |
17 | 23 | using Foundation; |
@@ -295,6 +301,168 @@ void OnMarshalObjectiveCException(object? sender, MarshalObjectiveCExceptionEven |
295 | 301 | } |
296 | 302 | #endif |
297 | 303 |
|
| 304 | +#if ENABLE_RUNTIME_DRIFT_CASE_REMOTECONFIG_REALTIME_CUSTOMSIGNALS |
| 305 | + static async Task<string> VerifyRemoteConfigRealtimeCustomSignalsAsync() |
| 306 | + { |
| 307 | + const string listenerSelector = "addOnConfigUpdateListener:"; |
| 308 | + const string customSignalsSelector = "setCustomSignals:withCompletion:"; |
| 309 | + |
| 310 | + var listenerSignature = typeof(RemoteConfig).GetMethod( |
| 311 | + nameof(RemoteConfig.AddOnConfigUpdateListener), |
| 312 | + BindingFlags.Instance | BindingFlags.Public, |
| 313 | + binder: null, |
| 314 | + types: new[] { typeof(RemoteConfigUpdateCompletionHandler) }, |
| 315 | + modifiers: null); |
| 316 | + if (listenerSignature is null) |
| 317 | + { |
| 318 | + throw new InvalidOperationException( |
| 319 | + $"Expected managed API '{nameof(RemoteConfig.AddOnConfigUpdateListener)}({typeof(RemoteConfigUpdateCompletionHandler).FullName})' was not found."); |
| 320 | + } |
| 321 | + |
| 322 | + if (listenerSignature.ReturnType != typeof(ConfigUpdateListenerRegistration)) |
| 323 | + { |
| 324 | + throw new InvalidOperationException( |
| 325 | + $"Managed signature regression: expected '{nameof(RemoteConfig.AddOnConfigUpdateListener)}' to return '{typeof(ConfigUpdateListenerRegistration).FullName}', observed '{listenerSignature.ReturnType.FullName}'."); |
| 326 | + } |
| 327 | + |
| 328 | + var customSignalsSignature = typeof(RemoteConfig).GetMethod( |
| 329 | + nameof(RemoteConfig.SetCustomSignals), |
| 330 | + BindingFlags.Instance | BindingFlags.Public, |
| 331 | + binder: null, |
| 332 | + types: new[] { typeof(NSDictionary<NSString, NSObject>), typeof(Action<NSError>) }, |
| 333 | + modifiers: null); |
| 334 | + if (customSignalsSignature is null) |
| 335 | + { |
| 336 | + throw new InvalidOperationException( |
| 337 | + $"Expected managed API '{nameof(RemoteConfig.SetCustomSignals)}({typeof(NSDictionary<NSString, NSObject>).FullName}, {typeof(Action<NSError>).FullName})' was not found."); |
| 338 | + } |
| 339 | + |
| 340 | + var remoteConfig = RemoteConfig.SharedInstance; |
| 341 | + if (remoteConfig is null) |
| 342 | + { |
| 343 | + throw new InvalidOperationException("Firebase.RemoteConfig.RemoteConfig.SharedInstance returned null after App.Configure()."); |
| 344 | + } |
| 345 | + |
| 346 | + if (!remoteConfig.RespondsToSelector(new Selector(listenerSelector))) |
| 347 | + { |
| 348 | + throw new InvalidOperationException($"Native FIRRemoteConfig does not respond to expected selector '{listenerSelector}'."); |
| 349 | + } |
| 350 | + |
| 351 | + if (!remoteConfig.RespondsToSelector(new Selector(customSignalsSelector))) |
| 352 | + { |
| 353 | + throw new InvalidOperationException($"Native FIRRemoteConfig does not respond to expected selector '{customSignalsSelector}'."); |
| 354 | + } |
| 355 | + |
| 356 | + var listenerInvoked = false; |
| 357 | + var listenerUpdateWasNull = false; |
| 358 | + NSError? listenerError = null; |
| 359 | + var customSignalsCompletionInvoked = false; |
| 360 | + NSError? customSignalsCompletionError = null; |
| 361 | + NSException? marshaledException = null; |
| 362 | + MarshalObjectiveCExceptionMode? marshaledExceptionMode = null; |
| 363 | + var customSignalsCompletionSource = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously); |
| 364 | + |
| 365 | + void OnMarshalObjectiveCException(object? sender, MarshalObjectiveCExceptionEventArgs args) |
| 366 | + { |
| 367 | + marshaledException ??= args.Exception; |
| 368 | + marshaledExceptionMode ??= args.ExceptionMode; |
| 369 | + } |
| 370 | + |
| 371 | + Runtime.MarshalObjectiveCException += OnMarshalObjectiveCException; |
| 372 | + try |
| 373 | + { |
| 374 | + using var signalKey = new NSString("codex_signal"); |
| 375 | + using var signalValue = new NSString("enabled"); |
| 376 | + using var customSignals = NSDictionary<NSString, NSObject>.FromObjectsAndKeys( |
| 377 | + new NSObject[] { signalValue }, |
| 378 | + new[] { signalKey }, |
| 379 | + 1); |
| 380 | + |
| 381 | + ConfigUpdateListenerRegistration registration; |
| 382 | + try |
| 383 | + { |
| 384 | + registration = remoteConfig.AddOnConfigUpdateListener((configUpdate, error) => |
| 385 | + { |
| 386 | + listenerInvoked = true; |
| 387 | + listenerUpdateWasNull = configUpdate is null; |
| 388 | + listenerError = error; |
| 389 | + }); |
| 390 | + } |
| 391 | + catch (ObjCException ex) |
| 392 | + { |
| 393 | + throw new InvalidOperationException( |
| 394 | + $"Selector '{listenerSelector}' should not throw after the missing binding is added, but observed {ex.GetType().FullName}. " + |
| 395 | + $"Listener delegate type: {typeof(RemoteConfigUpdateCompletionHandler).FullName}. " + |
| 396 | + $"NSException.Name: {FormatDetail(marshaledException?.Name?.ToString())}. " + |
| 397 | + $"NSException.Reason: {FormatDetail(marshaledException?.Reason)}. " + |
| 398 | + $"Marshal mode: {FormatDetail(marshaledExceptionMode?.ToString())}.", |
| 399 | + ex); |
| 400 | + } |
| 401 | + |
| 402 | + using (registration) |
| 403 | + { |
| 404 | + if (registration is null) |
| 405 | + { |
| 406 | + throw new InvalidOperationException($"Selector '{listenerSelector}' returned null registration."); |
| 407 | + } |
| 408 | + |
| 409 | + registration.Remove(); |
| 410 | + } |
| 411 | + |
| 412 | + try |
| 413 | + { |
| 414 | + remoteConfig.SetCustomSignals(customSignals, error => |
| 415 | + { |
| 416 | + customSignalsCompletionInvoked = true; |
| 417 | + customSignalsCompletionError = error; |
| 418 | + customSignalsCompletionSource.TrySetResult(true); |
| 419 | + }); |
| 420 | + } |
| 421 | + catch (ObjCException ex) |
| 422 | + { |
| 423 | + throw new InvalidOperationException( |
| 424 | + $"Selector '{customSignalsSelector}' should not throw after the missing binding is added, but observed {ex.GetType().FullName}. " + |
| 425 | + $"Signals dictionary type: {customSignals.GetType().FullName}. Completion delegate type: {typeof(Action<NSError>).FullName}. " + |
| 426 | + $"NSException.Name: {FormatDetail(marshaledException?.Name?.ToString())}. " + |
| 427 | + $"NSException.Reason: {FormatDetail(marshaledException?.Reason)}. " + |
| 428 | + $"Marshal mode: {FormatDetail(marshaledExceptionMode?.ToString())}.", |
| 429 | + ex); |
| 430 | + } |
| 431 | + |
| 432 | + var completedTask = await Task.WhenAny(customSignalsCompletionSource.Task, Task.Delay(AsyncTimeout)); |
| 433 | + if (marshaledException is not null) |
| 434 | + { |
| 435 | + throw new InvalidOperationException( |
| 436 | + $"RemoteConfig missing-surface selectors completed, but Runtime.MarshalObjectiveCException captured unexpected NSException.Name '{marshaledException.Name}'. " + |
| 437 | + $"Reason: {FormatDetail(marshaledException.Reason)}. Marshal mode: {FormatDetail(marshaledExceptionMode?.ToString())}."); |
| 438 | + } |
| 439 | + |
| 440 | + if (completedTask != customSignalsCompletionSource.Task) |
| 441 | + { |
| 442 | + throw new TimeoutException( |
| 443 | + $"Selector '{customSignalsSelector}' did not invoke its completion callback within {AsyncTimeout.TotalSeconds} seconds. " + |
| 444 | + $"Signals dictionary type: {customSignals.GetType().FullName}. Completion delegate type: {typeof(Action<NSError>).FullName}."); |
| 445 | + } |
| 446 | + |
| 447 | + return |
| 448 | + $"Selectors '{listenerSelector}' and '{customSignalsSelector}' completed without ObjC exception after the missing bindings were added. " + |
| 449 | + $"Listener delegate type: {typeof(RemoteConfigUpdateCompletionHandler).FullName}. " + |
| 450 | + $"Registration type: {typeof(ConfigUpdateListenerRegistration).FullName}. " + |
| 451 | + $"Update type: {typeof(RemoteConfigUpdate).FullName}. " + |
| 452 | + $"Signals dictionary type: {customSignals.GetType().FullName}. " + |
| 453 | + $"Custom signals callback observed: {completedTask == customSignalsCompletionSource.Task}. " + |
| 454 | + $"Custom signals callback invoked: {customSignalsCompletionInvoked}. " + |
| 455 | + $"Custom signals NSError: {FormatDetail(customSignalsCompletionError?.LocalizedDescription)}. " + |
| 456 | + $"Listener invoked: {listenerInvoked}. Listener update was null: {listenerUpdateWasNull}. " + |
| 457 | + $"Listener NSError: {FormatDetail(listenerError?.LocalizedDescription)}."; |
| 458 | + } |
| 459 | + finally |
| 460 | + { |
| 461 | + Runtime.MarshalObjectiveCException -= OnMarshalObjectiveCException; |
| 462 | + } |
| 463 | + } |
| 464 | +#endif |
| 465 | + |
298 | 466 | #if ENABLE_RUNTIME_DRIFT_CASE_DATABASE_SERVERVALUE_INCREMENT |
299 | 467 | static Task<string> VerifyDatabaseServerValueIncrementAsync() |
300 | 468 | { |
|
0 commit comments