forked from TextureGroup/Texture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathASDisplayNodeImplicitHierarchyTests.m
328 lines (259 loc) · 11.7 KB
/
ASDisplayNodeImplicitHierarchyTests.m
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
//
// ASDisplayNodeImplicitHierarchyTests.m
// Texture
//
// Copyright (c) Facebook, Inc. and its affiliates. All rights reserved.
// Changes after 4/13/2017 are: Copyright (c) Pinterest, Inc. All rights reserved.
// Licensed under Apache 2.0: http://www.apache.org/licenses/LICENSE-2.0
//
#import <XCTest/XCTest.h>
#import <AsyncDisplayKit/AsyncDisplayKit.h>
#import <AsyncDisplayKit/ASDisplayNode+FrameworkPrivate.h>
#import "ASDisplayNodeTestsHelper.h"
@interface ASSpecTestDisplayNode : ASDisplayNode
/**
Simple state identifier to allow control of current spec inside of the layoutSpecBlock
*/
@property (nonatomic) NSNumber *layoutState;
@end
@implementation ASSpecTestDisplayNode
- (instancetype)init
{
self = [super init];
if (self) {
_layoutState = @1;
}
return self;
}
@end
@interface ASDisplayNodeImplicitHierarchyTests : XCTestCase
@end
@implementation ASDisplayNodeImplicitHierarchyTests
- (void)testFeatureFlag
{
ASDisplayNode *node = [[ASDisplayNode alloc] init];
XCTAssertFalse(node.automaticallyManagesSubnodes);
node.automaticallyManagesSubnodes = YES;
XCTAssertTrue(node.automaticallyManagesSubnodes);
}
- (void)testInitialNodeInsertionWithOrdering
{
static CGSize kSize = {100, 100};
ASDisplayNode *node1 = [[ASDisplayNode alloc] init];
ASDisplayNode *node2 = [[ASDisplayNode alloc] init];
ASDisplayNode *node3 = [[ASDisplayNode alloc] init];
ASDisplayNode *node4 = [[ASDisplayNode alloc] init];
ASDisplayNode *node5 = [[ASDisplayNode alloc] init];
// As we will involve a stack spec we have to give the nodes an intrinsic content size
node1.style.preferredSize = kSize;
node2.style.preferredSize = kSize;
node3.style.preferredSize = kSize;
node4.style.preferredSize = kSize;
node5.style.preferredSize = kSize;
ASSpecTestDisplayNode *node = [[ASSpecTestDisplayNode alloc] init];
node.automaticallyManagesSubnodes = YES;
node.layoutSpecBlock = ^(ASDisplayNode *weakNode, ASSizeRange constrainedSize) {
ASAbsoluteLayoutSpec *absoluteLayout = [ASAbsoluteLayoutSpec absoluteLayoutSpecWithChildren:@[node4]];
ASStackLayoutSpec *stack1 = [[ASStackLayoutSpec alloc] init];
[stack1 setChildren:@[node1, node2]];
ASStackLayoutSpec *stack2 = [[ASStackLayoutSpec alloc] init];
[stack2 setChildren:@[node3, absoluteLayout]];
return [ASAbsoluteLayoutSpec absoluteLayoutSpecWithChildren:@[stack1, stack2, node5]];
};
ASDisplayNodeSizeToFitSizeRange(node, ASSizeRangeMake(CGSizeZero, CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX)));
[node.view layoutIfNeeded];
XCTAssertEqual(node.subnodes[0], node1);
XCTAssertEqual(node.subnodes[1], node2);
XCTAssertEqual(node.subnodes[2], node3);
XCTAssertEqual(node.subnodes[3], node4);
XCTAssertEqual(node.subnodes[4], node5);
}
- (void)testInitialNodeInsertionWhenEnterPreloadState
{
static CGSize kSize = {100, 100};
static NSInteger subnodeCount = 5;
NSMutableArray<ASDisplayNode *> *subnodes = [NSMutableArray arrayWithCapacity:subnodeCount];
for (NSInteger i = 0; i < subnodeCount; i++) {
ASDisplayNode *subnode = [[ASDisplayNode alloc] init];
// As we will involve a stack spec we have to give the nodes an intrinsic content size
subnode.style.preferredSize = kSize;
[subnodes addObject:subnode];
}
ASSpecTestDisplayNode *node = [[ASSpecTestDisplayNode alloc] init];
[node setHierarchyState:ASHierarchyStateRangeManaged];
node.automaticallyManagesSubnodes = YES;
node.layoutSpecBlock = ^(ASDisplayNode *weakNode, ASSizeRange constrainedSize) {
ASAbsoluteLayoutSpec *absoluteLayout = [ASAbsoluteLayoutSpec absoluteLayoutSpecWithChildren:@[subnodes[3]]];
ASStackLayoutSpec *stack1 = [[ASStackLayoutSpec alloc] init];
[stack1 setChildren:@[subnodes[0], subnodes[1]]];
ASStackLayoutSpec *stack2 = [[ASStackLayoutSpec alloc] init];
[stack2 setChildren:@[subnodes[2], absoluteLayout]];
return [ASAbsoluteLayoutSpec absoluteLayoutSpecWithChildren:@[stack1, stack2, subnodes[4]]];
};
ASDisplayNodeSizeToFitSizeRange(node, ASSizeRangeMake(CGSizeZero, CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX)));
[node recursivelySetInterfaceState:ASInterfaceStatePreload];
ASCATransactionQueueWait(nil);
// No premature view allocation
XCTAssertFalse(node.isNodeLoaded);
// Subnodes should be inserted, laid out and entered preload state
XCTAssertTrue([subnodes isEqualToArray:node.subnodes]);
for (NSInteger i = 0; i < subnodeCount; i++) {
ASDisplayNode *subnode = subnodes[i];
XCTAssertTrue(CGSizeEqualToSize(kSize, subnode.bounds.size));
XCTAssertTrue(ASInterfaceStateIncludesPreload(subnode.interfaceState));
}
}
- (void)testCalculatedLayoutHierarchyTransitions
{
static CGSize kSize = {100, 100};
ASDisplayNode *node1 = [[ASDisplayNode alloc] init];
ASDisplayNode *node2 = [[ASDisplayNode alloc] init];
ASDisplayNode *node3 = [[ASDisplayNode alloc] init];
node1.debugName = @"a";
node2.debugName = @"b";
node3.debugName = @"c";
// As we will involve a stack spec we have to give the nodes an intrinsic content size
node1.style.preferredSize = kSize;
node2.style.preferredSize = kSize;
node3.style.preferredSize = kSize;
ASSpecTestDisplayNode *node = [[ASSpecTestDisplayNode alloc] init];
node.automaticallyManagesSubnodes = YES;
node.layoutSpecBlock = ^(ASDisplayNode *weakNode, ASSizeRange constrainedSize){
ASSpecTestDisplayNode *strongNode = (ASSpecTestDisplayNode *)weakNode;
if ([strongNode.layoutState isEqualToNumber:@1]) {
return [ASAbsoluteLayoutSpec absoluteLayoutSpecWithChildren:@[node1, node2]];
} else {
ASStackLayoutSpec *stackLayout = [[ASStackLayoutSpec alloc] init];
[stackLayout setChildren:@[node3, node2]];
return [ASAbsoluteLayoutSpec absoluteLayoutSpecWithChildren:@[node1, stackLayout]];
}
};
ASDisplayNodeSizeToFitSizeRange(node, ASSizeRangeMake(CGSizeZero, CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX)));
[node.view layoutIfNeeded];
XCTAssertEqual(node.subnodes[0], node1);
XCTAssertEqual(node.subnodes[1], node2);
node.layoutState = @2;
[node setNeedsLayout]; // After a state change the layout needs to be invalidated
[node.view layoutIfNeeded]; // A new layout pass will trigger the hiearchy transition
XCTAssertEqual(node.subnodes[0], node1);
XCTAssertEqual(node.subnodes[1], node3);
XCTAssertEqual(node.subnodes[2], node2);
}
// Disable test for now as we disabled the assertion
//- (void)testLayoutTransitionWillThrowForManualSubnodeManagement
//{
// ASDisplayNode *node1 = [[ASDisplayNode alloc] init];
// node1.name = @"node1";
//
// ASSpecTestDisplayNode *node = [[ASSpecTestDisplayNode alloc] init];
// node.automaticallyManagesSubnodes = YES;
// node.layoutSpecBlock = ^ASLayoutSpec *(ASDisplayNode *weakNode, ASSizeRange constrainedSize){
// return [ASAbsoluteLayoutSpec absoluteLayoutSpecWithChildren:@[node1]];
// };
//
// XCTAssertNoThrow([node layoutThatFits:ASSizeRangeMake(CGSizeZero)]);
// XCTAssertThrows([node1 removeFromSupernode]);
//}
- (void)testLayoutTransitionMeasurementCompletionBlockIsCalledOnMainThread
{
const CGSize kSize = CGSizeMake(100, 100);
ASDisplayNode *displayNode = [[ASDisplayNode alloc] init];
displayNode.style.preferredSize = kSize;
// Trigger explicit view creation to be able to use the Transition API
[displayNode view];
XCTestExpectation *expectation = [self expectationWithDescription:@"Call measurement completion block on main"];
[displayNode transitionLayoutWithSizeRange:ASSizeRangeMake(CGSizeZero, CGSizeMake(INFINITY, INFINITY)) animated:YES shouldMeasureAsync:YES measurementCompletion:^{
XCTAssertTrue(ASDisplayNodeThreadIsMain(), @"Measurement completion block should be called on main thread");
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:2.0 handler:nil];
}
- (void)testMeasurementInBackgroundThreadWithLoadedNode
{
const CGSize kNodeSize = CGSizeMake(100, 100);
ASDisplayNode *node1 = [[ASDisplayNode alloc] init];
ASDisplayNode *node2 = [[ASDisplayNode alloc] init];
ASSpecTestDisplayNode *node = [[ASSpecTestDisplayNode alloc] init];
node.style.preferredSize = kNodeSize;
node.automaticallyManagesSubnodes = YES;
node.layoutSpecBlock = ^(ASDisplayNode *weakNode, ASSizeRange constrainedSize) {
ASSpecTestDisplayNode *strongNode = (ASSpecTestDisplayNode *)weakNode;
if ([strongNode.layoutState isEqualToNumber:@1]) {
return [ASAbsoluteLayoutSpec absoluteLayoutSpecWithChildren:@[node1]];
} else {
return [ASAbsoluteLayoutSpec absoluteLayoutSpecWithChildren:@[node2]];
}
};
// Intentionally trigger view creation
[node view];
[node2 view];
XCTestExpectation *expectation = [self expectationWithDescription:@"Fix IHM layout also if one node is already loaded"];
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Measurement happens in the background
ASDisplayNodeSizeToFitSizeRange(node, ASSizeRangeMake(CGSizeZero, CGSizeMake(INFINITY, INFINITY)));
// Dispatch back to the main thread to let the insertion / deletion of subnodes happening
dispatch_async(dispatch_get_main_queue(), ^{
// Layout on main
[node setNeedsLayout];
[node.view layoutIfNeeded];
XCTAssertEqual(node.subnodes[0], node1);
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Change state and measure in the background
node.layoutState = @2;
[node setNeedsLayout];
ASDisplayNodeSizeToFitSizeRange(node, ASSizeRangeMake(CGSizeZero, CGSizeMake(INFINITY, INFINITY)));
// Dispatch back to the main thread to let the insertion / deletion of subnodes happening
dispatch_async(dispatch_get_main_queue(), ^{
// Layout on main again
[node.view layoutIfNeeded];
XCTAssertEqual(node.subnodes[0], node2);
[expectation fulfill];
});
});
});
});
[self waitForExpectationsWithTimeout:5.0 handler:^(NSError *error) {
if (error) {
NSLog(@"Timeout Error: %@", error);
}
}];
}
- (void)testTransitionLayoutWithAnimationWithLoadedNodes
{
const CGSize kNodeSize = CGSizeMake(100, 100);
ASDisplayNode *node1 = [[ASDisplayNode alloc] init];
ASDisplayNode *node2 = [[ASDisplayNode alloc] init];
ASSpecTestDisplayNode *node = [[ASSpecTestDisplayNode alloc] init];
node.automaticallyManagesSubnodes = YES;
node.style.preferredSize = kNodeSize;
node.layoutSpecBlock = ^(ASDisplayNode *weakNode, ASSizeRange constrainedSize) {
ASSpecTestDisplayNode *strongNode = (ASSpecTestDisplayNode *)weakNode;
if ([strongNode.layoutState isEqualToNumber:@1]) {
return [ASAbsoluteLayoutSpec absoluteLayoutSpecWithChildren:@[node1]];
} else {
return [ASAbsoluteLayoutSpec absoluteLayoutSpecWithChildren:@[node2]];
}
};
// Intentionally trigger view creation
[node1 view];
[node2 view];
XCTestExpectation *expectation = [self expectationWithDescription:@"Fix IHM layout transition also if one node is already loaded"];
ASDisplayNodeSizeToFitSizeRange(node, ASSizeRangeMake(CGSizeZero, CGSizeMake(INFINITY, INFINITY)));
[node.view layoutIfNeeded];
XCTAssertEqual(node.subnodes[0], node1);
node.layoutState = @2;
[node invalidateCalculatedLayout];
[node transitionLayoutWithAnimation:YES shouldMeasureAsync:YES measurementCompletion:^{
// Push this to the next runloop to let async insertion / removing of nodes finished before checking
dispatch_async(dispatch_get_main_queue(), ^{
XCTAssertEqual(node.subnodes[0], node2);
[expectation fulfill];
});
}];
[self waitForExpectationsWithTimeout:5.0 handler:^(NSError *error) {
if (error) {
NSLog(@"Timeout Error: %@", error);
}
}];
}
@end