FPRDiagnosticsTest.m 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright 2020 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 <XCTest/XCTest.h>
  15. #import "FirebasePerformance/Sources/Common/FPRDiagnostics.h"
  16. #import "FirebasePerformance/Sources/Common/FPRDiagnostics_Private.h"
  17. #import "FirebasePerformance/Sources/Configurations/FPRConfigurations+Private.h"
  18. #import "FirebasePerformance/Sources/Configurations/FPRConfigurations.h"
  19. #import "FirebasePerformance/Tests/Unit/Fakes/FPRFakeConfigurations.h"
  20. static BOOL classEmitDiagnosticsCalled = NO;
  21. @interface FPRDiagnosticsTestClass : NSObject <FPRDiagnosticsProtocol>
  22. @property(nonatomic, assign) BOOL instanceEmitDiagnosticsCalled;
  23. @end
  24. @implementation FPRDiagnosticsTestClass
  25. + (void)emitDiagnostics {
  26. classEmitDiagnosticsCalled = YES;
  27. }
  28. - (void)emitDiagnostics {
  29. self.instanceEmitDiagnosticsCalled = YES;
  30. }
  31. - (NSString *)description {
  32. FPRAssert(NO, @"You should never describe this class! Noooooooo!");
  33. return [super description];
  34. }
  35. @end
  36. @interface FPRDiagnosticsTest : XCTestCase
  37. @end
  38. @implementation FPRDiagnosticsTest
  39. /** Asserts using NSAssert. */
  40. - (void)regularAssert {
  41. // Yes, technically this will fail once in a while--done this way to avoid compiler optimizations.
  42. NSAssert(arc4random_uniform(UINT32_MAX) > 0, @"A regular assert failure!");
  43. }
  44. /** Asserts using FPRAssert. */
  45. - (void)fancyAssert {
  46. // Yes, technically this will fail once in a while--done this way to avoid compiler optimizations.
  47. FPRAssert(arc4random_uniform(UINT32_MAX) > 0, @"A fancy assert failure!");
  48. }
  49. /** Tests that FPRAssert's execution time is less than 0.001s. */
  50. - (void)testFPRAssertSpeed {
  51. NSDate *start = [NSDate date];
  52. XCTAssertNoThrow([self regularAssert]);
  53. NSDate *end = [NSDate date];
  54. start = [NSDate date];
  55. XCTAssertNoThrow([self fancyAssert]);
  56. end = [NSDate date];
  57. NSTimeInterval fancyAssertTime = [end timeIntervalSinceDate:start];
  58. XCTAssertLessThan(fancyAssertTime, 0.001);
  59. }
  60. /** Tests that FPRAssert actually asserts (when NS_BLOCK_ASSERTIONS=0|undefined). */
  61. - (void)testFPRAssert {
  62. XCTAssertThrows({FPRAssert(NO, @"This is a failed assert!")});
  63. }
  64. /** Tests emit diagnostics methods. */
  65. - (void)testEmitDiagnostics {
  66. FPRDiagnosticsTestClass *testObject = [[FPRDiagnosticsTestClass alloc] init];
  67. FPRFakeConfigurations *fakeConfigs =
  68. [[FPRFakeConfigurations alloc] initWithSources:FPRConfigurationSourceNone];
  69. FPRDiagnostics.configuration = fakeConfigs;
  70. fakeConfigs.diagnosticsEnabled = YES;
  71. #if NS_BLOCK_ASSERTS
  72. XCTAssertNoThrow([testObject description]);
  73. #else
  74. XCTAssertThrows([testObject description]);
  75. #endif
  76. XCTAssertTrue(classEmitDiagnosticsCalled);
  77. XCTAssertTrue(testObject.instanceEmitDiagnosticsCalled);
  78. FPRDiagnostics.configuration = [FPRConfigurations sharedInstance];
  79. }
  80. @end