ios: allow using RCTBridgeDelegate in test runs

Summary: This allows callers of RCTTestRunner to provide custom bridge delegate for custom test run handling.

Reviewed By: jdthomas

Differential Revision: D9373947

fbshipit-source-id: fcc9080bd6962d6a0497aee85e900853c4727c6d
This commit is contained in:
Kevin Gozali 2018-08-17 13:10:00 -07:00 committed by Facebook Github Bot
parent 625c54d406
commit b4f02262c8
2 changed files with 64 additions and 9 deletions

View File

@ -34,7 +34,12 @@
[[RCTTestRunner alloc] initWithApp:(app__) \ [[RCTTestRunner alloc] initWithApp:(app__) \
referenceDirectory:@FB_REFERENCE_IMAGE_DIR \ referenceDirectory:@FB_REFERENCE_IMAGE_DIR \
moduleProvider:(moduleProvider__) \ moduleProvider:(moduleProvider__) \
scriptURL: scriptURL__] scriptURL: scriptURL__]
#define RCTInitRunnerForAppWithDelegate(app__, bridgeDelegate__) \
[[RCTTestRunner alloc] initWithApp:(app__) \
referenceDirectory:@FB_REFERENCE_IMAGE_DIR \
bridgeDelegate:(bridgeDelegate__)]
@protocol RCTBridgeModule; @protocol RCTBridgeModule;
@class RCTBridge; @class RCTBridge;
@ -64,11 +69,25 @@
* @param app The path to the app bundle without suffixes, e.g. IntegrationTests/IntegrationTestsApp * @param app The path to the app bundle without suffixes, e.g. IntegrationTests/IntegrationTestsApp
* @param referenceDirectory The path for snapshot references images. * @param referenceDirectory The path for snapshot references images.
* @param block A block that returns an array of extra modules to be used by the test runner. * @param block A block that returns an array of extra modules to be used by the test runner.
* @param scriptURL URL to the JS bundle to use.
* @param bridgeDelegate Custom delegate for bridge activities.
*/ */
- (instancetype)initWithApp:(NSString *)app - (instancetype)initWithApp:(NSString *)app
referenceDirectory:(NSString *)referenceDirectory referenceDirectory:(NSString *)referenceDirectory
moduleProvider:(RCTBridgeModuleListProvider)block moduleProvider:(RCTBridgeModuleListProvider)block
scriptURL:(NSURL *)scriptURL NS_DESIGNATED_INITIALIZER; scriptURL:(NSURL *)scriptURL
bridgeDelegate:(id<RCTBridgeDelegate>)bridgeDelegate NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithApp:(NSString *)app
referenceDirectory:(NSString *)referenceDirectory
moduleProvider:(RCTBridgeModuleListProvider)block
scriptURL:(NSURL *)scriptURL;
- (instancetype)initWithApp:(NSString *)app
referenceDirectory:(NSString *)referenceDirectory
bridgeDelegate:(id<RCTBridgeDelegate>)bridgeDelegate;
- (NSURL *)defaultScriptURL;
/** /**
* Simplest runTest function simply mounts the specified JS module with no * Simplest runTest function simply mounts the specified JS module with no

View File

@ -25,12 +25,37 @@ static const NSTimeInterval kTestTimeoutSeconds = 120;
FBSnapshotTestController *_testController; FBSnapshotTestController *_testController;
RCTBridgeModuleListProvider _moduleProvider; RCTBridgeModuleListProvider _moduleProvider;
NSString *_appPath; NSString *_appPath;
__weak id<RCTBridgeDelegate> _bridgeDelegate;
} }
- (instancetype)initWithApp:(NSString *)app - (instancetype)initWithApp:(NSString *)app
referenceDirectory:(NSString *)referenceDirectory referenceDirectory:(NSString *)referenceDirectory
moduleProvider:(RCTBridgeModuleListProvider)block moduleProvider:(RCTBridgeModuleListProvider)block
scriptURL:(NSURL *)scriptURL scriptURL:(NSURL *)scriptURL
{
return [self initWithApp:app
referenceDirectory:referenceDirectory
moduleProvider:block
scriptURL:scriptURL
bridgeDelegate:nil];
}
- (instancetype)initWithApp:(NSString *)app
referenceDirectory:(NSString *)referenceDirectory
bridgeDelegate:(id<RCTBridgeDelegate>)bridgeDelegate
{
return [self initWithApp:app
referenceDirectory:referenceDirectory
moduleProvider:nil
scriptURL:nil
bridgeDelegate:bridgeDelegate];
}
- (instancetype)initWithApp:(NSString *)app
referenceDirectory:(NSString *)referenceDirectory
moduleProvider:(RCTBridgeModuleListProvider)block
scriptURL:(NSURL *)scriptURL
bridgeDelegate:(id<RCTBridgeDelegate>)bridgeDelegate
{ {
RCTAssertParam(app); RCTAssertParam(app);
RCTAssertParam(referenceDirectory); RCTAssertParam(referenceDirectory);
@ -46,10 +71,11 @@ static const NSTimeInterval kTestTimeoutSeconds = 120;
_testController.referenceImagesDirectory = referenceDirectory; _testController.referenceImagesDirectory = referenceDirectory;
_moduleProvider = [block copy]; _moduleProvider = [block copy];
_appPath = app; _appPath = app;
_bridgeDelegate = bridgeDelegate;
if (scriptURL != nil) { if (scriptURL != nil) {
_scriptURL = scriptURL; _scriptURL = scriptURL;
} else { } else if (!_bridgeDelegate) {
[self updateScript]; [self updateScript];
} }
} }
@ -58,13 +84,18 @@ static const NSTimeInterval kTestTimeoutSeconds = 120;
RCT_NOT_IMPLEMENTED(- (instancetype)init) RCT_NOT_IMPLEMENTED(- (instancetype)init)
- (void)updateScript - (NSURL *)defaultScriptURL
{ {
if (getenv("CI_USE_PACKAGER") || _useBundler) { if (getenv("CI_USE_PACKAGER") || _useBundler) {
_scriptURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://localhost:8081/%@.bundle?platform=ios&dev=true", _appPath]]; return [NSURL URLWithString:[NSString stringWithFormat:@"http://localhost:8081/%@.bundle?platform=ios&dev=true", _appPath]];
} else { } else {
_scriptURL = [[NSBundle bundleForClass:[RCTBridge class]] URLForResource:@"main" withExtension:@"jsbundle"]; return [[NSBundle bundleForClass:[RCTBridge class]] URLForResource:@"main" withExtension:@"jsbundle"];
} }
}
- (void)updateScript
{
_scriptURL = [self defaultScriptURL];
RCTAssert(_scriptURL != nil, @"No scriptURL set"); RCTAssert(_scriptURL != nil, @"No scriptURL set");
} }
@ -129,9 +160,14 @@ expectErrorBlock:(BOOL(^)(NSString *error))expectErrorBlock
}); });
@autoreleasepool { @autoreleasepool {
RCTBridge *bridge = [[RCTBridge alloc] initWithBundleURL:_scriptURL RCTBridge *bridge;
moduleProvider:_moduleProvider if (_bridgeDelegate) {
launchOptions:nil]; bridge = [[RCTBridge alloc] initWithDelegate:_bridgeDelegate launchOptions:nil];
} else {
bridge= [[RCTBridge alloc] initWithBundleURL:_scriptURL
moduleProvider:_moduleProvider
launchOptions:nil];
}
[bridge.devSettings setIsDebuggingRemotely:_useJSDebugger]; [bridge.devSettings setIsDebuggingRemotely:_useJSDebugger];
batchedBridge = [bridge batchedBridge]; batchedBridge = [bridge batchedBridge];