2015-07-13 15:42:32 +00:00
|
|
|
/**
|
2017-05-06 03:50:47 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
2015-07-13 15:42:32 +00:00
|
|
|
*
|
2017-05-06 03:50:47 +00:00
|
|
|
* 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.
|
2015-07-13 15:42:32 +00:00
|
|
|
*
|
|
|
|
*/
|
2015-06-24 08:19:26 +00:00
|
|
|
|
|
|
|
#import <XCTest/XCTest.h>
|
|
|
|
|
2016-11-24 17:44:51 +00:00
|
|
|
#import <React/RCTRootShadowView.h>
|
2017-05-08 06:54:51 +00:00
|
|
|
#import <React/RCTShadowView+Layout.h>
|
2016-11-24 17:44:51 +00:00
|
|
|
#import <React/RCTShadowView.h>
|
2016-03-21 10:20:49 +00:00
|
|
|
|
2015-06-24 08:19:26 +00:00
|
|
|
|
|
|
|
@interface RCTShadowViewTests : XCTestCase
|
2016-03-21 10:20:49 +00:00
|
|
|
@property (nonatomic, strong) RCTRootShadowView *parentView;
|
2015-06-24 08:19:26 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation RCTShadowViewTests
|
|
|
|
|
2016-03-01 18:13:22 +00:00
|
|
|
- (void)setUp
|
|
|
|
{
|
|
|
|
[super setUp];
|
2016-03-21 10:20:49 +00:00
|
|
|
|
2017-02-01 00:47:11 +00:00
|
|
|
self.parentView = [RCTRootShadowView new];
|
2017-02-21 16:43:34 +00:00
|
|
|
YGNodeStyleSetFlexDirection(self.parentView.yogaNode, YGFlexDirectionColumn);
|
|
|
|
YGNodeStyleSetWidth(self.parentView.yogaNode, 440);
|
|
|
|
YGNodeStyleSetHeight(self.parentView.yogaNode, 440);
|
2016-03-01 18:13:22 +00:00
|
|
|
self.parentView.reactTag = @1; // must be valid rootView tag
|
|
|
|
}
|
|
|
|
|
2015-06-24 08:19:26 +00:00
|
|
|
// Just a basic sanity test to ensure css-layout is applied correctly in the context of our shadow view hierarchy.
|
|
|
|
//
|
|
|
|
// ====================================
|
|
|
|
// || header ||
|
|
|
|
// ====================================
|
|
|
|
// || || || ||
|
|
|
|
// || left || center || right ||
|
|
|
|
// || || || ||
|
|
|
|
// ====================================
|
|
|
|
// || footer ||
|
|
|
|
// ====================================
|
|
|
|
//
|
|
|
|
- (void)testApplyingLayoutRecursivelyToShadowView
|
|
|
|
{
|
2016-12-03 12:40:18 +00:00
|
|
|
RCTShadowView *leftView = [self _shadowViewWithConfig:^(YGNodeRef node) {
|
|
|
|
YGNodeStyleSetFlex(node, 1);
|
2015-06-24 08:19:26 +00:00
|
|
|
}];
|
|
|
|
|
2016-12-03 12:40:18 +00:00
|
|
|
RCTShadowView *centerView = [self _shadowViewWithConfig:^(YGNodeRef node) {
|
|
|
|
YGNodeStyleSetFlex(node, 2);
|
|
|
|
YGNodeStyleSetMargin(node, YGEdgeLeft, 10);
|
|
|
|
YGNodeStyleSetMargin(node, YGEdgeRight, 10);
|
2015-06-24 08:19:26 +00:00
|
|
|
}];
|
|
|
|
|
2016-12-03 12:40:18 +00:00
|
|
|
RCTShadowView *rightView = [self _shadowViewWithConfig:^(YGNodeRef node) {
|
|
|
|
YGNodeStyleSetFlex(node, 1);
|
2015-06-24 08:19:26 +00:00
|
|
|
}];
|
|
|
|
|
2016-12-03 12:40:18 +00:00
|
|
|
RCTShadowView *mainView = [self _shadowViewWithConfig:^(YGNodeRef node) {
|
|
|
|
YGNodeStyleSetFlexDirection(node, YGFlexDirectionRow);
|
|
|
|
YGNodeStyleSetFlex(node, 2);
|
|
|
|
YGNodeStyleSetMargin(node, YGEdgeTop, 10);
|
|
|
|
YGNodeStyleSetMargin(node, YGEdgeBottom, 10);
|
2015-06-24 08:19:26 +00:00
|
|
|
}];
|
|
|
|
|
|
|
|
[mainView insertReactSubview:leftView atIndex:0];
|
|
|
|
[mainView insertReactSubview:centerView atIndex:1];
|
|
|
|
[mainView insertReactSubview:rightView atIndex:2];
|
|
|
|
|
2016-12-03 12:40:18 +00:00
|
|
|
RCTShadowView *headerView = [self _shadowViewWithConfig:^(YGNodeRef node) {
|
|
|
|
YGNodeStyleSetFlex(node, 1);
|
2015-06-24 08:19:26 +00:00
|
|
|
}];
|
|
|
|
|
2016-12-03 12:40:18 +00:00
|
|
|
RCTShadowView *footerView = [self _shadowViewWithConfig:^(YGNodeRef node) {
|
|
|
|
YGNodeStyleSetFlex(node, 1);
|
2015-06-24 08:19:26 +00:00
|
|
|
}];
|
|
|
|
|
2017-02-21 16:43:34 +00:00
|
|
|
YGNodeStyleSetPadding(self.parentView.yogaNode, YGEdgeLeft, 10);
|
|
|
|
YGNodeStyleSetPadding(self.parentView.yogaNode, YGEdgeTop, 10);
|
|
|
|
YGNodeStyleSetPadding(self.parentView.yogaNode, YGEdgeRight, 10);
|
|
|
|
YGNodeStyleSetPadding(self.parentView.yogaNode, YGEdgeBottom, 10);
|
2016-03-01 18:13:22 +00:00
|
|
|
|
|
|
|
[self.parentView insertReactSubview:headerView atIndex:0];
|
|
|
|
[self.parentView insertReactSubview:mainView atIndex:1];
|
|
|
|
[self.parentView insertReactSubview:footerView atIndex:2];
|
2015-06-24 08:19:26 +00:00
|
|
|
|
2018-02-12 08:04:16 +00:00
|
|
|
[self.parentView layoutWithAffectedShadowViews:[NSHashTable weakObjectsHashTable]];
|
2015-06-24 08:19:26 +00:00
|
|
|
|
2016-03-01 18:13:22 +00:00
|
|
|
XCTAssertTrue(CGRectEqualToRect([self.parentView measureLayoutRelativeToAncestor:self.parentView], CGRectMake(0, 0, 440, 440)));
|
|
|
|
XCTAssertTrue(UIEdgeInsetsEqualToEdgeInsets([self.parentView paddingAsInsets], UIEdgeInsetsMake(10, 10, 10, 10)));
|
2015-06-24 08:19:26 +00:00
|
|
|
|
2016-03-01 18:13:22 +00:00
|
|
|
XCTAssertTrue(CGRectEqualToRect([headerView measureLayoutRelativeToAncestor:self.parentView], CGRectMake(10, 10, 420, 100)));
|
|
|
|
XCTAssertTrue(CGRectEqualToRect([mainView measureLayoutRelativeToAncestor:self.parentView], CGRectMake(10, 120, 420, 200)));
|
|
|
|
XCTAssertTrue(CGRectEqualToRect([footerView measureLayoutRelativeToAncestor:self.parentView], CGRectMake(10, 330, 420, 100)));
|
2015-06-24 08:19:26 +00:00
|
|
|
|
2016-03-01 18:13:22 +00:00
|
|
|
XCTAssertTrue(CGRectEqualToRect([leftView measureLayoutRelativeToAncestor:self.parentView], CGRectMake(10, 120, 100, 200)));
|
|
|
|
XCTAssertTrue(CGRectEqualToRect([centerView measureLayoutRelativeToAncestor:self.parentView], CGRectMake(120, 120, 200, 200)));
|
|
|
|
XCTAssertTrue(CGRectEqualToRect([rightView measureLayoutRelativeToAncestor:self.parentView], CGRectMake(330, 120, 100, 200)));
|
|
|
|
}
|
2015-06-24 08:19:26 +00:00
|
|
|
|
2016-08-03 11:01:45 +00:00
|
|
|
- (void)testAncestorCheck
|
|
|
|
{
|
2016-12-03 12:40:18 +00:00
|
|
|
RCTShadowView *centerView = [self _shadowViewWithConfig:^(YGNodeRef node) {
|
|
|
|
YGNodeStyleSetFlex(node, 1);
|
2016-08-03 11:01:45 +00:00
|
|
|
}];
|
2016-08-15 16:15:10 +00:00
|
|
|
|
2016-12-03 12:40:18 +00:00
|
|
|
RCTShadowView *mainView = [self _shadowViewWithConfig:^(YGNodeRef node) {
|
|
|
|
YGNodeStyleSetFlex(node, 1);
|
2016-08-03 11:01:45 +00:00
|
|
|
}];
|
2016-08-15 16:15:10 +00:00
|
|
|
|
2016-08-03 11:01:45 +00:00
|
|
|
[mainView insertReactSubview:centerView atIndex:0];
|
|
|
|
|
2016-12-03 12:40:18 +00:00
|
|
|
RCTShadowView *footerView = [self _shadowViewWithConfig:^(YGNodeRef node) {
|
|
|
|
YGNodeStyleSetFlex(node, 1);
|
2016-08-03 11:01:45 +00:00
|
|
|
}];
|
2016-08-15 16:15:10 +00:00
|
|
|
|
2016-08-03 11:01:45 +00:00
|
|
|
[self.parentView insertReactSubview:mainView atIndex:0];
|
|
|
|
[self.parentView insertReactSubview:footerView atIndex:1];
|
2016-08-15 16:15:10 +00:00
|
|
|
|
2016-08-03 11:01:45 +00:00
|
|
|
XCTAssertTrue([centerView viewIsDescendantOf:mainView]);
|
|
|
|
XCTAssertFalse([footerView viewIsDescendantOf:mainView]);
|
|
|
|
}
|
|
|
|
|
2016-03-01 18:13:22 +00:00
|
|
|
- (void)testAssignsSuggestedWidthDimension
|
|
|
|
{
|
2016-12-03 12:40:18 +00:00
|
|
|
[self _withShadowViewWithStyle:^(YGNodeRef node) {
|
2017-02-01 00:47:11 +00:00
|
|
|
YGNodeStyleSetPositionType(node, YGPositionTypeAbsolute);
|
2016-12-03 12:40:18 +00:00
|
|
|
YGNodeStyleSetPosition(node, YGEdgeLeft, 0);
|
|
|
|
YGNodeStyleSetPosition(node, YGEdgeTop, 0);
|
|
|
|
YGNodeStyleSetHeight(node, 10);
|
2016-03-01 18:13:22 +00:00
|
|
|
}
|
|
|
|
assertRelativeLayout:CGRectMake(0, 0, 3, 10)
|
|
|
|
withIntrinsicContentSize:CGSizeMake(3, UIViewNoIntrinsicMetric)];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)testAssignsSuggestedHeightDimension
|
|
|
|
{
|
2016-12-03 12:40:18 +00:00
|
|
|
[self _withShadowViewWithStyle:^(YGNodeRef node) {
|
2017-02-01 00:47:11 +00:00
|
|
|
YGNodeStyleSetPositionType(node, YGPositionTypeAbsolute);
|
2016-12-03 12:40:18 +00:00
|
|
|
YGNodeStyleSetPosition(node, YGEdgeLeft, 0);
|
|
|
|
YGNodeStyleSetPosition(node, YGEdgeTop, 0);
|
|
|
|
YGNodeStyleSetWidth(node, 10);
|
2016-03-01 18:13:22 +00:00
|
|
|
}
|
|
|
|
assertRelativeLayout:CGRectMake(0, 0, 10, 4)
|
|
|
|
withIntrinsicContentSize:CGSizeMake(UIViewNoIntrinsicMetric, 4)];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)testDoesNotOverrideDimensionStyleWithSuggestedDimensions
|
|
|
|
{
|
2016-12-03 12:40:18 +00:00
|
|
|
[self _withShadowViewWithStyle:^(YGNodeRef node) {
|
2017-02-01 00:47:11 +00:00
|
|
|
YGNodeStyleSetPositionType(node, YGPositionTypeAbsolute);
|
2016-12-03 12:40:18 +00:00
|
|
|
YGNodeStyleSetPosition(node, YGEdgeLeft, 0);
|
|
|
|
YGNodeStyleSetPosition(node, YGEdgeTop, 0);
|
|
|
|
YGNodeStyleSetWidth(node, 10);
|
|
|
|
YGNodeStyleSetHeight(node, 10);
|
2016-03-01 18:13:22 +00:00
|
|
|
}
|
|
|
|
assertRelativeLayout:CGRectMake(0, 0, 10, 10)
|
|
|
|
withIntrinsicContentSize:CGSizeMake(3, 4)];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)testDoesNotAssignSuggestedDimensionsWhenStyledWithFlexAttribute
|
|
|
|
{
|
2017-02-21 16:43:34 +00:00
|
|
|
float parentWidth = YGNodeStyleGetWidth(self.parentView.yogaNode).value;
|
|
|
|
float parentHeight = YGNodeStyleGetHeight(self.parentView.yogaNode).value;
|
2016-12-03 12:40:18 +00:00
|
|
|
[self _withShadowViewWithStyle:^(YGNodeRef node) {
|
|
|
|
YGNodeStyleSetFlex(node, 1);
|
2016-03-01 18:13:22 +00:00
|
|
|
}
|
|
|
|
assertRelativeLayout:CGRectMake(0, 0, parentWidth, parentHeight)
|
|
|
|
withIntrinsicContentSize:CGSizeMake(3, 4)];
|
|
|
|
}
|
|
|
|
|
2016-12-03 12:40:18 +00:00
|
|
|
- (void)_withShadowViewWithStyle:(void(^)(YGNodeRef node))configBlock
|
2016-03-01 18:13:22 +00:00
|
|
|
assertRelativeLayout:(CGRect)expectedRect
|
|
|
|
withIntrinsicContentSize:(CGSize)contentSize
|
|
|
|
{
|
2016-07-20 15:46:00 +00:00
|
|
|
RCTShadowView *view = [self _shadowViewWithConfig:configBlock];
|
2016-03-01 18:13:22 +00:00
|
|
|
[self.parentView insertReactSubview:view atIndex:0];
|
|
|
|
view.intrinsicContentSize = contentSize;
|
2018-02-12 08:04:16 +00:00
|
|
|
[self.parentView layoutWithAffectedShadowViews:[NSHashTable weakObjectsHashTable]];
|
2016-03-01 18:13:22 +00:00
|
|
|
CGRect actualRect = [view measureLayoutRelativeToAncestor:self.parentView];
|
|
|
|
XCTAssertTrue(CGRectEqualToRect(expectedRect, actualRect),
|
|
|
|
@"Expected layout to be %@, got %@",
|
|
|
|
NSStringFromCGRect(expectedRect),
|
|
|
|
NSStringFromCGRect(actualRect));
|
2015-06-24 08:19:26 +00:00
|
|
|
}
|
|
|
|
|
2017-02-01 00:47:11 +00:00
|
|
|
- (RCTShadowView *)_shadowViewWithConfig:(void(^)(YGNodeRef node))configBlock
|
2015-06-24 08:19:26 +00:00
|
|
|
{
|
2017-02-01 00:47:11 +00:00
|
|
|
RCTShadowView *shadowView = [RCTShadowView new];
|
2017-02-21 16:43:34 +00:00
|
|
|
configBlock(shadowView.yogaNode);
|
2015-06-24 08:19:26 +00:00
|
|
|
return shadowView;
|
|
|
|
}
|
|
|
|
|
2017-02-01 00:47:11 +00:00
|
|
|
|
2015-06-24 08:19:26 +00:00
|
|
|
@end
|