2015-03-23 22:07:33 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* 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-03-14 17:52:40 +00:00
|
|
|
|
|
|
|
#import "RCTTestRunner.h"
|
|
|
|
|
2015-03-25 02:34:12 +00:00
|
|
|
#import "FBSnapshotTestController.h"
|
2015-03-14 17:52:40 +00:00
|
|
|
#import "RCTRedBox.h"
|
|
|
|
#import "RCTRootView.h"
|
|
|
|
#import "RCTTestModule.h"
|
|
|
|
#import "RCTUtils.h"
|
|
|
|
|
2015-03-17 20:42:44 +00:00
|
|
|
#define TIMEOUT_SECONDS 240
|
2015-03-14 17:52:40 +00:00
|
|
|
|
|
|
|
@implementation RCTTestRunner
|
2015-03-25 02:34:12 +00:00
|
|
|
{
|
|
|
|
FBSnapshotTestController *_snapshotController;
|
|
|
|
}
|
2015-03-14 17:52:40 +00:00
|
|
|
|
2015-03-25 02:34:12 +00:00
|
|
|
- (instancetype)initWithApp:(NSString *)app referenceDir:(NSString *)referenceDir
|
2015-03-14 17:52:40 +00:00
|
|
|
{
|
2015-03-25 02:34:12 +00:00
|
|
|
if ((self = [super init])) {
|
|
|
|
NSString *sanitizedAppName = [app stringByReplacingOccurrencesOfString:@"/" withString:@"-"];
|
|
|
|
sanitizedAppName = [sanitizedAppName stringByReplacingOccurrencesOfString:@"\\" withString:@"-"];
|
|
|
|
_snapshotController = [[FBSnapshotTestController alloc] initWithTestName:sanitizedAppName];
|
|
|
|
_snapshotController.referenceImagesDirectory = referenceDir;
|
2015-03-14 17:52:40 +00:00
|
|
|
_script = [NSString stringWithFormat:@"http://localhost:8081/%@.includeRequire.runModule.bundle?dev=true", app];
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2015-03-25 02:34:12 +00:00
|
|
|
- (void)setRecordMode:(BOOL)recordMode
|
|
|
|
{
|
|
|
|
_snapshotController.recordMode = recordMode;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)recordMode
|
2015-03-14 17:52:40 +00:00
|
|
|
{
|
2015-03-25 02:34:12 +00:00
|
|
|
return _snapshotController.recordMode;
|
2015-03-14 17:52:40 +00:00
|
|
|
}
|
|
|
|
|
2015-03-25 02:34:12 +00:00
|
|
|
- (void)runTest:(SEL)test module:(NSString *)moduleName
|
2015-03-14 17:52:40 +00:00
|
|
|
{
|
2015-03-25 02:34:12 +00:00
|
|
|
[self runTest:test module:moduleName initialProps:nil expectErrorBlock:nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)runTest:(SEL)test module:(NSString *)moduleName initialProps:(NSDictionary *)initialProps expectErrorRegex:(NSRegularExpression *)errorRegex
|
|
|
|
{
|
|
|
|
[self runTest:test module:moduleName initialProps:initialProps expectErrorBlock:^BOOL(NSString *error){
|
2015-03-14 17:52:40 +00:00
|
|
|
return [errorRegex numberOfMatchesInString:error options:0 range:NSMakeRange(0, [error length])] > 0;
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2015-03-25 02:34:12 +00:00
|
|
|
- (void)runTest:(SEL)test module:(NSString *)moduleName initialProps:(NSDictionary *)initialProps expectErrorBlock:(BOOL(^)(NSString *error))expectErrorBlock
|
2015-03-14 17:52:40 +00:00
|
|
|
{
|
|
|
|
UIViewController *vc = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
|
2015-03-25 02:34:12 +00:00
|
|
|
if ([vc.view isKindOfClass:[RCTRootView class]]) {
|
|
|
|
[(RCTRootView *)vc.view invalidate]; // Make sure the normal app view doesn't interfere
|
|
|
|
}
|
|
|
|
vc.view = [[UIView alloc] init];
|
|
|
|
RCTRootView *rootView = [[RCTRootView alloc] initWithFrame:CGRectMake(0, 0, 320, 2000)]; // Constant size for testing on multiple devices
|
|
|
|
RCTTestModule *testModule = [[RCTTestModule alloc] initWithSnapshotController:_snapshotController view:rootView];
|
|
|
|
testModule.testSelector = test;
|
|
|
|
[vc.view addSubview:rootView]; // Add as subview so it doesn't get resized
|
2015-03-14 17:52:40 +00:00
|
|
|
rootView.moduleProvider = ^(void){
|
|
|
|
return @[testModule];
|
|
|
|
};
|
|
|
|
rootView.moduleName = moduleName;
|
|
|
|
rootView.initialProperties = initialProps;
|
|
|
|
rootView.scriptURL = [NSURL URLWithString:_script];
|
|
|
|
|
|
|
|
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:TIMEOUT_SECONDS];
|
|
|
|
NSString *error = [[RCTRedBox sharedInstance] currentErrorMessage];
|
|
|
|
while ([date timeIntervalSinceNow] > 0 && ![testModule isDone] && error == nil) {
|
|
|
|
[[NSRunLoop mainRunLoop] runMode:NSDefaultRunLoopMode beforeDate:date];
|
|
|
|
[[NSRunLoop mainRunLoop] runMode:NSRunLoopCommonModes beforeDate:date];
|
|
|
|
error = [[RCTRedBox sharedInstance] currentErrorMessage];
|
|
|
|
}
|
2015-03-25 02:34:12 +00:00
|
|
|
[rootView invalidate];
|
|
|
|
[rootView removeFromSuperview];
|
|
|
|
RCTAssert(vc.view.subviews.count == 0, @"There shouldn't be any other views: %@", vc.view);
|
|
|
|
vc.view = nil;
|
2015-03-14 17:52:40 +00:00
|
|
|
[[RCTRedBox sharedInstance] dismiss];
|
|
|
|
if (expectErrorBlock) {
|
2015-03-25 02:34:12 +00:00
|
|
|
RCTAssert(expectErrorBlock(error), @"Expected an error but nothing matched.");
|
2015-03-14 17:52:40 +00:00
|
|
|
} else if (error) {
|
|
|
|
RCTAssert(error == nil, @"RedBox error: %@", error);
|
|
|
|
} else {
|
|
|
|
RCTAssert([testModule isDone], @"Test didn't finish within %d seconds", TIMEOUT_SECONDS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|