FPRClassInstrumentorTest.m 6.3 KB

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