forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scoped_block_swizzler_unittest.mm
108 lines (83 loc) · 3.49 KB
/
scoped_block_swizzler_unittest.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ios/testing/scoped_block_swizzler.h"
#include "base/mac/foundation_util.h"
#include "testing/gtest/include/gtest/gtest.h"
#import "testing/gtest_mac.h"
#include "testing/platform_test.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
// Class containing two methods that will be swizzled by the unittests.
@interface ScopedBlockSwizzlerTestClass : NSObject
// An NSString property that will be accessed by one of the swizzled methods.
@property(nonatomic, copy) NSString* value;
+ (NSString*)classMethodToSwizzle;
- (NSString*)instanceMethodToSwizzle;
@end
namespace {
NSString* const kOriginalClassValue = @"Bar";
NSString* const kSwizzledClassValue = @"Foo";
NSString* const kOriginalInstanceValue = @"Bizz";
NSString* const kSwizzledInstanceValue = @"Buzz";
using ScopedBlockSwizzlerTest = PlatformTest;
// Tests that swizzling a class method works properly.
TEST_F(ScopedBlockSwizzlerTest, SwizzlingClassMethods) {
EXPECT_NSEQ(kOriginalClassValue,
[ScopedBlockSwizzlerTestClass classMethodToSwizzle]);
{
id block = ^NSString*(id self) { return kSwizzledClassValue; };
ScopedBlockSwizzler swizzler([ScopedBlockSwizzlerTestClass class],
@selector(classMethodToSwizzle), block);
EXPECT_NSEQ(kSwizzledClassValue,
[ScopedBlockSwizzlerTestClass classMethodToSwizzle]);
}
EXPECT_NSEQ(kOriginalClassValue,
[ScopedBlockSwizzlerTestClass classMethodToSwizzle]);
}
// Tests that swizzling an instance method works properly.
TEST_F(ScopedBlockSwizzlerTest, SwizzlingInstanceMethod) {
ScopedBlockSwizzlerTestClass* target =
[[ScopedBlockSwizzlerTestClass alloc] init];
target.value = kSwizzledInstanceValue;
EXPECT_NSEQ(kOriginalInstanceValue, [target instanceMethodToSwizzle]);
EXPECT_FALSE([[target instanceMethodToSwizzle]
isEqualToString:kSwizzledInstanceValue]);
{
id block = ^NSString*(id self) {
return base::mac::ObjCCastStrict<ScopedBlockSwizzlerTestClass>(self)
.value;
};
ScopedBlockSwizzler swizzler([ScopedBlockSwizzlerTestClass class],
@selector(instanceMethodToSwizzle), block);
EXPECT_NSEQ(kSwizzledInstanceValue, [target instanceMethodToSwizzle]);
}
EXPECT_NSEQ(kOriginalInstanceValue, [target instanceMethodToSwizzle]);
}
// Tests that calling |ScopedBlockSwizzler::reset()| properly unswizzles the
// method.
TEST_F(ScopedBlockSwizzlerTest, TestReset) {
EXPECT_NSEQ(kOriginalClassValue,
[ScopedBlockSwizzlerTestClass classMethodToSwizzle]);
id block = ^NSString*(id self) { return kSwizzledClassValue; };
std::unique_ptr<ScopedBlockSwizzler> swizzler(
new ScopedBlockSwizzler([ScopedBlockSwizzlerTestClass class],
@selector(classMethodToSwizzle), block));
EXPECT_NSEQ(kSwizzledClassValue,
[ScopedBlockSwizzlerTestClass classMethodToSwizzle]);
swizzler.reset();
EXPECT_NSEQ(kOriginalClassValue,
[ScopedBlockSwizzlerTestClass classMethodToSwizzle]);
}
} // namespace
#pragma mark - ScopedBlockSwizzlerTestClass
@implementation ScopedBlockSwizzlerTestClass
@synthesize value = _value;
+ (NSString*)classMethodToSwizzle {
return kOriginalClassValue;
}
- (NSString*)instanceMethodToSwizzle {
return kOriginalInstanceValue;
}
@end