|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +#import <React/RCTAssert.h> |
| 9 | +#import <React/RCTViewComponentView.h> |
| 10 | +#import <XCTest/XCTest.h> |
| 11 | +#import <react/renderer/components/view/ViewProps.h> |
| 12 | +#import <react/renderer/core/LayoutMetrics.h> |
| 13 | +#import <react/renderer/graphics/Color.h> |
| 14 | + |
| 15 | +using namespace facebook::react; |
| 16 | + |
| 17 | +// A view that clips and has a box shadow draws the shadow outside its bounds, |
| 18 | +// so RCTViewComponentView moves its children into a separate clipping |
| 19 | +// container view. |
| 20 | +static Props::Shared makeProps(bool clips, bool hasBoxShadow) |
| 21 | +{ |
| 22 | + auto props = std::make_shared<ViewProps>(); |
| 23 | + if (clips) { |
| 24 | + props->yogaStyle.setOverflow(facebook::yoga::Overflow::Hidden); |
| 25 | + } |
| 26 | + if (hasBoxShadow) { |
| 27 | + BoxShadow shadow; |
| 28 | + shadow.offsetY = 2; |
| 29 | + shadow.blurRadius = 6; |
| 30 | + shadow.color = colorFromRGBA(0, 0, 0, 102); |
| 31 | + props->boxShadow = {shadow}; |
| 32 | + } |
| 33 | + return props; |
| 34 | +} |
| 35 | + |
| 36 | +static void applyProps(RCTViewComponentView *view, const Props::Shared &props) |
| 37 | +{ |
| 38 | + [view updateProps:props oldProps:view.props]; |
| 39 | + LayoutMetrics layoutMetrics; |
| 40 | + layoutMetrics.frame.size = {100, 40}; |
| 41 | + [view updateLayoutMetrics:layoutMetrics oldLayoutMetrics:EmptyLayoutMetrics]; |
| 42 | + [view finalizeUpdates:RNComponentViewUpdateMaskAll]; |
| 43 | +} |
| 44 | + |
| 45 | +// Unmounts `child` and returns the message of any RCTAssert it raised, or nil. |
| 46 | +static NSString * |
| 47 | +unmountAndReturnAssertMessage(RCTViewComponentView *parent, UIView<RCTComponentViewProtocol> *child, NSInteger index) |
| 48 | +{ |
| 49 | + __block NSString *message = nil; |
| 50 | + RCTPerformBlockWithAssertFunction( |
| 51 | + ^{ |
| 52 | + @try { |
| 53 | + [parent unmountChildComponentView:child index:index]; |
| 54 | + } @catch (NSException *exception) { |
| 55 | + message = message ?: exception.reason; |
| 56 | + } |
| 57 | + }, |
| 58 | + ^(NSString *condition, NSString *fileName, NSNumber *lineNumber, NSString *function, NSString *assertMessage) { |
| 59 | + message = assertMessage; |
| 60 | + }); |
| 61 | + return message; |
| 62 | +} |
| 63 | + |
| 64 | +@interface RCTViewComponentViewContainerTests : XCTestCase |
| 65 | +@end |
| 66 | + |
| 67 | +@implementation RCTViewComponentViewContainerTests |
| 68 | + |
| 69 | +// When a view stops clipping, its children move from the container back into |
| 70 | +// the view. The box shadow layer was already re-added by then, and |
| 71 | +// `insertSubview:atIndex:` counts sublayers, so children appended after it were |
| 72 | +// mounted one index too low from then on. |
| 73 | +- (void)testChildrenMovedOutOfContainerStayBelowSelfDrawnLayers |
| 74 | +{ |
| 75 | + RCTViewComponentView *parent = [RCTViewComponentView new]; |
| 76 | + applyProps(parent, makeProps(/* clips */ true, /* hasBoxShadow */ true)); |
| 77 | + |
| 78 | + RCTViewComponentView *first = [RCTViewComponentView new]; |
| 79 | + RCTViewComponentView *second = [RCTViewComponentView new]; |
| 80 | + [parent mountChildComponentView:first index:0]; |
| 81 | + [parent mountChildComponentView:second index:1]; |
| 82 | + XCTAssertNotEqual(first.superview, parent, @"children of a clipping view with a box shadow go into a container"); |
| 83 | + |
| 84 | + applyProps(parent, makeProps(/* clips */ false, /* hasBoxShadow */ true)); |
| 85 | + XCTAssertEqual(first.superview, parent); |
| 86 | + XCTAssertEqual(parent.layer.sublayers.firstObject, first.layer, @"children must precede the box shadow layer"); |
| 87 | + |
| 88 | + RCTViewComponentView *third = [RCTViewComponentView new]; |
| 89 | + [parent mountChildComponentView:third index:2]; |
| 90 | + NSArray *expected = @[ first, second, third ]; |
| 91 | + XCTAssertEqualObjects(parent.subviews, expected); |
| 92 | + |
| 93 | + XCTAssertNil(unmountAndReturnAssertMessage(parent, third, 2)); |
| 94 | +} |
| 95 | + |
| 96 | +// A recycled view must not keep the container of its previous owner. Fabric |
| 97 | +// mounts a view's children before it applies that view's props, so those |
| 98 | +// children used to go into the stale container and were later moved above the |
| 99 | +// next owner's box shadow layer. |
| 100 | +- (void)testRecycledViewDropsContainerBeforeNextOwnerMountsChildren |
| 101 | +{ |
| 102 | + RCTViewComponentView *view = [RCTViewComponentView new]; |
| 103 | + applyProps(view, makeProps(/* clips */ true, /* hasBoxShadow */ true)); |
| 104 | + RCTViewComponentView *previousChild = [RCTViewComponentView new]; |
| 105 | + [view mountChildComponentView:previousChild index:0]; |
| 106 | + UIView *previousContainer = previousChild.superview; |
| 107 | + XCTAssertNotEqual(previousContainer, view); |
| 108 | + [view unmountChildComponentView:previousChild index:0]; |
| 109 | + |
| 110 | + [view prepareForRecycle]; |
| 111 | + XCTAssertNil(previousContainer.superview, @"the container is removed on recycle"); |
| 112 | + XCTAssertEqual(view.subviews.count, 0u); |
| 113 | + |
| 114 | + // The next owner has a box shadow but doesn't clip. |
| 115 | + RCTViewComponentView *first = [RCTViewComponentView new]; |
| 116 | + [view mountChildComponentView:first index:0]; |
| 117 | + XCTAssertEqual(first.superview, view); |
| 118 | + applyProps(view, makeProps(/* clips */ false, /* hasBoxShadow */ true)); |
| 119 | + XCTAssertFalse(view.clipsToBounds); |
| 120 | + |
| 121 | + RCTViewComponentView *second = [RCTViewComponentView new]; |
| 122 | + [view mountChildComponentView:second index:1]; |
| 123 | + NSArray *expected = @[ first, second ]; |
| 124 | + XCTAssertEqualObjects(view.subviews, expected); |
| 125 | + |
| 126 | + XCTAssertNil(unmountAndReturnAssertMessage(view, second, 1)); |
| 127 | +} |
| 128 | + |
| 129 | +// `updateProps` compares against the props kept from the previous owner, so a |
| 130 | +// next owner that also clips gets no `overflow` update. The clipping that the |
| 131 | +// container held must move back to the view when the container is dropped. |
| 132 | +- (void)testRecycledViewKeepsClippingForNextOwnerThatClips |
| 133 | +{ |
| 134 | + RCTViewComponentView *view = [RCTViewComponentView new]; |
| 135 | + applyProps(view, makeProps(/* clips */ true, /* hasBoxShadow */ true)); |
| 136 | + [view prepareForRecycle]; |
| 137 | + |
| 138 | + RCTViewComponentView *child = [RCTViewComponentView new]; |
| 139 | + [view mountChildComponentView:child index:0]; |
| 140 | + applyProps(view, makeProps(/* clips */ true, /* hasBoxShadow */ false)); |
| 141 | + |
| 142 | + XCTAssertEqual(child.superview, view); |
| 143 | + XCTAssertTrue(view.clipsToBounds); |
| 144 | +} |
| 145 | + |
| 146 | +// Subclasses such as image and text views put their own content view into the |
| 147 | +// container. It has to come back with the children. |
| 148 | +- (void)testRecycledViewMovesContentViewOutOfContainer |
| 149 | +{ |
| 150 | + RCTViewComponentView *view = [RCTViewComponentView new]; |
| 151 | + applyProps(view, makeProps(/* clips */ true, /* hasBoxShadow */ true)); |
| 152 | + UIView *contentView = [UIView new]; |
| 153 | + view.contentView = contentView; |
| 154 | + XCTAssertNotEqual(contentView.superview, view); |
| 155 | + |
| 156 | + [view prepareForRecycle]; |
| 157 | + |
| 158 | + XCTAssertEqual(contentView.superview, view); |
| 159 | +} |
| 160 | + |
| 161 | +@end |
0 commit comments