forked from TextureGroup/Texture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathASStackLayoutSpecSnapshotTests.mm
1395 lines (1145 loc) · 63.2 KB
/
ASStackLayoutSpecSnapshotTests.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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// ASStackLayoutSpecSnapshotTests.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 "ASLayoutSpecSnapshotTestsHelper.h"
#import <AsyncDisplayKit/ASStackLayoutSpec.h>
#import <AsyncDisplayKit/ASStackLayoutSpecUtilities.h>
#import <AsyncDisplayKit/ASBackgroundLayoutSpec.h>
#import <AsyncDisplayKit/ASRatioLayoutSpec.h>
#import <AsyncDisplayKit/ASInsetLayoutSpec.h>
#import <AsyncDisplayKit/ASTextNode.h>
@interface ASStackLayoutSpecSnapshotTests : ASLayoutSpecSnapshotTestCase
@end
@implementation ASStackLayoutSpecSnapshotTests
#pragma mark - Utility methods
static NSArray<ASDisplayNode *> *defaultSubnodes()
{
return defaultSubnodesWithSameSize(CGSizeZero, 0);
}
static NSArray<ASDisplayNode *> *defaultSubnodesWithSameSize(CGSize subnodeSize, CGFloat flex)
{
NSArray<ASDisplayNode *> *subnodes = @[
ASDisplayNodeWithBackgroundColor([UIColor redColor], subnodeSize),
ASDisplayNodeWithBackgroundColor([UIColor blueColor], subnodeSize),
ASDisplayNodeWithBackgroundColor([UIColor greenColor], subnodeSize)
];
for (ASDisplayNode *subnode in subnodes) {
subnode.style.flexGrow = flex;
subnode.style.flexShrink = flex;
}
return subnodes;
}
static void setCGSizeToNode(CGSize size, ASDisplayNode *node)
{
node.style.width = ASDimensionMakeWithPoints(size.width);
node.style.height = ASDimensionMakeWithPoints(size.height);
}
static NSArray<ASTextNode*> *defaultTextNodes()
{
ASTextNode *textNode1 = [[ASTextNode alloc] init];
textNode1.attributedText = [[NSAttributedString alloc] initWithString:@"Hello"
attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:20]}];
textNode1.backgroundColor = [UIColor redColor];
textNode1.layerBacked = YES;
ASTextNode *textNode2 = [[ASTextNode alloc] init];
textNode2.attributedText = [[NSAttributedString alloc] initWithString:@"Why, hello there! How are you?"
attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:12]}];
textNode2.backgroundColor = [UIColor blueColor];
textNode2.layerBacked = YES;
return @[textNode1, textNode2];
}
- (void)testStackLayoutSpecWithJustify:(ASStackLayoutJustifyContent)justify
flexFactor:(CGFloat)flex
sizeRange:(ASSizeRange)sizeRange
identifier:(NSString *)identifier
{
ASStackLayoutSpecStyle style = {
.direction = ASStackLayoutDirectionHorizontal,
.justifyContent = justify
};
NSArray<ASDisplayNode *> *subnodes = defaultSubnodesWithSameSize({50, 50}, flex);
[self testStackLayoutSpecWithStyle:style sizeRange:sizeRange subnodes:subnodes identifier:identifier];
}
- (void)testStackLayoutSpecWithStyle:(ASStackLayoutSpecStyle)style
sizeRange:(ASSizeRange)sizeRange
subnodes:(NSArray *)subnodes
identifier:(NSString *)identifier
{
[self testStackLayoutSpecWithStyle:style children:subnodes sizeRange:sizeRange subnodes:subnodes identifier:identifier];
}
- (void)testStackLayoutSpecWithStyle:(ASStackLayoutSpecStyle)style
children:(NSArray *)children
sizeRange:(ASSizeRange)sizeRange
subnodes:(NSArray *)subnodes
identifier:(NSString *)identifier
{
ASStackLayoutSpec *stackLayoutSpec =
[ASStackLayoutSpec
stackLayoutSpecWithDirection:style.direction
spacing:style.spacing
justifyContent:style.justifyContent
alignItems:style.alignItems
flexWrap:style.flexWrap
alignContent:style.alignContent
lineSpacing:style.lineSpacing
children:children];
[self testStackLayoutSpec:stackLayoutSpec sizeRange:sizeRange subnodes:subnodes identifier:identifier];
}
- (void)testStackLayoutSpecWithDirection:(ASStackLayoutDirection)direction
itemsHorizontalAlignment:(ASHorizontalAlignment)horizontalAlignment
itemsVerticalAlignment:(ASVerticalAlignment)verticalAlignment
identifier:(NSString *)identifier
{
NSArray<ASDisplayNode *> *subnodes = defaultSubnodesWithSameSize({50, 50}, 0);
ASStackLayoutSpec *stackLayoutSpec = [[ASStackLayoutSpec alloc] init];
stackLayoutSpec.direction = direction;
stackLayoutSpec.children = subnodes;
stackLayoutSpec.horizontalAlignment = horizontalAlignment;
stackLayoutSpec.verticalAlignment = verticalAlignment;
CGSize exactSize = CGSizeMake(200, 200);
static ASSizeRange kSize = ASSizeRangeMake(exactSize, exactSize);
[self testStackLayoutSpec:stackLayoutSpec sizeRange:kSize subnodes:subnodes identifier:identifier];
}
- (void)testStackLayoutSpecWithBaselineAlignment:(ASStackLayoutAlignItems)baselineAlignment
identifier:(NSString *)identifier
{
NSAssert(baselineAlignment == ASStackLayoutAlignItemsBaselineFirst || baselineAlignment == ASStackLayoutAlignItemsBaselineLast, @"Unexpected baseline alignment");
NSArray<ASTextNode *> *textNodes = defaultTextNodes();
textNodes[1].style.flexShrink = 1.0;
ASStackLayoutSpec *stackLayoutSpec = [ASStackLayoutSpec horizontalStackLayoutSpec];
stackLayoutSpec.children = textNodes;
stackLayoutSpec.alignItems = baselineAlignment;
static ASSizeRange kSize = ASSizeRangeMake(CGSizeMake(150, 0), CGSizeMake(150, CGFLOAT_MAX));
[self testStackLayoutSpec:stackLayoutSpec sizeRange:kSize subnodes:textNodes identifier:identifier];
}
- (void)testStackLayoutSpec:(ASStackLayoutSpec *)stackLayoutSpec
sizeRange:(ASSizeRange)sizeRange
subnodes:(NSArray *)subnodes
identifier:(NSString *)identifier
{
ASDisplayNode *backgroundNode = ASDisplayNodeWithBackgroundColor([UIColor whiteColor]);
ASLayoutSpec *layoutSpec = [ASBackgroundLayoutSpec backgroundLayoutSpecWithChild:stackLayoutSpec background:backgroundNode];
NSMutableArray *newSubnodes = [NSMutableArray arrayWithObject:backgroundNode];
[newSubnodes addObjectsFromArray:subnodes];
[self testLayoutSpec:layoutSpec sizeRange:sizeRange subnodes:newSubnodes identifier:identifier];
}
- (void)testStackLayoutSpecWithAlignContent:(ASStackLayoutAlignContent)alignContent
lineSpacing:(CGFloat)lineSpacing
sizeRange:(ASSizeRange)sizeRange
identifier:(NSString *)identifier
{
ASStackLayoutSpecStyle style = {
.direction = ASStackLayoutDirectionHorizontal,
.flexWrap = ASStackLayoutFlexWrapWrap,
.alignContent = alignContent,
.lineSpacing = lineSpacing,
};
CGSize subnodeSize = {50, 50};
NSArray<ASDisplayNode *> *subnodes = @[
ASDisplayNodeWithBackgroundColor([UIColor redColor], subnodeSize),
ASDisplayNodeWithBackgroundColor([UIColor yellowColor], subnodeSize),
ASDisplayNodeWithBackgroundColor([UIColor blueColor], subnodeSize),
ASDisplayNodeWithBackgroundColor([UIColor magentaColor], subnodeSize),
ASDisplayNodeWithBackgroundColor([UIColor greenColor], subnodeSize),
ASDisplayNodeWithBackgroundColor([UIColor cyanColor], subnodeSize),
];
[self testStackLayoutSpecWithStyle:style sizeRange:sizeRange subnodes:subnodes identifier:identifier];
}
- (void)testStackLayoutSpecWithAlignContent:(ASStackLayoutAlignContent)alignContent
sizeRange:(ASSizeRange)sizeRange
identifier:(NSString *)identifier
{
[self testStackLayoutSpecWithAlignContent:alignContent lineSpacing:0.0 sizeRange:sizeRange identifier:identifier];
}
#pragma mark -
- (void)testDefaultStackLayoutElementFlexProperties
{
ASDisplayNode *displayNode = [[ASDisplayNode alloc] init];
XCTAssertEqual(displayNode.style.flexShrink, NO);
XCTAssertEqual(displayNode.style.flexGrow, NO);
const ASDimension unconstrainedDimension = ASDimensionAuto;
const ASDimension flexBasis = displayNode.style.flexBasis;
XCTAssertEqual(flexBasis.unit, unconstrainedDimension.unit);
XCTAssertEqual(flexBasis.value, unconstrainedDimension.value);
}
- (void)testUnderflowBehaviors
{
// width 300px; height 0-300px
static ASSizeRange kSize = {{300, 0}, {300, 300}};
[self testStackLayoutSpecWithJustify:ASStackLayoutJustifyContentStart flexFactor:0 sizeRange:kSize identifier:@"justifyStart"];
[self testStackLayoutSpecWithJustify:ASStackLayoutJustifyContentCenter flexFactor:0 sizeRange:kSize identifier:@"justifyCenter"];
[self testStackLayoutSpecWithJustify:ASStackLayoutJustifyContentEnd flexFactor:0 sizeRange:kSize identifier:@"justifyEnd"];
[self testStackLayoutSpecWithJustify:ASStackLayoutJustifyContentStart flexFactor:1 sizeRange:kSize identifier:@"flex"];
[self testStackLayoutSpecWithJustify:ASStackLayoutJustifyContentSpaceBetween flexFactor:0 sizeRange:kSize identifier:@"justifySpaceBetween"];
[self testStackLayoutSpecWithJustify:ASStackLayoutJustifyContentSpaceAround flexFactor:0 sizeRange:kSize identifier:@"justifySpaceAround"];
}
- (void)testOverflowBehaviors
{
// width 110px; height 0-300px
static ASSizeRange kSize = {{110, 0}, {110, 300}};
[self testStackLayoutSpecWithJustify:ASStackLayoutJustifyContentStart flexFactor:0 sizeRange:kSize identifier:@"justifyStart"];
[self testStackLayoutSpecWithJustify:ASStackLayoutJustifyContentCenter flexFactor:0 sizeRange:kSize identifier:@"justifyCenter"];
[self testStackLayoutSpecWithJustify:ASStackLayoutJustifyContentEnd flexFactor:0 sizeRange:kSize identifier:@"justifyEnd"];
[self testStackLayoutSpecWithJustify:ASStackLayoutJustifyContentStart flexFactor:1 sizeRange:kSize identifier:@"flex"];
// On overflow, "space between" is identical to "content start"
[self testStackLayoutSpecWithJustify:ASStackLayoutJustifyContentSpaceBetween flexFactor:0 sizeRange:kSize identifier:@"justifyStart"];
// On overflow, "space around" is identical to "content center"
[self testStackLayoutSpecWithJustify:ASStackLayoutJustifyContentSpaceAround flexFactor:0 sizeRange:kSize identifier:@"justifyCenter"];
}
- (void)testOverflowBehaviorsWhenAllFlexShrinkChildrenHaveBeenClampedToZeroButViolationStillExists
{
ASStackLayoutSpecStyle style = {.direction = ASStackLayoutDirectionHorizontal};
NSArray<ASDisplayNode *> *subnodes = defaultSubnodesWithSameSize({50, 50}, 0);
subnodes[1].style.flexShrink = 1;
// Width is 75px--that's less than the sum of the widths of the children, which is 100px.
static ASSizeRange kSize = {{75, 0}, {75, 150}};
[self testStackLayoutSpecWithStyle:style sizeRange:kSize subnodes:subnodes identifier:nil];
}
- (void)testFlexWithUnequalIntrinsicSizes
{
ASStackLayoutSpecStyle style = {.direction = ASStackLayoutDirectionHorizontal};
NSArray<ASDisplayNode *> *subnodes = defaultSubnodesWithSameSize({50, 50}, 1);
setCGSizeToNode({150, 150}, subnodes[1]);
// width 300px; height 0-150px.
static ASSizeRange kUnderflowSize = {{300, 0}, {300, 150}};
[self testStackLayoutSpecWithStyle:style sizeRange:kUnderflowSize subnodes:subnodes identifier:@"underflow"];
// width 200px; height 0-150px.
static ASSizeRange kOverflowSize = {{200, 0}, {200, 150}};
[self testStackLayoutSpecWithStyle:style sizeRange:kOverflowSize subnodes:subnodes identifier:@"overflow"];
}
- (void)testCrossAxisSizeBehaviors
{
ASStackLayoutSpecStyle style = {.direction = ASStackLayoutDirectionVertical};
NSArray<ASDisplayNode *> *subnodes = defaultSubnodes();
setCGSizeToNode({50, 50}, subnodes[0]);
setCGSizeToNode({100, 50}, subnodes[1]);
setCGSizeToNode({150, 50}, subnodes[2]);
// width 0-300px; height 300px
static ASSizeRange kVariableHeight = {{0, 300}, {300, 300}};
[self testStackLayoutSpecWithStyle:style sizeRange:kVariableHeight subnodes:subnodes identifier:@"variableHeight"];
// width 300px; height 300px
static ASSizeRange kFixedHeight = {{300, 300}, {300, 300}};
[self testStackLayoutSpecWithStyle:style sizeRange:kFixedHeight subnodes:subnodes identifier:@"fixedHeight"];
}
- (void)testStackSpacing
{
ASStackLayoutSpecStyle style = {
.direction = ASStackLayoutDirectionVertical,
.spacing = 10
};
NSArray<ASDisplayNode *> *subnodes = defaultSubnodes();
setCGSizeToNode({50, 50}, subnodes[0]);
setCGSizeToNode({100, 50}, subnodes[1]);
setCGSizeToNode({150, 50}, subnodes[2]);
// width 0-300px; height 300px
static ASSizeRange kVariableHeight = {{0, 300}, {300, 300}};
[self testStackLayoutSpecWithStyle:style sizeRange:kVariableHeight subnodes:subnodes identifier:@"variableHeight"];
}
- (void)testStackSpacingWithChildrenHavingNilObjects
{
// This should take a zero height since all children have a nil node. If it takes a height > 0, a blue background
// will show up, hence failing the test.
ASDisplayNode *backgroundNode = ASDisplayNodeWithBackgroundColor([UIColor blueColor]);
ASLayoutSpec *layoutSpec =
[ASInsetLayoutSpec
insetLayoutSpecWithInsets:{10, 10, 10, 10}
child:
[ASBackgroundLayoutSpec
backgroundLayoutSpecWithChild:
[ASStackLayoutSpec
stackLayoutSpecWithDirection:ASStackLayoutDirectionVertical
spacing:10
justifyContent:ASStackLayoutJustifyContentStart
alignItems:ASStackLayoutAlignItemsStretch
children:@[]]
background:backgroundNode]];
// width 300px; height 0-300px
static ASSizeRange kVariableHeight = {{300, 0}, {300, 300}};
[self testLayoutSpec:layoutSpec sizeRange:kVariableHeight subnodes:@[backgroundNode] identifier:@"variableHeight"];
}
- (void)testChildSpacing
{
// width 0-INF; height 0-INF
static ASSizeRange kAnySize = {{0, 0}, {INFINITY, INFINITY}};
ASStackLayoutSpecStyle style = {.direction = ASStackLayoutDirectionVertical};
NSArray<ASDisplayNode *> *subnodes = defaultSubnodes();
setCGSizeToNode({50, 50}, subnodes[0]);
setCGSizeToNode({100, 70}, subnodes[1]);
setCGSizeToNode({150, 90}, subnodes[2]);
subnodes[1].style.spacingBefore = 10;
subnodes[2].style.spacingBefore = 20;
[self testStackLayoutSpecWithStyle:style sizeRange:kAnySize subnodes:subnodes identifier:@"spacingBefore"];
// Reset above spacing values
subnodes[1].style.spacingBefore = 0;
subnodes[2].style.spacingBefore = 0;
subnodes[1].style.spacingAfter = 10;
subnodes[2].style.spacingAfter = 20;
[self testStackLayoutSpecWithStyle:style sizeRange:kAnySize subnodes:subnodes identifier:@"spacingAfter"];
// Reset above spacing values
subnodes[1].style.spacingAfter = 0;
subnodes[2].style.spacingAfter = 0;
style.spacing = 10;
subnodes[1].style.spacingBefore = -10;
subnodes[1].style.spacingAfter = -10;
[self testStackLayoutSpecWithStyle:style sizeRange:kAnySize subnodes:subnodes identifier:@"spacingBalancedOut"];
}
- (void)testJustifiedCenterWithChildSpacing
{
ASStackLayoutSpecStyle style = {
.direction = ASStackLayoutDirectionVertical,
.justifyContent = ASStackLayoutJustifyContentCenter
};
NSArray<ASDisplayNode *> *subnodes = defaultSubnodes();
setCGSizeToNode({50, 50}, subnodes[0]);
setCGSizeToNode({100, 70}, subnodes[1]);
setCGSizeToNode({150, 90}, subnodes[2]);
subnodes[0].style.spacingBefore = 0;
subnodes[1].style.spacingBefore = 20;
subnodes[2].style.spacingBefore = 30;
// width 0-300px; height 300px
static ASSizeRange kVariableHeight = {{0, 300}, {300, 300}};
[self testStackLayoutSpecWithStyle:style sizeRange:kVariableHeight subnodes:subnodes identifier:@"variableHeight"];
}
- (void)testJustifiedSpaceBetweenWithOneChild
{
ASStackLayoutSpecStyle style = {
.direction = ASStackLayoutDirectionHorizontal,
.justifyContent = ASStackLayoutJustifyContentSpaceBetween
};
ASDisplayNode *child = ASDisplayNodeWithBackgroundColor([UIColor redColor], {50, 50});
// width 300px; height 0-INF
static ASSizeRange kVariableHeight = {{300, 0}, {300, INFINITY}};
[self testStackLayoutSpecWithStyle:style sizeRange:kVariableHeight subnodes:@[child] identifier:nil];
}
- (void)testJustifiedSpaceAroundWithOneChild
{
ASStackLayoutSpecStyle style = {
.direction = ASStackLayoutDirectionHorizontal,
.justifyContent = ASStackLayoutJustifyContentSpaceAround
};
ASDisplayNode *child = ASDisplayNodeWithBackgroundColor([UIColor redColor], {50, 50});
// width 300px; height 0-INF
static ASSizeRange kVariableHeight = {{300, 0}, {300, INFINITY}};
[self testStackLayoutSpecWithStyle:style sizeRange:kVariableHeight subnodes:@[child] identifier:nil];
}
- (void)testJustifiedSpaceBetweenWithRemainingSpace
{
// width 301px; height 0-300px;
static ASSizeRange kSize = {{301, 0}, {301, 300}};
[self testStackLayoutSpecWithJustify:ASStackLayoutJustifyContentSpaceBetween flexFactor:0 sizeRange:kSize identifier:nil];
}
- (void)testJustifiedSpaceAroundWithRemainingSpace
{
// width 305px; height 0-300px;
static ASSizeRange kSize = {{305, 0}, {305, 300}};
[self testStackLayoutSpecWithJustify:ASStackLayoutJustifyContentSpaceAround flexFactor:0 sizeRange:kSize identifier:nil];
}
- (void)testChildThatChangesCrossSizeWhenMainSizeIsFlexed
{
ASStackLayoutSpecStyle style = {.direction = ASStackLayoutDirectionHorizontal};
ASDisplayNode *subnode1 = ASDisplayNodeWithBackgroundColor([UIColor blueColor]);
ASDisplayNode *subnode2 = ASDisplayNodeWithBackgroundColor([UIColor redColor], {50, 50});
ASRatioLayoutSpec *child1 = [ASRatioLayoutSpec ratioLayoutSpecWithRatio:1.5 child:subnode1];
child1.style.flexBasis = ASDimensionMakeWithFraction(1);
child1.style.flexGrow = 1;
child1.style.flexShrink = 1;
static ASSizeRange kFixedWidth = {{150, 0}, {150, INFINITY}};
[self testStackLayoutSpecWithStyle:style children:@[child1, subnode2] sizeRange:kFixedWidth subnodes:@[subnode1, subnode2] identifier:nil];
}
- (void)testAlignCenterWithFlexedMainDimension
{
ASStackLayoutSpecStyle style = {
.direction = ASStackLayoutDirectionVertical,
.alignItems = ASStackLayoutAlignItemsCenter
};
NSArray<ASDisplayNode *> *subnodes = @[
ASDisplayNodeWithBackgroundColor([UIColor redColor], {100, 100}),
ASDisplayNodeWithBackgroundColor([UIColor blueColor], {50, 50})
];
subnodes[0].style.flexShrink = 1;
subnodes[1].style.flexShrink = 1;
static ASSizeRange kFixedWidth = {{150, 0}, {150, 100}};
[self testStackLayoutSpecWithStyle:style sizeRange:kFixedWidth subnodes:subnodes identifier:nil];
}
- (void)testAlignCenterWithIndefiniteCrossDimension
{
ASStackLayoutSpecStyle style = {.direction = ASStackLayoutDirectionHorizontal};
ASDisplayNode *subnode1 = ASDisplayNodeWithBackgroundColor([UIColor redColor], {100, 100});
ASDisplayNode *subnode2 = ASDisplayNodeWithBackgroundColor([UIColor blueColor], {50, 50});
subnode2.style.alignSelf = ASStackLayoutAlignSelfCenter;
NSArray<ASDisplayNode *> *subnodes = @[subnode1, subnode2];
static ASSizeRange kFixedWidth = {{150, 0}, {150, INFINITY}};
[self testStackLayoutSpecWithStyle:style sizeRange:kFixedWidth subnodes:subnodes identifier:nil];
}
- (void)testAlignedStart
{
ASStackLayoutSpecStyle style = {
.direction = ASStackLayoutDirectionVertical,
.justifyContent = ASStackLayoutJustifyContentCenter,
.alignItems = ASStackLayoutAlignItemsStart
};
NSArray<ASDisplayNode *> *subnodes = defaultSubnodes();
setCGSizeToNode({50, 50}, subnodes[0]);
setCGSizeToNode({100, 70}, subnodes[1]);
setCGSizeToNode({150, 90}, subnodes[2]);
subnodes[0].style.spacingBefore = 0;
subnodes[1].style.spacingBefore = 20;
subnodes[2].style.spacingBefore = 30;
static ASSizeRange kExactSize = {{300, 300}, {300, 300}};
[self testStackLayoutSpecWithStyle:style sizeRange:kExactSize subnodes:subnodes identifier:nil];
}
- (void)testAlignedEnd
{
ASStackLayoutSpecStyle style = {
.direction = ASStackLayoutDirectionVertical,
.justifyContent = ASStackLayoutJustifyContentCenter,
.alignItems = ASStackLayoutAlignItemsEnd
};
NSArray<ASDisplayNode *> *subnodes = defaultSubnodes();
setCGSizeToNode({50, 50}, subnodes[0]);
setCGSizeToNode({100, 70}, subnodes[1]);
setCGSizeToNode({150, 90}, subnodes[2]);
subnodes[0].style.spacingBefore = 0;
subnodes[1].style.spacingBefore = 20;
subnodes[2].style.spacingBefore = 30;
static ASSizeRange kExactSize = {{300, 300}, {300, 300}};
[self testStackLayoutSpecWithStyle:style sizeRange:kExactSize subnodes:subnodes identifier:nil];
}
- (void)testAlignedCenter
{
ASStackLayoutSpecStyle style = {
.direction = ASStackLayoutDirectionVertical,
.justifyContent = ASStackLayoutJustifyContentCenter,
.alignItems = ASStackLayoutAlignItemsCenter
};
NSArray<ASDisplayNode *> *subnodes = defaultSubnodes();
setCGSizeToNode({50, 50}, subnodes[0]);
setCGSizeToNode({100, 70}, subnodes[1]);
setCGSizeToNode({150, 90}, subnodes[2]);
subnodes[0].style.spacingBefore = 0;
subnodes[1].style.spacingBefore = 20;
subnodes[2].style.spacingBefore = 30;
static ASSizeRange kExactSize = {{300, 300}, {300, 300}};
[self testStackLayoutSpecWithStyle:style sizeRange:kExactSize subnodes:subnodes identifier:nil];
}
- (void)testAlignedStretchNoChildExceedsMin
{
ASStackLayoutSpecStyle style = {
.direction = ASStackLayoutDirectionVertical,
.justifyContent = ASStackLayoutJustifyContentCenter,
.alignItems = ASStackLayoutAlignItemsStretch
};
NSArray<ASDisplayNode *> *subnodes = defaultSubnodes();
setCGSizeToNode({50, 50}, subnodes[0]);
setCGSizeToNode({100, 70}, subnodes[1]);
setCGSizeToNode({150, 90}, subnodes[2]);
subnodes[0].style.spacingBefore = 0;
subnodes[1].style.spacingBefore = 20;
subnodes[2].style.spacingBefore = 30;
static ASSizeRange kVariableSize = {{200, 200}, {300, 300}};
// all children should be 200px wide
[self testStackLayoutSpecWithStyle:style sizeRange:kVariableSize subnodes:subnodes identifier:nil];
}
- (void)testAlignedStretchOneChildExceedsMin
{
ASStackLayoutSpecStyle style = {
.direction = ASStackLayoutDirectionVertical,
.justifyContent = ASStackLayoutJustifyContentCenter,
.alignItems = ASStackLayoutAlignItemsStretch
};
NSArray<ASDisplayNode *> *subnodes = defaultSubnodes();
setCGSizeToNode({50, 50}, subnodes[0]);
setCGSizeToNode({100, 70}, subnodes[1]);
setCGSizeToNode({150, 90}, subnodes[2]);
subnodes[0].style.spacingBefore = 0;
subnodes[1].style.spacingBefore = 20;
subnodes[2].style.spacingBefore = 30;
static ASSizeRange kVariableSize = {{50, 50}, {300, 300}};
// all children should be 150px wide
[self testStackLayoutSpecWithStyle:style sizeRange:kVariableSize subnodes:subnodes identifier:nil];
}
- (void)testEmptyStack
{
static ASSizeRange kVariableSize = {{50, 50}, {300, 300}};
[self testStackLayoutSpecWithStyle:{} sizeRange:kVariableSize subnodes:@[] identifier:nil];
}
- (void)testFixedFlexBasisAppliedWhenFlexingItems
{
ASStackLayoutSpecStyle style = {.direction = ASStackLayoutDirectionHorizontal};
NSArray<ASDisplayNode *> *subnodes = defaultSubnodesWithSameSize({50, 50}, 0);
setCGSizeToNode({150, 150}, subnodes[1]);
for (ASDisplayNode *subnode in subnodes) {
subnode.style.flexGrow = 1;
subnode.style.flexBasis = ASDimensionMakeWithPoints(10);
}
// width 300px; height 0-150px.
static ASSizeRange kUnderflowSize = {{300, 0}, {300, 150}};
[self testStackLayoutSpecWithStyle:style sizeRange:kUnderflowSize subnodes:subnodes identifier:@"underflow"];
// width 200px; height 0-150px.
static ASSizeRange kOverflowSize = {{200, 0}, {200, 150}};
[self testStackLayoutSpecWithStyle:style sizeRange:kOverflowSize subnodes:subnodes identifier:@"overflow"];
}
- (void)testFractionalFlexBasisResolvesAgainstParentSize
{
ASStackLayoutSpecStyle style = {.direction = ASStackLayoutDirectionHorizontal};
NSArray<ASDisplayNode *> *subnodes = defaultSubnodesWithSameSize({50, 50}, 0);
for (ASDisplayNode *subnode in subnodes) {
subnode.style.flexGrow = 1;
}
// This should override the intrinsic size of 50pts and instead compute to 50% = 100pts.
// The result should be that the red box is twice as wide as the blue and gree boxes after flexing.
subnodes[0].style.flexBasis = ASDimensionMakeWithFraction(0.5);
static ASSizeRange kSize = {{200, 0}, {200, INFINITY}};
[self testStackLayoutSpecWithStyle:style sizeRange:kSize subnodes:subnodes identifier:nil];
}
- (void)testFixedFlexBasisOverridesIntrinsicSizeForNonFlexingChildren
{
ASStackLayoutSpecStyle style = {.direction = ASStackLayoutDirectionHorizontal};
NSArray<ASDisplayNode *> *subnodes = defaultSubnodes();
setCGSizeToNode({50, 50}, subnodes[0]);
setCGSizeToNode({150, 150}, subnodes[1]);
setCGSizeToNode({150, 50}, subnodes[2]);
for (ASDisplayNode *subnode in subnodes) {
subnode.style.flexBasis = ASDimensionMakeWithPoints(20);
}
static ASSizeRange kSize = {{300, 0}, {300, 150}};
[self testStackLayoutSpecWithStyle:style sizeRange:kSize subnodes:subnodes identifier:nil];
}
- (void)testCrossAxisStretchingOccursAfterStackAxisFlexing
{
// If cross axis stretching occurred *before* flexing, then the blue child would be stretched to 3000 points tall.
// Instead it should be stretched to 300 points tall, matching the red child and not overlapping the green inset.
NSArray<ASDisplayNode *> *subnodes = @[
ASDisplayNodeWithBackgroundColor([UIColor greenColor]), // Inset background node
ASDisplayNodeWithBackgroundColor([UIColor blueColor]), // child1 of stack
ASDisplayNodeWithBackgroundColor([UIColor redColor], {500, 500}) // child2 of stack
];
subnodes[1].style.width = ASDimensionMake(10);
ASDisplayNode *child2 = subnodes[2];
child2.style.flexGrow = 1;
child2.style.flexShrink = 1;
// If cross axis stretching occurred *before* flexing, then the blue child would be stretched to 3000 points tall.
// Instead it should be stretched to 300 points tall, matching the red child and not overlapping the green inset.
ASLayoutSpec *layoutSpec =
[ASBackgroundLayoutSpec
backgroundLayoutSpecWithChild:
[ASInsetLayoutSpec
insetLayoutSpecWithInsets:UIEdgeInsetsMake(10, 10, 10, 10)
child:
[ASStackLayoutSpec
stackLayoutSpecWithDirection:ASStackLayoutDirectionHorizontal
spacing:0
justifyContent:ASStackLayoutJustifyContentStart
alignItems:ASStackLayoutAlignItemsStretch
children:@[subnodes[1], child2]]
]
background:subnodes[0]];
static ASSizeRange kSize = {{300, 0}, {300, INFINITY}};
[self testLayoutSpec:layoutSpec sizeRange:kSize subnodes:subnodes identifier:nil];
}
- (void)testPositiveViolationIsDistributedEqually
{
ASStackLayoutSpecStyle style = {.direction = ASStackLayoutDirectionHorizontal};
NSArray<ASDisplayNode *> *subnodes = defaultSubnodesWithSameSize({50, 50}, 0);
subnodes[0].style.flexGrow = 1;
subnodes[2].style.flexGrow = 1;
// In this scenario a width of 350 results in a positive violation of 200.
// Due to each flexible subnode specifying a flex grow factor of 1 the violation will be distributed evenly.
static ASSizeRange kSize = {{350, 350}, {350, 350}};
[self testStackLayoutSpecWithStyle:style sizeRange:kSize subnodes:subnodes identifier:nil];
}
- (void)testPositiveViolationIsDistributedEquallyWithArbitraryFloats
{
ASStackLayoutSpecStyle style = {.direction = ASStackLayoutDirectionHorizontal};
NSArray<ASDisplayNode *> *subnodes = defaultSubnodesWithSameSize({50, 50}, 0);
subnodes[0].style.flexGrow = 0.5;
subnodes[2].style.flexGrow = 0.5;
// In this scenario a width of 350 results in a positive violation of 200.
// Due to each flexible child component specifying a flex grow factor of 0.5 the violation will be distributed evenly.
static ASSizeRange kSize = {{350, 350}, {350, 350}};
[self testStackLayoutSpecWithStyle:style sizeRange:kSize subnodes:subnodes identifier:nil];
}
- (void)testPositiveViolationIsDistributedProportionally
{
ASStackLayoutSpecStyle style = {.direction = ASStackLayoutDirectionVertical};
NSArray<ASDisplayNode *> *subnodes = defaultSubnodesWithSameSize({50, 50}, 0);
subnodes[0].style.flexGrow = 1;
subnodes[1].style.flexGrow = 2;
subnodes[2].style.flexGrow = 1;
// In this scenario a width of 350 results in a positive violation of 200.
// The first and third subnodes specify a flex grow factor of 1 and will flex by 50.
// The second subnode specifies a flex grow factor of 2 and will flex by 100.
static ASSizeRange kSize = {{350, 350}, {350, 350}};
[self testStackLayoutSpecWithStyle:style sizeRange:kSize subnodes:subnodes identifier:nil];
}
- (void)testPositiveViolationIsDistributedProportionallyWithArbitraryFloats
{
ASStackLayoutSpecStyle style = {.direction = ASStackLayoutDirectionVertical};
NSArray<ASDisplayNode *> *subnodes = defaultSubnodesWithSameSize({50, 50}, 0);
subnodes[0].style.flexGrow = 0.25;
subnodes[1].style.flexGrow = 0.50;
subnodes[2].style.flexGrow = 0.25;
// In this scenario a width of 350 results in a positive violation of 200.
// The first and third child components specify a flex grow factor of 0.25 and will flex by 50.
// The second child component specifies a flex grow factor of 0.25 and will flex by 100.
static ASSizeRange kSize = {{350, 350}, {350, 350}};
[self testStackLayoutSpecWithStyle:style sizeRange:kSize subnodes:subnodes identifier:nil];
}
- (void)testPositiveViolationIsDistributedEquallyAmongMixedChildren
{
ASStackLayoutSpecStyle style = {.direction = ASStackLayoutDirectionHorizontal};
const CGSize kSubnodeSize = {50, 50};
NSArray<ASDisplayNode *> *subnodes = defaultSubnodesWithSameSize(kSubnodeSize, 0);
subnodes = [subnodes arrayByAddingObject:ASDisplayNodeWithBackgroundColor([UIColor yellowColor], kSubnodeSize)];
subnodes[0].style.flexShrink = 1;
subnodes[1].style.flexGrow = 1;
subnodes[2].style.flexShrink = 0;
subnodes[3].style.flexGrow = 1;
// In this scenario a width of 400 results in a positive violation of 200.
// The first and third subnode specify a flex shrink factor of 1 and 0, respectively. They won't flex.
// The second and fourth subnode specify a flex grow factor of 1 and will flex by 100.
static ASSizeRange kSize = {{400, 400}, {400, 400}};
[self testStackLayoutSpecWithStyle:style sizeRange:kSize subnodes:subnodes identifier:nil];
}
- (void)testPositiveViolationIsDistributedEquallyAmongMixedChildrenWithArbitraryFloats
{
ASStackLayoutSpecStyle style = {.direction = ASStackLayoutDirectionHorizontal};
const CGSize kSubnodeSize = {50, 50};
NSArray<ASDisplayNode *> *subnodes = defaultSubnodesWithSameSize(kSubnodeSize, 0);
subnodes = [subnodes arrayByAddingObject:ASDisplayNodeWithBackgroundColor([UIColor yellowColor], kSubnodeSize)];
subnodes[0].style.flexShrink = 1.0;
subnodes[1].style.flexGrow = 0.5;
subnodes[2].style.flexShrink = 0.0;
subnodes[3].style.flexGrow = 0.5;
// In this scenario a width of 400 results in a positive violation of 200.
// The first and third child components specify a flex shrink factor of 1 and 0, respectively. They won't flex.
// The second and fourth child components specify a flex grow factor of 0.5 and will flex by 100.
static ASSizeRange kSize = {{400, 400}, {400, 400}};
[self testStackLayoutSpecWithStyle:style sizeRange:kSize subnodes:subnodes identifier:nil];
}
- (void)testPositiveViolationIsDistributedProportionallyAmongMixedChildren
{
ASStackLayoutSpecStyle style = {.direction = ASStackLayoutDirectionVertical};
const CGSize kSubnodeSize = {50, 50};
NSArray<ASDisplayNode *> *subnodes = defaultSubnodesWithSameSize(kSubnodeSize, 0);
subnodes = [subnodes arrayByAddingObject:ASDisplayNodeWithBackgroundColor([UIColor yellowColor], kSubnodeSize)];
subnodes[0].style.flexShrink = 1;
subnodes[1].style.flexGrow = 3;
subnodes[2].style.flexShrink = 0;
subnodes[3].style.flexGrow = 1;
// In this scenario a width of 400 results in a positive violation of 200.
// The first and third subnodes specify a flex shrink factor of 1 and 0, respectively. They won't flex.
// The second child subnode specifies a flex grow factor of 3 and will flex by 150.
// The fourth child subnode specifies a flex grow factor of 1 and will flex by 50.
static ASSizeRange kSize = {{400, 400}, {400, 400}};
[self testStackLayoutSpecWithStyle:style sizeRange:kSize subnodes:subnodes identifier:nil];
}
- (void)testPositiveViolationIsDistributedProportionallyAmongMixedChildrenWithArbitraryFloats
{
ASStackLayoutSpecStyle style = {.direction = ASStackLayoutDirectionVertical};
const CGSize kSubnodeSize = {50, 50};
NSArray<ASDisplayNode *> *subnodes = defaultSubnodesWithSameSize(kSubnodeSize, 0);
subnodes = [subnodes arrayByAddingObject:ASDisplayNodeWithBackgroundColor([UIColor yellowColor], kSubnodeSize)];
subnodes[0].style.flexShrink = 1.0;
subnodes[1].style.flexGrow = 0.75;
subnodes[2].style.flexShrink = 0.0;
subnodes[3].style.flexGrow = 0.25;
// In this scenario a width of 400 results in a positive violation of 200.
// The first and third child components specify a flex shrink factor of 1 and 0, respectively. They won't flex.
// The second child component specifies a flex grow factor of 0.75 and will flex by 150.
// The fourth child component specifies a flex grow factor of 0.25 and will flex by 50.
static ASSizeRange kSize = {{400, 400}, {400, 400}};
[self testStackLayoutSpecWithStyle:style sizeRange:kSize subnodes:subnodes identifier:nil];
}
- (void)testRemainingViolationIsAppliedProperlyToFirstFlexibleChild
{
ASStackLayoutSpecStyle style = {.direction = ASStackLayoutDirectionVertical};
NSArray<ASDisplayNode *> *subnodes = @[
ASDisplayNodeWithBackgroundColor([UIColor greenColor], {50, 25}),
ASDisplayNodeWithBackgroundColor([UIColor blueColor], {50, 0}),
ASDisplayNodeWithBackgroundColor([UIColor redColor], {50, 100})
];
subnodes[0].style.flexGrow = 0;
subnodes[1].style.flexGrow = 1;
subnodes[2].style.flexGrow = 1;
// In this scenario a width of 300 results in a positive violation of 175.
// The second and third subnodes specify a flex grow factor of 1 and will flex by 88 and 87, respectively.
static ASSizeRange kSize = {{300, 300}, {300, 300}};
[self testStackLayoutSpecWithStyle:style sizeRange:kSize subnodes:subnodes identifier:nil];
}
- (void)testRemainingViolationIsAppliedProperlyToFirstFlexibleChildWithArbitraryFloats
{
ASStackLayoutSpecStyle style = {.direction = ASStackLayoutDirectionVertical};
NSArray<ASDisplayNode *> *subnodes = @[
ASDisplayNodeWithBackgroundColor([UIColor greenColor], {50, 25}),
ASDisplayNodeWithBackgroundColor([UIColor blueColor], {50, 0}),
ASDisplayNodeWithBackgroundColor([UIColor redColor], {50, 100})
];
subnodes[0].style.flexGrow = 0.0;
subnodes[1].style.flexGrow = 0.5;
subnodes[2].style.flexGrow = 0.5;
// In this scenario a width of 300 results in a positive violation of 175.
// The second and third child components specify a flex grow factor of 0.5 and will flex by 88 and 87, respectively.
static ASSizeRange kSize = {{300, 300}, {300, 300}};
[self testStackLayoutSpecWithStyle:style sizeRange:kSize subnodes:subnodes identifier:nil];
}
- (void)testNegativeViolationIsDistributedBasedOnSize
{
ASStackLayoutSpecStyle style = {.direction = ASStackLayoutDirectionHorizontal};
NSArray<ASDisplayNode *> *subnodes = @[
ASDisplayNodeWithBackgroundColor([UIColor greenColor], {300, 50}),
ASDisplayNodeWithBackgroundColor([UIColor blueColor], {100, 50}),
ASDisplayNodeWithBackgroundColor([UIColor redColor], {200, 50})
];
subnodes[0].style.flexShrink = 1;
subnodes[1].style.flexShrink = 0;
subnodes[2].style.flexShrink = 1;
// In this scenario a width of 400 results in a negative violation of 200.
// The first and third subnodes specify a flex shrink factor of 1 and will flex by -120 and -80, respectively.
static ASSizeRange kSize = {{400, 400}, {400, 400}};
[self testStackLayoutSpecWithStyle:style sizeRange:kSize subnodes:subnodes identifier:nil];
}
- (void)testNegativeViolationIsDistributedBasedOnSizeWithArbitraryFloats
{
ASStackLayoutSpecStyle style = {.direction = ASStackLayoutDirectionHorizontal};
NSArray<ASDisplayNode *> *subnodes = @[
ASDisplayNodeWithBackgroundColor([UIColor greenColor], {300, 50}),
ASDisplayNodeWithBackgroundColor([UIColor blueColor], {100, 50}),
ASDisplayNodeWithBackgroundColor([UIColor redColor], {200, 50})
];
subnodes[0].style.flexShrink = 0.5;
subnodes[1].style.flexShrink = 0.0;
subnodes[2].style.flexShrink = 0.5;
// In this scenario a width of 400 results in a negative violation of 200.
// The first and third child components specify a flex shrink factor of 0.5 and will flex by -120 and -80, respectively.
static ASSizeRange kSize = {{400, 400}, {400, 400}};
[self testStackLayoutSpecWithStyle:style sizeRange:kSize subnodes:subnodes identifier:nil];
}
- (void)testNegativeViolationIsDistributedBasedOnSizeAndFlexFactor
{
ASStackLayoutSpecStyle style = {.direction = ASStackLayoutDirectionVertical};
NSArray<ASDisplayNode *> *subnodes = @[
ASDisplayNodeWithBackgroundColor([UIColor greenColor], {50, 300}),
ASDisplayNodeWithBackgroundColor([UIColor blueColor], {50, 100}),
ASDisplayNodeWithBackgroundColor([UIColor redColor], {50, 200})
];
subnodes[0].style.flexShrink = 2;
subnodes[1].style.flexShrink = 1;
subnodes[2].style.flexShrink = 2;
// In this scenario a width of 400 results in a negative violation of 200.
// The first and third subnodes specify a flex shrink factor of 2 and will flex by -109 and -72, respectively.
// The second subnode specifies a flex shrink factor of 1 and will flex by -18.
static ASSizeRange kSize = {{400, 400}, {400, 400}};
[self testStackLayoutSpecWithStyle:style sizeRange:kSize subnodes:subnodes identifier:nil];
}
- (void)testNegativeViolationIsDistributedBasedOnSizeAndFlexFactorWithArbitraryFloats
{
ASStackLayoutSpecStyle style = {.direction = ASStackLayoutDirectionVertical};
NSArray<ASDisplayNode *> *subnodes = @[
ASDisplayNodeWithBackgroundColor([UIColor greenColor], {50, 300}),
ASDisplayNodeWithBackgroundColor([UIColor blueColor], {50, 100}),
ASDisplayNodeWithBackgroundColor([UIColor redColor], {50, 200})
];
subnodes[0].style.flexShrink = 0.4;
subnodes[1].style.flexShrink = 0.2;
subnodes[2].style.flexShrink = 0.4;
// In this scenario a width of 400 results in a negative violation of 200.
// The first and third child components specify a flex shrink factor of 0.4 and will flex by -109 and -72, respectively.
// The second child component specifies a flex shrink factor of 0.2 and will flex by -18.
static ASSizeRange kSize = {{400, 400}, {400, 400}};
[self testStackLayoutSpecWithStyle:style sizeRange:kSize subnodes:subnodes identifier:nil];
}
- (void)testNegativeViolationIsDistributedBasedOnSizeAmongMixedChildrenChildren
{
ASStackLayoutSpecStyle style = {.direction = ASStackLayoutDirectionHorizontal};
const CGSize kSubnodeSize = {150, 50};
NSArray<ASDisplayNode *> *subnodes = defaultSubnodesWithSameSize(kSubnodeSize, 0);
subnodes = [subnodes arrayByAddingObject:ASDisplayNodeWithBackgroundColor([UIColor yellowColor], kSubnodeSize)];
subnodes[0].style.flexGrow = 1;
subnodes[1].style.flexShrink = 1;
subnodes[2].style.flexGrow = 0;
subnodes[3].style.flexShrink = 1;
// In this scenario a width of 400 results in a negative violation of 200.
// The first and third subnodes specify a flex grow factor of 1 and 0, respectively. They won't flex.
// The second and fourth subnodes specify a flex grow factor of 1 and will flex by -100.
static ASSizeRange kSize = {{400, 400}, {400, 400}};
[self testStackLayoutSpecWithStyle:style sizeRange:kSize subnodes:subnodes identifier:nil];
}
- (void)testNegativeViolationIsDistributedBasedOnSizeAmongMixedChildrenWithArbitraryFloats
{
ASStackLayoutSpecStyle style = {.direction = ASStackLayoutDirectionHorizontal};
const CGSize kSubnodeSize = {150, 50};
NSArray<ASDisplayNode *> *subnodes = defaultSubnodesWithSameSize(kSubnodeSize, 0);
subnodes = [subnodes arrayByAddingObject:ASDisplayNodeWithBackgroundColor([UIColor yellowColor], kSubnodeSize)];
subnodes[0].style.flexGrow = 1.0;
subnodes[1].style.flexShrink = 0.5;
subnodes[2].style.flexGrow = 0.0;
subnodes[3].style.flexShrink = 0.5;
// In this scenario a width of 400 results in a negative violation of 200.
// The first and third child components specify a flex grow factor of 1 and 0, respectively. They won't flex.
// The second and fourth child components specify a flex shrink factor of 0.5 and will flex by -100.
static ASSizeRange kSize = {{400, 400}, {400, 400}};
[self testStackLayoutSpecWithStyle:style sizeRange:kSize subnodes:subnodes identifier:nil];
}
- (void)testNegativeViolationIsDistributedBasedOnSizeAndFlexFactorAmongMixedChildren
{
ASStackLayoutSpecStyle style = {.direction = ASStackLayoutDirectionVertical};
NSArray<ASDisplayNode *> *subnodes = @[
ASDisplayNodeWithBackgroundColor([UIColor greenColor], {50, 150}),
ASDisplayNodeWithBackgroundColor([UIColor blueColor], {50, 100}),
ASDisplayNodeWithBackgroundColor([UIColor redColor], {50, 150}),
ASDisplayNodeWithBackgroundColor([UIColor yellowColor], {50, 200})
];
subnodes[0].style.flexGrow = 1;
subnodes[1].style.flexShrink = 1;
subnodes[2].style.flexGrow = 0;
subnodes[3].style.flexShrink = 3;
// In this scenario a width of 400 results in a negative violation of 200.
// The first and third subnodes specify a flex grow factor of 1 and 0, respectively. They won't flex.
// The second subnode specifies a flex grow factor of 1 and will flex by -28.
// The fourth subnode specifies a flex grow factor of 3 and will flex by -171.
static ASSizeRange kSize = {{400, 400}, {400, 400}};
[self testStackLayoutSpecWithStyle:style sizeRange:kSize subnodes:subnodes identifier:nil];