2015-07-13 15:42:32 +00:00
|
|
|
/**
|
2017-05-06 03:50:47 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
2015-07-13 15:42:32 +00:00
|
|
|
*
|
2017-05-06 03:50:47 +00:00
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
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/RCTBridgeModule.h>
|
|
|
|
#import <React/RCTJavaScriptExecutor.h>
|
|
|
|
#import <React/RCTUtils.h>
|
2015-06-06 20:37:36 +00:00
|
|
|
|
2016-09-05 14:32:20 +00:00
|
|
|
static const NSUInteger kNameIndex = 0;
|
|
|
|
static const NSUInteger kConstantsIndex = 1;
|
|
|
|
static const NSUInteger kMethodsIndex = 2;
|
|
|
|
|
2015-06-06 20:37:36 +00:00
|
|
|
@interface TestExecutor : NSObject <RCTJavaScriptExecutor>
|
|
|
|
|
2015-11-14 18:25:00 +00:00
|
|
|
@property (nonatomic, readonly, copy) NSMutableDictionary<NSString *, id> *injectedStuff;
|
2015-06-06 20:37:36 +00:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation TestExecutor
|
|
|
|
|
2016-03-01 17:44:05 +00:00
|
|
|
@synthesize valid = _valid;
|
|
|
|
|
2015-06-09 22:42:10 +00:00
|
|
|
RCT_EXPORT_MODULE()
|
|
|
|
|
|
|
|
- (void)setUp {}
|
|
|
|
|
2015-06-06 20:37:36 +00:00
|
|
|
- (instancetype)init
|
|
|
|
{
|
|
|
|
if (self = [super init]) {
|
|
|
|
_injectedStuff = [NSMutableDictionary dictionary];
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)isValid
|
|
|
|
{
|
2016-03-01 17:44:05 +00:00
|
|
|
return _valid;
|
2015-06-06 20:37:36 +00:00
|
|
|
}
|
|
|
|
|
2015-12-08 23:57:34 +00:00
|
|
|
- (void)flushedQueue:(RCTJavaScriptCallback)onComplete
|
|
|
|
{
|
|
|
|
onComplete(nil, nil);
|
|
|
|
}
|
|
|
|
|
2015-12-23 22:41:39 +00:00
|
|
|
- (void)callFunctionOnModule:(__unused NSString *)module
|
|
|
|
method:(__unused NSString *)method
|
|
|
|
arguments:(__unused NSArray *)args
|
2015-12-08 23:57:34 +00:00
|
|
|
callback:(RCTJavaScriptCallback)onComplete
|
|
|
|
{
|
|
|
|
onComplete(nil, nil);
|
|
|
|
}
|
|
|
|
|
2015-12-23 22:41:39 +00:00
|
|
|
- (void)invokeCallbackID:(__unused NSNumber *)cbID
|
|
|
|
arguments:(__unused NSArray *)args
|
2015-12-08 23:57:34 +00:00
|
|
|
callback:(RCTJavaScriptCallback)onComplete
|
2015-06-06 20:37:36 +00:00
|
|
|
{
|
|
|
|
onComplete(nil, nil);
|
|
|
|
}
|
|
|
|
|
2015-06-15 14:53:45 +00:00
|
|
|
- (void)executeApplicationScript:(__unused NSString *)script
|
|
|
|
sourceURL:(__unused NSURL *)url
|
2015-06-06 20:37:36 +00:00
|
|
|
onComplete:(RCTJavaScriptCompleteBlock)onComplete
|
|
|
|
{
|
|
|
|
onComplete(nil);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)executeBlockOnJavaScriptQueue:(dispatch_block_t)block
|
|
|
|
{
|
|
|
|
block();
|
|
|
|
}
|
|
|
|
|
2016-07-11 20:16:09 +00:00
|
|
|
- (void)executeAsyncBlockOnJavaScriptQueue:(dispatch_block_t)block
|
|
|
|
{
|
|
|
|
block();
|
|
|
|
}
|
|
|
|
|
2015-06-06 20:37:36 +00:00
|
|
|
- (void)injectJSONText:(NSString *)script
|
|
|
|
asGlobalObjectNamed:(NSString *)objectName
|
|
|
|
callback:(RCTJavaScriptCompleteBlock)onComplete
|
|
|
|
{
|
|
|
|
_injectedStuff[objectName] = script;
|
|
|
|
onComplete(nil);
|
|
|
|
}
|
|
|
|
|
2016-03-01 17:44:05 +00:00
|
|
|
- (void)invalidate
|
|
|
|
{
|
|
|
|
_valid = NO;
|
|
|
|
}
|
2015-06-06 20:37:36 +00:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
2016-03-14 10:45:21 +00:00
|
|
|
// Define a module that is not explicitly registered with RCT_EXPORT_MODULE
|
|
|
|
@interface UnregisteredTestModule : NSObject <RCTBridgeModule>
|
|
|
|
|
|
|
|
@property (nonatomic, assign) BOOL testMethodCalled;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation UnregisteredTestModule
|
|
|
|
|
|
|
|
@synthesize methodQueue = _methodQueue;
|
|
|
|
|
|
|
|
+ (NSString *)moduleName
|
|
|
|
{
|
|
|
|
return @"UnregisteredTestModule";
|
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(testMethod)
|
|
|
|
{
|
|
|
|
_testMethodCalled = YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2015-06-06 20:37:36 +00:00
|
|
|
@interface RCTBridgeTests : XCTestCase <RCTBridgeModule>
|
|
|
|
{
|
|
|
|
RCTBridge *_bridge;
|
2016-01-05 23:29:23 +00:00
|
|
|
__weak TestExecutor *_jsExecutor;
|
|
|
|
|
2015-06-06 20:37:36 +00:00
|
|
|
BOOL _testMethodCalled;
|
2016-03-14 10:45:21 +00:00
|
|
|
UnregisteredTestModule *_unregisteredTestModule;
|
2015-06-06 20:37:36 +00:00
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation RCTBridgeTests
|
|
|
|
|
2015-06-19 11:18:54 +00:00
|
|
|
@synthesize methodQueue = _methodQueue;
|
2015-06-06 20:37:36 +00:00
|
|
|
|
2015-06-19 11:18:54 +00:00
|
|
|
RCT_EXPORT_MODULE(TestModule)
|
2015-06-06 20:37:36 +00:00
|
|
|
|
|
|
|
- (void)setUp
|
|
|
|
{
|
|
|
|
[super setUp];
|
|
|
|
|
2016-03-14 10:45:21 +00:00
|
|
|
_unregisteredTestModule = [UnregisteredTestModule new];
|
2016-07-11 20:16:09 +00:00
|
|
|
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
|
2017-05-06 03:50:47 +00:00
|
|
|
_bridge = [[RCTBridge alloc] initWithBundleURL:[bundle URLForResource:@"RNTesterUnitTestsBundle" withExtension:@"js"]
|
2016-07-07 19:36:56 +00:00
|
|
|
moduleProvider:^{ return @[self, self->_unregisteredTestModule]; }
|
2015-06-06 20:37:36 +00:00
|
|
|
launchOptions:nil];
|
|
|
|
|
|
|
|
_bridge.executorClass = [TestExecutor class];
|
2016-01-05 23:29:23 +00:00
|
|
|
|
2015-06-06 20:37:36 +00:00
|
|
|
// Force to recreate the executor with the new class
|
|
|
|
// - reload: doesn't work here since bridge hasn't loaded yet.
|
|
|
|
[_bridge invalidate];
|
|
|
|
[_bridge setUp];
|
2016-01-05 23:29:23 +00:00
|
|
|
|
2016-03-01 17:44:05 +00:00
|
|
|
_jsExecutor = _bridge.batchedBridge.javaScriptExecutor;
|
2016-01-05 23:29:23 +00:00
|
|
|
XCTAssertNotNil(_jsExecutor);
|
2015-06-06 20:37:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)tearDown
|
|
|
|
{
|
|
|
|
[super tearDown];
|
2015-12-23 22:41:39 +00:00
|
|
|
|
|
|
|
_testMethodCalled = NO;
|
2015-06-06 20:37:36 +00:00
|
|
|
|
2016-01-05 23:29:23 +00:00
|
|
|
[_bridge invalidate];
|
2017-04-21 15:16:17 +00:00
|
|
|
RCT_RUN_RUNLOOP_WHILE(_jsExecutor.isValid);
|
2016-01-05 23:29:23 +00:00
|
|
|
_bridge = nil;
|
|
|
|
}
|
2015-08-07 13:42:34 +00:00
|
|
|
|
2015-06-06 20:37:36 +00:00
|
|
|
- (void)testHookRegistration
|
|
|
|
{
|
2015-08-07 13:42:34 +00:00
|
|
|
NSString *injectedStuff;
|
2017-04-21 15:16:17 +00:00
|
|
|
RCT_RUN_RUNLOOP_WHILE(!(injectedStuff = _jsExecutor.injectedStuff[@"__fbBatchedBridgeConfig"]));
|
2016-01-05 23:29:23 +00:00
|
|
|
XCTAssertNotNil(injectedStuff);
|
2015-08-07 13:42:34 +00:00
|
|
|
|
2015-10-29 01:41:49 +00:00
|
|
|
__block NSNumber *testModuleID = nil;
|
2015-11-14 18:25:00 +00:00
|
|
|
__block NSDictionary<NSString *, id> *testConstants = nil;
|
2015-10-29 01:41:49 +00:00
|
|
|
__block NSNumber *testMethodID = nil;
|
|
|
|
|
|
|
|
NSArray *remoteModuleConfig = RCTJSONParse(injectedStuff, NULL)[@"remoteModuleConfig"];
|
|
|
|
[remoteModuleConfig enumerateObjectsUsingBlock:^(id moduleConfig, NSUInteger i, BOOL *stop) {
|
2016-09-05 14:32:20 +00:00
|
|
|
if ([moduleConfig isKindOfClass:[NSArray class]] && [moduleConfig[kNameIndex] isEqualToString:@"TestModule"]) {
|
2015-10-29 01:41:49 +00:00
|
|
|
testModuleID = @(i);
|
2016-09-05 14:32:20 +00:00
|
|
|
testConstants = moduleConfig[kConstantsIndex];
|
|
|
|
testMethodID = @([moduleConfig[kMethodsIndex] indexOfObject:@"testMethod"]);
|
2015-10-29 01:41:49 +00:00
|
|
|
*stop = YES;
|
|
|
|
}
|
|
|
|
}];
|
2015-06-06 20:37:36 +00:00
|
|
|
|
|
|
|
XCTAssertNotNil(remoteModuleConfig);
|
2015-10-29 01:41:49 +00:00
|
|
|
XCTAssertNotNil(testModuleID);
|
|
|
|
XCTAssertNotNil(testConstants);
|
|
|
|
XCTAssertEqualObjects(testConstants[@"eleventyMillion"], @42);
|
|
|
|
XCTAssertNotNil(testMethodID);
|
2015-06-06 20:37:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)testCallNativeMethod
|
|
|
|
{
|
2015-08-07 13:42:34 +00:00
|
|
|
NSString *injectedStuff;
|
2017-04-21 15:16:17 +00:00
|
|
|
RCT_RUN_RUNLOOP_WHILE(!(injectedStuff = _jsExecutor.injectedStuff[@"__fbBatchedBridgeConfig"]));
|
2016-01-05 23:29:23 +00:00
|
|
|
XCTAssertNotNil(injectedStuff);
|
2015-08-07 13:42:34 +00:00
|
|
|
|
2015-10-29 01:41:49 +00:00
|
|
|
__block NSNumber *testModuleID = nil;
|
|
|
|
__block NSNumber *testMethodID = nil;
|
|
|
|
|
|
|
|
NSArray *remoteModuleConfig = RCTJSONParse(injectedStuff, NULL)[@"remoteModuleConfig"];
|
|
|
|
[remoteModuleConfig enumerateObjectsUsingBlock:^(id moduleConfig, NSUInteger i, __unused BOOL *stop) {
|
2016-09-05 14:32:20 +00:00
|
|
|
if ([moduleConfig isKindOfClass:[NSArray class]] && [moduleConfig[kNameIndex] isEqualToString:@"TestModule"]) {
|
2015-10-29 01:41:49 +00:00
|
|
|
testModuleID = @(i);
|
2016-09-05 14:32:20 +00:00
|
|
|
testMethodID = @([moduleConfig[kMethodsIndex] indexOfObject:@"testMethod"]);
|
2015-10-29 01:41:49 +00:00
|
|
|
*stop = YES;
|
|
|
|
}
|
|
|
|
}];
|
2015-06-06 20:37:36 +00:00
|
|
|
|
2015-12-23 22:41:39 +00:00
|
|
|
XCTAssertNotNil(testModuleID);
|
|
|
|
XCTAssertNotNil(testMethodID);
|
|
|
|
|
2015-06-06 20:37:36 +00:00
|
|
|
NSArray *args = @[@1234, @5678, @"stringy", @{@"a": @1}, @42];
|
2015-12-23 22:41:39 +00:00
|
|
|
NSArray *buffer = @[@[testModuleID], @[testMethodID], @[args]];
|
2015-06-06 20:37:36 +00:00
|
|
|
|
2016-09-05 14:32:20 +00:00
|
|
|
[_bridge.batchedBridge handleBuffer:buffer batchEnded:YES];
|
2015-06-06 20:37:36 +00:00
|
|
|
|
2015-06-19 11:18:54 +00:00
|
|
|
dispatch_sync(_methodQueue, ^{
|
2015-06-06 20:37:36 +00:00
|
|
|
// clear the queue
|
2016-07-07 19:36:56 +00:00
|
|
|
XCTAssertTrue(self->_testMethodCalled);
|
2015-06-06 20:37:36 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-03-14 10:45:21 +00:00
|
|
|
- (void)testCallUnregisteredModuleMethod
|
|
|
|
{
|
|
|
|
NSString *injectedStuff;
|
2017-04-21 15:16:17 +00:00
|
|
|
RCT_RUN_RUNLOOP_WHILE(!(injectedStuff = _jsExecutor.injectedStuff[@"__fbBatchedBridgeConfig"]));
|
2016-03-14 10:45:21 +00:00
|
|
|
XCTAssertNotNil(injectedStuff);
|
2016-04-06 16:20:39 +00:00
|
|
|
|
2016-03-14 10:45:21 +00:00
|
|
|
__block NSNumber *testModuleID = nil;
|
|
|
|
__block NSNumber *testMethodID = nil;
|
2016-04-06 16:20:39 +00:00
|
|
|
|
2016-03-14 10:45:21 +00:00
|
|
|
NSArray *remoteModuleConfig = RCTJSONParse(injectedStuff, NULL)[@"remoteModuleConfig"];
|
|
|
|
[remoteModuleConfig enumerateObjectsUsingBlock:^(id moduleConfig, NSUInteger i, __unused BOOL *stop) {
|
2016-09-05 14:32:20 +00:00
|
|
|
if ([moduleConfig isKindOfClass:[NSArray class]] && [moduleConfig[kNameIndex] isEqualToString:@"UnregisteredTestModule"]) {
|
2016-03-14 10:45:21 +00:00
|
|
|
testModuleID = @(i);
|
2016-09-05 14:32:20 +00:00
|
|
|
testMethodID = @([moduleConfig[kMethodsIndex] indexOfObject:@"testMethod"]);
|
2016-03-14 10:45:21 +00:00
|
|
|
*stop = YES;
|
|
|
|
}
|
|
|
|
}];
|
2016-04-06 16:20:39 +00:00
|
|
|
|
2016-03-14 10:45:21 +00:00
|
|
|
XCTAssertNotNil(testModuleID);
|
|
|
|
XCTAssertNotNil(testMethodID);
|
2016-04-06 16:20:39 +00:00
|
|
|
|
2016-03-14 10:45:21 +00:00
|
|
|
NSArray *args = @[];
|
|
|
|
NSArray *buffer = @[@[testModuleID], @[testMethodID], @[args]];
|
2016-04-06 16:20:39 +00:00
|
|
|
|
2016-09-05 14:32:20 +00:00
|
|
|
[_bridge.batchedBridge handleBuffer:buffer batchEnded:YES];
|
2016-04-06 16:20:39 +00:00
|
|
|
|
2016-03-14 10:45:21 +00:00
|
|
|
dispatch_sync(_unregisteredTestModule.methodQueue, ^{
|
2016-07-07 19:36:56 +00:00
|
|
|
XCTAssertTrue(self->_unregisteredTestModule.testMethodCalled);
|
2016-03-14 10:45:21 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-06-06 20:37:36 +00:00
|
|
|
- (void)DISABLED_testBadArgumentsCount
|
|
|
|
{
|
|
|
|
//NSArray *bufferWithMissingArgument = @[@[@1], @[@0], @[@[@1234, @5678, @"stringy", @{@"a": @1}/*, @42*/]], @[], @1234567];
|
2015-10-19 15:02:50 +00:00
|
|
|
//[_bridge handleBuffer:bufferWithMissingArgument];
|
2015-06-06 20:37:36 +00:00
|
|
|
NSLog(@"WARNING: testBadArgumentsCount is temporarily disabled until we have a better way to test cases that we expect to trigger redbox errors");
|
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(testMethod:(NSInteger)integer
|
2015-08-01 08:44:05 +00:00
|
|
|
number:(nonnull NSNumber *)number
|
2015-06-06 20:37:36 +00:00
|
|
|
string:(NSString *)string
|
|
|
|
dictionary:(NSDictionary *)dict
|
|
|
|
callback:(RCTResponseSenderBlock)callback)
|
|
|
|
{
|
|
|
|
_testMethodCalled = YES;
|
|
|
|
|
|
|
|
XCTAssertTrue(integer == 1234);
|
|
|
|
XCTAssertEqualObjects(number, @5678);
|
|
|
|
XCTAssertEqualObjects(string, @"stringy");
|
|
|
|
XCTAssertEqualObjects(dict, @{@"a": @1});
|
|
|
|
XCTAssertNotNil(callback);
|
|
|
|
}
|
|
|
|
|
2015-11-14 18:25:00 +00:00
|
|
|
- (NSDictionary<NSString *, id> *)constantsToExport
|
2015-06-06 20:37:36 +00:00
|
|
|
{
|
|
|
|
return @{@"eleventyMillion": @42};
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|