forked from fachrifaul/Texture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathASLayoutTestNode.mm
88 lines (74 loc) · 2.56 KB
/
ASLayoutTestNode.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
//
// ASLayoutTestNode.mm
// Texture
//
// Copyright (c) Pinterest, Inc. All rights reserved.
// Licensed under Apache 2.0: http://www.apache.org/licenses/LICENSE-2.0
//
#import "ASLayoutTestNode.h"
#import <OCMock/OCMock.h>
#import "OCMockObject+ASAdditions.h"
@implementation ASLayoutTestNode
- (instancetype)init
{
if (self = [super init]) {
_mock = OCMStrictClassMock([ASDisplayNode class]);
// If errors occur (e.g. unexpected method) we need to quickly figure out
// which node is at fault, so we inject the node name into the mock instance
// description.
__weak __typeof(self) weakSelf = self;
[_mock setModifyDescriptionBlock:^(id mock, NSString *baseDescription){
return [NSString stringWithFormat:@"Mock(%@)", weakSelf.description];
}];
}
return self;
}
- (ASLayout *)currentLayoutBasedOnFrames
{
return [self _currentLayoutBasedOnFramesForRootNode:YES];
}
- (ASLayout *)_currentLayoutBasedOnFramesForRootNode:(BOOL)isRootNode
{
const auto sublayouts = [[NSMutableArray<ASLayout *> alloc] init];
for (ASLayoutTestNode *subnode in self.subnodes) {
[sublayouts addObject:[subnode _currentLayoutBasedOnFramesForRootNode:NO]];
}
CGPoint rootPosition = isRootNode ? ASPointNull : self.frame.origin;
return [ASLayout layoutWithLayoutElement:self size:self.frame.size position:rootPosition sublayouts:sublayouts];
}
- (void)setTestSize:(CGSize)testSize
{
if (!CGSizeEqualToSize(testSize, _testSize)) {
_testSize = testSize;
[self setNeedsLayout];
}
}
- (ASLayout *)calculateLayoutThatFits:(ASSizeRange)constrainedSize
{
[_mock calculateLayoutThatFits:constrainedSize];
// If we have a layout spec block, or no test size, return super.
if (self.layoutSpecBlock || CGSizeEqualToSize(self.testSize, CGSizeZero)) {
return [super calculateLayoutThatFits:constrainedSize];
} else {
// Interestingly, the infra will auto-clamp sizes from calculateSizeThatFits, but not from calculateLayoutThatFits.
const auto size = ASSizeRangeClamp(constrainedSize, self.testSize);
return [ASLayout layoutWithLayoutElement:self size:size];
}
}
#pragma mark - Forwarding to mock
- (void)calculatedLayoutDidChange
{
[_mock calculatedLayoutDidChange];
[super calculatedLayoutDidChange];
}
- (void)didCompleteLayoutTransition:(id<ASContextTransitioning>)context
{
[_mock didCompleteLayoutTransition:context];
[super didCompleteLayoutTransition:context];
}
- (void)animateLayoutTransition:(id<ASContextTransitioning>)context
{
[_mock animateLayoutTransition:context];
[super animateLayoutTransition:context];
}
@end