FPRObjectInstrumentorTest.m 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. /** Tests copying a selector that's not present on the target object. */
  31. - (void)testCopySelectorFromClassThatModifies {
  32. NSObject *object = [[NSObject alloc] init];
  33. FPRObjectInstrumentor *instrumentor = [[FPRObjectInstrumentor alloc] initWithObject:object];
  34. __weak FPRObjectInstrumentor *weakInstrumentor = instrumentor;
  35. [instrumentor copySelector:@selector(donorMethod) fromClass:[self class] isClassSelector:NO];
  36. XCTAssertTrue([instrumentor hasModifications]);
  37. [instrumentor swizzle];
  38. XCTAssertNoThrow([object performSelector:@selector(donorMethod) withObject:nil]);
  39. XCTAssertNotEqual([object class], [NSObject class]);
  40. XCTAssertTrue([[object class] isSubclassOfClass:[NSObject class]]);
  41. instrumentor = nil;
  42. XCTAssertNil(weakInstrumentor);
  43. XCTAssertNotNil([(FPRSwizzledObject *)object gul_objectSwizzler]);
  44. XCTAssertNoThrow([(FPRSwizzledObject *)object gul_class]);
  45. XCTAssertEqual([object class], [(FPRSwizzledObject *)object gul_class]);
  46. }
  47. /** Tests copying a selector that already exists on the object doesn't work. */
  48. - (void)testCopySelectorFromClassThatDoesNotModify {
  49. NSObject *object = [[NSObject alloc] init];
  50. FPRObjectInstrumentor *instrumentor = [[FPRObjectInstrumentor alloc] initWithObject:object];
  51. [instrumentor copySelector:@selector(description) fromClass:[self class] isClassSelector:NO];
  52. XCTAssertFalse([instrumentor hasModifications]);
  53. [instrumentor swizzle];
  54. XCTAssertEqual([object class], [NSObject class]);
  55. }
  56. @end