forked from TextureGroup/Texture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathASLayoutEngineTests.mm
591 lines (494 loc) · 21.7 KB
/
ASLayoutEngineTests.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
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
//
// ASLayoutEngineTests.mm
// Texture
//
// Copyright (c) Pinterest, Inc. All rights reserved.
// Licensed under Apache 2.0: http://www.apache.org/licenses/LICENSE-2.0
//
#import "ASTestCase.h"
#import "ASLayoutTestNode.h"
#import "ASXCTExtensions.h"
#import "ASTLayoutFixture.h"
@interface ASLayoutEngineTests : ASTestCase
@end
@implementation ASLayoutEngineTests {
ASLayoutTestNode *nodeA;
ASLayoutTestNode *nodeB;
ASLayoutTestNode *nodeC;
ASLayoutTestNode *nodeD;
ASLayoutTestNode *nodeE;
ASTLayoutFixture *fixture1;
ASTLayoutFixture *fixture2;
ASTLayoutFixture *fixture3;
ASTLayoutFixture *fixture4;
ASTLayoutFixture *fixture5;
// fixtures 1, 3 and 5 share the same exact node A layout spec block.
// we don't want the infra to call -setNeedsLayout when we switch fixtures
// so we need to use the same exact block.
ASLayoutSpecBlock fixture1and3and5NodeALayoutSpecBlock;
UIWindow *window;
UIViewController *vc;
NSArray<ASLayoutTestNode *> *allNodes;
NSTimeInterval verifyDelay;
// See -stubCalculatedLayoutDidChange.
BOOL stubbedCalculatedLayoutDidChange;
}
- (void)setUp
{
[super setUp];
verifyDelay = 3;
window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, 10, 1)];
vc = [[UIViewController alloc] init];
nodeA = [ASLayoutTestNode new];
nodeA.backgroundColor = [UIColor redColor];
// NOTE: nodeB has flexShrink, the others don't
nodeB = [ASLayoutTestNode new];
nodeB.style.flexShrink = 1;
nodeB.backgroundColor = [UIColor orangeColor];
nodeC = [ASLayoutTestNode new];
nodeC.backgroundColor = [UIColor yellowColor];
nodeD = [ASLayoutTestNode new];
nodeD.backgroundColor = [UIColor greenColor];
nodeE = [ASLayoutTestNode new];
nodeE.backgroundColor = [UIColor blueColor];
allNodes = @[ nodeA, nodeB, nodeC, nodeD, nodeE ];
ASSetDebugNames(nodeA, nodeB, nodeC, nodeD, nodeE);
ASLayoutSpecBlock b = ^ASLayoutSpec * _Nonnull(__kindof ASDisplayNode * _Nonnull node, ASSizeRange constrainedSize) {
return [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionHorizontal spacing:0 justifyContent:ASStackLayoutJustifyContentSpaceBetween alignItems:ASStackLayoutAlignItemsStart children:@[ self->nodeB, self->nodeC, self->nodeD ]];
};
fixture1and3and5NodeALayoutSpecBlock = b;
fixture1 = [self createFixture1];
fixture2 = [self createFixture2];
fixture3 = [self createFixture3];
fixture4 = [self createFixture4];
fixture5 = [self createFixture5];
nodeA.frame = vc.view.bounds;
nodeA.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[vc.view addSubnode:nodeA];
window.rootViewController = vc;
[window makeKeyAndVisible];
}
- (void)tearDown
{
nodeA.layoutSpecBlock = nil;
for (ASLayoutTestNode *node in allNodes) {
OCMVerifyAllWithDelay(node.mock, verifyDelay);
}
[super tearDown];
}
- (void)testFirstLayoutPassWhenInWindow
{
[self runFirstLayoutPassWithFixture:fixture1];
}
- (void)testSetNeedsLayoutAndNormalLayoutPass
{
[self runFirstLayoutPassWithFixture:fixture1];
[fixture2 apply];
// skip nodeB because its layout doesn't change.
for (ASLayoutTestNode *node in @[ nodeA, nodeC, nodeE ]) {
[fixture2 withSizeRangesForNode:node block:^(ASSizeRange sizeRange) {
OCMExpect([node.mock calculateLayoutThatFits:sizeRange]).onMainThread();
}];
OCMExpect([node.mock calculatedLayoutDidChange]).onMainThread();
}
[window layoutIfNeeded];
[self verifyFixture:fixture2];
}
/**
* Transition from fixture1 to Fixture2 on node A.
*
* Expect A and D to calculate once off main, and
* to receive calculatedLayoutDidChange on main,
* then to get the measurement completion call on main,
* then to get animateLayoutTransition: and didCompleteLayoutTransition: on main.
*/
- (void)testLayoutTransitionWithAsyncMeasurement
{
[self stubCalculatedLayoutDidChange];
[self runFirstLayoutPassWithFixture:fixture1];
[fixture2 apply];
// Expect A, C, E to calculate new layouts off-main
// dispatch_once onto main to run our injectedMainThread work while the transition calculates.
__block dispatch_block_t injectedMainThreadWork = nil;
for (ASLayoutTestNode *node in @[ nodeA, nodeC, nodeE ]) {
[fixture2 withSizeRangesForNode:node block:^(ASSizeRange sizeRange) {
OCMExpect([node.mock calculateLayoutThatFits:sizeRange])
.offMainThread()
.andDo(^(NSInvocation *inv) {
// On first calculateLayoutThatFits, schedule our injected main thread work.
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
dispatch_async(dispatch_get_main_queue(), ^{
injectedMainThreadWork();
});
});
});
}];
}
// The code in this section is designed to move in time order, all on the main thread:
OCMExpect([nodeA.mock animateLayoutTransition:OCMOCK_ANY]).onMainThread();
OCMExpect([nodeA.mock didCompleteLayoutTransition:OCMOCK_ANY]).onMainThread();
// Trigger the layout transition.
__block dispatch_block_t measurementCompletionBlock = nil;
[nodeA transitionLayoutWithAnimation:NO shouldMeasureAsync:YES measurementCompletion:^{
measurementCompletionBlock();
}];
// This block will get run after bg layout calculate starts, but before measurementCompletion
__block BOOL injectedMainThreadWorkDone = NO;
injectedMainThreadWork = ^{
injectedMainThreadWorkDone = YES;
[self->window layoutIfNeeded];
// Ensure we're still on the old layout. We should stay on this until the transition completes.
[self verifyFixture:self->fixture1];
};
measurementCompletionBlock = ^{
XCTAssert(injectedMainThreadWorkDone, @"We hoped to get onto the main thread before the measurementCompletion callback ran.");
};
for (ASLayoutTestNode *node in allNodes) {
OCMVerifyAllWithDelay(node.mock, verifyDelay);
}
[self verifyFixture:fixture2];
}
/**
* Transition from fixture1 to Fixture2 on node A.
*
* Expect A and D to calculate once on main, and
* to receive calculatedLayoutDidChange on main,
* then to get animateLayoutTransition: and didCompleteLayoutTransition: on main.
*/
- (void)testLayoutTransitionWithSyncMeasurement
{
[self stubCalculatedLayoutDidChange];
// Precondition
XCTAssertFalse(CGSizeEqualToSize(fixture5.layout.size, fixture1.layout.size));
// First, apply fixture 5 and run a measurement pass, but don't run a layout pass
// After this step, nodes will have pending layouts that are not yet applied
[fixture5 apply];
[fixture5 withSizeRangesForAllNodesUsingBlock:^(ASLayoutTestNode * _Nonnull node, ASSizeRange sizeRange) {
OCMExpect([node.mock calculateLayoutThatFits:sizeRange])
.onMainThread();
}];
[nodeA layoutThatFits:ASSizeRangeMake(fixture5.layout.size)];
// Assert that node A has layout size and size range from fixture 5
XCTAssertTrue(CGSizeEqualToSize(fixture5.layout.size, nodeA.calculatedSize));
XCTAssertTrue(ASSizeRangeEqualToSizeRange([fixture5 firstSizeRangeForNode:nodeA], nodeA.constrainedSizeForCalculatedLayout));
// Then switch to fixture 1 and kick off a synchronous layout transition
// Unapplied pending layouts from the previous measurement pass will be outdated
[fixture1 apply];
[fixture1 withSizeRangesForAllNodesUsingBlock:^(ASLayoutTestNode * _Nonnull node, ASSizeRange sizeRange) {
OCMExpect([node.mock calculateLayoutThatFits:sizeRange])
.onMainThread();
}];
OCMExpect([nodeA.mock animateLayoutTransition:OCMOCK_ANY]).onMainThread();
OCMExpect([nodeA.mock didCompleteLayoutTransition:OCMOCK_ANY]).onMainThread();
[nodeA transitionLayoutWithAnimation:NO shouldMeasureAsync:NO measurementCompletion:nil];
// Assert that node A picks up new layout size and size range from fixture 1
XCTAssertTrue(CGSizeEqualToSize(fixture1.layout.size, nodeA.calculatedSize));
XCTAssertTrue(ASSizeRangeEqualToSizeRange([fixture1 firstSizeRangeForNode:nodeA], nodeA.constrainedSizeForCalculatedLayout));
[window layoutIfNeeded];
[self verifyFixture:fixture1];
}
/**
* Start at fixture 1.
* Trigger an async transition to fixture 2.
* While it's measuring, on main switch to fixture 4 (setNeedsLayout A, D) and run a CA layout pass.
*
* Correct behavior, we end up at fixture 4 since it's newer.
* Current incorrect behavior, we end up at fixture 2 and we remeasure surviving node C.
* Note: incorrect behavior likely introduced by the early check in __layout added in
* https://github.com/facebookarchive/AsyncDisplayKit/pull/2657
*/
- (void)DISABLE_testASetNeedsLayoutInterferingWithTheCurrentTransition
{
static BOOL enforceCorrectBehavior = NO;
[self stubCalculatedLayoutDidChange];
[self runFirstLayoutPassWithFixture:fixture1];
[fixture2 apply];
// Expect A, C, E to calculate new layouts off-main
// dispatch_once onto main to run our injectedMainThread work while the transition calculates.
__block dispatch_block_t injectedMainThreadWork = nil;
for (ASLayoutTestNode *node in @[ nodeA, nodeC, nodeE ]) {
[fixture2 withSizeRangesForNode:node block:^(ASSizeRange sizeRange) {
OCMExpect([node.mock calculateLayoutThatFits:sizeRange])
.offMainThread()
.andDo(^(NSInvocation *inv) {
// On first calculateLayoutThatFits, schedule our injected main thread work.
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
dispatch_async(dispatch_get_main_queue(), ^{
injectedMainThreadWork();
});
});
});
}];
}
// The code in this section is designed to move in time order, all on the main thread:
// With the current behavior, the transition will continue and complete.
if (!enforceCorrectBehavior) {
OCMExpect([nodeA.mock animateLayoutTransition:OCMOCK_ANY]).onMainThread();
OCMExpect([nodeA.mock didCompleteLayoutTransition:OCMOCK_ANY]).onMainThread();
}
// Trigger the layout transition.
__block dispatch_block_t measurementCompletionBlock = nil;
[nodeA transitionLayoutWithAnimation:NO shouldMeasureAsync:YES measurementCompletion:^{
measurementCompletionBlock();
}];
// Injected block will get run on main after bg layout calculate starts, but before measurementCompletion
__block BOOL injectedMainThreadWorkDone = NO;
injectedMainThreadWork = ^{
as_log_verbose(OS_LOG_DEFAULT, "Begin injectedMainThreadWork");
injectedMainThreadWorkDone = YES;
[self->fixture4 apply];
as_log_verbose(OS_LOG_DEFAULT, "Did apply new fixture");
if (enforceCorrectBehavior) {
// Correct measurement behavior here is unclear, may depend on whether the layouts which
// are common to both fixture2 and fixture4 are available from the cache.
} else {
// Incorrect behavior: nodeC will get measured against its new bounds on main.
const auto cPendingSize = [self->fixture2 layoutForNode:self->nodeC].size;
OCMExpect([self->nodeC.mock calculateLayoutThatFits:ASSizeRangeMake(cPendingSize)]).onMainThread();
}
[self->window layoutIfNeeded];
as_log_verbose(OS_LOG_DEFAULT, "End injectedMainThreadWork");
};
measurementCompletionBlock = ^{
XCTAssert(injectedMainThreadWorkDone, @"We hoped to get onto the main thread before the measurementCompletion callback ran.");
};
for (ASLayoutTestNode *node in allNodes) {
OCMVerifyAllWithDelay(node.mock, verifyDelay);
}
// Incorrect behavior: The transition will "win" even though its transitioning to stale data.
if (enforceCorrectBehavior) {
[self verifyFixture:fixture4];
} else {
[self verifyFixture:fixture2];
}
}
/**
* Start on fixture 3 where nodeB is force-shrunk via multipass layout.
* Apply fixture 1, which just changes nodeB's size and calls -setNeedsLayout on it.
*
* This behavior is currently broken. See implementation for correct behavior and incorrect behavior.
*/
- (void)testCallingSetNeedsLayoutOnANodeThatWasSubjectToMultipassLayout
{
static BOOL const enforceCorrectBehavior = NO;
[self stubCalculatedLayoutDidChange];
[self runFirstLayoutPassWithFixture:fixture3];
// Switch to fixture 1, updating nodeB's desired size and calling -setNeedsLayout
// Now nodeB will fit happily into the stack.
[fixture1 apply];
if (enforceCorrectBehavior) {
/*
* Correct behavior: nodeB is remeasured against the first (unconstrained) size
* and when it's discovered that now nodeB fits, nodeA will re-layout and we'll
* end up correctly at fixture1.
*/
OCMExpect([nodeB.mock calculateLayoutThatFits:[fixture3 firstSizeRangeForNode:nodeB]]);
[fixture1 withSizeRangesForNode:nodeA block:^(ASSizeRange sizeRange) {
OCMExpect([self->nodeA.mock calculateLayoutThatFits:sizeRange]);
}];
[window layoutIfNeeded];
[self verifyFixture:fixture1];
} else {
/*
* Incorrect behavior: nodeB is remeasured against the second (fixed-width) constraint.
* The returned value (8) is clamped to the fixed with (7), and then compared to the previous
* width (7) and we decide not to propagate up the invalidation, and we stay stuck on the old
* layout (fixture3).
*/
OCMExpect([nodeB.mock calculateLayoutThatFits:nodeB.constrainedSizeForCalculatedLayout]);
[window layoutIfNeeded];
[self verifyFixture:fixture3];
}
}
#pragma mark - Helpers
- (void)verifyFixture:(ASTLayoutFixture *)fixture
{
const auto expected = fixture.layout;
// Ensure expected == frames
const auto frames = [fixture.rootNode currentLayoutBasedOnFrames];
if (![expected isEqual:frames]) {
XCTFail(@"\n*** Layout verification failed – frames don't match expected. ***\nGot:\n%@\nExpected:\n%@", [frames recursiveDescription], [expected recursiveDescription]);
}
// Ensure expected == calculatedLayout
const auto calculated = fixture.rootNode.calculatedLayout;
if (![expected isEqual:calculated]) {
XCTFail(@"\n*** Layout verification failed – calculated layout doesn't match expected. ***\nGot:\n%@\nExpected:\n%@", [calculated recursiveDescription], [expected recursiveDescription]);
}
}
/**
* Stubs calculatedLayoutDidChange for all nodes.
*
* It's not really a core layout engine method, and it's also
* currently bugged and gets called a lot so for most
* tests its better not to have expectations about it littered around.
* https://github.com/TextureGroup/Texture/issues/422
*/
- (void)stubCalculatedLayoutDidChange
{
stubbedCalculatedLayoutDidChange = YES;
for (ASLayoutTestNode *node in allNodes) {
OCMStub([node.mock calculatedLayoutDidChange]);
}
}
/**
* Fixture 1: A basic horizontal stack, all single-pass.
*
* [A: HorizStack([B, C, D])]. A is (10x1), B is (1x1), C is (2x1), D is (1x1)
*/
- (ASTLayoutFixture *)createFixture1
{
const auto fixture = [[ASTLayoutFixture alloc] init];
// nodeB
[fixture addSizeRange:{{0, 0}, {INFINITY, 1}} forNode:nodeB];
const auto layoutB = [ASLayout layoutWithLayoutElement:nodeB size:{1,1} position:{0,0} sublayouts:nil];
// nodeC
[fixture addSizeRange:{{0, 0}, {INFINITY, 1}} forNode:nodeC];
const auto layoutC = [ASLayout layoutWithLayoutElement:nodeC size:{2,1} position:{4,0} sublayouts:nil];
// nodeD
[fixture addSizeRange:{{0, 0}, {INFINITY, 1}} forNode:nodeD];
const auto layoutD = [ASLayout layoutWithLayoutElement:nodeD size:{1,1} position:{9,0} sublayouts:nil];
[fixture addSizeRange:{{10, 1}, {10, 1}} forNode:nodeA];
const auto layoutA = [ASLayout layoutWithLayoutElement:nodeA size:{10,1} position:ASPointNull sublayouts:@[ layoutB, layoutC, layoutD ]];
fixture.layout = layoutA;
[fixture.layoutSpecBlocks setObject:fixture1and3and5NodeALayoutSpecBlock forKey:nodeA];
return fixture;
}
/**
* Fixture 2: A simple transition away from fixture 1.
*
* [A: HorizStack([B, C, E])]. A is (10x1), B is (1x1), C is (4x1), E is (1x1)
*
* From fixture 1:
* B survives with same layout
* C survives with new layout
* D is removed
* E joins with first layout
*/
- (ASTLayoutFixture *)createFixture2
{
const auto fixture = [[ASTLayoutFixture alloc] init];
// nodeB
[fixture addSizeRange:{{0, 0}, {INFINITY, 1}} forNode:nodeB];
const auto layoutB = [ASLayout layoutWithLayoutElement:nodeB size:{1,1} position:{0,0} sublayouts:nil];
// nodeC
[fixture addSizeRange:{{0, 0}, {INFINITY, 1}} forNode:nodeC];
const auto layoutC = [ASLayout layoutWithLayoutElement:nodeC size:{4,1} position:{3,0} sublayouts:nil];
// nodeE
[fixture addSizeRange:{{0, 0}, {INFINITY, 1}} forNode:nodeE];
const auto layoutE = [ASLayout layoutWithLayoutElement:nodeE size:{1,1} position:{9,0} sublayouts:nil];
[fixture addSizeRange:{{10, 1}, {10, 1}} forNode:nodeA];
const auto layoutA = [ASLayout layoutWithLayoutElement:nodeA size:{10,1} position:ASPointNull sublayouts:@[ layoutB, layoutC, layoutE ]];
fixture.layout = layoutA;
ASLayoutSpecBlock specBlockA = ^ASLayoutSpec * _Nonnull(__kindof ASDisplayNode * _Nonnull node, ASSizeRange constrainedSize) {
return [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionHorizontal spacing:0 justifyContent:ASStackLayoutJustifyContentSpaceBetween alignItems:ASStackLayoutAlignItemsStart children:@[ self->nodeB, self->nodeC, self->nodeE ]];
};
[fixture.layoutSpecBlocks setObject:specBlockA forKey:nodeA];
return fixture;
}
/**
* Fixture 3: Multipass stack layout
*
* [A: HorizStack([B, C, D])]. A is (10x1), B is (7x1), C is (2x1), D is (1x1)
*
* nodeB (which has flexShrink=1) will return 8x1 for its size during the first
* stack pass, and it'll be subject to a second pass where it returns 7x1.
*
*/
- (ASTLayoutFixture *)createFixture3
{
const auto fixture = [[ASTLayoutFixture alloc] init];
// nodeB wants 8,1 but it will settle for 7,1
[fixture setReturnedSize:{8,1} forNode:nodeB];
[fixture addSizeRange:{{0, 0}, {INFINITY, 1}} forNode:nodeB];
[fixture addSizeRange:{{7, 0}, {7, 1}} forNode:nodeB];
const auto layoutB = [ASLayout layoutWithLayoutElement:nodeB size:{7,1} position:{0,0} sublayouts:nil];
// nodeC
[fixture addSizeRange:{{0, 0}, {INFINITY, 1}} forNode:nodeC];
const auto layoutC = [ASLayout layoutWithLayoutElement:nodeC size:{2,1} position:{7,0} sublayouts:nil];
// nodeD
[fixture addSizeRange:{{0, 0}, {INFINITY, 1}} forNode:nodeD];
const auto layoutD = [ASLayout layoutWithLayoutElement:nodeD size:{1,1} position:{9,0} sublayouts:nil];
[fixture addSizeRange:{{10, 1}, {10, 1}} forNode:nodeA];
const auto layoutA = [ASLayout layoutWithLayoutElement:nodeA size:{10,1} position:ASPointNull sublayouts:@[ layoutB, layoutC, layoutD ]];
fixture.layout = layoutA;
[fixture.layoutSpecBlocks setObject:fixture1and3and5NodeALayoutSpecBlock forKey:nodeA];
return fixture;
}
/**
* Fixture 4: A different simple transition away from fixture 1.
*
* [A: HorizStack([B, D, E])]. A is (10x1), B is (1x1), D is (2x1), E is (1x1)
*
* From fixture 1:
* B survives with same layout
* C is removed
* D survives with new layout
* E joins with first layout
*
* From fixture 2:
* B survives with same layout
* C is removed
* D joins with first layout
* E survives with same layout
*/
- (ASTLayoutFixture *)createFixture4
{
const auto fixture = [[ASTLayoutFixture alloc] init];
// nodeB
[fixture addSizeRange:{{0, 0}, {INFINITY, 1}} forNode:nodeB];
const auto layoutB = [ASLayout layoutWithLayoutElement:nodeB size:{1,1} position:{0,0} sublayouts:nil];
// nodeD
[fixture addSizeRange:{{0, 0}, {INFINITY, 1}} forNode:nodeD];
const auto layoutD = [ASLayout layoutWithLayoutElement:nodeD size:{2,1} position:{4,0} sublayouts:nil];
// nodeE
[fixture addSizeRange:{{0, 0}, {INFINITY, 1}} forNode:nodeE];
const auto layoutE = [ASLayout layoutWithLayoutElement:nodeE size:{1,1} position:{9,0} sublayouts:nil];
[fixture addSizeRange:{{10, 1}, {10, 1}} forNode:nodeA];
const auto layoutA = [ASLayout layoutWithLayoutElement:nodeA size:{10,1} position:ASPointNull sublayouts:@[ layoutB, layoutD, layoutE ]];
fixture.layout = layoutA;
ASLayoutSpecBlock specBlockA = ^ASLayoutSpec * _Nonnull(__kindof ASDisplayNode * _Nonnull node, ASSizeRange constrainedSize) {
return [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionHorizontal spacing:0 justifyContent:ASStackLayoutJustifyContentSpaceBetween alignItems:ASStackLayoutAlignItemsStart children:@[ self->nodeB, self->nodeD, self->nodeE ]];
};
[fixture.layoutSpecBlocks setObject:specBlockA forKey:nodeA];
return fixture;
}
/**
* Fixture 5: Same as fixture 1, but with a bigger root node (node A).
*
* [A: HorizStack([B, C, D])]. A is (15x1), B is (1x1), C is (2x1), D is (1x1)
*/
- (ASTLayoutFixture *)createFixture5
{
const auto fixture = [[ASTLayoutFixture alloc] init];
// nodeB
[fixture addSizeRange:{{0, 0}, {INFINITY, 1}} forNode:nodeB];
const auto layoutB = [ASLayout layoutWithLayoutElement:nodeB size:{1,1} position:{0,0} sublayouts:nil];
// nodeC
[fixture addSizeRange:{{0, 0}, {INFINITY, 1}} forNode:nodeC];
const auto layoutC = [ASLayout layoutWithLayoutElement:nodeC size:{2,1} position:{4,0} sublayouts:nil];
// nodeD
[fixture addSizeRange:{{0, 0}, {INFINITY, 1}} forNode:nodeD];
const auto layoutD = [ASLayout layoutWithLayoutElement:nodeD size:{1,1} position:{9,0} sublayouts:nil];
[fixture addSizeRange:{{15, 1}, {15, 1}} forNode:nodeA];
const auto layoutA = [ASLayout layoutWithLayoutElement:nodeA size:{15,1} position:ASPointNull sublayouts:@[ layoutB, layoutC, layoutD ]];
fixture.layout = layoutA;
[fixture.layoutSpecBlocks setObject:fixture1and3and5NodeALayoutSpecBlock forKey:nodeA];
return fixture;
}
- (void)runFirstLayoutPassWithFixture:(ASTLayoutFixture *)fixture
{
[fixture apply];
[fixture withSizeRangesForAllNodesUsingBlock:^(ASLayoutTestNode * _Nonnull node, ASSizeRange sizeRange) {
OCMExpect([node.mock calculateLayoutThatFits:sizeRange]).onMainThread();
if (!self->stubbedCalculatedLayoutDidChange) {
OCMExpect([node.mock calculatedLayoutDidChange]).onMainThread();
}
}];
// Trigger CA layout pass.
[window layoutIfNeeded];
// Make sure it went through.
[self verifyFixture:fixture];
}
@end