|
| 1 | +import { execFileSync } from 'node:child_process'; |
| 2 | +import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; |
| 3 | +import { tmpdir } from 'node:os'; |
| 4 | +import { join } from 'node:path'; |
| 5 | + |
| 6 | +// Compile the production update method against a recording navigation controller. |
| 7 | +// This verifies that one React replacement never starts competing UIKit transitions. |
| 8 | +const source = readFileSync(new URL('../../ios/helpers/stack/RNSStackNavigationController.mm', import.meta.url), 'utf8'); |
| 9 | +const method = source.slice(source.indexOf('- (void)performContainerUpdateIfNeeded'), source.indexOf('#pragma mark - Debug')); |
| 10 | +const directory = mkdtempSync(join(tmpdir(), 'rns-stack-replacement-')); |
| 11 | +try { |
| 12 | + writeFileSync(join(directory, 'test.mm'), ` |
| 13 | +#import <Foundation/Foundation.h> |
| 14 | +@compatibility_alias UIViewController NSObject; |
| 15 | +#define RCTAssert(condition, message) NSCAssert(condition, message) |
| 16 | +@interface Screen : NSObject |
| 17 | +@property NSObject *controller; |
| 18 | +@end |
| 19 | +@implementation Screen |
| 20 | +@end |
| 21 | +@interface RNSPopOperation : NSObject |
| 22 | +@property Screen *stackScreen; |
| 23 | +@end |
| 24 | +@implementation RNSPopOperation |
| 25 | +@end |
| 26 | +@interface RNSPushOperation : RNSPopOperation |
| 27 | +@end |
| 28 | +@implementation RNSPushOperation |
| 29 | +@end |
| 30 | +@interface Navigation : NSObject { |
| 31 | +@public |
| 32 | + NSMutableArray<RNSPushOperation *> *_pendingPushOperations; |
| 33 | + NSMutableArray<RNSPopOperation *> *_pendingPopOperations; |
| 34 | + NSObject *_emptyStackController; |
| 35 | + BOOL _performingReactUpdate; |
| 36 | +} |
| 37 | +@property NSMutableArray *viewControllers; |
| 38 | +@property NSMutableArray *transitions; |
| 39 | +@property BOOL allowsEmptyStack; |
| 40 | +- (void)performContainerUpdateIfNeeded; |
| 41 | +@end |
| 42 | +@implementation Navigation |
| 43 | +- (BOOL)hasPendingOperations { return _pendingPushOperations.count || _pendingPopOperations.count; } |
| 44 | +- (BOOL)isStackEmpty { return !self.viewControllers.count || self.viewControllers.firstObject == _emptyStackController; } |
| 45 | +- (NSObject *)topViewController { return self.viewControllers.lastObject; } |
| 46 | +- (void)prepareToolbarForController:(NSObject *)controller {} |
| 47 | +- (void)dumpStackModel {} |
| 48 | +- (void)setViewControllers:(NSArray *)controllers animated:(BOOL)animated { |
| 49 | + self.viewControllers = [controllers mutableCopy]; |
| 50 | + if (animated) [self.transitions addObject:@"set"]; |
| 51 | +} |
| 52 | +- (void)popViewControllerAnimated:(BOOL)animated { |
| 53 | + [self.viewControllers removeLastObject]; |
| 54 | + if (animated) [self.transitions addObject:@"pop"]; |
| 55 | +} |
| 56 | +- (void)pushViewController:(NSObject *)controller animated:(BOOL)animated { |
| 57 | + [self.viewControllers addObject:controller]; |
| 58 | + if (animated) [self.transitions addObject:@"push"]; |
| 59 | +} |
| 60 | +${method} |
| 61 | +@end |
| 62 | +static Screen *screen(NSString *name) { Screen *s = [Screen new]; s.controller = name; return s; } |
| 63 | +static Navigation *navigation(NSArray *controllers, BOOL empty) { |
| 64 | + Navigation *n = [Navigation new]; n.viewControllers = [controllers mutableCopy]; |
| 65 | + n.transitions = [NSMutableArray new]; n.allowsEmptyStack = empty; |
| 66 | + n->_pendingPushOperations = [NSMutableArray new]; n->_pendingPopOperations = [NSMutableArray new]; |
| 67 | + n->_emptyStackController = @"empty"; return n; |
| 68 | +} |
| 69 | +static void pop(Navigation *n, NSString *name) { |
| 70 | + RNSPopOperation *op = [RNSPopOperation new]; op.stackScreen = screen(name); [n->_pendingPopOperations addObject:op]; |
| 71 | +} |
| 72 | +static void push(Navigation *n, NSString *name) { |
| 73 | + RNSPushOperation *op = [RNSPushOperation new]; op.stackScreen = screen(name); [n->_pendingPushOperations addObject:op]; |
| 74 | +} |
| 75 | +int main() { @autoreleasepool { |
| 76 | + Navigation *n = navigation(@[@"home", @"pr", @"chooser", @"local"], NO); |
| 77 | + pop(n, @"local"); pop(n, @"chooser"); pop(n, @"pr"); push(n, @"draft"); |
| 78 | + [n performContainerUpdateIfNeeded]; |
| 79 | + NSCAssert(([n.viewControllers isEqual:@[@"home", @"draft"]]), @"replacement destination"); |
| 80 | + NSCAssert([n.transitions isEqual:@[@"set"]], @"replacement must have one atomic transition, got %@", n.transitions); |
| 81 | + NSCAssert(!n.hasPendingOperations, @"replacement must drain operations"); |
| 82 | + n = navigation(@[@"pr", @"chooser", @"local"], YES); |
| 83 | + pop(n, @"local"); pop(n, @"chooser"); pop(n, @"pr"); push(n, @"draft"); |
| 84 | + [n performContainerUpdateIfNeeded]; |
| 85 | + NSCAssert([n.viewControllers isEqual:@[@"draft"]], @"empty column replacement destination"); |
| 86 | + NSCAssert([n.transitions isEqual:@[@"set"]], @"empty column replacement transition"); |
| 87 | + n = navigation(@[@"home", @"draft", @"chooser"], NO); pop(n, @"chooser"); [n performContainerUpdateIfNeeded]; |
| 88 | + NSCAssert(([n.viewControllers isEqual:@[@"home", @"draft"]] && [n.transitions isEqual:@[@"pop"]]), @"ordinary back unchanged"); |
| 89 | + n = navigation(@[@"home"], NO); push(n, @"draft"); [n performContainerUpdateIfNeeded]; |
| 90 | + NSCAssert(([n.viewControllers isEqual:@[@"home", @"draft"]] && [n.transitions isEqual:@[@"push"]]), @"ordinary push unchanged"); |
| 91 | + puts("4 stack update scenarios passed"); |
| 92 | +} } |
| 93 | +`); |
| 94 | + execFileSync('xcrun', ['clang++', '-fobjc-arc', '-framework', 'Foundation', join(directory, 'test.mm'), '-o', join(directory, 'test')], { stdio: 'inherit' }); |
| 95 | + execFileSync(join(directory, 'test'), { stdio: 'inherit' }); |
| 96 | +} finally { |
| 97 | + rmSync(directory, { recursive: true, force: true }); |
| 98 | +} |
0 commit comments