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__) \
referenceDirectory:@FB_REFERENCE_IMAGE_DIR \
moduleProvider:(moduleProvider__) \
scriptURL: scriptURL__]
scriptURL: scriptURL__]
#define RCTInitRunnerForAppWithDelegate(app__, bridgeDelegate__) \
[[RCTTestRunner alloc] initWithApp:(app__) \
referenceDirectory:@FB_REFERENCE_IMAGE_DIR \
bridgeDelegate:(bridgeDelegate__)]
@protocol RCTBridgeModule;
@class RCTBridge;
@ -64,11 +69,25 @@
* @param app The path to the app bundle without suffixes, e.g. IntegrationTests/IntegrationTestsApp
* @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 scriptURL URL to the JS bundle to use.
* @param bridgeDelegate Custom delegate for bridge activities.
*/
- (instancetype)initWithApp:(NSString *)app
referenceDirectory:(NSString *)referenceDirectory
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

View File

@ -25,12 +25,37 @@ static const NSTimeInterval kTestTimeoutSeconds = 120;
FBSnapshotTestController *_testController;
RCTBridgeModuleListProvider _moduleProvider;
NSString *_appPath;
__weak id<RCTBridgeDelegate> _bridgeDelegate;
}
- (instancetype)initWithApp:(NSString *)app
referenceDirectory:(NSString *)referenceDirectory
moduleProvider:(RCTBridgeModuleListProvider)block
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(referenceDirectory);
@ -46,10 +71,11 @@ static const NSTimeInterval kTestTimeoutSeconds = 120;
_testController.referenceImagesDirectory = referenceDirectory;
_moduleProvider = [block copy];
_appPath = app;
_bridgeDelegate = bridgeDelegate;
if (scriptURL != nil) {
_scriptURL = scriptURL;
} else {
} else if (!_bridgeDelegate) {
[self updateScript];
}
}
@ -58,13 +84,18 @@ static const NSTimeInterval kTestTimeoutSeconds = 120;
RCT_NOT_IMPLEMENTED(- (instancetype)init)
- (void)updateScript
- (NSURL *)defaultScriptURL
{
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 {
_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");
}
@ -129,9 +160,14 @@ expectErrorBlock:(BOOL(^)(NSString *error))expectErrorBlock
});
@autoreleasepool {
RCTBridge *bridge = [[RCTBridge alloc] initWithBundleURL:_scriptURL
moduleProvider:_moduleProvider
launchOptions:nil];
RCTBridge *bridge;
if (_bridgeDelegate) {
bridge = [[RCTBridge alloc] initWithDelegate:_bridgeDelegate launchOptions:nil];
} else {
bridge= [[RCTBridge alloc] initWithBundleURL:_scriptURL
moduleProvider:_moduleProvider
launchOptions:nil];
}
[bridge.devSettings setIsDebuggingRemotely:_useJSDebugger];
batchedBridge = [bridge batchedBridge];