2015-03-23 20:28:42 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
*
|
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-03-23 20:28:42 +00:00
|
|
|
*/
|
2015-03-14 01:32:38 +00:00
|
|
|
|
|
|
|
#import "RCTTestRunner.h"
|
|
|
|
|
2016-11-23 15:47:52 +00:00
|
|
|
#import <React/RCTAssert.h>
|
2017-08-09 16:39:20 +00:00
|
|
|
#import <React/RCTBridge+Private.h>
|
2017-09-22 01:25:37 +00:00
|
|
|
#import <React/RCTDevSettings.h>
|
2016-11-23 15:47:52 +00:00
|
|
|
#import <React/RCTLog.h>
|
2016-11-24 17:44:51 +00:00
|
|
|
#import <React/RCTRootView.h>
|
2018-04-24 09:00:09 +00:00
|
|
|
#import <React/RCTUIManager.h>
|
2016-11-23 15:47:52 +00:00
|
|
|
#import <React/RCTUtils.h>
|
|
|
|
|
2015-03-24 17:20:13 +00:00
|
|
|
#import "FBSnapshotTestController.h"
|
2015-03-14 01:32:38 +00:00
|
|
|
#import "RCTTestModule.h"
|
|
|
|
|
2016-10-16 22:37:45 +00:00
|
|
|
static const NSTimeInterval kTestTimeoutSeconds = 120;
|
2015-05-04 17:35:49 +00:00
|
|
|
|
2015-03-14 01:32:38 +00:00
|
|
|
@implementation RCTTestRunner
|
2015-03-24 17:20:13 +00:00
|
|
|
{
|
2015-04-18 17:43:20 +00:00
|
|
|
FBSnapshotTestController *_testController;
|
2017-04-07 18:11:03 +00:00
|
|
|
RCTBridgeModuleListProvider _moduleProvider;
|
2017-09-22 01:25:37 +00:00
|
|
|
NSString *_appPath;
|
2015-03-24 17:20:13 +00:00
|
|
|
}
|
2015-03-14 01:32:38 +00:00
|
|
|
|
2015-07-07 23:19:40 +00:00
|
|
|
- (instancetype)initWithApp:(NSString *)app
|
|
|
|
referenceDirectory:(NSString *)referenceDirectory
|
2017-04-07 18:11:03 +00:00
|
|
|
moduleProvider:(RCTBridgeModuleListProvider)block
|
2017-11-12 21:30:13 +00:00
|
|
|
scriptURL:(NSURL *)scriptURL
|
2015-03-14 01:32:38 +00:00
|
|
|
{
|
2015-06-15 14:53:45 +00:00
|
|
|
RCTAssertParam(app);
|
2015-07-07 23:19:40 +00:00
|
|
|
RCTAssertParam(referenceDirectory);
|
2015-06-15 14:53:45 +00:00
|
|
|
|
2015-03-24 17:20:13 +00:00
|
|
|
if ((self = [super init])) {
|
2015-11-02 18:58:55 +00:00
|
|
|
if (!referenceDirectory.length) {
|
|
|
|
referenceDirectory = [[NSBundle bundleForClass:self.class].resourcePath stringByAppendingPathComponent:@"ReferenceImages"];
|
|
|
|
}
|
2015-07-07 23:19:40 +00:00
|
|
|
|
2015-03-24 17:20:13 +00:00
|
|
|
NSString *sanitizedAppName = [app stringByReplacingOccurrencesOfString:@"/" withString:@"-"];
|
|
|
|
sanitizedAppName = [sanitizedAppName stringByReplacingOccurrencesOfString:@"\\" withString:@"-"];
|
2015-04-18 17:43:20 +00:00
|
|
|
_testController = [[FBSnapshotTestController alloc] initWithTestName:sanitizedAppName];
|
2015-07-07 23:19:40 +00:00
|
|
|
_testController.referenceImagesDirectory = referenceDirectory;
|
|
|
|
_moduleProvider = [block copy];
|
2017-09-22 01:25:37 +00:00
|
|
|
_appPath = app;
|
2017-11-12 21:30:13 +00:00
|
|
|
|
|
|
|
if (scriptURL != nil) {
|
|
|
|
_scriptURL = scriptURL;
|
|
|
|
} else {
|
|
|
|
[self updateScript];
|
|
|
|
}
|
2015-03-14 01:32:38 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2015-08-24 10:14:33 +00:00
|
|
|
RCT_NOT_IMPLEMENTED(- (instancetype)init)
|
2015-06-15 14:53:45 +00:00
|
|
|
|
2017-09-22 01:25:37 +00:00
|
|
|
- (void)updateScript
|
|
|
|
{
|
|
|
|
if (getenv("CI_USE_PACKAGER") || _useBundler) {
|
|
|
|
_scriptURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://localhost:8081/%@.bundle?platform=ios&dev=true", _appPath]];
|
|
|
|
} else {
|
|
|
|
_scriptURL = [[NSBundle bundleForClass:[RCTBridge class]] URLForResource:@"main" withExtension:@"jsbundle"];
|
|
|
|
}
|
|
|
|
RCTAssert(_scriptURL != nil, @"No scriptURL set");
|
|
|
|
}
|
|
|
|
|
2015-03-24 17:20:13 +00:00
|
|
|
- (void)setRecordMode:(BOOL)recordMode
|
|
|
|
{
|
2015-04-18 17:43:20 +00:00
|
|
|
_testController.recordMode = recordMode;
|
2015-03-24 17:20:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)recordMode
|
2015-03-14 01:32:38 +00:00
|
|
|
{
|
2015-04-18 17:43:20 +00:00
|
|
|
return _testController.recordMode;
|
2015-03-14 01:32:38 +00:00
|
|
|
}
|
|
|
|
|
2017-09-22 01:25:37 +00:00
|
|
|
- (void)setUseBundler:(BOOL)useBundler
|
|
|
|
{
|
|
|
|
_useBundler = useBundler;
|
|
|
|
[self updateScript];
|
|
|
|
}
|
|
|
|
|
2015-03-24 17:20:13 +00:00
|
|
|
- (void)runTest:(SEL)test module:(NSString *)moduleName
|
2015-03-14 01:32:38 +00:00
|
|
|
{
|
2015-11-13 16:34:26 +00:00
|
|
|
[self runTest:test module:moduleName initialProps:nil configurationBlock:nil expectErrorBlock:nil];
|
2015-03-24 17:20:13 +00:00
|
|
|
}
|
|
|
|
|
2015-11-14 18:25:00 +00:00
|
|
|
- (void)runTest:(SEL)test module:(NSString *)moduleName
|
|
|
|
initialProps:(NSDictionary<NSString *, id> *)initialProps
|
|
|
|
configurationBlock:(void(^)(RCTRootView *rootView))configurationBlock
|
2015-11-13 16:34:26 +00:00
|
|
|
{
|
|
|
|
[self runTest:test module:moduleName initialProps:initialProps configurationBlock:configurationBlock expectErrorBlock:nil];
|
|
|
|
}
|
|
|
|
|
2015-11-14 18:25:00 +00:00
|
|
|
- (void)runTest:(SEL)test module:(NSString *)moduleName
|
|
|
|
initialProps:(NSDictionary<NSString *, id> *)initialProps
|
|
|
|
configurationBlock:(void(^)(RCTRootView *rootView))configurationBlock
|
|
|
|
expectErrorRegex:(NSString *)errorRegex
|
2015-03-24 17:20:13 +00:00
|
|
|
{
|
2015-11-13 16:34:26 +00:00
|
|
|
BOOL(^expectErrorBlock)(NSString *error) = ^BOOL(NSString *error){
|
2015-04-11 22:08:00 +00:00
|
|
|
return [error rangeOfString:errorRegex options:NSRegularExpressionSearch].location != NSNotFound;
|
2015-11-13 16:34:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
[self runTest:test module:moduleName initialProps:initialProps configurationBlock:configurationBlock expectErrorBlock:expectErrorBlock];
|
2015-03-14 01:32:38 +00:00
|
|
|
}
|
|
|
|
|
2015-11-14 18:25:00 +00:00
|
|
|
- (void)runTest:(SEL)test module:(NSString *)moduleName
|
|
|
|
initialProps:(NSDictionary<NSString *, id> *)initialProps
|
|
|
|
configurationBlock:(void(^)(RCTRootView *rootView))configurationBlock
|
|
|
|
expectErrorBlock:(BOOL(^)(NSString *error))expectErrorBlock
|
2015-03-14 01:32:38 +00:00
|
|
|
{
|
2017-08-09 16:39:20 +00:00
|
|
|
__weak RCTBridge *batchedBridge;
|
2018-04-24 09:00:09 +00:00
|
|
|
NSNumber *rootTag;
|
2017-08-09 16:39:20 +00:00
|
|
|
|
2015-09-04 10:21:57 +00:00
|
|
|
@autoreleasepool {
|
2017-12-09 00:38:27 +00:00
|
|
|
__block NSMutableArray<NSString *> *errors = nil;
|
2017-04-13 11:38:13 +00:00
|
|
|
RCTLogFunction defaultLogFunction = RCTGetLogFunction();
|
2015-11-11 14:42:27 +00:00
|
|
|
RCTSetLogFunction(^(RCTLogLevel level, RCTLogSource source, NSString *fileName, NSNumber *lineNumber, NSString *message) {
|
2017-08-09 16:39:20 +00:00
|
|
|
defaultLogFunction(level, source, fileName, lineNumber, message);
|
2015-09-04 10:21:57 +00:00
|
|
|
if (level >= RCTLogLevelError) {
|
2017-12-09 00:38:27 +00:00
|
|
|
if (errors == nil) {
|
|
|
|
errors = [NSMutableArray new];
|
|
|
|
}
|
|
|
|
[errors addObject:message];
|
2015-09-04 10:21:57 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
RCTBridge *bridge = [[RCTBridge alloc] initWithBundleURL:_scriptURL
|
|
|
|
moduleProvider:_moduleProvider
|
|
|
|
launchOptions:nil];
|
2017-09-22 01:25:37 +00:00
|
|
|
[bridge.devSettings setIsDebuggingRemotely:_useJSDebugger];
|
2017-08-09 16:39:20 +00:00
|
|
|
batchedBridge = [bridge batchedBridge];
|
|
|
|
|
2018-04-24 09:00:09 +00:00
|
|
|
UIViewController *vc = RCTSharedApplication().delegate.window.rootViewController;
|
|
|
|
vc.view = [UIView new];
|
2015-09-04 10:21:57 +00:00
|
|
|
|
2018-04-24 09:00:09 +00:00
|
|
|
RCTTestModule *testModule = [bridge moduleForClass:[RCTTestModule class]];
|
2015-09-04 10:21:57 +00:00
|
|
|
RCTAssert(_testController != nil, @"_testController should not be nil");
|
|
|
|
testModule.controller = _testController;
|
|
|
|
testModule.testSelector = test;
|
2016-08-17 17:37:01 +00:00
|
|
|
testModule.testSuffix = _testSuffix;
|
2015-09-04 10:21:57 +00:00
|
|
|
|
2018-04-24 09:00:09 +00:00
|
|
|
@autoreleasepool {
|
|
|
|
// The rootView needs to be deallocated after this @autoreleasepool block exits.
|
|
|
|
RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge moduleName:moduleName initialProperties:initialProps];
|
|
|
|
#if TARGET_OS_TV
|
|
|
|
rootView.frame = CGRectMake(0, 0, 1920, 1080); // Standard screen size for tvOS
|
|
|
|
#else
|
|
|
|
rootView.frame = CGRectMake(0, 0, 320, 2000); // Constant size for testing on multiple devices
|
|
|
|
#endif
|
2015-09-04 10:21:57 +00:00
|
|
|
|
2018-04-24 09:00:09 +00:00
|
|
|
rootTag = rootView.reactTag;
|
|
|
|
testModule.view = rootView;
|
2015-11-13 16:34:26 +00:00
|
|
|
|
2018-04-24 09:00:09 +00:00
|
|
|
[vc.view addSubview:rootView]; // Add as subview so it doesn't get resized
|
|
|
|
|
|
|
|
if (configurationBlock) {
|
|
|
|
configurationBlock(rootView);
|
|
|
|
}
|
2015-08-19 10:18:04 +00:00
|
|
|
|
2018-04-24 09:00:09 +00:00
|
|
|
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:kTestTimeoutSeconds];
|
|
|
|
while (date.timeIntervalSinceNow > 0 && testModule.status == RCTTestStatusPending && errors == nil) {
|
|
|
|
[[NSRunLoop mainRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
|
|
|
|
[[NSRunLoop mainRunLoop] runMode:NSRunLoopCommonModes beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
|
|
|
|
}
|
|
|
|
|
|
|
|
[rootView removeFromSuperview];
|
|
|
|
testModule.view = nil;
|
|
|
|
}
|
2015-07-07 23:19:40 +00:00
|
|
|
|
2017-04-13 11:38:13 +00:00
|
|
|
RCTSetLogFunction(defaultLogFunction);
|
2015-03-26 01:59:42 +00:00
|
|
|
|
2017-06-06 06:22:27 +00:00
|
|
|
#if RCT_DEV
|
2015-11-03 22:45:46 +00:00
|
|
|
NSArray<UIView *> *nonLayoutSubviews = [vc.view.subviews filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id subview, NSDictionary *bindings) {
|
2015-09-04 10:21:57 +00:00
|
|
|
return ![NSStringFromClass([subview class]) isEqualToString:@"_UILayoutGuide"];
|
|
|
|
}]];
|
2015-04-18 17:43:20 +00:00
|
|
|
|
2017-06-06 06:22:27 +00:00
|
|
|
RCTAssert(nonLayoutSubviews.count == 0, @"There shouldn't be any other views: %@", nonLayoutSubviews);
|
|
|
|
#endif
|
2017-08-25 16:47:51 +00:00
|
|
|
|
2015-09-04 10:21:57 +00:00
|
|
|
if (expectErrorBlock) {
|
2017-12-09 00:38:27 +00:00
|
|
|
RCTAssert(expectErrorBlock(errors[0]), @"Expected an error but the first one was missing or did not match.");
|
2015-09-04 10:21:57 +00:00
|
|
|
} else {
|
2017-12-09 00:38:27 +00:00
|
|
|
RCTAssert(errors == nil, @"RedBox errors: %@", errors);
|
2015-09-04 10:21:57 +00:00
|
|
|
RCTAssert(testModule.status != RCTTestStatusPending, @"Test didn't finish within %0.f seconds", kTestTimeoutSeconds);
|
|
|
|
RCTAssert(testModule.status == RCTTestStatusPassed, @"Test failed");
|
|
|
|
}
|
2017-08-09 16:39:20 +00:00
|
|
|
|
2018-04-24 09:00:09 +00:00
|
|
|
// Wait for the rootView to be deallocated completely before invalidating the bridge.
|
|
|
|
RCTUIManager *uiManager = [bridge moduleForClass:[RCTUIManager class]];
|
|
|
|
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:5];
|
|
|
|
while (date.timeIntervalSinceNow > 0 && [uiManager viewForReactTag:rootTag]) {
|
|
|
|
[[NSRunLoop mainRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
|
|
|
|
[[NSRunLoop mainRunLoop] runMode:NSRunLoopCommonModes beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
|
|
|
|
}
|
|
|
|
RCTAssert([uiManager viewForReactTag:rootTag] == nil, @"RootView should have been deallocated after removed.");
|
|
|
|
|
2015-09-04 10:21:57 +00:00
|
|
|
[bridge invalidate];
|
|
|
|
}
|
2017-08-09 16:39:20 +00:00
|
|
|
|
2018-04-24 09:00:09 +00:00
|
|
|
// Wait for the bridge to disappear before continuing to the next test.
|
2017-08-25 16:47:51 +00:00
|
|
|
NSDate *invalidateTimeout = [NSDate dateWithTimeIntervalSinceNow:30];
|
2017-08-09 16:39:20 +00:00
|
|
|
while (invalidateTimeout.timeIntervalSinceNow > 0 && batchedBridge != nil) {
|
|
|
|
[[NSRunLoop mainRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
|
|
|
|
[[NSRunLoop mainRunLoop] runMode:NSRunLoopCommonModes beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
|
|
|
|
}
|
2018-04-24 09:00:09 +00:00
|
|
|
RCTAssert(batchedBridge == nil, @"Bridge should be deallocated after the test");
|
2015-03-14 01:32:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|