|
| 1 | +// Copyright 2004-present Facebook. All Rights Reserved. |
| 2 | + |
| 3 | +#import <XCTest/XCTest.h> |
| 4 | + |
| 5 | +#import "RCTShadowView.h" |
| 6 | + |
| 7 | +@interface RCTShadowViewTests : XCTestCase |
| 8 | + |
| 9 | +@end |
| 10 | + |
| 11 | +@implementation RCTShadowViewTests |
| 12 | + |
| 13 | +// Just a basic sanity test to ensure css-layout is applied correctly in the context of our shadow view hierarchy. |
| 14 | +// |
| 15 | +// ==================================== |
| 16 | +// || header || |
| 17 | +// ==================================== |
| 18 | +// || || || || |
| 19 | +// || left || center || right || |
| 20 | +// || || || || |
| 21 | +// ==================================== |
| 22 | +// || footer || |
| 23 | +// ==================================== |
| 24 | +// |
| 25 | +- (void)testApplyingLayoutRecursivelyToShadowView |
| 26 | +{ |
| 27 | + RCTShadowView *leftView = [self _shadowViewWithStyle:^(css_style_t *style) { |
| 28 | + style->flex = 1; |
| 29 | + }]; |
| 30 | + |
| 31 | + RCTShadowView *centerView = [self _shadowViewWithStyle:^(css_style_t *style) { |
| 32 | + style->flex = 2; |
| 33 | + style->margin[0] = 10; |
| 34 | + style->margin[2] = 10; |
| 35 | + }]; |
| 36 | + |
| 37 | + RCTShadowView *rightView = [self _shadowViewWithStyle:^(css_style_t *style) { |
| 38 | + style->flex = 1; |
| 39 | + }]; |
| 40 | + |
| 41 | + RCTShadowView *mainView = [self _shadowViewWithStyle:^(css_style_t *style) { |
| 42 | + style->flex_direction = CSS_FLEX_DIRECTION_ROW; |
| 43 | + style->flex = 2; |
| 44 | + style->margin[1] = 10; |
| 45 | + style->margin[3] = 10; |
| 46 | + }]; |
| 47 | + |
| 48 | + [mainView insertReactSubview:leftView atIndex:0]; |
| 49 | + [mainView insertReactSubview:centerView atIndex:1]; |
| 50 | + [mainView insertReactSubview:rightView atIndex:2]; |
| 51 | + |
| 52 | + RCTShadowView *headerView = [self _shadowViewWithStyle:^(css_style_t *style) { |
| 53 | + style->flex = 1; |
| 54 | + }]; |
| 55 | + |
| 56 | + RCTShadowView *footerView = [self _shadowViewWithStyle:^(css_style_t *style) { |
| 57 | + style->flex = 1; |
| 58 | + }]; |
| 59 | + |
| 60 | + RCTShadowView *parentView = [self _shadowViewWithStyle:^(css_style_t *style) { |
| 61 | + style->flex_direction = CSS_FLEX_DIRECTION_COLUMN; |
| 62 | + style->padding[0] = 10; |
| 63 | + style->padding[1] = 10; |
| 64 | + style->padding[2] = 10; |
| 65 | + style->padding[3] = 10; |
| 66 | + style->dimensions[0] = 440; |
| 67 | + style->dimensions[1] = 440; |
| 68 | + }]; |
| 69 | + |
| 70 | + [parentView insertReactSubview:headerView atIndex:0]; |
| 71 | + [parentView insertReactSubview:mainView atIndex:1]; |
| 72 | + [parentView insertReactSubview:footerView atIndex:2]; |
| 73 | + |
| 74 | + [parentView collectRootUpdatedFrames:nil parentConstraint:CGSizeZero]; |
| 75 | + |
| 76 | + XCTAssertTrue(CGRectEqualToRect([parentView measureLayoutRelativeToAncestor:parentView], CGRectMake(0, 0, 440, 440))); |
| 77 | + XCTAssertTrue(UIEdgeInsetsEqualToEdgeInsets([parentView paddingAsInsets], UIEdgeInsetsMake(10, 10, 10, 10))); |
| 78 | + |
| 79 | + XCTAssertTrue(CGRectEqualToRect([headerView measureLayoutRelativeToAncestor:parentView], CGRectMake(10, 10, 420, 100))); |
| 80 | + XCTAssertTrue(CGRectEqualToRect([mainView measureLayoutRelativeToAncestor:parentView], CGRectMake(10, 120, 420, 200))); |
| 81 | + XCTAssertTrue(CGRectEqualToRect([footerView measureLayoutRelativeToAncestor:parentView], CGRectMake(10, 330, 420, 100))); |
| 82 | + |
| 83 | + XCTAssertTrue(CGRectEqualToRect([leftView measureLayoutRelativeToAncestor:parentView], CGRectMake(10, 120, 100, 200))); |
| 84 | + XCTAssertTrue(CGRectEqualToRect([centerView measureLayoutRelativeToAncestor:parentView], CGRectMake(120, 120, 200, 200))); |
| 85 | + XCTAssertTrue(CGRectEqualToRect([rightView measureLayoutRelativeToAncestor:parentView], CGRectMake(330, 120, 100, 200))); |
| 86 | +} |
| 87 | + |
| 88 | +- (RCTShadowView *)_shadowViewWithStyle:(void(^)(css_style_t *style))styleBlock |
| 89 | +{ |
| 90 | + RCTShadowView *shadowView = [[RCTShadowView alloc] init]; |
| 91 | + |
| 92 | + css_style_t style = shadowView.cssNode->style; |
| 93 | + styleBlock(&style); |
| 94 | + shadowView.cssNode->style = style; |
| 95 | + |
| 96 | + return shadowView; |
| 97 | +} |
| 98 | + |
| 99 | +@end |
0 commit comments