|
6 | 6 | */ |
7 | 7 |
|
8 | 8 | #import <React/RCTEnhancedScrollView.h> |
| 9 | +#import <React/RCTScrollContentViewComponentView.h> |
9 | 10 | #import <React/RCTScrollViewComponentView.h> |
10 | 11 | #import <XCTest/XCTest.h> |
| 12 | +#import <react/featureflags/ReactNativeFeatureFlags.h> |
| 13 | +#import <react/featureflags/ReactNativeFeatureFlagsDefaults.h> |
| 14 | +#import <react/renderer/components/scrollview/ScrollContentViewComponentDescriptor.h> |
| 15 | +#import <react/renderer/components/scrollview/ScrollContentViewState.h> |
11 | 16 | #import <react/renderer/components/scrollview/ScrollViewProps.h> |
12 | 17 | #import <react/renderer/components/scrollview/ScrollViewShadowNode.h> |
| 18 | +#import <react/renderer/core/EventBeat.h> |
| 19 | +#import <react/renderer/core/EventDispatcher.h> |
| 20 | +#import <react/renderer/core/EventQueueProcessor.h> |
| 21 | +#import <react/renderer/core/StateUpdate.h> |
| 22 | +#import <react/renderer/runtimescheduler/RuntimeScheduler.h> |
| 23 | +#import <react/utils/ContextContainer.h> |
| 24 | + |
| 25 | +#import <functional> |
| 26 | +#import <memory> |
| 27 | +#import <utility> |
13 | 28 |
|
14 | 29 | using facebook::react::Props; |
15 | 30 | using facebook::react::ScrollViewProps; |
|
24 | 39 | return props; |
25 | 40 | } |
26 | 41 |
|
| 42 | +namespace { |
| 43 | + |
| 44 | +using facebook::react::ComponentDescriptorParameters; |
| 45 | +using facebook::react::ContextContainer; |
| 46 | +using facebook::react::EventBeat; |
| 47 | +using facebook::react::EventDispatcher; |
| 48 | +using facebook::react::EventLogger; |
| 49 | +using facebook::react::EventQueue; |
| 50 | +using facebook::react::EventQueueProcessor; |
| 51 | +using facebook::react::ReactNativeFeatureFlags; |
| 52 | +using facebook::react::ReactNativeFeatureFlagsDefaults; |
| 53 | +using facebook::react::RuntimeScheduler; |
| 54 | +using facebook::react::ScrollContentViewComponentDescriptor; |
| 55 | +using facebook::react::ScrollContentViewShadowNode; |
| 56 | +using facebook::react::ScrollContentViewState; |
| 57 | +using facebook::react::ShadowNodeFamily; |
| 58 | +using facebook::react::State; |
| 59 | +using facebook::react::StateData; |
| 60 | +using facebook::react::StatePipe; |
| 61 | +using facebook::react::StateUpdate; |
| 62 | + |
| 63 | +class ImmediateScrollContentFeatureFlags : public ReactNativeFeatureFlagsDefaults { |
| 64 | + public: |
| 65 | + bool enableImmediateUpdateModeForScrollContentView() override |
| 66 | + { |
| 67 | + return true; |
| 68 | + } |
| 69 | +}; |
| 70 | + |
| 71 | +class ScopedScrollContentFeatureFlags { |
| 72 | + public: |
| 73 | + explicit ScopedScrollContentFeatureFlags(bool enabled) |
| 74 | + { |
| 75 | + ReactNativeFeatureFlags::dangerouslyReset(); |
| 76 | + if (enabled) { |
| 77 | + ReactNativeFeatureFlags::override(std::make_unique<ImmediateScrollContentFeatureFlags>()); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + ~ScopedScrollContentFeatureFlags() |
| 82 | + { |
| 83 | + ReactNativeFeatureFlags::dangerouslyReset(); |
| 84 | + } |
| 85 | +}; |
| 86 | + |
| 87 | +class ScrollContentStateDispatch { |
| 88 | + public: |
| 89 | + ScrollContentStateDispatch() : runtimeScheduler_([](std::function<void(facebook::jsi::Runtime &)> && /*callback*/) {}) |
| 90 | + { |
| 91 | + StatePipe statePipe = [this](const StateUpdate &update) { |
| 92 | + auto data = update.callback(initialData_); |
| 93 | + if (update.family == contentFamily_) { |
| 94 | + latestData = *std::static_pointer_cast<const ScrollContentViewState>(data); |
| 95 | + nativeStateChanges++; |
| 96 | + } else if (update.family == unrelatedFamily_) { |
| 97 | + latestUnrelatedData = *std::static_pointer_cast<const ScrollContentViewState>(data); |
| 98 | + unrelatedStateChanges++; |
| 99 | + } |
| 100 | + }; |
| 101 | + auto ownerBox = std::make_shared<EventBeat::OwnerBox>(); |
| 102 | + dispatcher_ = std::make_shared<EventDispatcher>( |
| 103 | + EventQueueProcessor({}, {}, statePipe, {}), |
| 104 | + std::make_unique<EventBeat>(ownerBox, runtimeScheduler_), |
| 105 | + statePipe, |
| 106 | + std::weak_ptr<EventLogger>{}); |
| 107 | + ownerBox->owner = dispatcher_; |
| 108 | + descriptor_ = std::make_unique<ScrollContentViewComponentDescriptor>(ComponentDescriptorParameters{ |
| 109 | + .eventDispatcher = dispatcher_, .contextContainer = std::make_shared<ContextContainer>(), .flavor = nullptr}); |
| 110 | + contentFamily_ = descriptor_->createFamily({.tag = 1, .surfaceId = 1, .instanceHandle = nullptr}); |
| 111 | + unrelatedFamily_ = descriptor_->createFamily({.tag = 2, .surfaceId = 1, .instanceHandle = nullptr}); |
| 112 | + flushFamily_ = descriptor_->createFamily({.tag = 3, .surfaceId = 1, .instanceHandle = nullptr}); |
| 113 | + state_ = descriptor_->createInitialState(ScrollContentViewShadowNode::defaultSharedProps(), contentFamily_); |
| 114 | + } |
| 115 | + |
| 116 | + const State::Shared &state() const |
| 117 | + { |
| 118 | + return state_; |
| 119 | + } |
| 120 | + |
| 121 | + void queueUnrelatedState() |
| 122 | + { |
| 123 | + auto data = ScrollContentViewState{}; |
| 124 | + data.safeAreaPadding.top = 99; |
| 125 | + auto newData = std::make_shared<const ScrollContentViewState>(data); |
| 126 | + dispatcher_->dispatchStateUpdate( |
| 127 | + {.family = unrelatedFamily_, |
| 128 | + .callback = [newData](const StateData::Shared & /*oldData*/) -> StateData::Shared { return newData; }}, |
| 129 | + EventQueue::UpdateMode::Asynchronous); |
| 130 | + } |
| 131 | + |
| 132 | + void flushQueuedState() |
| 133 | + { |
| 134 | + // A distinct-family legacy immediate update drains the dispatcher's private queue without a JavaScript runtime. |
| 135 | + dispatcher_->dispatchStateUpdate( |
| 136 | + {.family = flushFamily_, |
| 137 | + .callback = [data = initialData_](const StateData::Shared & /*oldData*/) -> StateData::Shared { |
| 138 | + return data; |
| 139 | + }}, |
| 140 | + EventQueue::UpdateMode::unstable_Immediate); |
| 141 | + } |
| 142 | + |
| 143 | + ScrollContentViewState latestData{}; |
| 144 | + ScrollContentViewState latestUnrelatedData{}; |
| 145 | + unsigned nativeStateChanges{0}; |
| 146 | + unsigned unrelatedStateChanges{0}; |
| 147 | + |
| 148 | + private: |
| 149 | + RuntimeScheduler runtimeScheduler_; |
| 150 | + std::unique_ptr<ScrollContentViewComponentDescriptor> descriptor_; |
| 151 | + std::shared_ptr<EventDispatcher> dispatcher_; |
| 152 | + ShadowNodeFamily::Shared contentFamily_; |
| 153 | + ShadowNodeFamily::Shared unrelatedFamily_; |
| 154 | + ShadowNodeFamily::Shared flushFamily_; |
| 155 | + State::Shared state_; |
| 156 | + std::shared_ptr<const ScrollContentViewState> initialData_ = std::make_shared<const ScrollContentViewState>(); |
| 157 | +}; |
| 158 | + |
| 159 | +} // namespace |
| 160 | + |
27 | 161 | @interface RCTScrollViewComponentView (Tests) |
28 | 162 | - (void)_keyboardWillChangeFrame:(NSNotification *)notification; |
29 | 163 | @end |
@@ -216,6 +350,97 @@ - (void)testBaseContentInsetReplacesPreviousInsetWithoutAccumulatingSafeAreaCorr |
216 | 350 | [view adjustedContentInsetDidChange]; |
217 | 351 | XCTAssertTrue(UIEdgeInsetsEqualToEdgeInsets(view.contentInset, UIEdgeInsetsMake(20, 15, 40, 25))); |
218 | 352 | } |
| 353 | + |
| 354 | +- (void)testScrollContentSafeAreaSetterDefaultsToAsynchronousStateUpdate |
| 355 | +{ |
| 356 | + ScopedScrollContentFeatureFlags flags(false); |
| 357 | + ScrollContentStateDispatch stateDispatch; |
| 358 | + RCTScrollContentViewComponentView *view = [RCTScrollContentViewComponentView new]; |
| 359 | + [view updateState:stateDispatch.state() oldState:nullptr]; |
| 360 | + XCTAssertFalse(facebook::react::ReactNativeFeatureFlags::enableImmediateUpdateModeForScrollContentView()); |
| 361 | + |
| 362 | + [view setSafeAreaPadding:UIEdgeInsetsMake(20, 0, 10, 0) contentInsetAdjustment:UIEdgeInsetsMake(7, 0, 3, 0)]; |
| 363 | + |
| 364 | + XCTAssertEqual(stateDispatch.nativeStateChanges, 0u); |
| 365 | + stateDispatch.flushQueuedState(); |
| 366 | + XCTAssertEqual(stateDispatch.nativeStateChanges, 1u); |
| 367 | + XCTAssertEqual(stateDispatch.latestData.safeAreaPadding.top, 20); |
| 368 | + XCTAssertEqual(stateDispatch.latestData.safeAreaPadding.bottom, 10); |
| 369 | + XCTAssertEqual(stateDispatch.latestData.contentInsetAdjustment.top, 7); |
| 370 | + XCTAssertEqual(stateDispatch.latestData.contentInsetAdjustment.bottom, 3); |
| 371 | +} |
| 372 | + |
| 373 | +- (void)testScrollContentSafeAreaSetterFlagUsesImmediateStateUpdate |
| 374 | +{ |
| 375 | + ScopedScrollContentFeatureFlags flags(true); |
| 376 | + ScrollContentStateDispatch stateDispatch; |
| 377 | + RCTScrollContentViewComponentView *view = [RCTScrollContentViewComponentView new]; |
| 378 | + [view updateState:stateDispatch.state() oldState:nullptr]; |
| 379 | + stateDispatch.queueUnrelatedState(); |
| 380 | + XCTAssertTrue(facebook::react::ReactNativeFeatureFlags::enableImmediateUpdateModeForScrollContentView()); |
| 381 | + |
| 382 | + [view setSafeAreaPadding:UIEdgeInsetsMake(20, 0, 10, 0) contentInsetAdjustment:UIEdgeInsetsMake(7, 0, 3, 0)]; |
| 383 | + |
| 384 | + XCTAssertEqual(stateDispatch.nativeStateChanges, 1u); |
| 385 | + XCTAssertEqual(stateDispatch.unrelatedStateChanges, 1u); |
| 386 | + XCTAssertEqual(stateDispatch.latestUnrelatedData.safeAreaPadding.top, 99); |
| 387 | + XCTAssertEqual(stateDispatch.latestData.safeAreaPadding.top, 20); |
| 388 | + XCTAssertEqual(stateDispatch.latestData.contentInsetAdjustment.top, 7); |
| 389 | + |
| 390 | + [view setSafeAreaPadding:UIEdgeInsetsMake(30, 0, 12, 0) contentInsetAdjustment:UIEdgeInsetsMake(9, 0, 4, 0)]; |
| 391 | + |
| 392 | + XCTAssertEqual(stateDispatch.nativeStateChanges, 2u); |
| 393 | + XCTAssertEqual(stateDispatch.unrelatedStateChanges, 1u); |
| 394 | + XCTAssertEqual(stateDispatch.latestData.safeAreaPadding.top, 30); |
| 395 | + XCTAssertEqual(stateDispatch.latestData.contentInsetAdjustment.top, 9); |
| 396 | + stateDispatch.flushQueuedState(); |
| 397 | + XCTAssertEqual(stateDispatch.nativeStateChanges, 2u); |
| 398 | + XCTAssertEqual(stateDispatch.unrelatedStateChanges, 1u); |
| 399 | + XCTAssertEqual(stateDispatch.latestUnrelatedData.safeAreaPadding.top, 99); |
| 400 | +} |
| 401 | + |
| 402 | +- (void)testScrollContentFinalizeDefaultsToAsynchronousStateUpdate |
| 403 | +{ |
| 404 | + ScopedScrollContentFeatureFlags flags(false); |
| 405 | + ScrollContentStateDispatch stateDispatch; |
| 406 | + RCTScrollContentViewComponentView *view = [RCTScrollContentViewComponentView new]; |
| 407 | + [view setSafeAreaPadding:UIEdgeInsetsMake(20, 0, 10, 0) contentInsetAdjustment:UIEdgeInsetsMake(7, 0, 3, 0)]; |
| 408 | + [view updateState:stateDispatch.state() oldState:nullptr]; |
| 409 | + XCTAssertEqual(stateDispatch.nativeStateChanges, 0u); |
| 410 | + XCTAssertFalse(facebook::react::ReactNativeFeatureFlags::enableImmediateUpdateModeForScrollContentView()); |
| 411 | + |
| 412 | + [view finalizeUpdates:RNComponentViewUpdateMaskState]; |
| 413 | + |
| 414 | + XCTAssertEqual(stateDispatch.nativeStateChanges, 0u); |
| 415 | + stateDispatch.flushQueuedState(); |
| 416 | + XCTAssertEqual(stateDispatch.nativeStateChanges, 1u); |
| 417 | + XCTAssertEqual(stateDispatch.latestData.safeAreaPadding.top, 20); |
| 418 | + XCTAssertEqual(stateDispatch.latestData.contentInsetAdjustment.top, 7); |
| 419 | +} |
| 420 | + |
| 421 | +- (void)testScrollContentFinalizeFlagUsesImmediateStateUpdate |
| 422 | +{ |
| 423 | + ScopedScrollContentFeatureFlags flags(true); |
| 424 | + ScrollContentStateDispatch stateDispatch; |
| 425 | + RCTScrollContentViewComponentView *view = [RCTScrollContentViewComponentView new]; |
| 426 | + [view setSafeAreaPadding:UIEdgeInsetsMake(20, 0, 10, 0) contentInsetAdjustment:UIEdgeInsetsMake(7, 0, 3, 0)]; |
| 427 | + [view updateState:stateDispatch.state() oldState:nullptr]; |
| 428 | + stateDispatch.queueUnrelatedState(); |
| 429 | + XCTAssertEqual(stateDispatch.nativeStateChanges, 0u); |
| 430 | + XCTAssertTrue(facebook::react::ReactNativeFeatureFlags::enableImmediateUpdateModeForScrollContentView()); |
| 431 | + |
| 432 | + [view finalizeUpdates:RNComponentViewUpdateMaskState]; |
| 433 | + |
| 434 | + XCTAssertEqual(stateDispatch.nativeStateChanges, 1u); |
| 435 | + XCTAssertEqual(stateDispatch.unrelatedStateChanges, 1u); |
| 436 | + XCTAssertEqual(stateDispatch.latestUnrelatedData.safeAreaPadding.top, 99); |
| 437 | + XCTAssertEqual(stateDispatch.latestData.safeAreaPadding.top, 20); |
| 438 | + XCTAssertEqual(stateDispatch.latestData.contentInsetAdjustment.top, 7); |
| 439 | + stateDispatch.flushQueuedState(); |
| 440 | + XCTAssertEqual(stateDispatch.nativeStateChanges, 1u); |
| 441 | + XCTAssertEqual(stateDispatch.unrelatedStateChanges, 1u); |
| 442 | + XCTAssertEqual(stateDispatch.latestUnrelatedData.safeAreaPadding.top, 99); |
| 443 | +} |
219 | 444 | // NOLINTEND(cppcoreguidelines-pro-type-vararg, misc-throw-by-value-catch-by-reference) |
220 | 445 |
|
221 | 446 | @end |
|
0 commit comments