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