Remove stack children type:

- ASLayoutable requires mutable properties that are used when attached to a stack layout.
- Thus, ASLayoutable objects (including ASDisplayNode) can be injected into stack layout directly.
- ASStackLayoutNodeChild no longer needed.
- Tests and Kitten sample updated.
This commit is contained in:
Huy Nguyen
2015-06-26 11:38:29 +07:00
parent f588bceb4d
commit 95e787b226
14 changed files with 252 additions and 503 deletions

View File

@@ -95,6 +95,7 @@ static NSString *suffixForCenteringOptions(ASCenterLayoutNodeCenteringOptions ce
ASDisplayNode *backgroundNode = ASDisplayNodeWithBackgroundColor([UIColor redColor]);
ASStaticSizeDisplayNode *foregroundNode = ASDisplayNodeWithBackgroundColor([UIColor redColor]);
foregroundNode.staticSize = {10, 10};
foregroundNode.flexGrow = YES;
ASCenterLayoutNode *layoutNode =
[ASCenterLayoutNode
@@ -102,13 +103,7 @@ static NSString *suffixForCenteringOptions(ASCenterLayoutNodeCenteringOptions ce
sizingOptions:{}
child:
[ASBackgroundLayoutNode
newWithChild:
[ASStackLayoutNode
newWithStyle:{}
children:@[[ASStackLayoutNodeChild newWithInitializer:^(ASMutableStackLayoutNodeChild *mutableChild) {
mutableChild.node = foregroundNode;
mutableChild.flexGrow = YES;
}]]]
newWithChild:[ASStackLayoutNode newWithStyle:{} children:@[foregroundNode]]
background:backgroundNode]];
[self testLayoutNode:layoutNode sizeRange:kSize subnodes:@[backgroundNode, foregroundNode] identifier:nil];

View File

@@ -26,21 +26,12 @@
self.recordMode = NO;
}
static ASStackLayoutNodeChild *flexChild(id<ASLayoutable> child, BOOL flex)
{
return [ASStackLayoutNodeChild newWithInitializer:^(ASMutableStackLayoutNodeChild *mutableChild) {
mutableChild.node = child;
mutableChild.flexGrow = flex;
mutableChild.flexShrink = flex;
}];
}
static NSArray *defaultSubnodes()
{
return defaultSubnodesWithSameSize(CGSizeZero);
return defaultSubnodesWithSameSize(CGSizeZero, NO);
}
static NSArray *defaultSubnodesWithSameSize(CGSize subnodeSize)
static NSArray *defaultSubnodesWithSameSize(CGSize subnodeSize, BOOL flex)
{
NSArray *subnodes = @[
ASDisplayNodeWithBackgroundColor([UIColor redColor]),
@@ -49,6 +40,8 @@ static NSArray *defaultSubnodesWithSameSize(CGSize subnodeSize)
];
for (ASStaticSizeDisplayNode *subnode in subnodes) {
subnode.staticSize = subnodeSize;
subnode.flexGrow = flex;
subnode.flexShrink = flex;
}
return subnodes;
}
@@ -62,14 +55,18 @@ static NSArray *defaultSubnodesWithSameSize(CGSize subnodeSize)
.direction = ASStackLayoutDirectionHorizontal,
.justifyContent = justify
};
NSArray *subnodes = defaultSubnodesWithSameSize({50, 50});
NSArray *children = @[
flexChild(subnodes[0], flex),
flexChild(subnodes[1], flex),
flexChild(subnodes[2], flex)
];
[self testStackLayoutNodeWithStyle:style children:children sizeRange:sizeRange subnodes:subnodes identifier:identifier];
NSArray *subnodes = defaultSubnodesWithSameSize({50, 50}, flex);
[self testStackLayoutNodeWithStyle:style sizeRange:sizeRange subnodes:subnodes identifier:identifier];
}
- (void)testStackLayoutNodeWithStyle:(ASStackLayoutNodeStyle)style
sizeRange:(ASSizeRange)sizeRange
subnodes:(NSArray *)subnodes
identifier:(NSString *)identifier
{
[self testStackLayoutNodeWithStyle:style children:subnodes sizeRange:sizeRange subnodes:subnodes identifier:identifier];
}
- (void)testStackLayoutNodeWithStyle:(ASStackLayoutNodeStyle)style
@@ -114,49 +111,29 @@ static NSArray *defaultSubnodesWithSameSize(CGSize subnodeSize)
- (void)testOverflowBehaviorsWhenAllFlexShrinkNodesHaveBeenClampedToZeroButViolationStillExists
{
ASStackLayoutNodeStyle style = {.direction = ASStackLayoutDirectionHorizontal};
NSArray *subnodes = defaultSubnodesWithSameSize({50, 50});
NSArray *children = @[
// After flexShrink-able children are all clamped to zero, the sum of their widths is 100px.
[ASStackLayoutNodeChild newWithInitializer:^(ASMutableStackLayoutNodeChild *mutableChild) {
mutableChild.node = subnodes[0];
mutableChild.flexShrink = NO;
}],
[ASStackLayoutNodeChild newWithInitializer:^(ASMutableStackLayoutNodeChild *mutableChild) {
mutableChild.node = subnodes[1];
mutableChild.flexShrink = YES;
}],
[ASStackLayoutNodeChild newWithInitializer:^(ASMutableStackLayoutNodeChild *mutableChild) {
mutableChild.node = subnodes[2];
mutableChild.flexShrink = NO;
}]
];
NSArray *subnodes = defaultSubnodesWithSameSize({50, 50}, NO);
((ASDisplayNode *)subnodes[1]).flexShrink = YES;
// Width is 75px--that's less than the sum of the widths of the child nodes, which is 100px.
static ASSizeRange kSize = {{75, 0}, {75, 150}};
[self testStackLayoutNodeWithStyle: style children:children sizeRange:kSize subnodes:subnodes identifier:nil];
[self testStackLayoutNodeWithStyle: style sizeRange:kSize subnodes:subnodes identifier:nil];
}
- (void)testFlexWithUnequalIntrinsicSizes
{
ASStackLayoutNodeStyle style = {.direction = ASStackLayoutDirectionHorizontal};
NSArray *subnodes = defaultSubnodes();
((ASStaticSizeDisplayNode *)subnodes[0]).staticSize = {50, 50};
NSArray *subnodes = defaultSubnodesWithSameSize({50, 50}, YES);
((ASStaticSizeDisplayNode *)subnodes[1]).staticSize = {150, 150};
((ASStaticSizeDisplayNode *)subnodes[2]).staticSize = {50, 50};
NSArray *children = @[
flexChild(subnodes[0], YES),
flexChild(subnodes[1], YES),
flexChild(subnodes[2], YES)
];
// width 300px; height 0-150px.
static ASSizeRange kUnderflowSize = {{300, 0}, {300, 150}};
[self testStackLayoutNodeWithStyle:style children:children sizeRange:kUnderflowSize subnodes:subnodes identifier:@"underflow"];
[self testStackLayoutNodeWithStyle:style sizeRange:kUnderflowSize subnodes:subnodes identifier:@"underflow"];
// width 200px; height 0-150px.
static ASSizeRange kOverflowSize = {{200, 0}, {200, 150}};
[self testStackLayoutNodeWithStyle:style children:children sizeRange:kOverflowSize subnodes:subnodes identifier:@"overflow"];
[self testStackLayoutNodeWithStyle:style sizeRange:kOverflowSize subnodes:subnodes identifier:@"overflow"];
}
- (void)testCrossAxisSizeBehaviors
@@ -168,25 +145,13 @@ static NSArray *defaultSubnodesWithSameSize(CGSize subnodeSize)
((ASStaticSizeDisplayNode *)subnodes[1]).staticSize = {100, 50};
((ASStaticSizeDisplayNode *)subnodes[2]).staticSize = {150, 50};
NSArray *children = @[
[ASStackLayoutNodeChild newWithInitializer:^(ASMutableStackLayoutNodeChild *mutableChild) {
mutableChild.node = subnodes[0];
}],
[ASStackLayoutNodeChild newWithInitializer:^(ASMutableStackLayoutNodeChild *mutableChild) {
mutableChild.node = subnodes[1];
}],
[ASStackLayoutNodeChild newWithInitializer:^(ASMutableStackLayoutNodeChild *mutableChild) {
mutableChild.node = subnodes[2];
}]
];
// width 0-300px; height 300px
static ASSizeRange kVariableHeight = {{0, 300}, {300, 300}};
[self testStackLayoutNodeWithStyle:style children:children sizeRange:kVariableHeight subnodes:subnodes identifier:@"variableHeight"];
[self testStackLayoutNodeWithStyle:style sizeRange:kVariableHeight subnodes:subnodes identifier:@"variableHeight"];
// width 300px; height 300px
static ASSizeRange kFixedHeight = {{300, 300}, {300, 300}};
[self testStackLayoutNodeWithStyle:style children:children sizeRange:kFixedHeight subnodes:subnodes identifier:@"fixedHeight"];
[self testStackLayoutNodeWithStyle:style sizeRange:kFixedHeight subnodes:subnodes identifier:@"fixedHeight"];
}
- (void)testStackSpacing
@@ -201,20 +166,9 @@ static NSArray *defaultSubnodesWithSameSize(CGSize subnodeSize)
((ASStaticSizeDisplayNode *)subnodes[1]).staticSize = {100, 50};
((ASStaticSizeDisplayNode *)subnodes[2]).staticSize = {150, 50};
NSArray *children = @[
[ASStackLayoutNodeChild newWithInitializer:^(ASMutableStackLayoutNodeChild *mutableChild) {
mutableChild.node = subnodes[0];
}],
[ASStackLayoutNodeChild newWithInitializer:^(ASMutableStackLayoutNodeChild *mutableChild) {
mutableChild.node = subnodes[1];
}],
[ASStackLayoutNodeChild newWithInitializer:^(ASMutableStackLayoutNodeChild *mutableChild) {
mutableChild.node = subnodes[2];
}]
];
// width 0-300px; height 300px
static ASSizeRange kVariableHeight = {{0, 300}, {300, 300}};
[self testStackLayoutNodeWithStyle:style children:children sizeRange:kVariableHeight subnodes:subnodes identifier:@"variableHeight"];
[self testStackLayoutNodeWithStyle:style sizeRange:kVariableHeight subnodes:subnodes identifier:@"variableHeight"];
}
- (void)testStackSpacingWithChildrenHavingNilNodes
@@ -235,10 +189,7 @@ static NSArray *defaultSubnodesWithSameSize(CGSize subnodeSize)
.spacing = 10,
.alignItems = ASStackLayoutAlignItemsStretch
}
children:@[
[ASStackLayoutNodeChild new],
[ASStackLayoutNodeChild new]
]]
children:@[]]
background:backgroundNode]];
// width 300px; height 0-300px
@@ -257,51 +208,24 @@ static NSArray *defaultSubnodesWithSameSize(CGSize subnodeSize)
((ASStaticSizeDisplayNode *)subnodes[1]).staticSize = {100, 70};
((ASStaticSizeDisplayNode *)subnodes[2]).staticSize = {150, 90};
NSArray *children = @[
[ASStackLayoutNodeChild newWithInitializer:^(ASMutableStackLayoutNodeChild *mutableChild) {
mutableChild.node = subnodes[0];
}],
[ASStackLayoutNodeChild newWithInitializer:^(ASMutableStackLayoutNodeChild *mutableChild) {
mutableChild.node = subnodes[1];
mutableChild.spacingBefore = 10;
}],
[ASStackLayoutNodeChild newWithInitializer:^(ASMutableStackLayoutNodeChild *mutableChild) {
mutableChild.node = subnodes[2];
mutableChild.spacingBefore = 20;
}]
];
[self testStackLayoutNodeWithStyle:style children:children sizeRange:kAnySize subnodes:subnodes identifier:@"spacingBefore"];
children = @[
[ASStackLayoutNodeChild newWithInitializer:^(ASMutableStackLayoutNodeChild *mutableChild) {
mutableChild.node = subnodes[0];
}],
[ASStackLayoutNodeChild newWithInitializer:^(ASMutableStackLayoutNodeChild *mutableChild) {
mutableChild.node = subnodes[1];
mutableChild.spacingAfter = 10;
}],
[ASStackLayoutNodeChild newWithInitializer:^(ASMutableStackLayoutNodeChild *mutableChild) {
mutableChild.node = subnodes[2];
mutableChild.spacingAfter = 20;
}]
];
[self testStackLayoutNodeWithStyle:style children:children sizeRange:kAnySize subnodes:subnodes identifier:@"spacingAfter"];
((ASStaticSizeDisplayNode *)subnodes[1]).spacingBefore = 10;
((ASStaticSizeDisplayNode *)subnodes[2]).spacingBefore = 20;
[self testStackLayoutNodeWithStyle:style sizeRange:kAnySize subnodes:subnodes identifier:@"spacingBefore"];
// Reset above spacing values
((ASStaticSizeDisplayNode *)subnodes[1]).spacingBefore = 0;
((ASStaticSizeDisplayNode *)subnodes[2]).spacingBefore = 0;
((ASStaticSizeDisplayNode *)subnodes[1]).spacingAfter = 10;
((ASStaticSizeDisplayNode *)subnodes[2]).spacingAfter = 20;
[self testStackLayoutNodeWithStyle:style sizeRange:kAnySize subnodes:subnodes identifier:@"spacingAfter"];
// Reset above spacing values
((ASStaticSizeDisplayNode *)subnodes[1]).spacingAfter = 0;
((ASStaticSizeDisplayNode *)subnodes[2]).spacingAfter = 0;
style.spacing = 10;
children = @[
[ASStackLayoutNodeChild newWithInitializer:^(ASMutableStackLayoutNodeChild *mutableChild) {
mutableChild.node = subnodes[0];
}],
[ASStackLayoutNodeChild newWithInitializer:^(ASMutableStackLayoutNodeChild *mutableChild) {
mutableChild.node = subnodes[1];
mutableChild.spacingBefore = -10;
mutableChild.spacingAfter = -10;
}],
[ASStackLayoutNodeChild newWithInitializer:^(ASMutableStackLayoutNodeChild *mutableChild) {
mutableChild.node = subnodes[2];
}]
];
[self testStackLayoutNodeWithStyle:style children:children sizeRange:kAnySize subnodes:subnodes identifier:@"spacingBalancedOut"];
((ASStaticSizeDisplayNode *)subnodes[1]).spacingBefore = -10;
((ASStaticSizeDisplayNode *)subnodes[1]).spacingAfter = -10;
[self testStackLayoutNodeWithStyle:style sizeRange:kAnySize subnodes:subnodes identifier:@"spacingBalancedOut"];
}
- (void)testJustifiedCenterWithNodeSpacing
@@ -316,49 +240,30 @@ static NSArray *defaultSubnodesWithSameSize(CGSize subnodeSize)
((ASStaticSizeDisplayNode *)subnodes[1]).staticSize = {100, 70};
((ASStaticSizeDisplayNode *)subnodes[2]).staticSize = {150, 90};
NSArray *children = @[
[ASStackLayoutNodeChild newWithInitializer:^(ASMutableStackLayoutNodeChild *mutableChild) {
mutableChild.node = subnodes[0];
mutableChild.spacingBefore = 0;
}],
[ASStackLayoutNodeChild newWithInitializer:^(ASMutableStackLayoutNodeChild *mutableChild) {
mutableChild.node = subnodes[1];
mutableChild.spacingBefore = 20;
}],
[ASStackLayoutNodeChild newWithInitializer:^(ASMutableStackLayoutNodeChild *mutableChild) {
mutableChild.node = subnodes[2];
mutableChild.spacingBefore = 30;
}]
];
((ASStaticSizeDisplayNode *)subnodes[0]).spacingBefore = 0;
((ASStaticSizeDisplayNode *)subnodes[1]).spacingBefore = 20;
((ASStaticSizeDisplayNode *)subnodes[2]).spacingBefore = 30;
// width 0-300px; height 300px
static ASSizeRange kVariableHeight = {{0, 300}, {300, 300}};
[self testStackLayoutNodeWithStyle:style children:children sizeRange:kVariableHeight subnodes:subnodes identifier:@"variableHeight"];
[self testStackLayoutNodeWithStyle:style sizeRange:kVariableHeight subnodes:subnodes identifier:@"variableHeight"];
}
- (void)testNodeThatChangesCrossSizeWhenMainSizeIsFlexed
{
ASStackLayoutNodeStyle style = {.direction = ASStackLayoutDirectionHorizontal};
NSArray *subnodes = @[
ASDisplayNodeWithBackgroundColor([UIColor blueColor]),
ASDisplayNodeWithBackgroundColor([UIColor redColor])
];
((ASStaticSizeDisplayNode *)subnodes[1]).staticSize = {50, 50};
ASStaticSizeDisplayNode * subnode1 = ASDisplayNodeWithBackgroundColor([UIColor blueColor]);
ASStaticSizeDisplayNode * subnode2 = ASDisplayNodeWithBackgroundColor([UIColor redColor]);
subnode2.staticSize = {50, 50};
ASRatioLayoutNode *child1 = [ASRatioLayoutNode newWithRatio:1.5 child:subnode1];
child1.flexBasis = ASRelativeDimensionMakeWithPercent(1);
child1.flexGrow = YES;
child1.flexShrink = YES;
NSArray *children = @[
[ASStackLayoutNodeChild newWithInitializer:^(ASMutableStackLayoutNodeChild *mutableChild) {
mutableChild.node = [ASRatioLayoutNode newWithRatio:1.5 child:subnodes[0]];
mutableChild.flexBasis = ASRelativeDimensionMakeWithPercent(1);
mutableChild.flexGrow = YES;
mutableChild.flexShrink = YES;
}],
[ASStackLayoutNodeChild newWithInitializer:^(ASMutableStackLayoutNodeChild *mutableChild) {
mutableChild.node = subnodes[1];
}]
];
static ASSizeRange kFixedWidth = {{150, 0}, {150, INFINITY}};
[self testStackLayoutNodeWithStyle:style children:children sizeRange:kFixedWidth subnodes:subnodes identifier:nil];
[self testStackLayoutNodeWithStyle:style children:@[child1, subnode2] sizeRange:kFixedWidth subnodes:@[subnode1, subnode2] identifier:nil];
}
- (void)testAlignCenterWithFlexedMainDimension
@@ -368,49 +273,33 @@ static NSArray *defaultSubnodesWithSameSize(CGSize subnodeSize)
.alignItems = ASStackLayoutAlignItemsCenter
};
NSArray *subnodes = @[
ASDisplayNodeWithBackgroundColor([UIColor redColor]),
ASDisplayNodeWithBackgroundColor([UIColor blueColor])
];
((ASStaticSizeDisplayNode *)subnodes[0]).staticSize = {100, 100};
((ASStaticSizeDisplayNode *)subnodes[1]).staticSize = {50, 50};
ASStaticSizeDisplayNode *subnode1 = ASDisplayNodeWithBackgroundColor([UIColor redColor]);
subnode1.staticSize = {100, 100};
subnode1.flexShrink = YES;
NSArray *children = @[
[ASStackLayoutNodeChild newWithInitializer:^(ASMutableStackLayoutNodeChild *mutableChild) {
mutableChild.node = subnodes[0];
mutableChild.flexShrink = YES;
}],
[ASStackLayoutNodeChild newWithInitializer:^(ASMutableStackLayoutNodeChild *mutableChild) {
mutableChild.node = subnodes[1];
mutableChild.flexShrink = YES;
}],
];
ASStaticSizeDisplayNode *subnode2 = ASDisplayNodeWithBackgroundColor([UIColor blueColor]);
subnode2.staticSize = {50, 50};
subnode2.flexShrink = YES;
NSArray *subnodes = @[subnode1, subnode2];
static ASSizeRange kFixedWidth = {{150, 0}, {150, 100}};
[self testStackLayoutNodeWithStyle:style children:children sizeRange:kFixedWidth subnodes:subnodes identifier:nil];
[self testStackLayoutNodeWithStyle:style sizeRange:kFixedWidth subnodes:subnodes identifier:nil];
}
- (void)testAlignCenterWithIndefiniteCrossDimension
{
ASStackLayoutNodeStyle style = {.direction = ASStackLayoutDirectionHorizontal};
NSArray *subnodes = @[
ASDisplayNodeWithBackgroundColor([UIColor redColor]),
ASDisplayNodeWithBackgroundColor([UIColor blueColor])
];
((ASStaticSizeDisplayNode *)subnodes[0]).staticSize = {100, 100};
((ASStaticSizeDisplayNode *)subnodes[1]).staticSize = {50, 50};
ASStaticSizeDisplayNode *subnode1 = ASDisplayNodeWithBackgroundColor([UIColor redColor]);
subnode1.staticSize = {100, 100};
ASStaticSizeDisplayNode *subnode2 = ASDisplayNodeWithBackgroundColor([UIColor blueColor]);
subnode2.staticSize = {50, 50};
subnode2.alignSelf = ASStackLayoutAlignSelfCenter;
NSArray *children = @[
[ASStackLayoutNodeChild newWithInitializer:^(ASMutableStackLayoutNodeChild *mutableChild) {
mutableChild.node = subnodes[0];
}],
[ASStackLayoutNodeChild newWithInitializer:^(ASMutableStackLayoutNodeChild *mutableChild) {
mutableChild.node = subnodes[1];
mutableChild.alignSelf = ASStackLayoutAlignSelfCenter;
}],
];
NSArray *subnodes = @[subnode1, subnode2];
static ASSizeRange kFixedWidth = {{150, 0}, {150, INFINITY}};
[self testStackLayoutNodeWithStyle:style children:children sizeRange:kFixedWidth subnodes:subnodes identifier:nil];
[self testStackLayoutNodeWithStyle:style sizeRange:kFixedWidth subnodes:subnodes identifier:nil];
}
- (void)testAlignedStart
@@ -425,23 +314,13 @@ static NSArray *defaultSubnodesWithSameSize(CGSize subnodeSize)
((ASStaticSizeDisplayNode *)subnodes[0]).staticSize = {50, 50};
((ASStaticSizeDisplayNode *)subnodes[1]).staticSize = {100, 70};
((ASStaticSizeDisplayNode *)subnodes[2]).staticSize = {150, 90};
((ASStaticSizeDisplayNode *)subnodes[0]).spacingBefore = 0;
((ASStaticSizeDisplayNode *)subnodes[1]).spacingBefore = 20;
((ASStaticSizeDisplayNode *)subnodes[2]).spacingBefore = 30;
NSArray *children = @[
[ASStackLayoutNodeChild newWithInitializer:^(ASMutableStackLayoutNodeChild *mutableChild) {
mutableChild.node = subnodes[0];
mutableChild.spacingBefore = 0;
}],
[ASStackLayoutNodeChild newWithInitializer:^(ASMutableStackLayoutNodeChild *mutableChild) {
mutableChild.node = subnodes[1];
mutableChild.spacingBefore = 20;
}],
[ASStackLayoutNodeChild newWithInitializer:^(ASMutableStackLayoutNodeChild *mutableChild) {
mutableChild.node = subnodes[2];
mutableChild.spacingBefore = 30;
}]
];
static ASSizeRange kExactSize = {{300, 300}, {300, 300}};
[self testStackLayoutNodeWithStyle:style children:children sizeRange:kExactSize subnodes:subnodes identifier:nil];
[self testStackLayoutNodeWithStyle:style sizeRange:kExactSize subnodes:subnodes identifier:nil];
}
- (void)testAlignedEnd
@@ -456,23 +335,13 @@ static NSArray *defaultSubnodesWithSameSize(CGSize subnodeSize)
((ASStaticSizeDisplayNode *)subnodes[0]).staticSize = {50, 50};
((ASStaticSizeDisplayNode *)subnodes[1]).staticSize = {100, 70};
((ASStaticSizeDisplayNode *)subnodes[2]).staticSize = {150, 90};
((ASStaticSizeDisplayNode *)subnodes[0]).spacingBefore = 0;
((ASStaticSizeDisplayNode *)subnodes[1]).spacingBefore = 20;
((ASStaticSizeDisplayNode *)subnodes[2]).spacingBefore = 30;
NSArray *children = @[
[ASStackLayoutNodeChild newWithInitializer:^(ASMutableStackLayoutNodeChild *mutableChild) {
mutableChild.node = subnodes[0];
mutableChild.spacingBefore = 0;
}],
[ASStackLayoutNodeChild newWithInitializer:^(ASMutableStackLayoutNodeChild *mutableChild) {
mutableChild.node = subnodes[1];
mutableChild.spacingBefore = 20;
}],
[ASStackLayoutNodeChild newWithInitializer:^(ASMutableStackLayoutNodeChild *mutableChild) {
mutableChild.node = subnodes[2];
mutableChild.spacingBefore = 30;
}]
];
static ASSizeRange kExactSize = {{300, 300}, {300, 300}};
[self testStackLayoutNodeWithStyle:style children:children sizeRange:kExactSize subnodes:subnodes identifier:nil];
[self testStackLayoutNodeWithStyle:style sizeRange:kExactSize subnodes:subnodes identifier:nil];
}
- (void)testAlignedCenter
@@ -487,23 +356,13 @@ static NSArray *defaultSubnodesWithSameSize(CGSize subnodeSize)
((ASStaticSizeDisplayNode *)subnodes[0]).staticSize = {50, 50};
((ASStaticSizeDisplayNode *)subnodes[1]).staticSize = {100, 70};
((ASStaticSizeDisplayNode *)subnodes[2]).staticSize = {150, 90};
((ASStaticSizeDisplayNode *)subnodes[0]).spacingBefore = 0;
((ASStaticSizeDisplayNode *)subnodes[1]).spacingBefore = 20;
((ASStaticSizeDisplayNode *)subnodes[2]).spacingBefore = 30;
NSArray *children = @[
[ASStackLayoutNodeChild newWithInitializer:^(ASMutableStackLayoutNodeChild *mutableChild) {
mutableChild.node = subnodes[0];
mutableChild.spacingBefore = 0;
}],
[ASStackLayoutNodeChild newWithInitializer:^(ASMutableStackLayoutNodeChild *mutableChild) {
mutableChild.node = subnodes[1];
mutableChild.spacingBefore = 20;
}],
[ASStackLayoutNodeChild newWithInitializer:^(ASMutableStackLayoutNodeChild *mutableChild) {
mutableChild.node = subnodes[2];
mutableChild.spacingBefore = 30;
}]
];
static ASSizeRange kExactSize = {{300, 300}, {300, 300}};
[self testStackLayoutNodeWithStyle:style children:children sizeRange:kExactSize subnodes:subnodes identifier:nil];
[self testStackLayoutNodeWithStyle:style sizeRange:kExactSize subnodes:subnodes identifier:nil];
}
- (void)testAlignedStretchNoChildExceedsMin
@@ -519,23 +378,13 @@ static NSArray *defaultSubnodesWithSameSize(CGSize subnodeSize)
((ASStaticSizeDisplayNode *)subnodes[1]).staticSize = {100, 70};
((ASStaticSizeDisplayNode *)subnodes[2]).staticSize = {150, 90};
NSArray *children = @[
[ASStackLayoutNodeChild newWithInitializer:^(ASMutableStackLayoutNodeChild *mutableChild) {
mutableChild.node = subnodes[0];
mutableChild.spacingBefore = 0;
}],
[ASStackLayoutNodeChild newWithInitializer:^(ASMutableStackLayoutNodeChild *mutableChild) {
mutableChild.node = subnodes[1];
mutableChild.spacingBefore = 20;
}],
[ASStackLayoutNodeChild newWithInitializer:^(ASMutableStackLayoutNodeChild *mutableChild) {
mutableChild.node = subnodes[2];
mutableChild.spacingBefore = 30;
}]
];
((ASStaticSizeDisplayNode *)subnodes[0]).spacingBefore = 0;
((ASStaticSizeDisplayNode *)subnodes[1]).spacingBefore = 20;
((ASStaticSizeDisplayNode *)subnodes[2]).spacingBefore = 30;
static ASSizeRange kVariableSize = {{200, 200}, {300, 300}};
// all children should be 200px wide
[self testStackLayoutNodeWithStyle:style children:children sizeRange:kVariableSize subnodes:subnodes identifier:nil];
[self testStackLayoutNodeWithStyle:style sizeRange:kVariableSize subnodes:subnodes identifier:nil];
}
- (void)testAlignedStretchOneChildExceedsMin
@@ -550,90 +399,59 @@ static NSArray *defaultSubnodesWithSameSize(CGSize subnodeSize)
((ASStaticSizeDisplayNode *)subnodes[0]).staticSize = {50, 50};
((ASStaticSizeDisplayNode *)subnodes[1]).staticSize = {100, 70};
((ASStaticSizeDisplayNode *)subnodes[2]).staticSize = {150, 90};
((ASStaticSizeDisplayNode *)subnodes[0]).spacingBefore = 0;
((ASStaticSizeDisplayNode *)subnodes[1]).spacingBefore = 20;
((ASStaticSizeDisplayNode *)subnodes[2]).spacingBefore = 30;
NSArray *children = @[
[ASStackLayoutNodeChild newWithInitializer:^(ASMutableStackLayoutNodeChild *mutableChild) {
mutableChild.node = subnodes[0];
mutableChild.spacingBefore = 0;
}],
[ASStackLayoutNodeChild newWithInitializer:^(ASMutableStackLayoutNodeChild *mutableChild) {
mutableChild.node = subnodes[1];
mutableChild.spacingBefore = 20;
}],
[ASStackLayoutNodeChild newWithInitializer:^(ASMutableStackLayoutNodeChild *mutableChild) {
mutableChild.node = subnodes[2];
mutableChild.spacingBefore = 30;
}]
];
static ASSizeRange kVariableSize = {{50, 50}, {300, 300}};
// all children should be 150px wide
[self testStackLayoutNodeWithStyle:style children:children sizeRange:kVariableSize subnodes:subnodes identifier:nil];
[self testStackLayoutNodeWithStyle:style sizeRange:kVariableSize subnodes:subnodes identifier:nil];
}
- (void)testEmptyStack
{
static ASSizeRange kVariableSize = {{50, 50}, {300, 300}};
[self testStackLayoutNodeWithStyle:{} children:@[] sizeRange:kVariableSize subnodes:@[] identifier:nil];
[self testStackLayoutNodeWithStyle:{} sizeRange:kVariableSize subnodes:@[] identifier:nil];
}
- (void)testFixedFlexBasisAppliedWhenFlexingItems
{
ASStackLayoutNodeStyle style = {.direction = ASStackLayoutDirectionHorizontal};
NSArray *subnodes = defaultSubnodes();
((ASStaticSizeDisplayNode *)subnodes[0]).staticSize = {50, 50};
NSArray *subnodes = defaultSubnodesWithSameSize({50, 50}, NO);
((ASStaticSizeDisplayNode *)subnodes[1]).staticSize = {150, 150};
((ASStaticSizeDisplayNode *)subnodes[2]).staticSize = {50, 50};
NSArray *children = @[
[ASStackLayoutNodeChild newWithInitializer:^(ASMutableStackLayoutNodeChild *mutableChild) {
mutableChild.node = subnodes[0];
mutableChild.flexGrow = YES;
mutableChild.flexBasis = ASRelativeDimensionMakeWithPoints(10);
}],
[ASStackLayoutNodeChild newWithInitializer:^(ASMutableStackLayoutNodeChild *mutableChild) {
mutableChild.node = subnodes[1];
mutableChild.flexGrow = YES;
mutableChild.flexBasis = ASRelativeDimensionMakeWithPoints(10);
}],
[ASStackLayoutNodeChild newWithInitializer:^(ASMutableStackLayoutNodeChild *mutableChild) {
mutableChild.node = subnodes[2];
mutableChild.flexGrow = YES;
mutableChild.flexBasis = ASRelativeDimensionMakeWithPoints(10);
}]
];
for (ASStaticSizeDisplayNode *subnode in subnodes) {
subnode.flexGrow = YES;
subnode.flexBasis = ASRelativeDimensionMakeWithPoints(10);
}
// width 300px; height 0-150px.
static ASSizeRange kUnderflowSize = {{300, 0}, {300, 150}};
[self testStackLayoutNodeWithStyle:style children:children sizeRange:kUnderflowSize subnodes:subnodes identifier:@"underflow"];
[self testStackLayoutNodeWithStyle:style sizeRange:kUnderflowSize subnodes:subnodes identifier:@"underflow"];
// width 200px; height 0-150px.
static ASSizeRange kOverflowSize = {{200, 0}, {200, 150}};
[self testStackLayoutNodeWithStyle:style children:children sizeRange:kOverflowSize subnodes:subnodes identifier:@"overflow"];
[self testStackLayoutNodeWithStyle:style sizeRange:kOverflowSize subnodes:subnodes identifier:@"overflow"];
}
- (void)testPercentageFlexBasisResolvesAgainstParentSize
{
ASStackLayoutNodeStyle style = {.direction = ASStackLayoutDirectionHorizontal};
NSArray *subnodes = defaultSubnodesWithSameSize({50, 50});
NSArray *children = @[
[ASStackLayoutNodeChild newWithInitializer:^(ASMutableStackLayoutNodeChild *mutableChild) {
mutableChild.node = subnodes[0];
mutableChild.flexGrow = YES;
// 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.
mutableChild.flexBasis = ASRelativeDimensionMakeWithPercent(0.5);
}],
[ASStackLayoutNodeChild newWithInitializer:^(ASMutableStackLayoutNodeChild *mutableChild) {
mutableChild.node = subnodes[1];
mutableChild.flexGrow = YES;
}],
[ASStackLayoutNodeChild newWithInitializer:^(ASMutableStackLayoutNodeChild *mutableChild) {
mutableChild.node = subnodes[2];
mutableChild.flexGrow = YES;
}]
];
NSArray *subnodes = defaultSubnodesWithSameSize({50, 50}, NO);
for (ASStaticSizeDisplayNode *subnode in subnodes) {
subnode.flexGrow = YES;
}
// 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.
((ASStaticSizeDisplayNode *)subnodes[0]).flexBasis = ASRelativeDimensionMakeWithPercent(0.5);
static ASSizeRange kSize = {{200, 0}, {200, INFINITY}};
[self testStackLayoutNodeWithStyle:style children:children sizeRange:kSize subnodes:subnodes identifier:nil];
[self testStackLayoutNodeWithStyle:style sizeRange:kSize subnodes:subnodes identifier:nil];
}
- (void)testFixedFlexBasisOverridesIntrinsicSizeForNonFlexingChildren
@@ -645,22 +463,12 @@ static NSArray *defaultSubnodesWithSameSize(CGSize subnodeSize)
((ASStaticSizeDisplayNode *)subnodes[1]).staticSize = {150, 150};
((ASStaticSizeDisplayNode *)subnodes[2]).staticSize = {50, 50};
NSArray *children = @[
[ASStackLayoutNodeChild newWithInitializer:^(ASMutableStackLayoutNodeChild *mutableChild) {
mutableChild.node = subnodes[0];
mutableChild.flexBasis = ASRelativeDimensionMakeWithPoints(20);
}],
[ASStackLayoutNodeChild newWithInitializer:^(ASMutableStackLayoutNodeChild *mutableChild) {
mutableChild.node = subnodes[1];
mutableChild.flexBasis = ASRelativeDimensionMakeWithPoints(20);
}],
[ASStackLayoutNodeChild newWithInitializer:^(ASMutableStackLayoutNodeChild *mutableChild) {
mutableChild.node = subnodes[2];
mutableChild.flexBasis = ASRelativeDimensionMakeWithPoints(20);
}]
];
for (ASStaticSizeDisplayNode *subnode in subnodes) {
subnode.flexBasis = ASRelativeDimensionMakeWithPoints(20);
}
static ASSizeRange kSize = {{300, 0}, {300, 150}};
[self testStackLayoutNodeWithStyle:style children:children sizeRange:kSize subnodes:subnodes identifier:nil];
[self testStackLayoutNodeWithStyle:style sizeRange:kSize subnodes:subnodes identifier:nil];
}
- (void)testCrossAxisStretchingOccursAfterStackAxisFlexing
@@ -672,6 +480,10 @@ static NSArray *defaultSubnodesWithSameSize(CGSize subnodeSize)
];
((ASStaticSizeDisplayNode *)subnodes[1]).staticSize = {10, 0};
((ASStaticSizeDisplayNode *)subnodes[2]).staticSize = {3000, 3000};
ASRatioLayoutNode *child2 = [ASRatioLayoutNode newWithRatio:1.0 child:subnodes[2]];
child2.flexGrow = YES;
child2.flexShrink = YES;
// 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.
@@ -686,13 +498,7 @@ static NSArray *defaultSubnodesWithSameSize(CGSize subnodeSize)
.direction = ASStackLayoutDirectionHorizontal,
.alignItems = ASStackLayoutAlignItemsStretch,
}
children:
@[
[ASStackLayoutNodeChild newWithInitializer:^(ASMutableStackLayoutNodeChild *mutableChild) {
mutableChild.node = subnodes[1];
}],
flexChild([ASRatioLayoutNode newWithRatio:1.0 child:subnodes[2]], YES),
]]]
children:@[subnodes[1], child2,]]]
background:subnodes[0]];
static ASSizeRange kSize = {{300, 0}, {300, INFINITY}};
@@ -704,31 +510,23 @@ static NSArray *defaultSubnodesWithSameSize(CGSize subnodeSize)
ASStackLayoutNodeStyle style = {.direction = ASStackLayoutDirectionHorizontal};
NSArray *subnodes = defaultSubnodes();
((ASStaticSizeDisplayNode *)subnodes[0]).staticSize = {300, 50};
((ASStaticSizeDisplayNode *)subnodes[0]).flexShrink = YES;
((ASStaticSizeDisplayNode *)subnodes[1]).staticSize = {100, 50};
((ASStaticSizeDisplayNode *)subnodes[1]).flexShrink = NO;
((ASStaticSizeDisplayNode *)subnodes[2]).staticSize = {200, 50};
NSArray *children = @[
[ASStackLayoutNodeChild newWithInitializer:^(ASMutableStackLayoutNodeChild *mutableChild) {
mutableChild.node = subnodes[0];
mutableChild.flexShrink = YES;
}],
[ASStackLayoutNodeChild newWithInitializer:^(ASMutableStackLayoutNodeChild *mutableChild) {
mutableChild.node = subnodes[1];
mutableChild.flexShrink = NO;
}],
[ASStackLayoutNodeChild newWithInitializer:^(ASMutableStackLayoutNodeChild *mutableChild) {
mutableChild.node = subnodes[2];
mutableChild.flexShrink = YES;
}]
];
((ASStaticSizeDisplayNode *)subnodes[2]).flexShrink = YES;
// A width of 400px results in a violation of 200px. This is distributed equally among each flexible node,
// causing both of them to be shrunk by 100px, resulting in widths of 300px, 100px, and 50px.
// In the W3 flexbox standard, flexible nodes are shrunk proportionate to their original sizes,
// resulting in widths of 180px, 100px, and 120px.
// This test verifies the current behavior--the snapshot contains widths 300px, 100px, and 50px.
static ASSizeRange kSize = {{400, 0}, {400, 150}};
[self testStackLayoutNodeWithStyle:style children:children sizeRange:kSize subnodes:subnodes identifier:nil];
[self testStackLayoutNodeWithStyle:style sizeRange:kSize subnodes:subnodes identifier:nil];
}
@end