|
25 | 25 |
|
26 | 26 | @interface RCTScrollViewComponentView (Tests) |
27 | 27 | - (void)_keyboardWillChangeFrame:(NSNotification *)notification; |
| 28 | +- (void)_prepareForMaintainVisibleScrollPosition; |
| 29 | +- (void)_adjustForMaintainVisibleContentPosition; |
28 | 30 | @end |
29 | 31 |
|
30 | 32 | @interface RCTScrollViewComponentViewTests : XCTestCase |
31 | 33 | @end |
32 | 34 |
|
33 | 35 | @implementation RCTScrollViewComponentViewTests |
34 | 36 |
|
| 37 | +- (void)testMaintainVisibleContentPositionAfterVerticalShrink |
| 38 | +{ |
| 39 | + RCTScrollViewComponentView *view = [[RCTScrollViewComponentView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; |
| 40 | + auto props = std::make_shared<ScrollViewProps>(); |
| 41 | + props->maintainVisibleContentPosition = facebook::react::ScrollViewMaintainVisibleContentPosition{}; |
| 42 | + [view updateProps:props oldProps:ScrollViewShadowNode::defaultSharedProps()]; |
| 43 | + |
| 44 | + RCTViewComponentView *contentView = [[RCTViewComponentView alloc] initWithFrame:CGRectMake(0, 0, 100, 1000)]; |
| 45 | + [view mountChildComponentView:contentView index:0]; |
| 46 | + UIView *anchor = [[UIView alloc] initWithFrame:CGRectMake(0, 800, 100, 40)]; |
| 47 | + anchor.tag = 42; |
| 48 | + [contentView addSubview:anchor]; |
| 49 | + |
| 50 | + view.scrollView.contentSize = CGSizeMake(100, 1000); |
| 51 | + view.scrollView.contentOffset = CGPointMake(0, 800); |
| 52 | + [view _prepareForMaintainVisibleScrollPosition]; |
| 53 | + |
| 54 | + anchor.frame = CGRectMake(0, 300, 100, 40); |
| 55 | + view.scrollView.contentSize = CGSizeMake(100, 400); |
| 56 | + view.scrollView.contentOffset = CGPointZero; |
| 57 | + [view _adjustForMaintainVisibleContentPosition]; |
| 58 | + |
| 59 | + XCTAssertEqualWithAccuracy(view.scrollView.contentOffset.y, 300, 0.5); |
| 60 | +} |
| 61 | + |
| 62 | +- (void)testMaintainVisibleContentPositionAfterHorizontalShrink |
| 63 | +{ |
| 64 | + RCTScrollViewComponentView *view = [[RCTScrollViewComponentView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; |
| 65 | + auto props = std::make_shared<ScrollViewProps>(); |
| 66 | + props->maintainVisibleContentPosition = facebook::react::ScrollViewMaintainVisibleContentPosition{}; |
| 67 | + [view updateProps:props oldProps:ScrollViewShadowNode::defaultSharedProps()]; |
| 68 | + |
| 69 | + RCTViewComponentView *contentView = [[RCTViewComponentView alloc] initWithFrame:CGRectMake(0, 0, 1000, 100)]; |
| 70 | + [view mountChildComponentView:contentView index:0]; |
| 71 | + UIView *anchor = [[UIView alloc] initWithFrame:CGRectMake(800, 0, 40, 100)]; |
| 72 | + anchor.tag = 42; |
| 73 | + [contentView addSubview:anchor]; |
| 74 | + |
| 75 | + view.scrollView.contentSize = CGSizeMake(1000, 100); |
| 76 | + view.scrollView.contentOffset = CGPointMake(800, 0); |
| 77 | + [view _prepareForMaintainVisibleScrollPosition]; |
| 78 | + |
| 79 | + anchor.frame = CGRectMake(300, 0, 40, 100); |
| 80 | + view.scrollView.contentSize = CGSizeMake(400, 100); |
| 81 | + view.scrollView.contentOffset = CGPointZero; |
| 82 | + [view _adjustForMaintainVisibleContentPosition]; |
| 83 | + |
| 84 | + XCTAssertEqualWithAccuracy(view.scrollView.contentOffset.x, 300, 0.5); |
| 85 | +} |
| 86 | + |
35 | 87 | - (void)testAutomaticallyAdjustKeyboardInsetsAcrossRecycling |
36 | 88 | { |
37 | 89 | RCTScrollViewComponentView *view = [[RCTScrollViewComponentView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; |
|
0 commit comments