|
17 | 17 | #import "RCTShadowView.h"
|
18 | 18 |
|
19 | 19 | @interface RCTShadowViewTests : XCTestCase
|
20 |
| - |
| 20 | +@property (nonatomic, strong) RCTShadowView *parentView; |
21 | 21 | @end
|
22 | 22 |
|
23 | 23 | @implementation RCTShadowViewTests
|
24 | 24 |
|
| 25 | +- (void)setUp |
| 26 | +{ |
| 27 | + [super setUp]; |
| 28 | + |
| 29 | + self.parentView = [self _shadowViewWithStyle:^(css_style_t *style) { |
| 30 | + style->flex_direction = CSS_FLEX_DIRECTION_COLUMN; |
| 31 | + style->dimensions[0] = 440; |
| 32 | + style->dimensions[1] = 440; |
| 33 | + }]; |
| 34 | + self.parentView.reactTag = @1; // must be valid rootView tag |
| 35 | +} |
| 36 | + |
25 | 37 | // Just a basic sanity test to ensure css-layout is applied correctly in the context of our shadow view hierarchy.
|
26 | 38 | //
|
27 | 39 | // ====================================
|
@@ -69,33 +81,87 @@ - (void)testApplyingLayoutRecursivelyToShadowView
|
69 | 81 | style->flex = 1;
|
70 | 82 | }];
|
71 | 83 |
|
72 |
| - RCTShadowView *parentView = [self _shadowViewWithStyle:^(css_style_t *style) { |
73 |
| - style->flex_direction = CSS_FLEX_DIRECTION_COLUMN; |
74 |
| - style->padding[0] = 10; |
75 |
| - style->padding[1] = 10; |
76 |
| - style->padding[2] = 10; |
77 |
| - style->padding[3] = 10; |
78 |
| - style->dimensions[0] = 440; |
79 |
| - style->dimensions[1] = 440; |
80 |
| - }]; |
| 84 | + self.parentView.cssNode->style.padding[0] = 10; |
| 85 | + self.parentView.cssNode->style.padding[1] = 10; |
| 86 | + self.parentView.cssNode->style.padding[2] = 10; |
| 87 | + self.parentView.cssNode->style.padding[3] = 10; |
| 88 | + |
| 89 | + [self.parentView insertReactSubview:headerView atIndex:0]; |
| 90 | + [self.parentView insertReactSubview:mainView atIndex:1]; |
| 91 | + [self.parentView insertReactSubview:footerView atIndex:2]; |
81 | 92 |
|
82 |
| - [parentView insertReactSubview:headerView atIndex:0]; |
83 |
| - [parentView insertReactSubview:mainView atIndex:1]; |
84 |
| - [parentView insertReactSubview:footerView atIndex:2]; |
| 93 | + [self.parentView collectRootUpdatedFrames]; |
85 | 94 |
|
86 |
| - parentView.reactTag = @1; // must be valid rootView tag |
87 |
| - [parentView collectRootUpdatedFrames]; |
| 95 | + XCTAssertTrue(CGRectEqualToRect([self.parentView measureLayoutRelativeToAncestor:self.parentView], CGRectMake(0, 0, 440, 440))); |
| 96 | + XCTAssertTrue(UIEdgeInsetsEqualToEdgeInsets([self.parentView paddingAsInsets], UIEdgeInsetsMake(10, 10, 10, 10))); |
88 | 97 |
|
89 |
| - XCTAssertTrue(CGRectEqualToRect([parentView measureLayoutRelativeToAncestor:parentView], CGRectMake(0, 0, 440, 440))); |
90 |
| - XCTAssertTrue(UIEdgeInsetsEqualToEdgeInsets([parentView paddingAsInsets], UIEdgeInsetsMake(10, 10, 10, 10))); |
| 98 | + XCTAssertTrue(CGRectEqualToRect([headerView measureLayoutRelativeToAncestor:self.parentView], CGRectMake(10, 10, 420, 100))); |
| 99 | + XCTAssertTrue(CGRectEqualToRect([mainView measureLayoutRelativeToAncestor:self.parentView], CGRectMake(10, 120, 420, 200))); |
| 100 | + XCTAssertTrue(CGRectEqualToRect([footerView measureLayoutRelativeToAncestor:self.parentView], CGRectMake(10, 330, 420, 100))); |
91 | 101 |
|
92 |
| - XCTAssertTrue(CGRectEqualToRect([headerView measureLayoutRelativeToAncestor:parentView], CGRectMake(10, 10, 420, 100))); |
93 |
| - XCTAssertTrue(CGRectEqualToRect([mainView measureLayoutRelativeToAncestor:parentView], CGRectMake(10, 120, 420, 200))); |
94 |
| - XCTAssertTrue(CGRectEqualToRect([footerView measureLayoutRelativeToAncestor:parentView], CGRectMake(10, 330, 420, 100))); |
| 102 | + XCTAssertTrue(CGRectEqualToRect([leftView measureLayoutRelativeToAncestor:self.parentView], CGRectMake(10, 120, 100, 200))); |
| 103 | + XCTAssertTrue(CGRectEqualToRect([centerView measureLayoutRelativeToAncestor:self.parentView], CGRectMake(120, 120, 200, 200))); |
| 104 | + XCTAssertTrue(CGRectEqualToRect([rightView measureLayoutRelativeToAncestor:self.parentView], CGRectMake(330, 120, 100, 200))); |
| 105 | +} |
95 | 106 |
|
96 |
| - XCTAssertTrue(CGRectEqualToRect([leftView measureLayoutRelativeToAncestor:parentView], CGRectMake(10, 120, 100, 200))); |
97 |
| - XCTAssertTrue(CGRectEqualToRect([centerView measureLayoutRelativeToAncestor:parentView], CGRectMake(120, 120, 200, 200))); |
98 |
| - XCTAssertTrue(CGRectEqualToRect([rightView measureLayoutRelativeToAncestor:parentView], CGRectMake(330, 120, 100, 200))); |
| 107 | +- (void)testAssignsSuggestedWidthDimension |
| 108 | +{ |
| 109 | + [self _withShadowViewWithStyle:^(css_style_t *style) { |
| 110 | + style->position[CSS_LEFT] = 0; |
| 111 | + style->position[CSS_TOP] = 0; |
| 112 | + style->dimensions[CSS_HEIGHT] = 10; |
| 113 | + } |
| 114 | + assertRelativeLayout:CGRectMake(0, 0, 3, 10) |
| 115 | + withIntrinsicContentSize:CGSizeMake(3, UIViewNoIntrinsicMetric)]; |
| 116 | +} |
| 117 | + |
| 118 | +- (void)testAssignsSuggestedHeightDimension |
| 119 | +{ |
| 120 | + [self _withShadowViewWithStyle:^(css_style_t *style) { |
| 121 | + style->position[CSS_LEFT] = 0; |
| 122 | + style->position[CSS_TOP] = 0; |
| 123 | + style->dimensions[CSS_WIDTH] = 10; |
| 124 | + } |
| 125 | + assertRelativeLayout:CGRectMake(0, 0, 10, 4) |
| 126 | + withIntrinsicContentSize:CGSizeMake(UIViewNoIntrinsicMetric, 4)]; |
| 127 | +} |
| 128 | + |
| 129 | +- (void)testDoesNotOverrideDimensionStyleWithSuggestedDimensions |
| 130 | +{ |
| 131 | + [self _withShadowViewWithStyle:^(css_style_t *style) { |
| 132 | + style->position[CSS_LEFT] = 0; |
| 133 | + style->position[CSS_TOP] = 0; |
| 134 | + style->dimensions[CSS_WIDTH] = 10; |
| 135 | + style->dimensions[CSS_HEIGHT] = 10; |
| 136 | + } |
| 137 | + assertRelativeLayout:CGRectMake(0, 0, 10, 10) |
| 138 | + withIntrinsicContentSize:CGSizeMake(3, 4)]; |
| 139 | +} |
| 140 | + |
| 141 | +- (void)testDoesNotAssignSuggestedDimensionsWhenStyledWithFlexAttribute |
| 142 | +{ |
| 143 | + float parentWidth = self.parentView.cssNode->style.dimensions[CSS_WIDTH]; |
| 144 | + float parentHeight = self.parentView.cssNode->style.dimensions[CSS_HEIGHT]; |
| 145 | + [self _withShadowViewWithStyle:^(css_style_t *style) { |
| 146 | + style->flex = 1; |
| 147 | + } |
| 148 | + assertRelativeLayout:CGRectMake(0, 0, parentWidth, parentHeight) |
| 149 | + withIntrinsicContentSize:CGSizeMake(3, 4)]; |
| 150 | +} |
| 151 | + |
| 152 | +- (void)_withShadowViewWithStyle:(void(^)(css_style_t *style))styleBlock |
| 153 | + assertRelativeLayout:(CGRect)expectedRect |
| 154 | + withIntrinsicContentSize:(CGSize)contentSize |
| 155 | +{ |
| 156 | + RCTShadowView *view = [self _shadowViewWithStyle:styleBlock]; |
| 157 | + [self.parentView insertReactSubview:view atIndex:0]; |
| 158 | + view.intrinsicContentSize = contentSize; |
| 159 | + [self.parentView collectRootUpdatedFrames]; |
| 160 | + CGRect actualRect = [view measureLayoutRelativeToAncestor:self.parentView]; |
| 161 | + XCTAssertTrue(CGRectEqualToRect(expectedRect, actualRect), |
| 162 | + @"Expected layout to be %@, got %@", |
| 163 | + NSStringFromCGRect(expectedRect), |
| 164 | + NSStringFromCGRect(actualRect)); |
99 | 165 | }
|
100 | 166 |
|
101 | 167 | - (RCTShadowView *)_shadowViewWithStyle:(void(^)(css_style_t *style))styleBlock
|
|
0 commit comments