2015-03-23 20:28:42 +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 01:32:38 +00:00
|
|
|
|
|
|
|
#import "RCTTestRunner.h"
|
|
|
|
|
2015-03-24 17:20:13 +00:00
|
|
|
#import "FBSnapshotTestController.h"
|
2015-06-15 14:53:45 +00:00
|
|
|
#import "RCTAssert.h"
|
2015-03-14 01:32:38 +00:00
|
|
|
#import "RCTRedBox.h"
|
|
|
|
#import "RCTRootView.h"
|
|
|
|
#import "RCTTestModule.h"
|
|
|
|
#import "RCTUtils.h"
|
|
|
|
|
2015-06-17 14:09:23 +00:00
|
|
|
#define TIMEOUT_SECONDS 60
|
2015-03-14 01:32:38 +00:00
|
|
|
|
2015-05-04 17:35:49 +00:00
|
|
|
@interface RCTBridge (RCTTestRunner)
|
|
|
|
|
|
|
|
@property (nonatomic, weak) RCTBridge *batchedBridge;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
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;
|
2015-07-07 23:19:40 +00:00
|
|
|
RCTBridgeModuleProviderBlock _moduleProvider;
|
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
|
|
|
|
moduleProvider:(RCTBridgeModuleProviderBlock)block
|
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-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];
|
|
|
|
|
2015-06-04 10:47:19 +00:00
|
|
|
#if RUNNING_ON_CI
|
|
|
|
_scriptURL = [[NSBundle bundleForClass:[RCTBridge class]] URLForResource:@"main" withExtension:@"jsbundle"];
|
2015-07-07 23:19:40 +00:00
|
|
|
RCTAssert(_scriptURL != nil, @"Could not locate main.jsBundle");
|
2015-06-04 10:47:19 +00:00
|
|
|
#else
|
2015-04-11 22:08:00 +00:00
|
|
|
_scriptURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://localhost:8081/%@.includeRequire.runModule.bundle?dev=true", app]];
|
2015-06-04 10:47:19 +00:00
|
|
|
#endif
|
2015-03-14 01:32:38 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2015-06-15 14:53:45 +00:00
|
|
|
RCT_NOT_IMPLEMENTED(-init)
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-03-24 17:20:13 +00:00
|
|
|
- (void)runTest:(SEL)test module:(NSString *)moduleName
|
2015-03-14 01:32:38 +00:00
|
|
|
{
|
2015-03-24 17:20:13 +00:00
|
|
|
[self runTest:test module:moduleName initialProps:nil expectErrorBlock:nil];
|
|
|
|
}
|
|
|
|
|
2015-04-15 00:51:28 +00:00
|
|
|
- (void)runTest:(SEL)test module:(NSString *)moduleName
|
|
|
|
initialProps:(NSDictionary *)initialProps expectErrorRegex:(NSString *)errorRegex
|
2015-03-24 17:20:13 +00:00
|
|
|
{
|
|
|
|
[self runTest:test module:moduleName initialProps:initialProps expectErrorBlock:^BOOL(NSString *error){
|
2015-04-11 22:08:00 +00:00
|
|
|
return [error rangeOfString:errorRegex options:NSRegularExpressionSearch].location != NSNotFound;
|
2015-03-14 01:32:38 +00:00
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2015-07-07 23:19:40 +00:00
|
|
|
- (void)runTest:(SEL)test module:(NSString *)moduleName
|
|
|
|
initialProps:(NSDictionary *)initialProps expectErrorBlock:(BOOL(^)(NSString *error))expectErrorBlock
|
2015-03-14 01:32:38 +00:00
|
|
|
{
|
2015-07-07 23:19:40 +00:00
|
|
|
RCTBridge *bridge = [[RCTBridge alloc] initWithBundleURL:_scriptURL
|
|
|
|
moduleProvider:_moduleProvider
|
|
|
|
launchOptions:nil];
|
|
|
|
|
2015-08-17 11:38:19 +00:00
|
|
|
RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge moduleName:moduleName initialProperties:initialProps];
|
2015-04-18 17:43:20 +00:00
|
|
|
rootView.frame = CGRectMake(0, 0, 320, 2000); // Constant size for testing on multiple devices
|
2015-03-26 01:59:42 +00:00
|
|
|
|
2015-04-18 17:43:20 +00:00
|
|
|
NSString *testModuleName = RCTBridgeModuleNameForClass([RCTTestModule class]);
|
2015-05-04 17:35:49 +00:00
|
|
|
RCTTestModule *testModule = rootView.bridge.batchedBridge.modules[testModuleName];
|
2015-07-07 23:19:40 +00:00
|
|
|
RCTAssert(_testController != nil, @"_testController should not be nil");
|
2015-04-18 17:43:20 +00:00
|
|
|
testModule.controller = _testController;
|
2015-03-24 17:20:13 +00:00
|
|
|
testModule.testSelector = test;
|
2015-04-02 14:33:21 +00:00
|
|
|
testModule.view = rootView;
|
2015-04-18 17:43:20 +00:00
|
|
|
|
|
|
|
UIViewController *vc = [UIApplication sharedApplication].delegate.window.rootViewController;
|
2015-08-17 14:35:34 +00:00
|
|
|
vc.view = [UIView new];
|
2015-03-24 17:20:13 +00:00
|
|
|
[vc.view addSubview:rootView]; // Add as subview so it doesn't get resized
|
2015-03-14 01:32:38 +00:00
|
|
|
|
|
|
|
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:TIMEOUT_SECONDS];
|
|
|
|
NSString *error = [[RCTRedBox sharedInstance] currentErrorMessage];
|
2015-06-17 14:09:23 +00:00
|
|
|
while ([date timeIntervalSinceNow] > 0 && testModule.status == RCTTestStatusPending && error == nil) {
|
2015-05-06 16:33:20 +00:00
|
|
|
[[NSRunLoop mainRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
|
|
|
|
[[NSRunLoop mainRunLoop] runMode:NSRunLoopCommonModes beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
|
2015-03-14 01:32:38 +00:00
|
|
|
error = [[RCTRedBox sharedInstance] currentErrorMessage];
|
|
|
|
}
|
2015-03-24 17:20:13 +00:00
|
|
|
[rootView removeFromSuperview];
|
2015-06-25 16:01:17 +00:00
|
|
|
|
|
|
|
NSArray *nonLayoutSubviews = [vc.view.subviews filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id subview, NSDictionary *bindings) {
|
|
|
|
return ![NSStringFromClass([subview class]) isEqualToString:@"_UILayoutGuide"];
|
|
|
|
}]];
|
|
|
|
RCTAssert(nonLayoutSubviews.count == 0, @"There shouldn't be any other views: %@", nonLayoutSubviews);
|
|
|
|
|
2015-03-14 01:32:38 +00:00
|
|
|
[[RCTRedBox sharedInstance] dismiss];
|
|
|
|
if (expectErrorBlock) {
|
2015-03-24 17:20:13 +00:00
|
|
|
RCTAssert(expectErrorBlock(error), @"Expected an error but nothing matched.");
|
2015-03-14 01:32:38 +00:00
|
|
|
} else {
|
2015-06-17 14:09:23 +00:00
|
|
|
RCTAssert(error == nil, @"RedBox error: %@", error);
|
|
|
|
RCTAssert(testModule.status != RCTTestStatusPending, @"Test didn't finish within %d seconds", TIMEOUT_SECONDS);
|
|
|
|
RCTAssert(testModule.status == RCTTestStatusPassed, @"Test failed");
|
2015-03-14 01:32:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|