2015-07-13 15:42:32 +00:00
|
|
|
/**
|
|
|
|
* The examples provided by Facebook are for non-commercial testing and
|
|
|
|
* evaluation purposes only.
|
|
|
|
*
|
|
|
|
* Facebook reserves all rights not expressly granted.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
|
|
|
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
2015-06-06 20:37:36 +00:00
|
|
|
|
|
|
|
#import <Foundation/Foundation.h>
|
|
|
|
#import <XCTest/XCTest.h>
|
|
|
|
|
|
|
|
#import "RCTBridge.h"
|
2015-12-15 13:39:30 +00:00
|
|
|
#import "RCTBridge+Private.h"
|
2015-06-06 20:37:36 +00:00
|
|
|
#import "RCTContextExecutor.h"
|
2015-08-03 11:37:47 +00:00
|
|
|
#import "RCTModuleMethod.h"
|
2015-06-06 20:37:36 +00:00
|
|
|
#import "RCTRootView.h"
|
|
|
|
|
2015-07-16 16:23:23 +00:00
|
|
|
#define RUN_RUNLOOP_WHILE(CONDITION) \
|
2015-06-06 20:37:36 +00:00
|
|
|
_Pragma("clang diagnostic push") \
|
|
|
|
_Pragma("clang diagnostic ignored \"-Wshadow\"") \
|
2015-09-01 16:36:39 +00:00
|
|
|
NSDate *timeout = [[NSDate date] dateByAddingTimeInterval:5]; \
|
2015-06-06 20:37:36 +00:00
|
|
|
while ((CONDITION) && [timeout timeIntervalSinceNow] > 0) { \
|
|
|
|
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:timeout]; \
|
|
|
|
} \
|
|
|
|
_Pragma("clang diagnostic pop")
|
|
|
|
|
|
|
|
@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
|
|
|
|
|
|
|
|
@implementation RCTAllocationTests
|
|
|
|
|
|
|
|
- (void)testBridgeIsDeallocated
|
|
|
|
{
|
|
|
|
__weak RCTBridge *weakBridge;
|
|
|
|
@autoreleasepool {
|
|
|
|
RCTRootView *view = [[RCTRootView alloc] initWithBundleURL:nil
|
|
|
|
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 {
|
|
|
|
RCTBridge *bridge = [[RCTBridge alloc] initWithBundleURL:nil
|
|
|
|
moduleProvider:^{
|
|
|
|
return @[module];
|
|
|
|
}
|
|
|
|
launchOptions:nil];
|
|
|
|
XCTAssertTrue(module.isValid, @"AllocationTestModule should be valid");
|
|
|
|
(void)bridge;
|
|
|
|
}
|
|
|
|
|
2015-07-16 16:23:23 +00:00
|
|
|
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-06-06 20:37:36 +00:00
|
|
|
RCTBridge *bridge = [[RCTBridge alloc] initWithBundleURL:nil
|
|
|
|
moduleProvider:^{
|
|
|
|
return @[module];
|
|
|
|
}
|
|
|
|
launchOptions:nil];
|
|
|
|
weakModule = module;
|
|
|
|
XCTAssertNotNil(weakModule, @"AllocationTestModule should have been created");
|
|
|
|
(void)bridge;
|
|
|
|
}
|
|
|
|
|
2015-07-16 16:23:23 +00:00
|
|
|
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
|
|
|
|
{
|
|
|
|
__weak RCTModuleMethod *weakMethod;
|
|
|
|
@autoreleasepool {
|
2015-12-10 18:09:04 +00:00
|
|
|
__autoreleasing RCTModuleMethod *method = [[RCTModuleMethod alloc] initWithMethodSignature:@"test:(NSString *)a :(nonnull NSNumber *)b :(RCTResponseSenderBlock)c :(RCTResponseErrorBlock)d" JSMethodName:@"" moduleClass:[AllocationTestModule class]];
|
2015-08-03 11:37:47 +00:00
|
|
|
weakMethod = method;
|
|
|
|
XCTAssertNotNil(method, @"RCTModuleMethod should have been created");
|
|
|
|
}
|
|
|
|
|
|
|
|
RUN_RUNLOOP_WHILE(weakMethod)
|
|
|
|
XCTAssertNil(weakMethod, @"RCTModuleMethod should have been deallocated");
|
|
|
|
}
|
|
|
|
|
2015-09-01 16:36:39 +00:00
|
|
|
- (void)testJavaScriptExecutorIsDeallocated
|
2015-06-06 20:37:36 +00:00
|
|
|
{
|
|
|
|
__weak id<RCTJavaScriptExecutor> weakExecutor;
|
|
|
|
@autoreleasepool {
|
|
|
|
RCTBridge *bridge = [[RCTBridge alloc] initWithBundleURL:nil
|
|
|
|
moduleProvider:nil
|
|
|
|
launchOptions:nil];
|
|
|
|
weakExecutor = [bridge.batchedBridge valueForKey:@"javaScriptExecutor"];
|
|
|
|
XCTAssertNotNil(weakExecutor, @"JavaScriptExecutor should have been created");
|
|
|
|
(void)bridge;
|
|
|
|
}
|
|
|
|
|
2015-07-16 16:23:23 +00:00
|
|
|
RUN_RUNLOOP_WHILE(weakExecutor);
|
2015-06-06 20:37:36 +00:00
|
|
|
XCTAssertNil(weakExecutor, @"JavaScriptExecutor should have been released");
|
|
|
|
}
|
|
|
|
|
2015-09-01 16:36:39 +00:00
|
|
|
- (void)testJavaScriptContextIsDeallocated
|
2015-06-06 20:37:36 +00:00
|
|
|
{
|
|
|
|
__weak id weakContext;
|
|
|
|
@autoreleasepool {
|
|
|
|
RCTBridge *bridge = [[RCTBridge alloc] initWithBundleURL:nil
|
|
|
|
moduleProvider:nil
|
|
|
|
launchOptions:nil];
|
|
|
|
id executor = [bridge.batchedBridge valueForKey:@"javaScriptExecutor"];
|
2015-07-16 16:23:23 +00:00
|
|
|
RUN_RUNLOOP_WHILE(!(weakContext = [executor valueForKey:@"context"]));
|
2015-06-06 20:37:36 +00:00
|
|
|
XCTAssertNotNil(weakContext, @"RCTJavaScriptContext should have been created");
|
|
|
|
(void)bridge;
|
|
|
|
}
|
|
|
|
|
2015-07-16 16:23:23 +00:00
|
|
|
RUN_RUNLOOP_WHILE(weakContext);
|
2015-06-06 20:37:36 +00:00
|
|
|
XCTAssertNil(weakContext, @"RCTJavaScriptContext should have been deallocated");
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)testContentViewIsInvalidated
|
|
|
|
{
|
2015-06-15 14:53:45 +00:00
|
|
|
RCTBridge *bridge = [[RCTBridge alloc] initWithBundleURL:nil
|
|
|
|
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];
|
2015-07-16 16:23:23 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-08-14 12:07:49 +00:00
|
|
|
XCTAssertFalse(rootContentView.userInteractionEnabled, @"RCTContentView should have been invalidated");
|
2015-06-06 20:37:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)testUnderlyingBridgeIsDeallocated
|
|
|
|
{
|
|
|
|
RCTBridge *bridge;
|
|
|
|
__weak id batchedBridge;
|
|
|
|
@autoreleasepool {
|
|
|
|
bridge = [[RCTBridge alloc] initWithBundleURL:nil moduleProvider:nil launchOptions:nil];
|
|
|
|
batchedBridge = bridge.batchedBridge;
|
|
|
|
XCTAssertTrue([batchedBridge isValid], @"RCTBatchedBridge should be valid");
|
|
|
|
[bridge reload];
|
|
|
|
}
|
|
|
|
|
2015-07-16 16:23:23 +00:00
|
|
|
RUN_RUNLOOP_WHILE(batchedBridge != nil)
|
2015-06-06 20:37:36 +00:00
|
|
|
|
|
|
|
XCTAssertNotNil(bridge, @"RCTBridge should not have been deallocated");
|
|
|
|
XCTAssertNil(batchedBridge, @"RCTBatchedBridge should have been deallocated");
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|