|
| 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/RCTScrollViewComponentView.h> |
| 9 | +#import <XCTest/XCTest.h> |
| 10 | +#import <react/renderer/components/scrollview/ScrollViewProps.h> |
| 11 | +#import <react/renderer/components/scrollview/ScrollViewShadowNode.h> |
| 12 | + |
| 13 | +using namespace facebook::react; |
| 14 | + |
| 15 | +#if TARGET_OS_IOS |
| 16 | + |
| 17 | +static Props::Shared makeScrollViewProps(bool automaticallyAdjustKeyboardInsets) |
| 18 | +{ |
| 19 | + auto props = std::make_shared<ScrollViewProps>(); |
| 20 | + props->automaticallyAdjustKeyboardInsets = automaticallyAdjustKeyboardInsets; |
| 21 | + return props; |
| 22 | +} |
| 23 | + |
| 24 | +@interface RCTScrollViewComponentView (Tests) |
| 25 | +- (void)_keyboardWillChangeFrame:(NSNotification *)notification; |
| 26 | +@end |
| 27 | + |
| 28 | +@interface RCTScrollViewComponentViewTests : XCTestCase |
| 29 | +@end |
| 30 | + |
| 31 | +@implementation RCTScrollViewComponentViewTests |
| 32 | + |
| 33 | +- (void)testAutomaticallyAdjustKeyboardInsetsAcrossRecycling |
| 34 | +{ |
| 35 | + RCTScrollViewComponentView *view = [[RCTScrollViewComponentView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; |
| 36 | + auto props = makeScrollViewProps(true); |
| 37 | + [view updateProps:props oldProps:ScrollViewShadowNode::defaultSharedProps()]; |
| 38 | + |
| 39 | + NSNotification *notification = [NSNotification |
| 40 | + notificationWithName:UIKeyboardWillChangeFrameNotification |
| 41 | + object:nil |
| 42 | + userInfo:@{ |
| 43 | + UIKeyboardAnimationDurationUserInfoKey : @0, |
| 44 | + UIKeyboardAnimationCurveUserInfoKey : @(UIViewAnimationCurveLinear), |
| 45 | + UIKeyboardFrameBeginUserInfoKey : [NSValue valueWithCGRect:CGRectMake(0, 100, 100, 50)], |
| 46 | + UIKeyboardFrameEndUserInfoKey : [NSValue valueWithCGRect:CGRectMake(0, 50, 100, 50)], |
| 47 | + }]; |
| 48 | + |
| 49 | + [view _keyboardWillChangeFrame:notification]; |
| 50 | + XCTAssertEqual(view.scrollView.contentInset.bottom, 50); |
| 51 | + |
| 52 | + [view prepareForRecycle]; |
| 53 | + [view _keyboardWillChangeFrame:notification]; |
| 54 | + XCTAssertTrue(UIEdgeInsetsEqualToEdgeInsets(view.scrollView.contentInset, UIEdgeInsetsZero)); |
| 55 | + |
| 56 | + [view updateProps:props oldProps:nullptr]; |
| 57 | + [view _keyboardWillChangeFrame:notification]; |
| 58 | + XCTAssertEqual(view.scrollView.contentInset.bottom, 50); |
| 59 | +} |
| 60 | + |
| 61 | +@end |
| 62 | + |
| 63 | +#endif |
0 commit comments