[Layout] Layout API based on content area (#2110)

* Initial commit for adding a size constraint to ASLayoutable's

* Some more commits

* Fix sample projects in extra/

* Remove preferredFrameSize test of ASEditableTextNode

* Remove preferredFrameSize from examples_extra

* Add deprecation warning to -[ASDisplayNode preferredFrameSize]

* Add deprecation warning to -[ASDisplayNode measureWithSizeRange:]

* Commit

* Commit

* Remove ASRelativeSizeRange

* Make ASRelativeSize private

* Adjust examples

* Improve setting of -[ASLayoutable size] with points or fractions

* Add ASWrapperLayoutSpec

* Improve creation of ASRelativeDimension

* Add `preferredFrameSize` back and add deprecated logging

* Add `layoutSpecBlock` setter and getter and add locking for it

* Add better documentation and fix macros to create ASRelativeDimension

* Create new ASSizeRangeMake with just a CGSize as parameter

* Update Kitten and Social App Layout example

* Add layoutThatFits: and deprecate measure:

* Rename ASRelativeDimension to ASDimension

* Fix examples for ASDimension renaming

* Remove fancy height and width setter

* Fix ASDimension helper

* Rename -[ASLayout layoutableObject] to -[ASLayout layoutable]

* Update layout related methods and more clearer documentation around how to use it

* Deprecate old ASLayout class constructors

* Don't unnecessary recalculate layout if constrained or parent size did not change

* Use shared pointer for ASDisplayNodeLayout

* Fix rebase conflicts

* Add documentation and move implementation in mm file of ASDisplayNodeLayout

* Fix test errors

* Rename ASSize to ASLayoutableSize

* Address comments

* Rename setSizeFromCGSize to setSizeWithCGSize

* Improve inline functions in ASDimension

* Fix rebase conflicts
This commit is contained in:
Michael Schneider
2016-09-07 17:44:48 +02:00
committed by Adlai Holler
parent 2bfeb6de92
commit 8897614f0e
109 changed files with 2089 additions and 1304 deletions

View File

@@ -24,8 +24,7 @@ static const ASSizeRange kSize = {{320, 320}, {320, 320}};
- (void)testBackground
{
ASDisplayNode *backgroundNode = ASDisplayNodeWithBackgroundColor([UIColor blueColor]);
ASStaticSizeDisplayNode *foregroundNode = ASDisplayNodeWithBackgroundColor([UIColor blackColor]);
foregroundNode.staticSize = {20, 20};
ASDisplayNode *foregroundNode = ASDisplayNodeWithBackgroundColor([UIColor blackColor], {20, 20});
ASLayoutSpec *layoutSpec =
[ASBackgroundLayoutSpec

View File

@@ -45,10 +45,16 @@ static const ASSizeRange kSize = {{100, 120}, {320, 160}};
sizingOptions:(ASCenterLayoutSpecSizingOptions)sizingOptions
{
ASDisplayNode *backgroundNode = ASDisplayNodeWithBackgroundColor([UIColor redColor]);
ASStaticSizeDisplayNode *foregroundNode = ASDisplayNodeWithBackgroundColor([UIColor greenColor]);
foregroundNode.staticSize = {70, 100};
ASDisplayNode *foregroundNode = ASDisplayNodeWithBackgroundColor([UIColor greenColor], CGSizeMake(70, 100));
ASLayoutSpec *layoutSpec = [ASBackgroundLayoutSpec backgroundLayoutSpecWithChild:[ASCenterLayoutSpec centerLayoutSpecWithCenteringOptions:options sizingOptions:sizingOptions child:foregroundNode]background:backgroundNode];
ASLayoutSpec *layoutSpec =
[ASBackgroundLayoutSpec
backgroundLayoutSpecWithChild:
[ASCenterLayoutSpec
centerLayoutSpecWithCenteringOptions:options
sizingOptions:sizingOptions
child:foregroundNode]
background:backgroundNode];
[self testLayoutSpec:layoutSpec
sizeRange:kSize
@@ -83,11 +89,23 @@ static NSString *suffixForCenteringOptions(ASCenterLayoutSpecCenteringOptions ce
- (void)testMinimumSizeRangeIsGivenToChildWhenNotCentering
{
ASDisplayNode *backgroundNode = ASDisplayNodeWithBackgroundColor([UIColor redColor]);
ASStaticSizeDisplayNode *foregroundNode = ASDisplayNodeWithBackgroundColor([UIColor redColor]);
foregroundNode.staticSize = {10, 10};
ASDisplayNode *foregroundNode = ASDisplayNodeWithBackgroundColor([UIColor redColor], CGSizeMake(10, 10));
foregroundNode.flexGrow = YES;
ASCenterLayoutSpec *layoutSpec = [ASCenterLayoutSpec centerLayoutSpecWithCenteringOptions:ASCenterLayoutSpecCenteringNone sizingOptions:{} child:[ASBackgroundLayoutSpec backgroundLayoutSpecWithChild:[ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionVertical spacing:0 justifyContent:ASStackLayoutJustifyContentStart alignItems:ASStackLayoutAlignItemsStart children:@[foregroundNode]] background:backgroundNode]];
ASCenterLayoutSpec *layoutSpec =
[ASCenterLayoutSpec
centerLayoutSpecWithCenteringOptions:ASCenterLayoutSpecCenteringNone
sizingOptions:{}
child:
[ASBackgroundLayoutSpec
backgroundLayoutSpecWithChild:
[ASStackLayoutSpec
stackLayoutSpecWithDirection:ASStackLayoutDirectionVertical
spacing:0
justifyContent:ASStackLayoutJustifyContentStart
alignItems:ASStackLayoutAlignItemsStart
children:@[foregroundNode]]
background:backgroundNode]];
[self testLayoutSpec:layoutSpec sizeRange:kSize subnodes:@[backgroundNode, foregroundNode] identifier:nil];
}

View File

@@ -77,7 +77,7 @@
return [ASStaticLayoutSpec staticLayoutSpecWithChildren:@[stack1, stack2, node5]];
};
[node measureWithSizeRange:ASSizeRangeMake(CGSizeZero, CGSizeZero)];
[node layoutThatFits:ASSizeRangeMake(CGSizeZero)];
XCTAssertEqual(node.subnodes[0], node5);
XCTAssertEqual(node.subnodes[1], node1);
XCTAssertEqual(node.subnodes[2], node2);
@@ -104,13 +104,13 @@
}
};
[node measureWithSizeRange:ASSizeRangeMake(CGSizeZero, CGSizeZero)];
[node layoutThatFits:ASSizeRangeMake(CGSizeZero)];
XCTAssertEqual(node.subnodes[0], node1);
XCTAssertEqual(node.subnodes[1], node2);
node.layoutState = @2;
[node invalidateCalculatedLayout];
[node measureWithSizeRange:ASSizeRangeMake(CGSizeZero, CGSizeZero)];
[node layoutThatFits:ASSizeRangeMake(CGSizeZero)];
XCTAssertEqual(node.subnodes[0], node1);
XCTAssertEqual(node.subnodes[1], node3);
@@ -157,12 +157,12 @@
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[node measureWithSizeRange:ASSizeRangeMake(CGSizeZero, CGSizeZero)];
[node layoutThatFits:ASSizeRangeMake(CGSizeZero)];
XCTAssertEqual(node.subnodes[0], node1);
node.layoutState = @2;
[node invalidateCalculatedLayout];
[node measureWithSizeRange:ASSizeRangeMake(CGSizeZero, CGSizeZero)];
[node layoutThatFits:ASSizeRangeMake(CGSizeZero)];
// Dispatch back to the main thread to let the insertion / deletion of subnodes happening
dispatch_async(dispatch_get_main_queue(), ^{
@@ -202,7 +202,7 @@
XCTestExpectation *expectation = [self expectationWithDescription:@"Fix IHM layout transition also if one node is already loaded"];
[node measureWithSizeRange:ASSizeRangeMake(CGSizeZero, CGSizeZero)];
[node layoutThatFits:ASSizeRangeMake(CGSizeZero)];
XCTAssertEqual(node.subnodes[0], node1);
node.layoutState = @2;

View File

@@ -22,8 +22,9 @@
{
CGSize nodeSize = CGSizeMake(100, 100);
ASStaticSizeDisplayNode *displayNode = [ASStaticSizeDisplayNode new];
displayNode.staticSize = nodeSize;
ASDisplayNode *displayNode = [[ASDisplayNode alloc] init];
displayNode.width = ASDimensionMake(100);
displayNode.height = ASDimensionMake(100);
// Use a button node in here as ASButtonNode uses layoutSpecThatFits:
ASButtonNode *buttonNode = [ASButtonNode new];
@@ -47,8 +48,8 @@
{
CGSize nodeSize = CGSizeMake(100, 100);
ASStaticSizeDisplayNode *displayNode = [ASStaticSizeDisplayNode new];
displayNode.staticSize = nodeSize;
ASDisplayNode *displayNode = [ASDisplayNode new];
[displayNode setSizeWithCGSize:nodeSize];
ASButtonNode *buttonNode = [ASButtonNode new];
[displayNode addSubnode:buttonNode];
@@ -79,7 +80,7 @@
return [ASInsetLayoutSpec insetLayoutSpecWithInsets:UIEdgeInsetsZero child:someOtherNode];
};
XCTAssertThrows([displayNode measure:CGSizeMake(100, 100)], @"Should throw if subnode was added in layoutSpecThatFits:");
XCTAssertThrows([displayNode layoutThatFits:ASSizeRangeMake(CGSizeZero, CGSizeMake(100, 100))], @"Should throw if subnode was added in layoutSpecThatFits:");
}
- (void)testNotAllowModifyingSubnodesInLayoutSpecThatFits
@@ -95,7 +96,7 @@
return [ASInsetLayoutSpec insetLayoutSpecWithInsets:UIEdgeInsetsZero child:someOtherNode];
};
XCTAssertThrows([displayNode measure:CGSizeMake(100, 100)], @"Should throw if subnodes where modified in layoutSpecThatFits:");
XCTAssertThrows([displayNode layoutThatFits:ASSizeRangeMake(CGSizeZero, CGSizeMake(100, 100))], @"Should throw if subnodes where modified in layoutSpecThatFits:");
}
#endif
@@ -103,8 +104,8 @@
{
CGSize nodeSize = CGSizeMake(100, 100);
ASStaticSizeDisplayNode *displayNode = [ASStaticSizeDisplayNode new];
displayNode.staticSize = nodeSize;
ASDisplayNode *displayNode = [ASDisplayNode new];
[displayNode setSizeWithCGSize:nodeSize];
ASButtonNode *buttonNode = [ASButtonNode new];
[displayNode addSubnode:buttonNode];
@@ -121,7 +122,7 @@
[displayNode.view layoutIfNeeded];
XCTAssertEqual(numberOfLayoutSpecThatFitsCalls, 1, @"Should measure during layout if not measured");
[displayNode measureWithSizeRange:ASSizeRangeMake(nodeSize, nodeSize)];
[displayNode layoutThatFits:ASSizeRangeMake(nodeSize, nodeSize)];
XCTAssertEqual(numberOfLayoutSpecThatFitsCalls, 1, @"Should not remeasure with same bounds");
}

View File

@@ -28,7 +28,7 @@
node.layoutSpecBlock = ^(ASDisplayNode * _Nonnull node, ASSizeRange constrainedSize) {
return [ASInsetLayoutSpec insetLayoutSpecWithInsets:UIEdgeInsetsMake(5, 5, 5, 5) child:subnode];
};
[node measure:CGSizeMake(100, 100)];
[node layoutThatFits:ASSizeRangeMake(CGSizeZero, CGSizeMake(100, 100))];
ASSnapshotVerifyNode(node, nil);
}

View File

@@ -1991,7 +1991,7 @@ static bool stringContainsPointer(NSString *description, id p) {
- (void)DISABLED_testThatItsSafeToAutomeasureANodeMidTransition
{
ASDisplayNode *supernode = [[ASDisplayNode alloc] init];
[supernode measure:CGSizeMake(100, 100)];
[supernode layoutThatFits:ASSizeRangeMake(CGSizeZero, CGSizeMake(100, 100))];
ASDisplayNode *node = [[ASDisplayNode alloc] init];
node.bounds = CGRectMake(0, 0, 50, 50);
[supernode addSubnode:node];

View File

@@ -11,6 +11,7 @@
//
#import <XCTest/XCTest.h>
#import <AsyncDisplayKit/ASLayout.h>
#import <AsyncDisplayKit/ASEditableTextNode.h>
static BOOL CGSizeEqualToSizeWithIn(CGSize size1, CGSize size2, CGFloat delta)
@@ -138,23 +139,11 @@ static BOOL CGSizeEqualToSizeWithIn(CGSize size1, CGSize size2, CGFloat delta)
XCTAssertTrue(secureEditableTextNode.textView.secureTextEntry == YES, @"textView's isSecureTextEntry should be YES.");
}
- (void)testSetPreferredFrameSize
{
CGSize preferredFrameSize = CGSizeMake(100, 100);
_editableTextNode.preferredFrameSize = preferredFrameSize;
CGSize calculatedSize = [_editableTextNode measure:CGSizeZero];
XCTAssertTrue(calculatedSize.width != preferredFrameSize.width, @"Calculated width (%f) should be equal to preferred width (%f)", calculatedSize.width, preferredFrameSize.width);
XCTAssertTrue(calculatedSize.width != preferredFrameSize.width, @"Calculated height (%f) should be equal to preferred height (%f)", calculatedSize.width, preferredFrameSize.width);
_editableTextNode.preferredFrameSize = CGSizeZero;
}
- (void)testCalculatedSizeIsGreaterThanOrEqualToConstrainedSize
{
for (NSInteger i = 10; i < 500; i += 50) {
CGSize constrainedSize = CGSizeMake(i, i);
CGSize calculatedSize = [_editableTextNode measure:constrainedSize];
CGSize calculatedSize = [_editableTextNode layoutThatFits:ASSizeRangeMake(CGSizeZero, constrainedSize)].size;
XCTAssertTrue(calculatedSize.width <= constrainedSize.width, @"Calculated width (%f) should be less than or equal to constrained width (%f)", calculatedSize.width, constrainedSize.width);
XCTAssertTrue(calculatedSize.height <= constrainedSize.height, @"Calculated height (%f) should be less than or equal to constrained height (%f)", calculatedSize.height, constrainedSize.height);
}
@@ -164,8 +153,8 @@ static BOOL CGSizeEqualToSizeWithIn(CGSize size1, CGSize size2, CGFloat delta)
{
for (NSInteger i = 10; i < 500; i += 50) {
CGSize constrainedSize = CGSizeMake(i, i);
CGSize calculatedSize = [_editableTextNode measure:constrainedSize];
CGSize recalculatedSize = [_editableTextNode measure:calculatedSize];
CGSize calculatedSize = [_editableTextNode layoutThatFits:ASSizeRangeMake(CGSizeZero, constrainedSize)].size;
CGSize recalculatedSize = [_editableTextNode layoutThatFits:ASSizeRangeMake(CGSizeZero, constrainedSize)].size;
XCTAssertTrue(CGSizeEqualToSizeWithIn(calculatedSize, recalculatedSize, 4.0), @"Recalculated size %@ should be same as original size %@", NSStringFromCGSize(recalculatedSize), NSStringFromCGSize(calculatedSize));
}
@@ -175,8 +164,8 @@ static BOOL CGSizeEqualToSizeWithIn(CGSize size1, CGSize size2, CGFloat delta)
{
for (CGFloat i = 10; i < 500; i *= 1.3) {
CGSize constrainedSize = CGSizeMake(i, i);
CGSize calculatedSize = [_editableTextNode measure:constrainedSize];
CGSize recalculatedSize = [_editableTextNode measure:calculatedSize];
CGSize calculatedSize = [_editableTextNode layoutThatFits:ASSizeRangeMake(CGSizeZero, constrainedSize)].size;
CGSize recalculatedSize = [_editableTextNode layoutThatFits:ASSizeRangeMake(CGSizeZero, constrainedSize)].size;
XCTAssertTrue(CGSizeEqualToSizeWithIn(calculatedSize, recalculatedSize, 11.0), @"Recalculated size %@ should be same as original size %@", NSStringFromCGSize(recalculatedSize), NSStringFromCGSize(calculatedSize));
}

View File

@@ -20,7 +20,8 @@
- (UIImage *)testImage
{
NSString *path = [[NSBundle bundleForClass:[self class]] pathForResource:@"logo-square"
ofType:@"png" inDirectory:@"TestResources"];
ofType:@"png"
inDirectory:@"TestResources"];
return [UIImage imageWithContentsOfFile:path];
}
@@ -29,30 +30,32 @@
// trivial test case to ensure ASSnapshotTestCase works
ASImageNode *imageNode = [[ASImageNode alloc] init];
imageNode.image = [self testImage];
[imageNode measure:CGSizeMake(100, 100)];
[imageNode layoutThatFits:ASSizeRangeMake(CGSizeZero, CGSizeMake(100, 100))];
ASSnapshotVerifyNode(imageNode, nil);
}
- (void)testForcedScaling
{
ASImageNode *imageNode = [[ASImageNode alloc] init];
CGSize forcedImageSize = CGSizeMake(100, 100);
ASImageNode *imageNode = [[ASImageNode alloc] init];
imageNode.forcedSize = forcedImageSize;
imageNode.image = [self testImage];
imageNode.forcedSize = CGSizeMake(100, 100);
// Snapshot testing requires that node is formally laid out.
imageNode.preferredFrameSize = CGSizeMake(100, 100);
[imageNode measure:CGSizeMake(100, 100)];
[imageNode setSizeWithCGSize:forcedImageSize];
[imageNode layoutThatFits:ASSizeRangeMake(CGSizeZero, forcedImageSize)];
ASSnapshotVerifyNode(imageNode, @"first");
imageNode.preferredFrameSize = CGSizeMake(200, 200);
[imageNode measure:CGSizeMake(200, 200)];
[imageNode setSizeWithCGSize:CGSizeMake(200, 200)];
[imageNode layoutThatFits:ASSizeRangeMake(CGSizeZero, CGSizeMake(200, 200))];
ASSnapshotVerifyNode(imageNode, @"second");
XCTAssert(CGImageGetWidth((CGImageRef)imageNode.contents) == 100 * imageNode.contentsScale && CGImageGetHeight((CGImageRef)imageNode.contents) == 100 * imageNode.contentsScale, @"contents should be 100 x 100 by contents scale.");
XCTAssert(CGImageGetWidth((CGImageRef)imageNode.contents) == forcedImageSize.width * imageNode.contentsScale &&
CGImageGetHeight((CGImageRef)imageNode.contents) == forcedImageSize.height * imageNode.contentsScale,
@"Contents should be 100 x 100 by contents scale.");
}
@end

View File

@@ -50,12 +50,14 @@ static NSString *nameForInsets(UIEdgeInsets insets)
for (NSUInteger combination = 0; combination < 16; combination++) {
UIEdgeInsets insets = insetsForCombination(combination, 10);
ASDisplayNode *backgroundNode = ASDisplayNodeWithBackgroundColor([UIColor grayColor]);
ASStaticSizeDisplayNode *foregroundNode = ASDisplayNodeWithBackgroundColor([UIColor greenColor]);
foregroundNode.staticSize = {10, 10};
ASDisplayNode *foregroundNode = ASDisplayNodeWithBackgroundColor([UIColor greenColor], {10, 10});
ASLayoutSpec *layoutSpec =
[ASBackgroundLayoutSpec
backgroundLayoutSpecWithChild:[ASInsetLayoutSpec insetLayoutSpecWithInsets:insets child:foregroundNode]
backgroundLayoutSpecWithChild:
[ASInsetLayoutSpec
insetLayoutSpecWithInsets:insets
child:foregroundNode]
background:backgroundNode];
static ASSizeRange kVariableSize = {{0, 0}, {300, 300}};
@@ -71,12 +73,14 @@ static NSString *nameForInsets(UIEdgeInsets insets)
for (NSUInteger combination = 0; combination < 16; combination++) {
UIEdgeInsets insets = insetsForCombination(combination, 10);
ASDisplayNode *backgroundNode = ASDisplayNodeWithBackgroundColor([UIColor grayColor]);
ASStaticSizeDisplayNode *foregroundNode = ASDisplayNodeWithBackgroundColor([UIColor greenColor]);
foregroundNode.staticSize = {10, 10};
ASDisplayNode *foregroundNode = ASDisplayNodeWithBackgroundColor([UIColor greenColor], {10, 10});
ASLayoutSpec *layoutSpec =
[ASBackgroundLayoutSpec
backgroundLayoutSpecWithChild:[ASInsetLayoutSpec insetLayoutSpecWithInsets:insets child:foregroundNode]
backgroundLayoutSpecWithChild:
[ASInsetLayoutSpec
insetLayoutSpecWithInsets:insets
child:foregroundNode]
background:backgroundNode];
static ASSizeRange kFixedSize = {{300, 300}, {300, 300}};
@@ -93,12 +97,14 @@ static NSString *nameForInsets(UIEdgeInsets insets)
for (NSUInteger combination = 0; combination < 16; combination++) {
UIEdgeInsets insets = insetsForCombination(combination, 0);
ASDisplayNode *backgroundNode = ASDisplayNodeWithBackgroundColor([UIColor grayColor]);
ASStaticSizeDisplayNode *foregroundNode = ASDisplayNodeWithBackgroundColor([UIColor greenColor]);
foregroundNode.staticSize = {10, 10};
ASDisplayNode *foregroundNode = ASDisplayNodeWithBackgroundColor([UIColor greenColor], {10, 10});
ASLayoutSpec *layoutSpec =
[ASBackgroundLayoutSpec
backgroundLayoutSpecWithChild:[ASInsetLayoutSpec insetLayoutSpecWithInsets:insets child:foregroundNode]
backgroundLayoutSpecWithChild:
[ASInsetLayoutSpec
insetLayoutSpecWithInsets:insets
child:foregroundNode]
background:backgroundNode];
static ASSizeRange kFixedSize = {{300, 300}, {300, 300}};

View File

@@ -31,17 +31,16 @@
identifier:(NSString *)identifier;
@end
@interface ASStaticSizeDisplayNode : ASDisplayNode
@property (nonatomic) CGSize staticSize;
@end
static inline ASStaticSizeDisplayNode *ASDisplayNodeWithBackgroundColor(UIColor *backgroundColor)
{
ASStaticSizeDisplayNode *node = [[ASStaticSizeDisplayNode alloc] init];
__attribute__((overloadable)) static inline ASDisplayNode *ASDisplayNodeWithBackgroundColor(UIColor *backgroundColor, CGSize size) {
ASDisplayNode *node = [[ASDisplayNode alloc] init];
node.layerBacked = YES;
node.backgroundColor = backgroundColor;
node.staticSize = CGSizeZero;
node.width = ASDimensionMakeWithPoints(size.width);
node.height = ASDimensionMakeWithPoints(size.height);
return node;
}
__attribute__((overloadable)) static inline ASDisplayNode *ASDisplayNodeWithBackgroundColor(UIColor *backgroundColor)
{
return ASDisplayNodeWithBackgroundColor(backgroundColor, CGSizeZero);
}

View File

@@ -39,7 +39,7 @@
node.layoutSpecUnderTest = layoutSpec;
[node measureWithSizeRange:sizeRange];
[node layoutThatFits:sizeRange];
ASSnapshotVerifyNode(node, identifier);
}
@@ -60,12 +60,3 @@
}
@end
@implementation ASStaticSizeDisplayNode
- (CGSize)calculateSizeThatFits:(CGSize)constrainedSize
{
return _staticSize;
}
@end

View File

@@ -23,8 +23,7 @@ static const ASSizeRange kSize = {{320, 320}, {320, 320}};
- (void)testOverlay
{
ASDisplayNode *backgroundNode = ASDisplayNodeWithBackgroundColor([UIColor blueColor]);
ASStaticSizeDisplayNode *foregroundNode = ASDisplayNodeWithBackgroundColor([UIColor blackColor]);
foregroundNode.staticSize = {20, 20};
ASDisplayNode *foregroundNode = ASDisplayNodeWithBackgroundColor([UIColor blackColor], {20, 20});
ASLayoutSpec *layoutSpec =
[ASOverlayLayoutSpec

View File

@@ -21,8 +21,7 @@ static const ASSizeRange kFixedSize = {{0, 0}, {100, 100}};
- (void)testRatioLayoutSpecWithRatio:(CGFloat)ratio childSize:(CGSize)childSize identifier:(NSString *)identifier
{
ASStaticSizeDisplayNode *subnode = ASDisplayNodeWithBackgroundColor([UIColor greenColor]);
subnode.staticSize = childSize;
ASDisplayNode *subnode = ASDisplayNodeWithBackgroundColor([UIColor greenColor], childSize);
ASLayoutSpec *layoutSpec = [ASRatioLayoutSpec ratioLayoutSpecWithRatio:ratio child:subnode];

View File

@@ -23,7 +23,6 @@ static const ASSizeRange kSize = {{100, 120}, {320, 160}};
- (void)testWithOptions
{
[self testAllVerticalPositionsForHorizontalPosition:ASRelativeLayoutSpecPositionStart];
[self testAllVerticalPositionsForHorizontalPosition:ASRelativeLayoutSpecPositionCenter];
[self testAllVerticalPositionsForHorizontalPosition:ASRelativeLayoutSpecPositionEnd];
@@ -57,14 +56,16 @@ static const ASSizeRange kSize = {{100, 120}, {320, 160}};
sizingOptions:(ASRelativeLayoutSpecSizingOption)sizingOptions
{
ASDisplayNode *backgroundNode = ASDisplayNodeWithBackgroundColor([UIColor redColor]);
ASStaticSizeDisplayNode *foregroundNode = ASDisplayNodeWithBackgroundColor([UIColor greenColor]);
foregroundNode.staticSize = {70, 100};
ASDisplayNode *foregroundNode = ASDisplayNodeWithBackgroundColor([UIColor greenColor], CGSizeMake(70, 100));
ASLayoutSpec *layoutSpec =
[ASBackgroundLayoutSpec
backgroundLayoutSpecWithChild:
[ASRelativeLayoutSpec
relativePositionLayoutSpecWithHorizontalPosition:horizontalPosition verticalPosition:verticalPosition sizingOption:sizingOptions child:foregroundNode]
relativePositionLayoutSpecWithHorizontalPosition:horizontalPosition
verticalPosition:verticalPosition
sizingOption:sizingOptions
child:foregroundNode]
background:backgroundNode];
[self testLayoutSpec:layoutSpec
@@ -105,17 +106,26 @@ static NSString *suffixForPositionOptions(ASRelativeLayoutSpecPosition horizonta
- (void)testMinimumSizeRangeIsGivenToChildWhenNotPositioning
{
ASDisplayNode *backgroundNode = ASDisplayNodeWithBackgroundColor([UIColor redColor]);
ASStaticSizeDisplayNode *foregroundNode = ASDisplayNodeWithBackgroundColor([UIColor redColor]);
foregroundNode.staticSize = {10, 10};
ASDisplayNode *foregroundNode = ASDisplayNodeWithBackgroundColor([UIColor redColor], CGSizeMake(10, 10));
foregroundNode.flexGrow = YES;
ASLayoutSpec *childSpec = [ASBackgroundLayoutSpec
backgroundLayoutSpecWithChild:[ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionVertical spacing:0 justifyContent:ASStackLayoutJustifyContentStart alignItems:ASStackLayoutAlignItemsStart children:@[foregroundNode]]
background:backgroundNode];
ASLayoutSpec *childSpec =
[ASBackgroundLayoutSpec
backgroundLayoutSpecWithChild:
[ASStackLayoutSpec
stackLayoutSpecWithDirection:ASStackLayoutDirectionVertical
spacing:0
justifyContent:ASStackLayoutJustifyContentStart
alignItems:ASStackLayoutAlignItemsStart
children:@[foregroundNode]]
background:backgroundNode];
ASRelativeLayoutSpec *layoutSpec = [ASRelativeLayoutSpec
relativePositionLayoutSpecWithHorizontalPosition:ASRelativeLayoutSpecPositionStart verticalPosition:ASRelativeLayoutSpecPositionStart sizingOption:{} child:childSpec];
ASRelativeLayoutSpec *layoutSpec =
[ASRelativeLayoutSpec
relativePositionLayoutSpecWithHorizontalPosition:ASRelativeLayoutSpecPositionStart
verticalPosition:ASRelativeLayoutSpecPositionStart
sizingOption:{}
child:childSpec];
[self testLayoutSpec:layoutSpec sizeRange:kSize subnodes:@[backgroundNode, foregroundNode] identifier:nil];
}

View File

@@ -23,26 +23,31 @@
#pragma mark - Utility methods
static NSArray *defaultSubnodes()
static NSArray<ASDisplayNode *> *defaultSubnodes()
{
return defaultSubnodesWithSameSize(CGSizeZero, NO);
}
static NSArray *defaultSubnodesWithSameSize(CGSize subnodeSize, BOOL flex)
static NSArray<ASDisplayNode *> *defaultSubnodesWithSameSize(CGSize subnodeSize, BOOL flex)
{
NSArray *subnodes = @[
ASDisplayNodeWithBackgroundColor([UIColor redColor]),
ASDisplayNodeWithBackgroundColor([UIColor blueColor]),
ASDisplayNodeWithBackgroundColor([UIColor greenColor])
];
for (ASStaticSizeDisplayNode *subnode in subnodes) {
subnode.staticSize = subnodeSize;
NSArray<ASDisplayNode *> *subnodes = @[
ASDisplayNodeWithBackgroundColor([UIColor redColor], subnodeSize),
ASDisplayNodeWithBackgroundColor([UIColor blueColor], subnodeSize),
ASDisplayNodeWithBackgroundColor([UIColor greenColor], subnodeSize)
];
for (ASDisplayNode *subnode in subnodes) {
subnode.flexGrow = flex;
subnode.flexShrink = flex;
}
return subnodes;
}
static void setCGSizeToNode(CGSize size, ASDisplayNode *node)
{
node.width = ASDimensionMakeWithPoints(size.width);
node.height = ASDimensionMakeWithPoints(size.height);
}
- (void)testStackLayoutSpecWithJustify:(ASStackLayoutJustifyContent)justify
flex:(BOOL)flex
sizeRange:(ASSizeRange)sizeRange
@@ -53,7 +58,7 @@ static NSArray *defaultSubnodesWithSameSize(CGSize subnodeSize, BOOL flex)
.justifyContent = justify
};
NSArray *subnodes = defaultSubnodesWithSameSize({50, 50}, flex);
NSArray<ASDisplayNode *> *subnodes = defaultSubnodesWithSameSize({50, 50}, flex);
[self testStackLayoutSpecWithStyle:style sizeRange:sizeRange subnodes:subnodes identifier:identifier];
}
@@ -63,13 +68,13 @@ static NSArray *defaultSubnodesWithSameSize(CGSize subnodeSize, BOOL flex)
itemsVerticalAlignment:(ASVerticalAlignment)verticalAlignment
identifier:(NSString *)identifier
{
NSArray *subnodes = defaultSubnodesWithSameSize({50, 50}, NO);
NSArray<ASDisplayNode *> *subnodes = defaultSubnodesWithSameSize({50, 50}, NO);
ASStackLayoutSpec *stackLayoutSpec = [[ASStackLayoutSpec alloc] init];
stackLayoutSpec.direction = direction;
stackLayoutSpec.children = subnodes;
[stackLayoutSpec setHorizontalAlignment:horizontalAlignment];
[stackLayoutSpec setVerticalAlignment:verticalAlignment];
stackLayoutSpec.horizontalAlignment = horizontalAlignment;
stackLayoutSpec.verticalAlignment = verticalAlignment;
CGSize exactSize = CGSizeMake(200, 200);
static ASSizeRange kSize = ASSizeRangeMake(exactSize, exactSize);
@@ -90,11 +95,14 @@ static NSArray *defaultSubnodesWithSameSize(CGSize subnodeSize, BOOL flex)
subnodes:(NSArray *)subnodes
identifier:(NSString *)identifier
{
ASStackLayoutSpec *stackLayoutSpec = [ASStackLayoutSpec stackLayoutSpecWithDirection:style.direction
spacing:style.spacing
justifyContent:style.justifyContent
alignItems:style.alignItems
children:children];
ASStackLayoutSpec *stackLayoutSpec =
[ASStackLayoutSpec
stackLayoutSpecWithDirection:style.direction
spacing:style.spacing
justifyContent:style.justifyContent
alignItems:style.alignItems
children:children];
[self testStackLayoutSpec:stackLayoutSpec sizeRange:sizeRange subnodes:subnodes identifier:identifier];
}
@@ -104,7 +112,6 @@ static NSArray *defaultSubnodesWithSameSize(CGSize subnodeSize, BOOL flex)
identifier:(NSString *)identifier
{
ASDisplayNode *backgroundNode = ASDisplayNodeWithBackgroundColor([UIColor whiteColor]);
ASLayoutSpec *layoutSpec = [ASBackgroundLayoutSpec backgroundLayoutSpecWithChild:stackLayoutSpec background:backgroundNode];
NSMutableArray *newSubnodes = [NSMutableArray arrayWithObject:backgroundNode];
@@ -145,8 +152,8 @@ static NSArray *defaultSubnodesWithSameSize(CGSize subnodeSize, BOOL flex)
{
ASStackLayoutSpecStyle style = {.direction = ASStackLayoutDirectionHorizontal};
NSArray *subnodes = defaultSubnodesWithSameSize({50, 50}, NO);
((ASDisplayNode *)subnodes[1]).flexShrink = YES;
NSArray<ASDisplayNode *> *subnodes = defaultSubnodesWithSameSize({50, 50}, NO);
subnodes[1].flexShrink = YES;
// 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}};
@@ -157,8 +164,8 @@ static NSArray *defaultSubnodesWithSameSize(CGSize subnodeSize, BOOL flex)
{
ASStackLayoutSpecStyle style = {.direction = ASStackLayoutDirectionHorizontal};
NSArray *subnodes = defaultSubnodesWithSameSize({50, 50}, YES);
((ASStaticSizeDisplayNode *)subnodes[1]).staticSize = {150, 150};
NSArray<ASDisplayNode *> *subnodes = defaultSubnodesWithSameSize({50, 50}, YES);
setCGSizeToNode({150, 150}, subnodes[1]);
// width 300px; height 0-150px.
static ASSizeRange kUnderflowSize = {{300, 0}, {300, 150}};
@@ -173,10 +180,10 @@ static NSArray *defaultSubnodesWithSameSize(CGSize subnodeSize, BOOL flex)
{
ASStackLayoutSpecStyle style = {.direction = ASStackLayoutDirectionVertical};
NSArray *subnodes = defaultSubnodes();
((ASStaticSizeDisplayNode *)subnodes[0]).staticSize = {50, 50};
((ASStaticSizeDisplayNode *)subnodes[1]).staticSize = {100, 50};
((ASStaticSizeDisplayNode *)subnodes[2]).staticSize = {150, 50};
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}};
@@ -194,10 +201,10 @@ static NSArray *defaultSubnodesWithSameSize(CGSize subnodeSize, BOOL flex)
.spacing = 10
};
NSArray *subnodes = defaultSubnodes();
((ASStaticSizeDisplayNode *)subnodes[0]).staticSize = {50, 50};
((ASStaticSizeDisplayNode *)subnodes[1]).staticSize = {100, 50};
((ASStaticSizeDisplayNode *)subnodes[2]).staticSize = {150, 50};
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}};
@@ -212,12 +219,16 @@ static NSArray *defaultSubnodesWithSameSize(CGSize subnodeSize, BOOL flex)
ASLayoutSpec *layoutSpec =
[ASInsetLayoutSpec
insetLayoutSpecWithInsets:{10, 10, 10 ,10}
insetLayoutSpecWithInsets:{10, 10, 10, 10}
child:
[ASBackgroundLayoutSpec
backgroundLayoutSpecWithChild:
[ASStackLayoutSpec
stackLayoutSpecWithDirection:ASStackLayoutDirectionVertical spacing:10 justifyContent:ASStackLayoutJustifyContentStart alignItems:ASStackLayoutAlignItemsStretch children:@[]]
stackLayoutSpecWithDirection:ASStackLayoutDirectionVertical
spacing:10
justifyContent:ASStackLayoutJustifyContentStart
alignItems:ASStackLayoutAlignItemsStretch
children:@[]]
background:backgroundNode]];
// width 300px; height 0-300px
@@ -231,28 +242,28 @@ static NSArray *defaultSubnodesWithSameSize(CGSize subnodeSize, BOOL flex)
static ASSizeRange kAnySize = {{0, 0}, {INFINITY, INFINITY}};
ASStackLayoutSpecStyle style = {.direction = ASStackLayoutDirectionVertical};
NSArray *subnodes = defaultSubnodes();
((ASStaticSizeDisplayNode *)subnodes[0]).staticSize = {50, 50};
((ASStaticSizeDisplayNode *)subnodes[1]).staticSize = {100, 70};
((ASStaticSizeDisplayNode *)subnodes[2]).staticSize = {150, 90};
NSArray<ASDisplayNode *> *subnodes = defaultSubnodes();
setCGSizeToNode({50, 50}, subnodes[0]);
setCGSizeToNode({100, 70}, subnodes[1]);
setCGSizeToNode({150, 90}, subnodes[2]);
((ASStaticSizeDisplayNode *)subnodes[1]).spacingBefore = 10;
((ASStaticSizeDisplayNode *)subnodes[2]).spacingBefore = 20;
subnodes[1].spacingBefore = 10;
subnodes[2].spacingBefore = 20;
[self testStackLayoutSpecWithStyle:style sizeRange:kAnySize subnodes:subnodes identifier:@"spacingBefore"];
// Reset above spacing values
((ASStaticSizeDisplayNode *)subnodes[1]).spacingBefore = 0;
((ASStaticSizeDisplayNode *)subnodes[2]).spacingBefore = 0;
subnodes[1].spacingBefore = 0;
subnodes[2].spacingBefore = 0;
((ASStaticSizeDisplayNode *)subnodes[1]).spacingAfter = 10;
((ASStaticSizeDisplayNode *)subnodes[2]).spacingAfter = 20;
subnodes[1].spacingAfter = 10;
subnodes[2].spacingAfter = 20;
[self testStackLayoutSpecWithStyle:style sizeRange:kAnySize subnodes:subnodes identifier:@"spacingAfter"];
// Reset above spacing values
((ASStaticSizeDisplayNode *)subnodes[1]).spacingAfter = 0;
((ASStaticSizeDisplayNode *)subnodes[2]).spacingAfter = 0;
subnodes[1].spacingAfter = 0;
subnodes[2].spacingAfter = 0;
style.spacing = 10;
((ASStaticSizeDisplayNode *)subnodes[1]).spacingBefore = -10;
((ASStaticSizeDisplayNode *)subnodes[1]).spacingAfter = -10;
subnodes[1].spacingBefore = -10;
subnodes[1].spacingAfter = -10;
[self testStackLayoutSpecWithStyle:style sizeRange:kAnySize subnodes:subnodes identifier:@"spacingBalancedOut"];
}
@@ -263,14 +274,14 @@ static NSArray *defaultSubnodesWithSameSize(CGSize subnodeSize, BOOL flex)
.justifyContent = ASStackLayoutJustifyContentCenter
};
NSArray *subnodes = defaultSubnodes();
((ASStaticSizeDisplayNode *)subnodes[0]).staticSize = {50, 50};
((ASStaticSizeDisplayNode *)subnodes[1]).staticSize = {100, 70};
((ASStaticSizeDisplayNode *)subnodes[2]).staticSize = {150, 90};
NSArray<ASDisplayNode *> *subnodes = defaultSubnodes();
setCGSizeToNode({50, 50}, subnodes[0]);
setCGSizeToNode({100, 70}, subnodes[1]);
setCGSizeToNode({150, 90}, subnodes[2]);
((ASStaticSizeDisplayNode *)subnodes[0]).spacingBefore = 0;
((ASStaticSizeDisplayNode *)subnodes[1]).spacingBefore = 20;
((ASStaticSizeDisplayNode *)subnodes[2]).spacingBefore = 30;
subnodes[0].spacingBefore = 0;
subnodes[1].spacingBefore = 20;
subnodes[2].spacingBefore = 30;
// width 0-300px; height 300px
static ASSizeRange kVariableHeight = {{0, 300}, {300, 300}};
@@ -284,8 +295,7 @@ static NSArray *defaultSubnodesWithSameSize(CGSize subnodeSize, BOOL flex)
.justifyContent = ASStackLayoutJustifyContentSpaceBetween
};
ASStaticSizeDisplayNode *child = ASDisplayNodeWithBackgroundColor([UIColor redColor]);
child.staticSize = {50, 50};
ASDisplayNode *child = ASDisplayNodeWithBackgroundColor([UIColor redColor], {50, 50});
// width 300px; height 0-INF
static ASSizeRange kVariableHeight = {{300, 0}, {300, INFINITY}};
@@ -299,8 +309,7 @@ static NSArray *defaultSubnodesWithSameSize(CGSize subnodeSize, BOOL flex)
.justifyContent = ASStackLayoutJustifyContentSpaceAround
};
ASStaticSizeDisplayNode *child = ASDisplayNodeWithBackgroundColor([UIColor redColor]);
child.staticSize = {50, 50};
ASDisplayNode *child = ASDisplayNodeWithBackgroundColor([UIColor redColor], {50, 50});
// width 300px; height 0-INF
static ASSizeRange kVariableHeight = {{300, 0}, {300, INFINITY}};
@@ -325,12 +334,11 @@ static NSArray *defaultSubnodesWithSameSize(CGSize subnodeSize, BOOL flex)
{
ASStackLayoutSpecStyle style = {.direction = ASStackLayoutDirectionHorizontal};
ASStaticSizeDisplayNode * subnode1 = ASDisplayNodeWithBackgroundColor([UIColor blueColor]);
ASStaticSizeDisplayNode * subnode2 = ASDisplayNodeWithBackgroundColor([UIColor redColor]);
subnode2.staticSize = {50, 50};
ASDisplayNode * subnode1 = ASDisplayNodeWithBackgroundColor([UIColor blueColor]);
ASDisplayNode * subnode2 = ASDisplayNodeWithBackgroundColor([UIColor redColor], {50, 50});
ASRatioLayoutSpec *child1 = [ASRatioLayoutSpec ratioLayoutSpecWithRatio:1.5 child:subnode1];
child1.flexBasis = ASRelativeDimensionMakeWithFraction(1);
child1.flexBasis = ASDimensionMakeWithFraction(1);
child1.flexGrow = YES;
child1.flexShrink = YES;
@@ -345,15 +353,13 @@ static NSArray *defaultSubnodesWithSameSize(CGSize subnodeSize, BOOL flex)
.alignItems = ASStackLayoutAlignItemsCenter
};
ASStaticSizeDisplayNode *subnode1 = ASDisplayNodeWithBackgroundColor([UIColor redColor]);
subnode1.staticSize = {100, 100};
ASDisplayNode *subnode1 = ASDisplayNodeWithBackgroundColor([UIColor redColor], {100, 100});
subnode1.flexShrink = YES;
ASStaticSizeDisplayNode *subnode2 = ASDisplayNodeWithBackgroundColor([UIColor blueColor]);
subnode2.staticSize = {50, 50};
ASDisplayNode *subnode2 = ASDisplayNodeWithBackgroundColor([UIColor blueColor], {50, 50});
subnode2.flexShrink = YES;
NSArray *subnodes = @[subnode1, subnode2];
NSArray<ASDisplayNode *> *subnodes = @[subnode1, subnode2];
static ASSizeRange kFixedWidth = {{150, 0}, {150, 100}};
[self testStackLayoutSpecWithStyle:style sizeRange:kFixedWidth subnodes:subnodes identifier:nil];
}
@@ -362,14 +368,12 @@ static NSArray *defaultSubnodesWithSameSize(CGSize subnodeSize, BOOL flex)
{
ASStackLayoutSpecStyle style = {.direction = ASStackLayoutDirectionHorizontal};
ASStaticSizeDisplayNode *subnode1 = ASDisplayNodeWithBackgroundColor([UIColor redColor]);
subnode1.staticSize = {100, 100};
ASDisplayNode *subnode1 = ASDisplayNodeWithBackgroundColor([UIColor redColor], {100, 100});
ASStaticSizeDisplayNode *subnode2 = ASDisplayNodeWithBackgroundColor([UIColor blueColor]);
subnode2.staticSize = {50, 50};
ASDisplayNode *subnode2 = ASDisplayNodeWithBackgroundColor([UIColor blueColor], {50, 50});
subnode2.alignSelf = ASStackLayoutAlignSelfCenter;
NSArray *subnodes = @[subnode1, subnode2];
NSArray<ASDisplayNode *> *subnodes = @[subnode1, subnode2];
static ASSizeRange kFixedWidth = {{150, 0}, {150, INFINITY}};
[self testStackLayoutSpecWithStyle:style sizeRange:kFixedWidth subnodes:subnodes identifier:nil];
}
@@ -382,14 +386,14 @@ static NSArray *defaultSubnodesWithSameSize(CGSize subnodeSize, BOOL flex)
.alignItems = ASStackLayoutAlignItemsStart
};
NSArray *subnodes = defaultSubnodes();
((ASStaticSizeDisplayNode *)subnodes[0]).staticSize = {50, 50};
((ASStaticSizeDisplayNode *)subnodes[1]).staticSize = {100, 70};
((ASStaticSizeDisplayNode *)subnodes[2]).staticSize = {150, 90};
NSArray<ASDisplayNode *> *subnodes = defaultSubnodes();
setCGSizeToNode({50, 50}, subnodes[0]);
setCGSizeToNode({100, 70}, subnodes[1]);
setCGSizeToNode({150, 90}, subnodes[2]);
((ASStaticSizeDisplayNode *)subnodes[0]).spacingBefore = 0;
((ASStaticSizeDisplayNode *)subnodes[1]).spacingBefore = 20;
((ASStaticSizeDisplayNode *)subnodes[2]).spacingBefore = 30;
subnodes[0].spacingBefore = 0;
subnodes[1].spacingBefore = 20;
subnodes[2].spacingBefore = 30;
static ASSizeRange kExactSize = {{300, 300}, {300, 300}};
[self testStackLayoutSpecWithStyle:style sizeRange:kExactSize subnodes:subnodes identifier:nil];
@@ -403,14 +407,14 @@ static NSArray *defaultSubnodesWithSameSize(CGSize subnodeSize, BOOL flex)
.alignItems = ASStackLayoutAlignItemsEnd
};
NSArray *subnodes = defaultSubnodes();
((ASStaticSizeDisplayNode *)subnodes[0]).staticSize = {50, 50};
((ASStaticSizeDisplayNode *)subnodes[1]).staticSize = {100, 70};
((ASStaticSizeDisplayNode *)subnodes[2]).staticSize = {150, 90};
NSArray<ASDisplayNode *> *subnodes = defaultSubnodes();
setCGSizeToNode({50, 50}, subnodes[0]);
setCGSizeToNode({100, 70}, subnodes[1]);
setCGSizeToNode({150, 90}, subnodes[2]);
((ASStaticSizeDisplayNode *)subnodes[0]).spacingBefore = 0;
((ASStaticSizeDisplayNode *)subnodes[1]).spacingBefore = 20;
((ASStaticSizeDisplayNode *)subnodes[2]).spacingBefore = 30;
subnodes[0].spacingBefore = 0;
subnodes[1].spacingBefore = 20;
subnodes[2].spacingBefore = 30;
static ASSizeRange kExactSize = {{300, 300}, {300, 300}};
[self testStackLayoutSpecWithStyle:style sizeRange:kExactSize subnodes:subnodes identifier:nil];
@@ -424,14 +428,14 @@ static NSArray *defaultSubnodesWithSameSize(CGSize subnodeSize, BOOL flex)
.alignItems = ASStackLayoutAlignItemsCenter
};
NSArray *subnodes = defaultSubnodes();
((ASStaticSizeDisplayNode *)subnodes[0]).staticSize = {50, 50};
((ASStaticSizeDisplayNode *)subnodes[1]).staticSize = {100, 70};
((ASStaticSizeDisplayNode *)subnodes[2]).staticSize = {150, 90};
NSArray<ASDisplayNode *> *subnodes = defaultSubnodes();
setCGSizeToNode({50, 50}, subnodes[0]);
setCGSizeToNode({100, 70}, subnodes[1]);
setCGSizeToNode({150, 90}, subnodes[2]);
((ASStaticSizeDisplayNode *)subnodes[0]).spacingBefore = 0;
((ASStaticSizeDisplayNode *)subnodes[1]).spacingBefore = 20;
((ASStaticSizeDisplayNode *)subnodes[2]).spacingBefore = 30;
subnodes[0].spacingBefore = 0;
subnodes[1].spacingBefore = 20;
subnodes[2].spacingBefore = 30;
static ASSizeRange kExactSize = {{300, 300}, {300, 300}};
[self testStackLayoutSpecWithStyle:style sizeRange:kExactSize subnodes:subnodes identifier:nil];
@@ -445,14 +449,14 @@ static NSArray *defaultSubnodesWithSameSize(CGSize subnodeSize, BOOL flex)
.alignItems = ASStackLayoutAlignItemsStretch
};
NSArray *subnodes = defaultSubnodes();
((ASStaticSizeDisplayNode *)subnodes[0]).staticSize = {50, 50};
((ASStaticSizeDisplayNode *)subnodes[1]).staticSize = {100, 70};
((ASStaticSizeDisplayNode *)subnodes[2]).staticSize = {150, 90};
NSArray<ASDisplayNode *> *subnodes = defaultSubnodes();
setCGSizeToNode({50, 50}, subnodes[0]);
setCGSizeToNode({100, 70}, subnodes[1]);
setCGSizeToNode({150, 90}, subnodes[2]);
((ASStaticSizeDisplayNode *)subnodes[0]).spacingBefore = 0;
((ASStaticSizeDisplayNode *)subnodes[1]).spacingBefore = 20;
((ASStaticSizeDisplayNode *)subnodes[2]).spacingBefore = 30;
subnodes[0].spacingBefore = 0;
subnodes[1].spacingBefore = 20;
subnodes[2].spacingBefore = 30;
static ASSizeRange kVariableSize = {{200, 200}, {300, 300}};
// all children should be 200px wide
@@ -467,14 +471,14 @@ static NSArray *defaultSubnodesWithSameSize(CGSize subnodeSize, BOOL flex)
.alignItems = ASStackLayoutAlignItemsStretch
};
NSArray *subnodes = defaultSubnodes();
((ASStaticSizeDisplayNode *)subnodes[0]).staticSize = {50, 50};
((ASStaticSizeDisplayNode *)subnodes[1]).staticSize = {100, 70};
((ASStaticSizeDisplayNode *)subnodes[2]).staticSize = {150, 90};
NSArray<ASDisplayNode *> *subnodes = defaultSubnodes();
setCGSizeToNode({50, 50}, subnodes[0]);
setCGSizeToNode({100, 70}, subnodes[1]);
setCGSizeToNode({150, 90}, subnodes[2]);
((ASStaticSizeDisplayNode *)subnodes[0]).spacingBefore = 0;
((ASStaticSizeDisplayNode *)subnodes[1]).spacingBefore = 20;
((ASStaticSizeDisplayNode *)subnodes[2]).spacingBefore = 30;
subnodes[0].spacingBefore = 0;
subnodes[1].spacingBefore = 20;
subnodes[2].spacingBefore = 30;
static ASSizeRange kVariableSize = {{50, 50}, {300, 300}};
// all children should be 150px wide
@@ -491,12 +495,12 @@ static NSArray *defaultSubnodesWithSameSize(CGSize subnodeSize, BOOL flex)
{
ASStackLayoutSpecStyle style = {.direction = ASStackLayoutDirectionHorizontal};
NSArray *subnodes = defaultSubnodesWithSameSize({50, 50}, NO);
((ASStaticSizeDisplayNode *)subnodes[1]).staticSize = {150, 150};
NSArray<ASDisplayNode *> *subnodes = defaultSubnodesWithSameSize({50, 50}, NO);
setCGSizeToNode({150, 150}, subnodes[1]);
for (ASStaticSizeDisplayNode *subnode in subnodes) {
for (ASDisplayNode *subnode in subnodes) {
subnode.flexGrow = YES;
subnode.flexBasis = ASRelativeDimensionMakeWithPoints(10);
subnode.flexBasis = ASDimensionMakeWithPoints(10);
}
// width 300px; height 0-150px.
@@ -512,15 +516,14 @@ static NSArray *defaultSubnodesWithSameSize(CGSize subnodeSize, BOOL flex)
{
ASStackLayoutSpecStyle style = {.direction = ASStackLayoutDirectionHorizontal};
NSArray *subnodes = defaultSubnodesWithSameSize({50, 50}, NO);
for (ASStaticSizeDisplayNode *subnode in subnodes) {
NSArray<ASDisplayNode *> *subnodes = defaultSubnodesWithSameSize({50, 50}, NO);
for (ASDisplayNode *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 = ASRelativeDimensionMakeWithFraction(0.5);
subnodes[0].flexBasis = ASDimensionMakeWithFraction(0.5);
static ASSizeRange kSize = {{200, 0}, {200, INFINITY}};
[self testStackLayoutSpecWithStyle:style sizeRange:kSize subnodes:subnodes identifier:nil];
@@ -530,13 +533,13 @@ static NSArray *defaultSubnodesWithSameSize(CGSize subnodeSize, BOOL flex)
{
ASStackLayoutSpecStyle style = {.direction = ASStackLayoutDirectionHorizontal};
NSArray *subnodes = defaultSubnodes();
((ASStaticSizeDisplayNode *)subnodes[0]).staticSize = {50, 50};
((ASStaticSizeDisplayNode *)subnodes[1]).staticSize = {150, 150};
((ASStaticSizeDisplayNode *)subnodes[2]).staticSize = {50, 50};
NSArray<ASDisplayNode *> *subnodes = defaultSubnodes();
setCGSizeToNode({50, 50}, subnodes[0]);
setCGSizeToNode({150, 150}, subnodes[1]);
setCGSizeToNode({150, 50}, subnodes[2]);
for (ASStaticSizeDisplayNode *subnode in subnodes) {
subnode.flexBasis = ASRelativeDimensionMakeWithPoints(20);
for (ASDisplayNode *subnode in subnodes) {
subnode.flexBasis = ASDimensionMakeWithPoints(20);
}
static ASSizeRange kSize = {{300, 0}, {300, 150}};
@@ -545,13 +548,11 @@ static NSArray *defaultSubnodesWithSameSize(CGSize subnodeSize, BOOL flex)
- (void)testCrossAxisStretchingOccursAfterStackAxisFlexing
{
NSArray *subnodes = @[
ASDisplayNodeWithBackgroundColor([UIColor greenColor]),
ASDisplayNodeWithBackgroundColor([UIColor blueColor]),
ASDisplayNodeWithBackgroundColor([UIColor redColor])
];
((ASStaticSizeDisplayNode *)subnodes[1]).staticSize = {10, 0};
((ASStaticSizeDisplayNode *)subnodes[2]).staticSize = {3000, 3000};
NSArray<ASDisplayNode *> *subnodes = @[
ASDisplayNodeWithBackgroundColor([UIColor greenColor]),
ASDisplayNodeWithBackgroundColor([UIColor blueColor], {10, 0}),
ASDisplayNodeWithBackgroundColor([UIColor redColor], {3000, 3000})
];
ASRatioLayoutSpec *child2 = [ASRatioLayoutSpec ratioLayoutSpecWithRatio:1.0 child:subnodes[2]];
child2.flexGrow = YES;
@@ -565,7 +566,12 @@ static NSArray *defaultSubnodesWithSameSize(CGSize subnodeSize, BOOL flex)
[ASInsetLayoutSpec
insetLayoutSpecWithInsets:UIEdgeInsetsMake(10, 10, 10, 10)
child:
[ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionHorizontal spacing:0 justifyContent:ASStackLayoutJustifyContentStart alignItems:ASStackLayoutAlignItemsStretch children:@[subnodes[1], child2,]]
[ASStackLayoutSpec
stackLayoutSpecWithDirection:ASStackLayoutDirectionHorizontal
spacing:0
justifyContent:ASStackLayoutJustifyContentStart
alignItems:ASStackLayoutAlignItemsStretch
children:@[subnodes[1], child2]]
]
background:subnodes[0]];
@@ -576,17 +582,16 @@ static NSArray *defaultSubnodesWithSameSize(CGSize subnodeSize, BOOL flex)
- (void)testViolationIsDistributedEquallyAmongFlexibleChildren
{
ASStackLayoutSpecStyle style = {.direction = ASStackLayoutDirectionHorizontal};
NSArray<ASDisplayNode *> *subnodes = defaultSubnodes();
NSArray *subnodes = defaultSubnodes();
setCGSizeToNode({300, 50}, subnodes[0]);
setCGSizeToNode({100, 50}, subnodes[1]);
setCGSizeToNode({200, 50}, subnodes[2]);
((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};
((ASStaticSizeDisplayNode *)subnodes[2]).flexShrink = YES;
subnodes[0].flexShrink = YES;
subnodes[1].flexShrink = NO;
subnodes[2].flexShrink = YES;
// A width of 400px results in a violation of 200px. This is distributed equally among each flexible child,
// causing both of them to be shrunk by 100px, resulting in widths of 300px, 100px, and 50px.

View File

@@ -22,46 +22,34 @@
- (void)testSizingBehaviour
{
[self testWithSizeRange:ASSizeRangeMake(CGSizeMake(150, 200), CGSizeMake(FLT_MAX, FLT_MAX))
[self testWithSizeRange:ASSizeRangeMake(CGSizeMake(150, 200), CGSizeMake(INFINITY, INFINITY))
identifier:@"underflowChildren"];
[self testWithSizeRange:ASSizeRangeMake(CGSizeZero, CGSizeMake(50, 100))
identifier:@"overflowChildren"];
// Expect the spec to wrap its content because children sizes are between constrained size
[self testWithSizeRange:ASSizeRangeMake(CGSizeZero, CGSizeMake(FLT_MAX / 2, FLT_MAX / 2))
[self testWithSizeRange:ASSizeRangeMake(CGSizeZero, CGSizeMake(INFINITY / 2, INFINITY / 2))
identifier:@"wrappedChildren"];
}
- (void)testChildrenMeasuredWithAutoMaxSize
{
ASStaticSizeDisplayNode *firstChild = ASDisplayNodeWithBackgroundColor([UIColor redColor]);
ASDisplayNode *firstChild = ASDisplayNodeWithBackgroundColor([UIColor redColor], (CGSize){50, 50});
firstChild.layoutPosition = CGPointMake(0, 0);
firstChild.staticSize = CGSizeMake(50, 50);
ASStaticSizeDisplayNode *secondChild = ASDisplayNodeWithBackgroundColor([UIColor blueColor]);
ASDisplayNode *secondChild = ASDisplayNodeWithBackgroundColor([UIColor blueColor], (CGSize){100, 100});
secondChild.layoutPosition = CGPointMake(10, 60);
secondChild.staticSize = CGSizeMake(100, 100);
ASSizeRange sizeRange = ASSizeRangeMake(CGSizeMake(10, 10), CGSizeMake(110, 160));
[self testWithChildren:@[firstChild, secondChild] sizeRange:sizeRange identifier:nil];
XCTAssertTrue(ASSizeRangeEqualToSizeRange(firstChild.constrainedSizeForCalculatedLayout,
ASSizeRangeMake(CGSizeZero, sizeRange.max)));
CGSize secondChildMaxSize = CGSizeMake(sizeRange.max.width - secondChild.layoutPosition.x,
sizeRange.max.height - secondChild.layoutPosition.y);
XCTAssertTrue(ASSizeRangeEqualToSizeRange(secondChild.constrainedSizeForCalculatedLayout,
ASSizeRangeMake(CGSizeZero, secondChildMaxSize)));
}
- (void)testWithSizeRange:(ASSizeRange)sizeRange identifier:(NSString *)identifier
{
ASDisplayNode *firstChild = ASDisplayNodeWithBackgroundColor([UIColor redColor]);
ASDisplayNode *firstChild = ASDisplayNodeWithBackgroundColor([UIColor redColor], (CGSize){50, 50});
firstChild.layoutPosition = CGPointMake(0, 0);
firstChild.sizeRange = ASRelativeSizeRangeMakeWithExactCGSize(CGSizeMake(50, 50));
ASDisplayNode *secondChild = ASDisplayNodeWithBackgroundColor([UIColor blueColor]);
ASDisplayNode *secondChild = ASDisplayNodeWithBackgroundColor([UIColor blueColor], (CGSize){100, 100});
secondChild.layoutPosition = CGPointMake(0, 50);
secondChild.sizeRange = ASRelativeSizeRangeMakeWithExactCGSize(CGSizeMake(100, 100));
[self testWithChildren:@[firstChild, secondChild] sizeRange:sizeRange identifier:identifier];
}
@@ -73,9 +61,12 @@
NSMutableArray *subnodes = [NSMutableArray arrayWithArray:children];
[subnodes insertObject:backgroundNode atIndex:0];
ASStaticLayoutSpec *staticLayoutSpec = [ASStaticLayoutSpec staticLayoutSpecWithChildren:children];
ASLayoutSpec *layoutSpec = [ASBackgroundLayoutSpec backgroundLayoutSpecWithChild:staticLayoutSpec
background:backgroundNode];
ASLayoutSpec *layoutSpec =
[ASBackgroundLayoutSpec
backgroundLayoutSpecWithChild:
[ASStaticLayoutSpec
staticLayoutSpecWithChildren:children]
background:backgroundNode];
[self testLayoutSpec:layoutSpec sizeRange:sizeRange subnodes:subnodes identifier:identifier];
}

View File

@@ -148,7 +148,7 @@
- (ASSizeRange)tableView:(ASTableView *)tableView constrainedSizeForRowAtIndexPath:(NSIndexPath *)indexPath
{
return ASSizeRangeMakeExactSize(CGSizeMake(10, 42));
return ASSizeRangeMake(CGSizeMake(10, 42));
}
@end

View File

@@ -9,6 +9,7 @@
#import <XCTest/XCTest.h>
#import "ASPerformanceTestContext.h"
#import <AsyncDisplayKit/ASTextNode.h>
#import <AsyncDisplayKit/ASLayout.h>
#import "ASinternalHelpers.h"
#import "ASXCTExtensions.h"
#include "CGRect+ASConvenience.h"
@@ -71,7 +72,7 @@ static NSString *const kTestCaseUIKitWithReusedContext = @"UIKitReusedContext";
NSAttributedString *text = data[i % data.count];
startMeasuring();
node.attributedText = text;
asdkSize = [node measure:maxSize];
asdkSize = [node layoutThatFits:ASSizeRangeMake(CGSizeZero, maxSize)].size;
stopMeasuring();
}];
ctx.results[kTestCaseASDK].userInfo[@"size"] = NSStringFromCGSize(asdkSize);
@@ -101,7 +102,7 @@ static NSString *const kTestCaseUIKitWithReusedContext = @"UIKitReusedContext";
ASTextNode *node = [[ASTextNode alloc] init];
startMeasuring();
node.attributedText = text;
asdkSize = [node measure:maxSize];
asdkSize = [node layoutThatFits:ASSizeRangeMake(CGSizeZero, maxSize)].size;
stopMeasuring();
}];
ctx.results[kTestCaseASDK].userInfo[@"size"] = NSStringFromCGSize(asdkSize);
@@ -131,7 +132,7 @@ static NSString *const kTestCaseUIKitWithReusedContext = @"UIKitReusedContext";
ASTextNode *node = [[ASTextNode alloc] init];
startMeasuring();
node.attributedText = text;
asdkSize = [node measure:maxSize];
asdkSize = [node layoutThatFits:ASSizeRangeMake(CGSizeZero, maxSize)].size;
stopMeasuring();
}];
testCtx.results[kTestCaseASDK].userInfo[@"size"] = NSStringFromCGSize(asdkSize);

View File

@@ -10,8 +10,8 @@
//
#import "ASSnapshotTestCase.h"
#import <AsyncDisplayKit/AsyncDisplayKit.h>
#import "ASLayout.h"
@interface ASTextNodeSnapshotTests : ASSnapshotTestCase
@@ -25,7 +25,7 @@
ASTextNode *textNode = [[ASTextNode alloc] init];
textNode.attributedText = [[NSAttributedString alloc] initWithString:@"judar"
attributes:@{NSFontAttributeName : [UIFont italicSystemFontOfSize:24]}];
[textNode measureWithSizeRange:ASSizeRangeMake(CGSizeZero, CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX))];
[textNode layoutThatFits:ASSizeRangeMake(CGSizeZero, CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX))];
textNode.textContainerInset = UIEdgeInsetsMake(0, 2, 0, 2);
ASSnapshotVerifyNode(textNode, nil);
@@ -40,7 +40,7 @@
textNode.attributedText = [[NSAttributedString alloc] initWithString:@"judar judar judar judar judar judar"
attributes:@{ NSFontAttributeName : [UIFont systemFontOfSize:30] }];
[textNode measureWithSizeRange:ASSizeRangeMake(CGSizeZero, CGSizeMake(100, 80))];
[textNode layoutThatFits:ASSizeRangeMake(CGSizeZero, CGSizeMake(100, 80))];
textNode.frame = CGRectMake(50, 50, textNode.calculatedSize.width, textNode.calculatedSize.height);
textNode.textContainerInset = UIEdgeInsetsMake(10, 10, 10, 10);
@@ -62,7 +62,7 @@
textNode.attributedText = [[NSAttributedString alloc] initWithString:@"yolo"
attributes:@{ NSFontAttributeName : [UIFont systemFontOfSize:30] }];
[textNode measureWithSizeRange:ASSizeRangeMake(CGSizeZero, CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX))];
[textNode layoutThatFits:ASSizeRangeMake(CGSizeZero, CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX))];
textNode.frame = CGRectMake(50, 50, textNode.calculatedSize.width, textNode.calculatedSize.height);
textNode.textContainerInset = UIEdgeInsetsMake(5, 10, 10, 5);

View File

@@ -12,6 +12,7 @@
#import <OCMock/OCMock.h>
#import <AsyncDisplayKit/ASLayout.h>
#import <AsyncDisplayKit/ASTextNode.h>
#import <XCTest/XCTest.h>
@@ -111,7 +112,7 @@
{
for (NSInteger i = 10; i < 500; i += 50) {
CGSize constrainedSize = CGSizeMake(i, i);
CGSize calculatedSize = [_textNode measure:constrainedSize];
CGSize calculatedSize = [_textNode layoutThatFits:ASSizeRangeMake(CGSizeZero, constrainedSize)].size;
XCTAssertTrue(calculatedSize.width <= constrainedSize.width, @"Calculated width (%f) should be less than or equal to constrained width (%f)", calculatedSize.width, constrainedSize.width);
XCTAssertTrue(calculatedSize.height <= constrainedSize.height, @"Calculated height (%f) should be less than or equal to constrained height (%f)", calculatedSize.height, constrainedSize.height);
}
@@ -121,8 +122,8 @@
{
for (NSInteger i = 10; i < 500; i += 50) {
CGSize constrainedSize = CGSizeMake(i, i);
CGSize calculatedSize = [_textNode measure:constrainedSize];
CGSize recalculatedSize = [_textNode measure:constrainedSize];
CGSize calculatedSize = [_textNode layoutThatFits:ASSizeRangeMake(CGSizeZero, constrainedSize)].size;
CGSize recalculatedSize = [_textNode layoutThatFits:ASSizeRangeMake(CGSizeZero, constrainedSize)].size;
XCTAssertTrue(CGSizeEqualToSizeWithIn(calculatedSize, recalculatedSize, 4.0), @"Recalculated size %@ should be same as original size %@", NSStringFromCGSize(recalculatedSize), NSStringFromCGSize(calculatedSize));
}
@@ -132,8 +133,8 @@
{
for (CGFloat i = 10; i < 500; i *= 1.3) {
CGSize constrainedSize = CGSizeMake(i, i);
CGSize calculatedSize = [_textNode measure:constrainedSize];
CGSize recalculatedSize = [_textNode measure:constrainedSize];
CGSize calculatedSize = [_textNode layoutThatFits:ASSizeRangeMake(CGSizeZero, constrainedSize)].size;
CGSize recalculatedSize = [_textNode layoutThatFits:ASSizeRangeMake(CGSizeZero, constrainedSize)].size;
XCTAssertTrue(CGSizeEqualToSizeWithIn(calculatedSize, recalculatedSize, 11.0), @"Recalculated size %@ should be same as original size %@", NSStringFromCGSize(recalculatedSize), NSStringFromCGSize(calculatedSize));
}
@@ -143,9 +144,9 @@
{
_textNode.placeholderEnabled = YES;
XCTAssertNoThrow([_textNode measure:CGSizeZero], @"Measure with zero size and placeholder enabled should not throw an exception");
XCTAssertNoThrow([_textNode measure:CGSizeMake(0, 100)], @"Measure with zero width and placeholder enabled should not throw an exception");
XCTAssertNoThrow([_textNode measure:CGSizeMake(100, 0)], @"Measure with zero height and placeholder enabled should not throw an exception");
XCTAssertNoThrow([_textNode layoutThatFits:ASSizeRangeMake(CGSizeZero, CGSizeZero)], @"Measure with zero size and placeholder enabled should not throw an exception");
XCTAssertNoThrow([_textNode layoutThatFits:ASSizeRangeMake(CGSizeZero, CGSizeMake(0, 100))], @"Measure with zero width and placeholder enabled should not throw an exception");
XCTAssertNoThrow([_textNode layoutThatFits:ASSizeRangeMake(CGSizeZero, CGSizeMake(100, 0))], @"Measure with zero height and placeholder enabled should not throw an exception");
}
- (void)testAccessibility
@@ -170,7 +171,7 @@
ASTextNodeTestDelegate *delegate = [ASTextNodeTestDelegate new];
_textNode.delegate = delegate;
[_textNode measure:CGSizeMake(100, 100)];
[_textNode layoutThatFits:ASSizeRangeMake(CGSizeZero, CGSizeMake(100, 100))];
NSRange returnedLinkRange;
NSString *returnedAttributeName;
NSString *returnedLinkAttributeValue = [_textNode linkAttributeValueAtPoint:CGPointMake(3, 3) attributeName:&returnedAttributeName range:&returnedLinkRange];
@@ -192,7 +193,7 @@
ASTextNodeTestDelegate *delegate = [ASTextNodeTestDelegate new];
_textNode.delegate = delegate;
CGSize calculatedSize = [_textNode measure:CGSizeMake(100, 100)];
CGSize calculatedSize = [_textNode layoutThatFits:ASSizeRangeMake(CGSizeZero, CGSizeMake(100, 100))].size;
NSRange returnedLinkRange = NSMakeRange(NSNotFound, 0);
NSRange expectedRange = NSMakeRange(NSNotFound, 0);
NSString *returnedAttributeName;
@@ -218,9 +219,9 @@
- (void)testAddingExclusionPathsShouldInvalidateAndIncreaseTheSize
{
CGSize constrainedSize = CGSizeMake(100, CGFLOAT_MAX);
CGSize sizeWithoutExclusionPaths = [_textNode measure:constrainedSize];
CGSize sizeWithoutExclusionPaths = [_textNode layoutThatFits:ASSizeRangeMake(CGSizeZero, constrainedSize)].size;
_textNode.exclusionPaths = @[[UIBezierPath bezierPathWithRect:CGRectMake(50, 20, 30, 40)]];
CGSize sizeWithExclusionPaths = [_textNode measure:constrainedSize];
CGSize sizeWithExclusionPaths = [_textNode layoutThatFits:ASSizeRangeMake(CGSizeZero, constrainedSize)].size;
XCTAssertGreaterThan(sizeWithExclusionPaths.height, sizeWithoutExclusionPaths.height, @"Setting exclusions paths should invalidate the calculated size and return a greater size");
}