FPRInstrumentTest.m 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. - (void)testDeregisterInstrumentors {
  56. FPRInstrument *instrument = [[FPRInstrument alloc] init];
  57. FPRClassInstrumentor *classInstrumentor =
  58. [[FPRClassInstrumentor alloc] initWithClass:[NSObject class]];
  59. FPRSelectorInstrumentor *selectorInstrumentor =
  60. [classInstrumentor instrumentorForInstanceSelector:@selector(description)];
  61. [selectorInstrumentor setReplacingBlock:^NSString *(id _self) {
  62. return @"testing";
  63. }];
  64. [instrument registerClassInstrumentor:classInstrumentor];
  65. [classInstrumentor swizzle];
  66. XCTAssertGreaterThan(instrument.classInstrumentors.count, 0);
  67. XCTAssertGreaterThan(instrument.instrumentedClasses.count, 0);
  68. [instrument deregisterInstrumentors];
  69. XCTAssertEqual(instrument.classInstrumentors.count, 0);
  70. XCTAssertEqual(instrument.instrumentedClasses.count, 0);
  71. [classInstrumentor unswizzle];
  72. }
  73. @end