2015-07-13 15:42:32 +00:00
|
|
|
/**
|
2017-05-06 03:50:47 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
2015-07-13 15:42:32 +00:00
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2015-07-13 15:42:32 +00:00
|
|
|
*
|
|
|
|
*/
|
2015-06-06 20:37:36 +00:00
|
|
|
|
|
|
|
#import <Foundation/Foundation.h>
|
|
|
|
#import <XCTest/XCTest.h>
|
|
|
|
|
2017-04-21 15:16:17 +00:00
|
|
|
#import <RCTTest/RCTTestRunner.h>
|
2016-11-24 17:44:51 +00:00
|
|
|
#import <React/RCTBridge+Private.h>
|
|
|
|
#import <React/RCTBridge.h>
|
|
|
|
#import <React/RCTModuleMethod.h>
|
|
|
|
#import <React/RCTRootView.h>
|
2015-06-06 20:37:36 +00:00
|
|
|
|
|
|
|
@interface RCTJavaScriptContext : NSObject
|
|
|
|
|
|
|
|
@property (nonatomic, assign, readonly) JSGlobalContextRef ctx;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface AllocationTestModule : NSObject<RCTBridgeModule, RCTInvalidating>
|
2015-08-14 08:59:42 +00:00
|
|
|
|
|
|
|
@property (nonatomic, assign, getter=isValid) BOOL valid;
|
|
|
|
|
2015-06-06 20:37:36 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation AllocationTestModule
|
|
|
|
|
|
|
|
RCT_EXPORT_MODULE();
|
|
|
|
|
2015-08-14 08:59:42 +00:00
|
|
|
- (instancetype)init
|
2015-06-06 20:37:36 +00:00
|
|
|
{
|
|
|
|
if ((self = [super init])) {
|
|
|
|
_valid = YES;
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)invalidate
|
|
|
|
{
|
|
|
|
_valid = NO;
|
|
|
|
}
|
|
|
|
|
2015-08-03 11:37:47 +00:00
|
|
|
RCT_EXPORT_METHOD(test:(__unused NSString *)a
|
|
|
|
:(__unused NSNumber *)b
|
|
|
|
:(__unused RCTResponseSenderBlock)c
|
|
|
|
:(__unused RCTResponseErrorBlock)d) {}
|
|
|
|
|
2015-06-06 20:37:36 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@interface RCTAllocationTests : XCTestCase
|
|
|
|
@end
|
|
|
|
|
2015-12-18 01:26:12 +00:00
|
|
|
@implementation RCTAllocationTests {
|
|
|
|
NSURL *_bundleURL;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setUp
|
|
|
|
{
|
|
|
|
[super setUp];
|
|
|
|
|
|
|
|
NSString *bundleContents =
|
|
|
|
@"var __fbBatchedBridge = {"
|
2017-04-19 23:53:44 +00:00
|
|
|
" callFunctionReturnFlushedQueue: function() { return null; },"
|
|
|
|
" invokeCallbackAndReturnFlushedQueue: function() { return null; },"
|
|
|
|
" flushedQueue: function() { return null; },"
|
|
|
|
" callFunctionReturnResultAndFlushedQueue: function() { return null; },"
|
2015-12-18 01:26:12 +00:00
|
|
|
"};";
|
|
|
|
|
|
|
|
NSURL *tempDir = [NSURL fileURLWithPath:NSTemporaryDirectory() isDirectory:YES];
|
|
|
|
[[NSFileManager defaultManager] createDirectoryAtURL:tempDir withIntermediateDirectories:YES attributes:nil error:NULL];
|
2016-09-08 22:00:51 +00:00
|
|
|
NSString *guid = [[NSProcessInfo processInfo] globallyUniqueString];
|
|
|
|
NSString *fileName = [NSString stringWithFormat:@"rctallocationtests-bundle-%@.js", guid];
|
|
|
|
|
|
|
|
_bundleURL = [tempDir URLByAppendingPathComponent:fileName];
|
|
|
|
NSError *saveError;
|
|
|
|
if (![bundleContents writeToURL:_bundleURL atomically:YES encoding:NSUTF8StringEncoding error:&saveError]) {
|
|
|
|
XCTFail(@"Failed to save test bundle to %@, error: %@", _bundleURL, saveError);
|
|
|
|
};
|
2015-12-18 01:26:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)tearDown
|
|
|
|
{
|
|
|
|
[super tearDown];
|
|
|
|
|
|
|
|
[[NSFileManager defaultManager] removeItemAtURL:_bundleURL error:NULL];
|
|
|
|
}
|
2015-06-06 20:37:36 +00:00
|
|
|
|
|
|
|
- (void)testBridgeIsDeallocated
|
|
|
|
{
|
|
|
|
__weak RCTBridge *weakBridge;
|
|
|
|
@autoreleasepool {
|
2015-12-18 01:26:12 +00:00
|
|
|
RCTRootView *view = [[RCTRootView alloc] initWithBundleURL:_bundleURL
|
2015-06-06 20:37:36 +00:00
|
|
|
moduleName:@""
|
2015-08-17 11:38:19 +00:00
|
|
|
initialProperties:nil
|
2015-06-06 20:37:36 +00:00
|
|
|
launchOptions:nil];
|
|
|
|
weakBridge = view.bridge;
|
|
|
|
XCTAssertNotNil(weakBridge, @"RCTBridge should have been created");
|
|
|
|
(void)view;
|
|
|
|
}
|
|
|
|
|
|
|
|
XCTAssertNil(weakBridge, @"RCTBridge should have been deallocated");
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)testModulesAreInvalidated
|
|
|
|
{
|
2015-08-17 14:35:34 +00:00
|
|
|
AllocationTestModule *module = [AllocationTestModule new];
|
2015-06-06 20:37:36 +00:00
|
|
|
@autoreleasepool {
|
2015-12-18 01:26:12 +00:00
|
|
|
RCTBridge *bridge = [[RCTBridge alloc] initWithBundleURL:_bundleURL
|
2017-07-24 13:46:01 +00:00
|
|
|
moduleProvider:^{ return @[module]; }
|
2015-06-06 20:37:36 +00:00
|
|
|
launchOptions:nil];
|
|
|
|
XCTAssertTrue(module.isValid, @"AllocationTestModule should be valid");
|
|
|
|
(void)bridge;
|
|
|
|
}
|
|
|
|
|
2017-04-21 15:16:17 +00:00
|
|
|
RCT_RUN_RUNLOOP_WHILE(module.isValid)
|
2015-06-06 20:37:36 +00:00
|
|
|
XCTAssertFalse(module.isValid, @"AllocationTestModule should have been invalidated by the bridge");
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)testModulesAreDeallocated
|
|
|
|
{
|
|
|
|
__weak AllocationTestModule *weakModule;
|
|
|
|
@autoreleasepool {
|
2015-08-17 14:35:34 +00:00
|
|
|
AllocationTestModule *module = [AllocationTestModule new];
|
2015-12-18 01:26:12 +00:00
|
|
|
RCTBridge *bridge = [[RCTBridge alloc] initWithBundleURL:_bundleURL
|
2017-07-24 13:46:01 +00:00
|
|
|
moduleProvider:^{ return @[module]; }
|
2015-12-18 01:26:12 +00:00
|
|
|
launchOptions:nil];
|
2017-07-24 13:46:01 +00:00
|
|
|
XCTAssertNotNil(module, @"AllocationTestModule should have been created");
|
2015-06-06 20:37:36 +00:00
|
|
|
weakModule = module;
|
|
|
|
(void)bridge;
|
|
|
|
}
|
|
|
|
|
2017-04-21 15:16:17 +00:00
|
|
|
RCT_RUN_RUNLOOP_WHILE(weakModule)
|
2015-06-06 20:37:36 +00:00
|
|
|
XCTAssertNil(weakModule, @"AllocationTestModule should have been deallocated");
|
|
|
|
}
|
|
|
|
|
2015-08-03 11:37:47 +00:00
|
|
|
- (void)testModuleMethodsAreDeallocated
|
|
|
|
{
|
2017-07-24 13:46:01 +00:00
|
|
|
static RCTMethodInfo methodInfo = {
|
|
|
|
.objcName = "test:(NSString *)a :(nonnull NSNumber *)b :(RCTResponseSenderBlock)c :(RCTResponseErrorBlock)d",
|
|
|
|
.jsName = "",
|
|
|
|
.isSync = false
|
|
|
|
};
|
|
|
|
|
2015-08-03 11:37:47 +00:00
|
|
|
__weak RCTModuleMethod *weakMethod;
|
|
|
|
@autoreleasepool {
|
2017-07-24 13:46:01 +00:00
|
|
|
__autoreleasing RCTModuleMethod *method = [[RCTModuleMethod alloc] initWithExportedMethod:&methodInfo
|
|
|
|
moduleClass:[AllocationTestModule class]];
|
2015-08-03 11:37:47 +00:00
|
|
|
XCTAssertNotNil(method, @"RCTModuleMethod should have been created");
|
2017-07-24 13:46:01 +00:00
|
|
|
weakMethod = method;
|
2015-08-03 11:37:47 +00:00
|
|
|
}
|
|
|
|
|
2017-04-21 15:16:17 +00:00
|
|
|
RCT_RUN_RUNLOOP_WHILE(weakMethod)
|
2015-08-03 11:37:47 +00:00
|
|
|
XCTAssertNil(weakMethod, @"RCTModuleMethod should have been deallocated");
|
|
|
|
}
|
|
|
|
|
2015-06-06 20:37:36 +00:00
|
|
|
- (void)testContentViewIsInvalidated
|
|
|
|
{
|
2015-12-18 01:26:12 +00:00
|
|
|
RCTBridge *bridge = [[RCTBridge alloc] initWithBundleURL:_bundleURL
|
2015-06-15 14:53:45 +00:00
|
|
|
moduleProvider:nil
|
|
|
|
launchOptions:nil];
|
2015-08-14 12:07:49 +00:00
|
|
|
__weak UIView *rootContentView;
|
2015-06-06 20:37:36 +00:00
|
|
|
@autoreleasepool {
|
2015-08-17 11:38:19 +00:00
|
|
|
RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge moduleName:@"" initialProperties:nil];
|
2017-04-21 15:16:17 +00:00
|
|
|
RCT_RUN_RUNLOOP_WHILE(!(rootContentView = [rootView valueForKey:@"contentView"]))
|
2015-08-14 12:07:49 +00:00
|
|
|
XCTAssertTrue(rootContentView.userInteractionEnabled, @"RCTContentView should be valid");
|
2015-06-06 20:37:36 +00:00
|
|
|
(void)rootView;
|
|
|
|
}
|
|
|
|
|
2016-12-19 14:26:07 +00:00
|
|
|
#if !TARGET_OS_TV // userInteractionEnabled is true for Apple TV views
|
2015-08-14 12:07:49 +00:00
|
|
|
XCTAssertFalse(rootContentView.userInteractionEnabled, @"RCTContentView should have been invalidated");
|
2016-12-19 14:26:07 +00:00
|
|
|
#endif
|
2015-06-06 20:37:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)testUnderlyingBridgeIsDeallocated
|
|
|
|
{
|
|
|
|
RCTBridge *bridge;
|
|
|
|
__weak id batchedBridge;
|
|
|
|
@autoreleasepool {
|
2015-12-18 01:26:12 +00:00
|
|
|
bridge = [[RCTBridge alloc] initWithBundleURL:_bundleURL moduleProvider:nil launchOptions:nil];
|
2015-06-06 20:37:36 +00:00
|
|
|
batchedBridge = bridge.batchedBridge;
|
2018-02-05 19:47:10 +00:00
|
|
|
XCTAssertTrue([batchedBridge isValid], @"RCTBridge impl should be valid");
|
2015-06-06 20:37:36 +00:00
|
|
|
[bridge reload];
|
|
|
|
}
|
|
|
|
|
2017-04-21 15:16:17 +00:00
|
|
|
RCT_RUN_RUNLOOP_WHILE(batchedBridge != nil)
|
2015-06-06 20:37:36 +00:00
|
|
|
|
|
|
|
XCTAssertNotNil(bridge, @"RCTBridge should not have been deallocated");
|
2018-02-05 19:47:10 +00:00
|
|
|
XCTAssertNil(batchedBridge, @"RCTBridge impl should have been deallocated");
|
2016-01-06 17:02:55 +00:00
|
|
|
|
2018-02-05 19:47:10 +00:00
|
|
|
// Wait to complete the test until the new bridge impl is also deallocated
|
2016-01-06 17:02:55 +00:00
|
|
|
@autoreleasepool {
|
|
|
|
batchedBridge = bridge.batchedBridge;
|
2016-09-15 11:24:56 +00:00
|
|
|
[bridge invalidate];
|
2016-01-06 17:02:55 +00:00
|
|
|
bridge = nil;
|
|
|
|
}
|
|
|
|
|
2017-04-21 15:16:17 +00:00
|
|
|
RCT_RUN_RUNLOOP_WHILE(batchedBridge != nil);
|
2016-01-06 17:02:55 +00:00
|
|
|
XCTAssertNil(batchedBridge);
|
2015-06-06 20:37:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|