| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/mac/foundation_util.h" | 5 #include "base/mac/foundation_util.h" |
| 6 #import "base/mac/scoped_nsobject.h" | |
| 7 #include "ios/chrome/test/base/scoped_block_swizzler.h" | 6 #include "ios/chrome/test/base/scoped_block_swizzler.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
| 9 #import "testing/gtest_mac.h" | 8 #import "testing/gtest_mac.h" |
| 10 #include "testing/platform_test.h" | 9 #include "testing/platform_test.h" |
| 11 | 10 |
| 11 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 12 #error "This file requires ARC support." |
| 13 #endif |
| 14 |
| 12 // Class containing two methods that will be swizzled by the unittests. | 15 // Class containing two methods that will be swizzled by the unittests. |
| 13 @interface ScopedBlockSwizzlerTestClass : NSObject | 16 @interface ScopedBlockSwizzlerTestClass : NSObject |
| 14 | 17 |
| 15 // An NSString property that will be accessed by one of the swizzled methods. | 18 // An NSString property that will be accessed by one of the swizzled methods. |
| 16 @property(nonatomic, copy) NSString* value; | 19 @property(nonatomic, copy) NSString* value; |
| 17 | 20 |
| 18 + (NSString*)classMethodToSwizzle; | 21 + (NSString*)classMethodToSwizzle; |
| 19 - (NSString*)instanceMethodToSwizzle; | 22 - (NSString*)instanceMethodToSwizzle; |
| 20 @end | 23 @end |
| 21 | 24 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 38 EXPECT_NSEQ(kSwizzledClassValue, | 41 EXPECT_NSEQ(kSwizzledClassValue, |
| 39 [ScopedBlockSwizzlerTestClass classMethodToSwizzle]); | 42 [ScopedBlockSwizzlerTestClass classMethodToSwizzle]); |
| 40 } | 43 } |
| 41 | 44 |
| 42 EXPECT_NSEQ(kOriginalClassValue, | 45 EXPECT_NSEQ(kOriginalClassValue, |
| 43 [ScopedBlockSwizzlerTestClass classMethodToSwizzle]); | 46 [ScopedBlockSwizzlerTestClass classMethodToSwizzle]); |
| 44 } | 47 } |
| 45 | 48 |
| 46 // Tests that swizzling an instance method works properly. | 49 // Tests that swizzling an instance method works properly. |
| 47 TEST(ScopedBlockSwizzlerTest, SwizzlingInstanceMethod) { | 50 TEST(ScopedBlockSwizzlerTest, SwizzlingInstanceMethod) { |
| 48 base::scoped_nsobject<ScopedBlockSwizzlerTestClass> target( | 51 ScopedBlockSwizzlerTestClass* target = |
| 49 [[ScopedBlockSwizzlerTestClass alloc] init]); | 52 [[ScopedBlockSwizzlerTestClass alloc] init]; |
| 50 target.get().value = kSwizzledInstanceValue; | 53 target.value = kSwizzledInstanceValue; |
| 51 | 54 |
| 52 EXPECT_NSEQ(kOriginalInstanceValue, [target instanceMethodToSwizzle]); | 55 EXPECT_NSEQ(kOriginalInstanceValue, [target instanceMethodToSwizzle]); |
| 53 EXPECT_FALSE([[target instanceMethodToSwizzle] | 56 EXPECT_FALSE([[target instanceMethodToSwizzle] |
| 54 isEqualToString:kSwizzledInstanceValue]); | 57 isEqualToString:kSwizzledInstanceValue]); |
| 55 | 58 |
| 56 { | 59 { |
| 57 id block = ^NSString*(id self) { | 60 id block = ^NSString*(id self) { |
| 58 return base::mac::ObjCCastStrict<ScopedBlockSwizzlerTestClass>(self) | 61 return base::mac::ObjCCastStrict<ScopedBlockSwizzlerTestClass>(self) |
| 59 .value; | 62 .value; |
| 60 }; | 63 }; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 | 97 |
| 95 + (NSString*)classMethodToSwizzle { | 98 + (NSString*)classMethodToSwizzle { |
| 96 return kOriginalClassValue; | 99 return kOriginalClassValue; |
| 97 } | 100 } |
| 98 | 101 |
| 99 - (NSString*)instanceMethodToSwizzle { | 102 - (NSString*)instanceMethodToSwizzle { |
| 100 return kOriginalInstanceValue; | 103 return kOriginalInstanceValue; |
| 101 } | 104 } |
| 102 | 105 |
| 103 @end | 106 @end |
| OLD | NEW |