FPRClassInstrumentorTest.m 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. /** Tests swizzling an instance selector. */
  50. - (void)testSwizzleInstanceSelector {
  51. FPRClassInstrumentor *classInstrumentor =
  52. [[FPRClassInstrumentor alloc] initWithClass:[NSObject class]];
  53. FPRSelectorInstrumentor *selectorInstrumentor =
  54. [classInstrumentor instrumentorForInstanceSelector:@selector(description)];
  55. NSString *expectedString = @"Swizzled!";
  56. [selectorInstrumentor setReplacingBlock:^NSString *(id _self) {
  57. return expectedString;
  58. }];
  59. [classInstrumentor swizzle];
  60. XCTAssertEqualObjects([[[NSObject alloc] init] description], expectedString);
  61. [classInstrumentor unswizzle];
  62. }
  63. /** Tests unswizzling an instance selector. */
  64. - (void)testUnswizzleInstanceSelector {
  65. FPRClassInstrumentor *classInstrumentor =
  66. [[FPRClassInstrumentor alloc] initWithClass:[NSObject class]];
  67. FPRSelectorInstrumentor *selectorInstrumentor =
  68. [classInstrumentor instrumentorForInstanceSelector:@selector(description)];
  69. [selectorInstrumentor setReplacingBlock:^NSString *(id _self) {
  70. return @"Swizzled!";
  71. }];
  72. [classInstrumentor swizzle];
  73. [classInstrumentor unswizzle];
  74. XCTAssertEqual(classInstrumentor.selectorInstrumentors.count, 0);
  75. }
  76. /** Tests swizzling a class selector. */
  77. - (void)testSwizzleClassSelector {
  78. FPRClassInstrumentor *classInstrumentor =
  79. [[FPRClassInstrumentor alloc] initWithClass:[NSObject class]];
  80. FPRSelectorInstrumentor *selectorInstrumentor =
  81. [classInstrumentor instrumentorForClassSelector:@selector(description)];
  82. [selectorInstrumentor setReplacingBlock:^NSString *(id _self) {
  83. return @"Swizzled!";
  84. }];
  85. [classInstrumentor swizzle];
  86. XCTAssertEqualObjects([NSObject description], @"Swizzled!");
  87. [classInstrumentor unswizzle];
  88. }
  89. /** Tests unswizzling a class selector. */
  90. - (void)testUnswizzleClassSelector {
  91. FPRClassInstrumentor *classInstrumentor =
  92. [[FPRClassInstrumentor alloc] initWithClass:[NSObject class]];
  93. FPRSelectorInstrumentor *selectorInstrumentor =
  94. [classInstrumentor instrumentorForClassSelector:@selector(description)];
  95. [selectorInstrumentor setReplacingBlock:^NSString *(id _self) {
  96. return @"Swizzled!";
  97. }];
  98. [classInstrumentor swizzle];
  99. [classInstrumentor unswizzle];
  100. XCTAssertEqual(classInstrumentor.selectorInstrumentors.count, 0);
  101. }
  102. /** Tests swizzling an instance method with a call-through that doesn't return anything. */
  103. - (void)testVoidReturnInstanceSelector {
  104. __block BOOL wasInvoked = NO;
  105. FPRClassInstrumentor *classInstrumentor =
  106. [[FPRClassInstrumentor alloc] initWithClass:[NSObject class]];
  107. SEL instrumentedSelector = @selector(doesNotRecognizeSelector:);
  108. FPRSelectorInstrumentor *selectorInstrumentor =
  109. [classInstrumentor instrumentorForInstanceSelector:instrumentedSelector];
  110. IMP originalIMP = selectorInstrumentor.currentIMP;
  111. [selectorInstrumentor setReplacingBlock:^(id object, SEL selector) {
  112. wasInvoked = YES;
  113. typedef void (*OriginalImp)(id, SEL, SEL);
  114. ((OriginalImp)originalIMP)(object, instrumentedSelector, selector);
  115. }];
  116. [classInstrumentor swizzle];
  117. XCTAssertThrows([[[NSObject alloc] init] doesNotRecognizeSelector:@selector(setValue:forKey:)]);
  118. XCTAssertTrue(wasInvoked);
  119. [classInstrumentor unswizzle];
  120. }
  121. /** Tests swizzling a class method with a call-through that doesn't return anything. */
  122. - (void)testVoidReturnClassSelector {
  123. __block BOOL wasInvoked = NO;
  124. FPRClassInstrumentor *classInstrumentor =
  125. [[FPRClassInstrumentor alloc] initWithClass:[NSObject class]];
  126. SEL selector = @selector(setVersion:);
  127. FPRSelectorInstrumentor *selectorInstrumentor =
  128. [classInstrumentor instrumentorForClassSelector:selector];
  129. IMP originalIMP = selectorInstrumentor.currentIMP;
  130. [selectorInstrumentor setReplacingBlock:^(id object, NSInteger version) {
  131. wasInvoked = YES;
  132. typedef void (*OriginalImp)(Class, SEL, NSInteger);
  133. ((OriginalImp)originalIMP)(object, selector, version);
  134. }];
  135. [classInstrumentor swizzle];
  136. [NSObject setVersion:1];
  137. XCTAssertTrue(wasInvoked);
  138. [classInstrumentor unswizzle];
  139. }
  140. @end