|
| 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/RCTDevLoadingView.h> |
| 9 | +#import <React/RCTDevLoadingViewSetEnabled.h> |
| 10 | +#import <React/RCTJavaScriptLoader.h> |
| 11 | +#import <React/RCTUtils.h> |
| 12 | +#import <XCTest/XCTest.h> |
| 13 | + |
| 14 | +#if RCT_DEV_MENU |
| 15 | + |
| 16 | +@interface RCTDevLoadingView (Tests) |
| 17 | +- (void)invalidate; |
| 18 | +- (BOOL)isRunningInTestEnvironment; |
| 19 | +@end |
| 20 | + |
| 21 | +@interface RCTDevLoadingViewForTests : RCTDevLoadingView |
| 22 | +@end |
| 23 | + |
| 24 | +@implementation RCTDevLoadingViewForTests |
| 25 | +- (BOOL)isRunningInTestEnvironment |
| 26 | +{ |
| 27 | + // Exercise the real display path instead of the XCTest early return. |
| 28 | + return NO; |
| 29 | +} |
| 30 | +@end |
| 31 | + |
| 32 | +@interface RCTDevLoadingViewTests : XCTestCase |
| 33 | +@end |
| 34 | + |
| 35 | +@implementation RCTDevLoadingViewTests { |
| 36 | + BOOL _wasEnabled; |
| 37 | + NSMutableArray<RCTDevLoadingView *> *_loadingViews; |
| 38 | +} |
| 39 | + |
| 40 | +- (void)setUp |
| 41 | +{ |
| 42 | + [super setUp]; |
| 43 | + XCTAssertTrue(RCTIsMainQueue()); |
| 44 | + _wasEnabled = RCTDevLoadingViewGetEnabled(); |
| 45 | + RCTDevLoadingViewSetEnabled(YES); |
| 46 | + _loadingViews = [NSMutableArray new]; |
| 47 | +} |
| 48 | + |
| 49 | +- (void)tearDown |
| 50 | +{ |
| 51 | + for (RCTDevLoadingView *loadingView in _loadingViews) { |
| 52 | + [loadingView invalidate]; |
| 53 | + } |
| 54 | + [self drainMainQueue]; |
| 55 | + _loadingViews = nil; |
| 56 | + RCTDevLoadingViewSetEnabled(_wasEnabled); |
| 57 | + [super tearDown]; |
| 58 | +} |
| 59 | + |
| 60 | +- (RCTDevLoadingView *)newLoadingView |
| 61 | +{ |
| 62 | + RCTDevLoadingView *loadingView = [RCTDevLoadingViewForTests new]; |
| 63 | + [_loadingViews addObject:loadingView]; |
| 64 | + return loadingView; |
| 65 | +} |
| 66 | + |
| 67 | +- (void)drainMainQueue |
| 68 | +{ |
| 69 | + XCTestExpectation *drained = [self expectationWithDescription:@"Queued loading messages finished"]; |
| 70 | + dispatch_async(dispatch_get_main_queue(), ^{ |
| 71 | + // updateProgress enqueues another display block from its main-queue callback. |
| 72 | + dispatch_async(dispatch_get_main_queue(), ^{ |
| 73 | + [drained fulfill]; |
| 74 | + }); |
| 75 | + }); |
| 76 | + [self waitForExpectations:@[ drained ] timeout:2]; |
| 77 | +} |
| 78 | + |
| 79 | +- (void)showMessage:(RCTDevLoadingView *)loadingView |
| 80 | +{ |
| 81 | + [loadingView showMessage:@"Loading from Metro…" |
| 82 | + color:UIColor.whiteColor |
| 83 | + backgroundColor:UIColor.blackColor |
| 84 | + dismissButton:YES]; |
| 85 | +} |
| 86 | + |
| 87 | +- (void)testInvalidateClearsVisibleWindow |
| 88 | +{ |
| 89 | + RCTDevLoadingView *loadingView = [self newLoadingView]; |
| 90 | + [self showMessage:loadingView]; |
| 91 | + [self drainMainQueue]; |
| 92 | + |
| 93 | + UIWindow *window = [loadingView valueForKey:@"window"]; |
| 94 | + XCTAssertNotNil(window); |
| 95 | + XCTAssertFalse(window.hidden); |
| 96 | + XCTAssertNotNil([loadingView valueForKey:@"dismissButton"]); |
| 97 | + |
| 98 | + [loadingView invalidate]; |
| 99 | + [self drainMainQueue]; |
| 100 | + |
| 101 | + XCTAssertTrue(window.hidden); |
| 102 | + for (NSString *key in @[ @"window", @"container", @"label", @"dismissButton" ]) { |
| 103 | + XCTAssertNil([loadingView valueForKey:key], @"%@ should be released during invalidation", key); |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +- (void)testInvalidateCancelsPendingInitialMessage |
| 108 | +{ |
| 109 | + RCTDevLoadingView *loadingView = [self newLoadingView]; |
| 110 | + [loadingView showWithURL:[NSURL URLWithString:@"http://localhost:8081/index.bundle"]]; |
| 111 | + dispatch_block_t pendingMessage = [loadingView valueForKey:@"initialMessageBlock"]; |
| 112 | + XCTAssertNotNil(pendingMessage); |
| 113 | + XCTAssertEqual(dispatch_block_testcancel(pendingMessage), 0L); |
| 114 | + |
| 115 | + [loadingView invalidate]; |
| 116 | + [self drainMainQueue]; |
| 117 | + |
| 118 | + XCTAssertNil([loadingView valueForKey:@"initialMessageBlock"]); |
| 119 | + XCTAssertNotEqual(dispatch_block_testcancel(pendingMessage), 0L); |
| 120 | +} |
| 121 | + |
| 122 | +- (void)testInvalidatePreventsQueuedMessagesFromRecreatingWindow |
| 123 | +{ |
| 124 | + RCTDevLoadingView *messageView = [self newLoadingView]; |
| 125 | + RCTDevLoadingView *progressView = [self newLoadingView]; |
| 126 | + RCTLoadingProgress *progress = [RCTLoadingProgress new]; |
| 127 | + progress.status = @"Downloading"; |
| 128 | + |
| 129 | + [self showMessage:messageView]; |
| 130 | + [progressView updateProgress:progress]; |
| 131 | + [messageView invalidate]; |
| 132 | + [progressView invalidate]; |
| 133 | + [self drainMainQueue]; |
| 134 | + |
| 135 | + XCTAssertNil([messageView valueForKey:@"window"]); |
| 136 | + XCTAssertNil([progressView valueForKey:@"window"]); |
| 137 | +} |
| 138 | + |
| 139 | +@end |
| 140 | + |
| 141 | +#endif |
0 commit comments