// // 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 #import "ASLayoutSpecSnapshotTestsHelper.h" #import "ASDisplayNode+FrameworkPrivate.h" @interface ASDisplayNodeLayoutTests : XCTestCase @end @implementation ASDisplayNodeLayoutTests - (void)testMeasurePassOnLayoutIfNotHappenedBefore { ASStaticSizeDisplayNode *displayNode = [ASStaticSizeDisplayNode new]; displayNode.staticSize = CGSizeMake(100, 100); displayNode.frame = CGRectMake(0, 0, 100, 100); ASXCTAssertEqualSizes(displayNode.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, CGSizeMake(100, 100), @"Automatic measurement pass should be happened in layout"); } #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 measure: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 measure:CGSizeMake(100, 100)], @"Should throw if subnodes where modified in layoutSpecThatFits:"); } #endif @end