Avi Drissman | ea1be23 | 2022-09-14 23:29:06 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors |
rohitrao | 4aaf4523 | 2016-12-05 16:02:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Gauthier Ambard | 999088c | 2022-09-13 08:36:57 | [diff] [blame] | 5 | #import "ios/testing/scoped_block_swizzler.h" |
Sylvain Defresne | 14176c9f | 2025-01-23 10:57:23 | [diff] [blame] | 6 | |
Avi Drissman | eac566b0 | 2023-08-18 02:56:21 | [diff] [blame] | 7 | #import "base/apple/foundation_util.h" |
Gauthier Ambard | 999088c | 2022-09-13 08:36:57 | [diff] [blame] | 8 | #import "testing/gtest/include/gtest/gtest.h" |
olivierrobin | 3c03c1c | 2016-12-29 17:35:19 | [diff] [blame] | 9 | #import "testing/gtest_mac.h" |
Gauthier Ambard | 999088c | 2022-09-13 08:36:57 | [diff] [blame] | 10 | #import "testing/platform_test.h" |
rohitrao | 4aaf4523 | 2016-12-05 16:02:55 | [diff] [blame] | 11 | |
| 12 | // Class containing two methods that will be swizzled by the unittests. |
| 13 | @interface ScopedBlockSwizzlerTestClass : NSObject |
| 14 | |
| 15 | // An NSString property that will be accessed by one of the swizzled methods. |
| 16 | @property(nonatomic, copy) NSString* value; |
| 17 | |
| 18 | + (NSString*)classMethodToSwizzle; |
| 19 | - (NSString*)instanceMethodToSwizzle; |
| 20 | @end |
| 21 | |
| 22 | namespace { |
| 23 | |
| 24 | NSString* const kOriginalClassValue = @"Bar"; |
| 25 | NSString* const kSwizzledClassValue = @"Foo"; |
| 26 | NSString* const kOriginalInstanceValue = @"Bizz"; |
| 27 | NSString* const kSwizzledInstanceValue = @"Buzz"; |
| 28 | |
Sylvain Defresne | 64778080 | 2017-10-09 14:59:27 | [diff] [blame] | 29 | using ScopedBlockSwizzlerTest = PlatformTest; |
| 30 | |
rohitrao | 4aaf4523 | 2016-12-05 16:02:55 | [diff] [blame] | 31 | // Tests that swizzling a class method works properly. |
Sylvain Defresne | 64778080 | 2017-10-09 14:59:27 | [diff] [blame] | 32 | TEST_F(ScopedBlockSwizzlerTest, SwizzlingClassMethods) { |
rohitrao | 4aaf4523 | 2016-12-05 16:02:55 | [diff] [blame] | 33 | EXPECT_NSEQ(kOriginalClassValue, |
| 34 | [ScopedBlockSwizzlerTestClass classMethodToSwizzle]); |
| 35 | |
| 36 | { |
Sylvain Defresne | 14176c9f | 2025-01-23 10:57:23 | [diff] [blame] | 37 | id block = ^NSString*(id self) { |
| 38 | return kSwizzledClassValue; |
| 39 | }; |
rohitrao | 4aaf4523 | 2016-12-05 16:02:55 | [diff] [blame] | 40 | ScopedBlockSwizzler swizzler([ScopedBlockSwizzlerTestClass class], |
| 41 | @selector(classMethodToSwizzle), block); |
| 42 | EXPECT_NSEQ(kSwizzledClassValue, |
| 43 | [ScopedBlockSwizzlerTestClass classMethodToSwizzle]); |
| 44 | } |
| 45 | |
| 46 | EXPECT_NSEQ(kOriginalClassValue, |
| 47 | [ScopedBlockSwizzlerTestClass classMethodToSwizzle]); |
| 48 | } |
| 49 | |
| 50 | // Tests that swizzling an instance method works properly. |
Sylvain Defresne | 64778080 | 2017-10-09 14:59:27 | [diff] [blame] | 51 | TEST_F(ScopedBlockSwizzlerTest, SwizzlingInstanceMethod) { |
liaoyuke | 0d45a3e | 2017-05-17 16:25:11 | [diff] [blame] | 52 | ScopedBlockSwizzlerTestClass* target = |
| 53 | [[ScopedBlockSwizzlerTestClass alloc] init]; |
| 54 | target.value = kSwizzledInstanceValue; |
rohitrao | 4aaf4523 | 2016-12-05 16:02:55 | [diff] [blame] | 55 | |
| 56 | EXPECT_NSEQ(kOriginalInstanceValue, [target instanceMethodToSwizzle]); |
Federica Germinario | 6c19d4f | 2024-09-11 10:00:19 | [diff] [blame] | 57 | EXPECT_NSNE([target instanceMethodToSwizzle], kSwizzledInstanceValue); |
rohitrao | 4aaf4523 | 2016-12-05 16:02:55 | [diff] [blame] | 58 | |
| 59 | { |
| 60 | id block = ^NSString*(id self) { |
Avi Drissman | eac566b0 | 2023-08-18 02:56:21 | [diff] [blame] | 61 | return base::apple::ObjCCastStrict<ScopedBlockSwizzlerTestClass>(self) |
rohitrao | 4aaf4523 | 2016-12-05 16:02:55 | [diff] [blame] | 62 | .value; |
| 63 | }; |
| 64 | ScopedBlockSwizzler swizzler([ScopedBlockSwizzlerTestClass class], |
| 65 | @selector(instanceMethodToSwizzle), block); |
| 66 | EXPECT_NSEQ(kSwizzledInstanceValue, [target instanceMethodToSwizzle]); |
| 67 | } |
| 68 | |
| 69 | EXPECT_NSEQ(kOriginalInstanceValue, [target instanceMethodToSwizzle]); |
| 70 | } |
| 71 | |
| 72 | // Tests that calling |ScopedBlockSwizzler::reset()| properly unswizzles the |
| 73 | // method. |
Sylvain Defresne | 64778080 | 2017-10-09 14:59:27 | [diff] [blame] | 74 | TEST_F(ScopedBlockSwizzlerTest, TestReset) { |
rohitrao | 4aaf4523 | 2016-12-05 16:02:55 | [diff] [blame] | 75 | EXPECT_NSEQ(kOriginalClassValue, |
| 76 | [ScopedBlockSwizzlerTestClass classMethodToSwizzle]); |
| 77 | |
Sylvain Defresne | 14176c9f | 2025-01-23 10:57:23 | [diff] [blame] | 78 | id block = ^NSString*(id self) { |
| 79 | return kSwizzledClassValue; |
| 80 | }; |
rohitrao | 4aaf4523 | 2016-12-05 16:02:55 | [diff] [blame] | 81 | std::unique_ptr<ScopedBlockSwizzler> swizzler( |
| 82 | new ScopedBlockSwizzler([ScopedBlockSwizzlerTestClass class], |
| 83 | @selector(classMethodToSwizzle), block)); |
| 84 | EXPECT_NSEQ(kSwizzledClassValue, |
| 85 | [ScopedBlockSwizzlerTestClass classMethodToSwizzle]); |
| 86 | |
| 87 | swizzler.reset(); |
| 88 | EXPECT_NSEQ(kOriginalClassValue, |
| 89 | [ScopedBlockSwizzlerTestClass classMethodToSwizzle]); |
| 90 | } |
| 91 | |
| 92 | } // namespace |
| 93 | |
| 94 | #pragma mark - ScopedBlockSwizzlerTestClass |
| 95 | |
| 96 | @implementation ScopedBlockSwizzlerTestClass |
| 97 | |
| 98 | @synthesize value = _value; |
| 99 | |
| 100 | + (NSString*)classMethodToSwizzle { |
| 101 | return kOriginalClassValue; |
| 102 | } |
| 103 | |
| 104 | - (NSString*)instanceMethodToSwizzle { |
| 105 | return kOriginalInstanceValue; |
| 106 | } |
| 107 | |
| 108 | @end |