forked from TextureGroup/Texture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathASScrollNodeTests.mm
206 lines (157 loc) · 7.48 KB
/
ASScrollNodeTests.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
//
// ASScrollNodeTests.mm
// Texture
//
// Copyright (c) Pinterest, Inc. All rights reserved.
// Licensed under Apache 2.0: http://www.apache.org/licenses/LICENSE-2.0
//
#import <UIKit/UIKit.h>
#import <XCTest/XCTest.h>
#import <AsyncDisplayKit/AsyncDisplayKit.h>
#import "ASXCTExtensions.h"
@interface ASScrollNodeTests : XCTestCase
@property (nonatomic) ASScrollNode *scrollNode;
@property (nonatomic) ASDisplayNode *subnode;
@end
@implementation ASScrollNodeTests
- (void)setUp
{
ASDisplayNode *subnode = [[ASDisplayNode alloc] init];
self.subnode = subnode;
self.scrollNode = [[ASScrollNode alloc] init];
self.scrollNode.scrollableDirections = ASScrollDirectionVerticalDirections;
self.scrollNode.automaticallyManagesContentSize = YES;
self.scrollNode.automaticallyManagesSubnodes = YES;
self.scrollNode.layoutSpecBlock = ^ASLayoutSpec * _Nonnull(__kindof ASDisplayNode * _Nonnull node, ASSizeRange constrainedSize) {
return [[ASWrapperLayoutSpec alloc] initWithLayoutElement:subnode];
};
[self.scrollNode view];
}
- (void)testSubnodeLayoutCalculatedWithUnconstrainedMaxSizeInScrollableDirection
{
CGSize parentSize = CGSizeMake(100, 100);
ASSizeRange sizeRange = ASSizeRangeMake(parentSize);
[self.scrollNode layoutThatFits:sizeRange parentSize:parentSize];
ASSizeRange subnodeSizeRange = sizeRange;
subnodeSizeRange.max.height = CGFLOAT_MAX;
XCTAssertEqual(self.scrollNode.scrollableDirections, ASScrollDirectionVerticalDirections);
ASXCTAssertEqualSizeRanges(self.subnode.constrainedSizeForCalculatedLayout, subnodeSizeRange);
// Same test for horizontal scrollable directions
self.scrollNode.scrollableDirections = ASScrollDirectionHorizontalDirections;
[self.scrollNode layoutThatFits:sizeRange parentSize:parentSize];
subnodeSizeRange = sizeRange;
subnodeSizeRange.max.width = CGFLOAT_MAX;
ASXCTAssertEqualSizeRanges(self.subnode.constrainedSizeForCalculatedLayout, subnodeSizeRange);
}
- (void)testAutomaticallyManagesContentSizeUnderflow
{
CGSize subnodeSize = CGSizeMake(100, 100);
CGSize parentSize = CGSizeMake(100, 200);
ASSizeRange sizeRange = ASSizeRangeUnconstrained;
self.subnode.style.preferredSize = subnodeSize;
[self.scrollNode layoutThatFits:sizeRange parentSize:parentSize];
[self.scrollNode layout];
ASXCTAssertEqualSizes(self.scrollNode.calculatedSize, parentSize);
ASXCTAssertEqualSizes(self.scrollNode.view.contentSize, subnodeSize);
}
- (void)testAutomaticallyManagesContentSizeOverflow
{
CGSize subnodeSize = CGSizeMake(100, 500);
CGSize parentSize = CGSizeMake(100, 200);
ASSizeRange sizeRange = ASSizeRangeUnconstrained;
self.subnode.style.preferredSize = subnodeSize;
[self.scrollNode layoutThatFits:sizeRange parentSize:parentSize];
[self.scrollNode layout];
ASXCTAssertEqualSizes(self.scrollNode.calculatedSize, parentSize);
ASXCTAssertEqualSizes(self.scrollNode.view.contentSize, subnodeSize);
}
- (void)testAutomaticallyManagesContentSizeWithSizeRangeSmallerThanParentSize
{
CGSize subnodeSize = CGSizeMake(100, 100);
CGSize parentSize = CGSizeMake(100, 500);
ASSizeRange sizeRange = ASSizeRangeMake(CGSizeMake(100, 100), CGSizeMake(100, 200));
self.subnode.style.preferredSize = subnodeSize;
[self.scrollNode layoutThatFits:sizeRange parentSize:parentSize];
[self.scrollNode layout];
ASXCTAssertEqualSizes(self.scrollNode.calculatedSize, sizeRange.max);
ASXCTAssertEqualSizes(self.scrollNode.view.contentSize, subnodeSize);
}
- (void)testAutomaticallyManagesContentSizeWithSizeRangeSmallerThanParentSizeFillContainer
{
CGSize subnodeSize = CGSizeMake(100, 200);
CGSize parentSize = CGSizeMake(100, 500);
ASSizeRange sizeRange = ASSizeRangeMake(CGSizeMake(100, 200), CGSizeMake(100, 200));
self.subnode.style.flexGrow = 1.0;
self.subnode.style.minHeight = ASDimensionMake(ASDimensionUnitPoints, 50.0);
self.subnode.style.width = ASDimensionMake(ASDimensionUnitPoints, 100.0);
[self.scrollNode layoutThatFits:sizeRange parentSize:parentSize];
[self.scrollNode layout];
ASXCTAssertEqualSizes(self.scrollNode.calculatedSize, sizeRange.max);
ASXCTAssertEqualSizes(self.scrollNode.view.contentSize, subnodeSize);
}
// It's expected that the contentSize is 100x100 when ScrollNode's bounds is 100x200, which currently
// not the case for LayoutSepc but work in Yoga.
- (void)disable_testAutomaticallyManagesContentSizeWithSizeRangeSmallerThanParentSizeKeepChildSize
{
CGSize subnodeSize = CGSizeMake(100, 100);
CGSize parentSize = CGSizeMake(100, 200);
ASSizeRange sizeRange = ASSizeRangeMake(CGSizeMake(100, 200), CGSizeMake(100, 200));
self.subnode.style.width = ASDimensionMake(ASDimensionUnitPoints, 100.0);
self.subnode.style.height = ASDimensionMake(ASDimensionUnitPoints, 100.0);
[self.scrollNode layoutThatFits:sizeRange parentSize:parentSize];
[self.scrollNode layout];
ASXCTAssertEqualSizes(self.scrollNode.calculatedSize, sizeRange.max);
ASXCTAssertEqualSizes(self.scrollNode.view.contentSize, subnodeSize);
}
- (void)testAutomaticallyManagesContentSizeWithSizeRangeBiggerThanParentSize
{
CGSize subnodeSize = CGSizeMake(100, 200);
CGSize parentSize = CGSizeMake(100, 100);
ASSizeRange sizeRange = ASSizeRangeMake(CGSizeMake(100, 150));
self.subnode.style.preferredSize = subnodeSize;
[self.scrollNode layoutThatFits:sizeRange parentSize:parentSize];
[self.scrollNode layout];
ASXCTAssertEqualSizes(self.scrollNode.calculatedSize, sizeRange.min);
ASXCTAssertEqualSizes(self.scrollNode.view.contentSize, subnodeSize);
}
- (void)testAutomaticallyManagesContentSizeWithInvalidCalculatedSizeForLayout
{
CGSize subnodeSize = CGSizeMake(100, 200);
CGSize parentSize = CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX);
ASSizeRange sizeRange = ASSizeRangeUnconstrained;
self.subnode.style.preferredSize = subnodeSize;
[self.scrollNode layoutThatFits:sizeRange parentSize:parentSize];
[self.scrollNode layout];
ASXCTAssertEqualSizes(self.scrollNode.calculatedSize, subnodeSize);
ASXCTAssertEqualSizes(self.scrollNode.view.contentSize, subnodeSize);
}
- (void)testASScrollNodeAccessibility {
ASDisplayNode *scrollNode = [[ASDisplayNode alloc] init];
UIWindow *window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, 320, 560)];
[window addSubnode:scrollNode];
[window makeKeyAndVisible];
ASDisplayNode *node = [[ASDisplayNode alloc] init];
node.isAccessibilityContainer = YES;
node.accessibilityLabel = @"node";
[scrollNode addSubnode:node];
node.frame = CGRectMake(0,0,100,100);
ASTextNode2 *text = [[ASTextNode2 alloc] init];
text.attributedText = [[NSAttributedString alloc] initWithString:@"text"];
[node addSubnode:text];
ASTextNode2 *text2 = [[ASTextNode2 alloc] init];
text2.attributedText = [[NSAttributedString alloc] initWithString:@"text2"];
[node addSubnode:text2];
__unused UIView *view = scrollNode.view;
XCTAssertTrue(node.view.accessibilityElements.firstObject, @"node");
// Following tests will only pass when accessibility is enabled.
// More details: https://github.com/TextureGroup/Texture/pull/1188
// A bunch of a11y containers each of which hold aggregated labels.
/* NSArray *a11yElements = [scrollNode.view accessibilityElements];
XCTAssertTrue(a11yElements.count > 0, @"accessibilityElements should exist");
UIAccessibilityElement *container = a11yElements.firstObject;
XCTAssertTrue(container.isAccessibilityElement == false && container.accessibilityElements.count > 0);
UIAccessibilityElement *ae = container.accessibilityElements.firstObject;
XCTAssertTrue([[ae accessibilityLabel] isEqualToString:@"node, text, text2"]);
*/
}
@end