forked from TextureGroup/Texture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathASDisplayNodeLayoutTests.mm
182 lines (144 loc) · 7.4 KB
/
ASDisplayNodeLayoutTests.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//
// ASDisplayNodeLayoutTests.mm
// Texture
//
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the /ASDK-Licenses directory of this source tree. An additional
// grant of patent rights can be found in the PATENTS file in the same directory.
//
// Modifications to this file made after 4/13/2017 are: Copyright (c) 2017-present,
// Pinterest, Inc. Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
#import "ASXCTExtensions.h"
#import <AsyncDisplayKit/AsyncDisplayKit.h>
#import "ASLayoutSpecSnapshotTestsHelper.h"
#import <AsyncDisplayKit/ASDisplayNode+FrameworkPrivate.h>
#import <stdatomic.h>
@interface ASDisplayNodeLayoutTests : XCTestCase
@end
@implementation ASDisplayNodeLayoutTests
- (void)testMeasureOnLayoutIfNotHappenedBefore
{
CGSize nodeSize = CGSizeMake(100, 100);
ASDisplayNode *displayNode = [[ASDisplayNode alloc] init];
displayNode.style.width = ASDimensionMake(100);
displayNode.style.height = ASDimensionMake(100);
// Use a button node in here as ASButtonNode uses layoutSpecThatFits:
ASButtonNode *buttonNode = [ASButtonNode new];
[displayNode addSubnode:buttonNode];
displayNode.frame = {.size = nodeSize};
buttonNode.frame = {.size = nodeSize};
ASXCTAssertEqualSizes(displayNode.calculatedSize, CGSizeZero, @"Calculated size before measurement and layout should be 0");
ASXCTAssertEqualSizes(buttonNode.calculatedSize, CGSizeZero, @"Calculated size before measurement and layout should be 0");
// Trigger view creation and layout pass without a manual -layoutThatFits: call before so the automatic measurement
// pass will trigger in the layout pass
[displayNode.view layoutIfNeeded];
ASXCTAssertEqualSizes(displayNode.calculatedSize, nodeSize, @"Automatic measurement pass should have happened in layout pass");
ASXCTAssertEqualSizes(buttonNode.calculatedSize, nodeSize, @"Automatic measurement pass should have happened in layout pass");
}
#if DEBUG
- (void)testNotAllowAddingSubnodesInLayoutSpecThatFits
{
ASDisplayNode *displayNode = [ASDisplayNode new];
ASDisplayNode *someOtherNode = [ASDisplayNode new];
displayNode.layoutSpecBlock = ^(ASDisplayNode * _Nonnull node, ASSizeRange constrainedSize) {
[node addSubnode:someOtherNode];
return [ASInsetLayoutSpec insetLayoutSpecWithInsets:UIEdgeInsetsZero child:someOtherNode];
};
XCTAssertThrows([displayNode layoutThatFits:ASSizeRangeMake(CGSizeZero, CGSizeMake(100, 100))], @"Should throw if subnode was added in layoutSpecThatFits:");
}
- (void)testNotAllowModifyingSubnodesInLayoutSpecThatFits
{
ASDisplayNode *displayNode = [ASDisplayNode new];
ASDisplayNode *someOtherNode = [ASDisplayNode new];
[displayNode addSubnode:someOtherNode];
displayNode.layoutSpecBlock = ^(ASDisplayNode * _Nonnull node, ASSizeRange constrainedSize) {
[someOtherNode removeFromSupernode];
[node addSubnode:[ASDisplayNode new]];
return [ASInsetLayoutSpec insetLayoutSpecWithInsets:UIEdgeInsetsZero child:someOtherNode];
};
XCTAssertThrows([displayNode layoutThatFits:ASSizeRangeMake(CGSizeZero, CGSizeMake(100, 100))], @"Should throw if subnodes where modified in layoutSpecThatFits:");
}
#endif
- (void)testMeasureOnLayoutIfNotHappenedBeforeNoRemeasureForSameBounds
{
CGSize nodeSize = CGSizeMake(100, 100);
ASDisplayNode *displayNode = [ASDisplayNode new];
displayNode.style.width = ASDimensionMake(nodeSize.width);
displayNode.style.height = ASDimensionMake(nodeSize.height);
ASButtonNode *buttonNode = [ASButtonNode new];
[displayNode addSubnode:buttonNode];
__block atomic_int numberOfLayoutSpecThatFitsCalls = ATOMIC_VAR_INIT(0);
displayNode.layoutSpecBlock = ^(ASDisplayNode * _Nonnull node, ASSizeRange constrainedSize) {
atomic_fetch_add(&numberOfLayoutSpecThatFitsCalls, 1);
return [ASInsetLayoutSpec insetLayoutSpecWithInsets:UIEdgeInsetsZero child:buttonNode];
};
displayNode.frame = {.size = nodeSize};
// Trigger initial layout pass without a measurement pass before
[displayNode.view layoutIfNeeded];
XCTAssertEqual(numberOfLayoutSpecThatFitsCalls, 1, @"Should measure during layout if not measured");
[displayNode layoutThatFits:ASSizeRangeMake(nodeSize, nodeSize)];
XCTAssertEqual(numberOfLayoutSpecThatFitsCalls, 1, @"Should not remeasure with same bounds");
}
- (void)testThatLayoutWithInvalidSizeCausesException
{
ASDisplayNode *displayNode = [[ASDisplayNode alloc] init];
ASDisplayNode *node = [[ASDisplayNode alloc] init];
node.layoutSpecBlock = ^ASLayoutSpec *(ASDisplayNode *node, ASSizeRange constrainedSize) {
return [ASWrapperLayoutSpec wrapperWithLayoutElement:displayNode];
};
XCTAssertThrows([node layoutThatFits:ASSizeRangeMake(CGSizeMake(0, FLT_MAX))]);
}
- (void)testThatLayoutCreatedWithInvalidSizeCausesException
{
ASDisplayNode *displayNode = [[ASDisplayNode alloc] init];
XCTAssertThrows([ASLayout layoutWithLayoutElement:displayNode size:CGSizeMake(FLT_MAX, FLT_MAX)]);
XCTAssertThrows([ASLayout layoutWithLayoutElement:displayNode size:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX)]);
XCTAssertThrows([ASLayout layoutWithLayoutElement:displayNode size:CGSizeMake(INFINITY, INFINITY)]);
}
- (void)testThatLayoutElementCreatedInLayoutSpecThatFitsDoNotGetDeallocated
{
const CGSize kSize = CGSizeMake(300, 300);
ASDisplayNode *subNode = [[ASDisplayNode alloc] init];
subNode.automaticallyManagesSubnodes = YES;
subNode.layoutSpecBlock = ^(ASDisplayNode * _Nonnull node, ASSizeRange constrainedSize) {
ASTextNode *textNode = [ASTextNode new];
textNode.attributedText = [[NSAttributedString alloc] initWithString:@"Test Test Test Test Test Test Test Test"];
ASInsetLayoutSpec *insetSpec = [ASInsetLayoutSpec insetLayoutSpecWithInsets:UIEdgeInsetsZero child:textNode];
return [ASInsetLayoutSpec insetLayoutSpecWithInsets:UIEdgeInsetsZero child:insetSpec];
};
ASDisplayNode *rootNode = [[ASDisplayNode alloc] init];
rootNode.automaticallyManagesSubnodes = YES;
rootNode.layoutSpecBlock = ^(ASDisplayNode * _Nonnull node, ASSizeRange constrainedSize) {
ASTextNode *textNode = [ASTextNode new];
textNode.attributedText = [[NSAttributedString alloc] initWithString:@"Test Test Test Test Test"];
ASInsetLayoutSpec *insetSpec = [ASInsetLayoutSpec insetLayoutSpecWithInsets:UIEdgeInsetsZero child:textNode];
return [ASStackLayoutSpec
stackLayoutSpecWithDirection:ASStackLayoutDirectionVertical
spacing:0.0
justifyContent:ASStackLayoutJustifyContentStart
alignItems:ASStackLayoutAlignItemsStretch
children:@[insetSpec, subNode]];
};
rootNode.frame = CGRectMake(0, 0, kSize.width, kSize.height);
[rootNode view];
XCTestExpectation *expectation = [self expectationWithDescription:@"Execute measure and layout pass"];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[rootNode layoutThatFits:ASSizeRangeMake(kSize)];
dispatch_async(dispatch_get_main_queue(), ^{
XCTAssertNoThrow([rootNode.view layoutIfNeeded]);
[expectation fulfill];
});
});
[self waitForExpectationsWithTimeout:5.0 handler:^(NSError *error) {
if (error) {
XCTFail(@"Expectation failed: %@", error);
}
}];
}
@end