Swiftgram/AsyncDisplayKitTests/ASDisplayNodeLayoutTests.mm
Scott Goodson 8c01fccfca [ASDisplayNode] Convert assertion to log so developer can see layout issues onscreen.
In this case, the user hasn't specified enough about the layout of a node.  We default to
0, 0 - but ideally the case should not occur at all.  So it's important to help developers
detect these cases and fix them quickly.

An assertion causes developers several problems:
- They can't see important information unless an exception breakpoint is manually added.
- They can't see their layout onscreen and so visually understanding the problem is impossible.
- They can't proceed with testing to trigger other faults in the layout and are blocked fixing one at a time.

A log fixes every one of those problems:
- They can set a breakpoint on the log line very easily if desired.
- They can see their layout display and recognize the 0,0 node even more quickly than the log text information.
- They can see if multiple logs print out, or if the log only occurs for one item in a feed, or certain types --
    This helps them debug faster by knowing if the layout is always broken or certain conditions break it.

Since developing with ASLayoutSpecs is an iterative process, it's crucial that we let developers have the
freedom to experiment and test without hitting too many assertions.  Fortunately it will be impossible to ignore
these huge logs (full recursive description) and their nodes will be 0, 0 size, so they will get fixed.
2016-11-16 21:14:15 -08:00

125 lines
5.0 KiB
Plaintext

//
// ASDisplayNodeLayoutTests.m
// AsyncDisplayKit
//
// 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 root directory of this source tree. An additional grant
// of patent rights can be found in the PATENTS file in the same directory.
//
#import "ASXCTExtensions.h"
#import <AsyncDisplayKit/AsyncDisplayKit.h>
#import "ASLayoutSpecSnapshotTestsHelper.h"
#import "ASDisplayNode+FrameworkPrivate.h"
@interface ASDisplayNodeLayoutTests : XCTestCase
@end
@implementation ASDisplayNodeLayoutTests
- (void)testMeasureOnLayoutIfNotHappenedBefore
{
CGSize nodeSize = CGSizeMake(100, 100);
ASDisplayNode *displayNode = [[ASDisplayNode alloc] init];
displayNode.style.width = ASDimensionMake(100);
displayNode.style.height = ASDimensionMake(100);
// Use a button node in here as ASButtonNode uses layoutSpecThatFits:
ASButtonNode *buttonNode = [ASButtonNode new];
[displayNode addSubnode:buttonNode];
displayNode.frame = {.size = nodeSize};
buttonNode.frame = {.size = nodeSize};
ASXCTAssertEqualSizes(displayNode.calculatedSize, CGSizeZero, @"Calculated size before measurement and layout should be 0");
ASXCTAssertEqualSizes(buttonNode.calculatedSize, CGSizeZero, @"Calculated size before measurement and layout should be 0");
// Trigger view creation and layout pass without a manual measure: call before so the automatic measurement
// pass will trigger in the layout pass
[displayNode.view layoutIfNeeded];
ASXCTAssertEqualSizes(displayNode.calculatedSize, nodeSize, @"Automatic measurement pass should have happened in layout pass");
ASXCTAssertEqualSizes(buttonNode.calculatedSize, nodeSize, @"Automatic measurement pass should have happened in layout pass");
}
#if DEBUG
- (void)testNotAllowAddingSubnodesInLayoutSpecThatFits
{
ASDisplayNode *displayNode = [ASDisplayNode new];
ASDisplayNode *someOtherNode = [ASDisplayNode new];
displayNode.layoutSpecBlock = ^(ASDisplayNode * _Nonnull node, ASSizeRange constrainedSize) {
[node addSubnode:someOtherNode];
return [ASInsetLayoutSpec insetLayoutSpecWithInsets:UIEdgeInsetsZero child:someOtherNode];
};
XCTAssertThrows([displayNode layoutThatFits:ASSizeRangeMake(CGSizeZero, CGSizeMake(100, 100))], @"Should throw if subnode was added in layoutSpecThatFits:");
}
- (void)testNotAllowModifyingSubnodesInLayoutSpecThatFits
{
ASDisplayNode *displayNode = [ASDisplayNode new];
ASDisplayNode *someOtherNode = [ASDisplayNode new];
[displayNode addSubnode:someOtherNode];
displayNode.layoutSpecBlock = ^(ASDisplayNode * _Nonnull node, ASSizeRange constrainedSize) {
[someOtherNode removeFromSupernode];
[node addSubnode:[ASDisplayNode new]];
return [ASInsetLayoutSpec insetLayoutSpecWithInsets:UIEdgeInsetsZero child:someOtherNode];
};
XCTAssertThrows([displayNode layoutThatFits:ASSizeRangeMake(CGSizeZero, CGSizeMake(100, 100))], @"Should throw if subnodes where modified in layoutSpecThatFits:");
}
#endif
- (void)testMeasureOnLayoutIfNotHappenedBeforeNoRemeasureForSameBounds
{
CGSize nodeSize = CGSizeMake(100, 100);
ASDisplayNode *displayNode = [ASDisplayNode new];
displayNode.style.width = ASDimensionMake(nodeSize.width);
displayNode.style.height = ASDimensionMake(nodeSize.height);
ASButtonNode *buttonNode = [ASButtonNode new];
[displayNode addSubnode:buttonNode];
__block size_t numberOfLayoutSpecThatFitsCalls = 0;
displayNode.layoutSpecBlock = ^(ASDisplayNode * _Nonnull node, ASSizeRange constrainedSize) {
__sync_fetch_and_add(&numberOfLayoutSpecThatFitsCalls, 1);
return [ASInsetLayoutSpec insetLayoutSpecWithInsets:UIEdgeInsetsZero child:buttonNode];
};
displayNode.frame = {.size = nodeSize};
// Trigger initial layout pass without a measurement pass before
[displayNode.view layoutIfNeeded];
XCTAssertEqual(numberOfLayoutSpecThatFitsCalls, 1, @"Should measure during layout if not measured");
[displayNode layoutThatFits:ASSizeRangeMake(nodeSize, nodeSize)];
XCTAssertEqual(numberOfLayoutSpecThatFitsCalls, 1, @"Should not remeasure with same bounds");
}
- (void)testThatLayoutWithInvalidSizeCausesException
{
ASDisplayNode *displayNode = [[ASDisplayNode alloc] init];
ASDisplayNode *node = [[ASDisplayNode alloc] init];
node.layoutSpecBlock = ^ASLayoutSpec *(ASDisplayNode *node, ASSizeRange constrainedSize) {
return [ASWrapperLayoutSpec wrapperWithLayoutElement:displayNode];
};
XCTAssertThrows([node layoutThatFits:ASSizeRangeMake(CGSizeMake(0, FLT_MAX))]);
}
- (void)testThatLayoutCreatedWithInvalidSizeCausesException
{
ASDisplayNode *displayNode = [[ASDisplayNode alloc] init];
XCTAssertThrows([ASLayout layoutWithLayoutElement:displayNode size:CGSizeMake(FLT_MAX, FLT_MAX)]);
XCTAssertThrows([ASLayout layoutWithLayoutElement:displayNode size:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX)]);
XCTAssertThrows([ASLayout layoutWithLayoutElement:displayNode size:CGSizeMake(INFINITY, INFINITY)]);
}
@end