FPRClassInstrumentorTest.m 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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/FPRClassInstrumentor_Private.h"
  17. #import "FirebasePerformance/Sources/Instrumentation/FPRSelectorInstrumentor.h"
  18. @interface FPRClassInstrumentorTest : XCTestCase
  19. @end
  20. @implementation FPRClassInstrumentorTest
  21. /** Tests calling the designated initializer. */
  22. - (void)testDesignatedInitializer {
  23. FPRClassInstrumentor *classInstrumentor =
  24. [[FPRClassInstrumentor alloc] initWithClass:[NSObject class]];
  25. XCTAssertEqual(classInstrumentor.instrumentedClass, [NSObject class]);
  26. }
  27. /** Tests building and adding a selector instrumentor for an instance method. */
  28. - (void)testBuildAndAddSelectorInstrumentorForInstanceSelector {
  29. FPRClassInstrumentor *classInstrumentor =
  30. [[FPRClassInstrumentor alloc] initWithClass:[NSObject class]];
  31. [classInstrumentor instrumentorForInstanceSelector:@selector(description)];
  32. XCTAssertEqual([classInstrumentor selectorInstrumentors].count, 1);
  33. }
  34. /** Tests building and adding a selector instrumentor for a class method. */
  35. - (void)testBuildAndAddSelectorInstrumentorForClassSelector {
  36. FPRClassInstrumentor *classInstrumentor =
  37. [[FPRClassInstrumentor alloc] initWithClass:[NSObject class]];
  38. [classInstrumentor instrumentorForClassSelector:@selector(description)];
  39. XCTAssertEqual([classInstrumentor selectorInstrumentors].count, 1);
  40. }
  41. /** Tests that you cannot attempt to instrument the same selector twice. */
  42. - (void)testCannotInstrumentSameSelectorTwice {
  43. FPRClassInstrumentor *classInstrumentor =
  44. [[FPRClassInstrumentor alloc] initWithClass:[NSObject class]];
  45. [classInstrumentor instrumentorForClassSelector:@selector(description)];
  46. XCTAssertThrows([classInstrumentor instrumentorForClassSelector:@selector(description)]);
  47. }
  48. #pragma mark - Unswizzle based tests
  49. #if !SWIFT_PACKAGE
  50. /** Tests swizzling an instance selector. */
  51. - (void)testSwizzleInstanceSelector {
  52. FPRClassInstrumentor *classInstrumentor =
  53. [[FPRClassInstrumentor alloc] initWithClass:[NSObject class]];
  54. FPRSelectorInstrumentor *selectorInstrumentor =
  55. [classInstrumentor instrumentorForInstanceSelector:@selector(description)];
  56. NSString *expectedString = @"Swizzled!";
  57. [selectorInstrumentor setReplacingBlock:^NSString *(id _self) {
  58. return expectedString;
  59. }];
  60. [classInstrumentor swizzle];
  61. XCTAssertEqualObjects([[[NSObject alloc] init] description], expectedString);
  62. [classInstrumentor unswizzle];
  63. }
  64. /** Tests unswizzling an instance selector. */
  65. - (void)testUnswizzleInstanceSelector {
  66. FPRClassInstrumentor *classInstrumentor =
  67. [[FPRClassInstrumentor alloc] initWithClass:[NSObject class]];
  68. FPRSelectorInstrumentor *selectorInstrumentor =
  69. [classInstrumentor instrumentorForInstanceSelector:@selector(description)];
  70. [selectorInstrumentor setReplacingBlock:^NSString *(id _self) {
  71. return @"Swizzled!";
  72. }];
  73. [classInstrumentor swizzle];
  74. [classInstrumentor unswizzle];
  75. XCTAssertEqual(classInstrumentor.selectorInstrumentors.count, 0);
  76. }
  77. /** Tests swizzling a class selector. */
  78. - (void)testSwizzleClassSelector {
  79. FPRClassInstrumentor *classInstrumentor =
  80. [[FPRClassInstrumentor alloc] initWithClass:[NSObject class]];
  81. FPRSelectorInstrumentor *selectorInstrumentor =
  82. [classInstrumentor instrumentorForClassSelector:@selector(description)];
  83. [selectorInstrumentor setReplacingBlock:^NSString *(id _self) {
  84. return @"Swizzled!";
  85. }];
  86. [classInstrumentor swizzle];
  87. XCTAssertEqualObjects([NSObject description], @"Swizzled!");
  88. [classInstrumentor unswizzle];
  89. }
  90. /** Tests unswizzling a class selector. */
  91. - (void)testUnswizzleClassSelector {
  92. FPRClassInstrumentor *classInstrumentor =
  93. [[FPRClassInstrumentor alloc] initWithClass:[NSObject class]];
  94. FPRSelectorInstrumentor *selectorInstrumentor =
  95. [classInstrumentor instrumentorForClassSelector:@selector(description)];
  96. [selectorInstrumentor setReplacingBlock:^NSString *(id _self) {
  97. return @"Swizzled!";
  98. }];
  99. [classInstrumentor swizzle];
  100. [classInstrumentor unswizzle];
  101. XCTAssertEqual(classInstrumentor.selectorInstrumentors.count, 0);
  102. }
  103. /** Tests swizzling an instance method with a call-through that doesn't return anything. */
  104. - (void)testVoidReturnInstanceSelector {
  105. __block BOOL wasInvoked = NO;
  106. FPRClassInstrumentor *classInstrumentor =
  107. [[FPRClassInstrumentor alloc] initWithClass:[NSObject class]];
  108. SEL instrumentedSelector = @selector(doesNotRecognizeSelector:);
  109. FPRSelectorInstrumentor *selectorInstrumentor =
  110. [classInstrumentor instrumentorForInstanceSelector:instrumentedSelector];
  111. IMP originalIMP = selectorInstrumentor.currentIMP;
  112. [selectorInstrumentor setReplacingBlock:^(id object, SEL selector) {
  113. wasInvoked = YES;
  114. typedef void (*OriginalImp)(id, SEL, SEL);
  115. ((OriginalImp)originalIMP)(object, instrumentedSelector, selector);
  116. }];
  117. [classInstrumentor swizzle];
  118. XCTAssertThrows([[[NSObject alloc] init] doesNotRecognizeSelector:@selector(setValue:forKey:)]);
  119. XCTAssertTrue(wasInvoked);
  120. [classInstrumentor unswizzle];
  121. }
  122. /** Tests swizzling a class method with a call-through that doesn't return anything. */
  123. - (void)testVoidReturnClassSelector {
  124. __block BOOL wasInvoked = NO;
  125. FPRClassInstrumentor *classInstrumentor =
  126. [[FPRClassInstrumentor alloc] initWithClass:[NSObject class]];
  127. SEL selector = @selector(setVersion:);
  128. FPRSelectorInstrumentor *selectorInstrumentor =
  129. [classInstrumentor instrumentorForClassSelector:selector];
  130. IMP originalIMP = selectorInstrumentor.currentIMP;
  131. [selectorInstrumentor setReplacingBlock:^(id object, NSInteger version) {
  132. wasInvoked = YES;
  133. typedef void (*OriginalImp)(Class, SEL, NSInteger);
  134. ((OriginalImp)originalIMP)(object, selector, version);
  135. }];
  136. [classInstrumentor swizzle];
  137. [NSObject setVersion:1];
  138. XCTAssertTrue(wasInvoked);
  139. [classInstrumentor unswizzle];
  140. }
  141. #endif // SWIFT_PACKAGE
  142. @end