FPRObjectInstrumentorTest.m 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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/FPRObjectInstrumentor.h"
  16. @interface FPRObjectInstrumentorTest : XCTestCase
  17. @end
  18. @implementation FPRObjectInstrumentorTest
  19. /** Exists only as a donor method. */
  20. - (void)donorMethod {
  21. }
  22. /** Tests initialization works. */
  23. - (void)testInitWithObject {
  24. NSObject *object = [[NSObject alloc] init];
  25. FPRObjectInstrumentor *instrumentor = [[FPRObjectInstrumentor alloc] initWithObject:object];
  26. XCTAssertNotNil(instrumentor);
  27. XCTAssertNotNil(instrumentor.instrumentedObject);
  28. XCTAssertFalse(instrumentor.hasModifications);
  29. }
  30. #ifdef TODO
  31. // On Xcode 15, this test fails
  32. /** Tests copying a selector that's not present on the target object. */
  33. - (void)testCopySelectorFromClassThatModifies {
  34. NSObject *object = [[NSObject alloc] init];
  35. FPRObjectInstrumentor *instrumentor = [[FPRObjectInstrumentor alloc] initWithObject:object];
  36. __weak FPRObjectInstrumentor *weakInstrumentor = instrumentor;
  37. [instrumentor copySelector:@selector(donorMethod) fromClass:[self class] isClassSelector:NO];
  38. XCTAssertTrue([instrumentor hasModifications]);
  39. [instrumentor swizzle];
  40. XCTAssertNoThrow([object performSelector:@selector(donorMethod) withObject:nil]);
  41. XCTAssertNotEqual([object class], [NSObject class]);
  42. XCTAssertTrue([[object class] isSubclassOfClass:[NSObject class]]);
  43. instrumentor = nil;
  44. XCTAssertNil(weakInstrumentor);
  45. XCTAssertNoThrow([(GULSwizzledObject *)object gul_class]);
  46. XCTAssertEqual([object class], [(GULSwizzledObject *)object gul_class]);
  47. XCTAssertNotNil([(GULSwizzledObject *)object gul_objectSwizzler]);
  48. }
  49. #endif
  50. /** Tests copying a selector that already exists on the object doesn't work. */
  51. - (void)testCopySelectorFromClassThatDoesNotModify {
  52. NSObject *object = [[NSObject alloc] init];
  53. FPRObjectInstrumentor *instrumentor = [[FPRObjectInstrumentor alloc] initWithObject:object];
  54. [instrumentor copySelector:@selector(description) fromClass:[self class] isClassSelector:NO];
  55. XCTAssertFalse([instrumentor hasModifications]);
  56. [instrumentor swizzle];
  57. XCTAssertEqual([object class], [NSObject class]);
  58. }
  59. @end