FPRInstrumentTest.m 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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/Instrumentation/FPRClassInstrumentor.h"
  16. #import "FirebasePerformance/Sources/Instrumentation/FPRInstrument.h"
  17. #import "FirebasePerformance/Sources/Instrumentation/FPRInstrument_Private.h"
  18. #import "FirebasePerformance/Sources/Instrumentation/FPRSelectorInstrumentor.h"
  19. #import <OCMock/OCMock.h>
  20. @interface FPRInstrumentTest : XCTestCase
  21. @end
  22. @implementation FPRInstrumentTest
  23. - (void)testInit {
  24. FPRInstrument *instrument = [[FPRInstrument alloc] init];
  25. XCTAssertNotNil(instrument);
  26. XCTAssertNotNil(instrument.classInstrumentors);
  27. XCTAssertNotNil(instrument.instrumentedClasses);
  28. }
  29. - (void)testRegisterInstrumentorsThrows {
  30. FPRInstrument *instrument = [[FPRInstrument alloc] init];
  31. XCTAssertThrows([instrument registerInstrumentors]);
  32. }
  33. - (void)testRegisterClassInstrumentor {
  34. FPRInstrument *instrument = [[FPRInstrument alloc] init];
  35. FPRClassInstrumentor *instrumentor =
  36. [[FPRClassInstrumentor alloc] initWithClass:[NSObject class]];
  37. BOOL success = [instrument registerClassInstrumentor:instrumentor];
  38. XCTAssertTrue(success);
  39. XCTAssertGreaterThan(instrument.classInstrumentors.count, 0);
  40. XCTAssertGreaterThan(instrument.instrumentedClasses.count, 0);
  41. [instrument deregisterInstrumentors];
  42. }
  43. - (void)testRegisterAlreadyRegisteredClassInstrumentor {
  44. FPRInstrument *instrument = [[FPRInstrument alloc] init];
  45. FPRClassInstrumentor *instrumentor =
  46. [[FPRClassInstrumentor alloc] initWithClass:[NSObject class]];
  47. FPRClassInstrumentor *secondInstrumentor =
  48. [[FPRClassInstrumentor alloc] initWithClass:[NSObject class]];
  49. BOOL succeedsTheFirstTime = [instrument registerClassInstrumentor:instrumentor];
  50. BOOL succeedsTheSecondTime = [instrument registerClassInstrumentor:secondInstrumentor];
  51. XCTAssertTrue(succeedsTheFirstTime);
  52. XCTAssertFalse(succeedsTheSecondTime);
  53. [instrument deregisterInstrumentors];
  54. }
  55. #pragma mark - Unswizzle based tests
  56. - (void)testDeregisterInstrumentors {
  57. FPRInstrument *instrument = [[FPRInstrument alloc] init];
  58. FPRClassInstrumentor *classInstrumentor =
  59. [[FPRClassInstrumentor alloc] initWithClass:[NSObject class]];
  60. FPRSelectorInstrumentor *selectorInstrumentor =
  61. [classInstrumentor instrumentorForInstanceSelector:@selector(description)];
  62. [selectorInstrumentor setReplacingBlock:^NSString *(id _self) {
  63. return @"testing";
  64. }];
  65. [instrument registerClassInstrumentor:classInstrumentor];
  66. [classInstrumentor swizzle];
  67. XCTAssertGreaterThan(instrument.classInstrumentors.count, 0);
  68. XCTAssertGreaterThan(instrument.instrumentedClasses.count, 0);
  69. [instrument deregisterInstrumentors];
  70. XCTAssertEqual(instrument.classInstrumentors.count, 0);
  71. XCTAssertEqual(instrument.instrumentedClasses.count, 0);
  72. [classInstrumentor unswizzle];
  73. }
  74. @end