FIRCLSMockMetricKitManager.m 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright 2021 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import "Crashlytics/UnitTests/Mocks/FIRCLSMockMetricKitManager.h"
  15. #import "Crashlytics/Crashlytics/Controllers/FIRCLSManagerData.h"
  16. #import "Crashlytics/Crashlytics/Models/FIRCLSFileManager.h"
  17. #import "Crashlytics/UnitTests/Mocks/FIRCLSMockExistingReportManager.h"
  18. #if CLS_METRICKIT_SUPPORTED
  19. @interface FIRCLSMockMetricKitManager ()
  20. @property FBLPromise *metricKitDataAvailable;
  21. @property FIRCLSExistingReportManager *existingReportManager;
  22. @property FIRCLSFileManager *fileManager;
  23. @property FIRCLSManagerData *managerData;
  24. @property BOOL metricKitPromiseFulfilled;
  25. @end
  26. @implementation FIRCLSMockMetricKitManager
  27. - (instancetype)initWithManagerData:(FIRCLSManagerData *)managerData
  28. existingReportManager:(FIRCLSExistingReportManager *)existingReportManager
  29. fileManager:(FIRCLSFileManager *)fileManager {
  30. self = [super initWithManagerData:managerData
  31. existingReportManager:existingReportManager
  32. fileManager:fileManager];
  33. _existingReportManager = existingReportManager;
  34. _fileManager = fileManager;
  35. _managerData = managerData;
  36. _metricKitPromiseFulfilled = NO;
  37. return self;
  38. }
  39. // This mock skips the normal flow of resolving the metricKitDataAvailable promise if there are no
  40. // fatal reports available on the device but otherwise registers as normal.
  41. - (void)registerMetricKitManager API_AVAILABLE(ios(14)) {
  42. [[MXMetricManager sharedManager] addSubscriber:self];
  43. self.metricKitDataAvailable = [FBLPromise pendingPromise];
  44. // If we haven't resolved this promise within three seconds, resolve it now so that we're not
  45. // waiting indefinitely for MetricKit payloads that won't arrive.
  46. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 3 * NSEC_PER_SEC), self.managerData.dispatchQueue,
  47. ^{
  48. @synchronized(self) {
  49. if (!self.metricKitPromiseFulfilled) {
  50. [self.metricKitDataAvailable fulfill:nil];
  51. self.metricKitPromiseFulfilled = YES;
  52. }
  53. }
  54. });
  55. }
  56. @end
  57. #endif