2015-10-27 20:59:15 +00:00
|
|
|
/* Copyright 2015 Realm Inc - All Rights Reserved
|
|
|
|
* Proprietary and Confidential
|
|
|
|
*/
|
2015-10-15 01:52:55 +00:00
|
|
|
|
|
|
|
#import "RealmJSTests.h"
|
2015-11-30 20:14:50 +00:00
|
|
|
#import "RCTJavaScriptExecutor.h"
|
|
|
|
#import "RCTBridge.h"
|
|
|
|
#import "RCTDevMenu.h"
|
2016-01-06 23:24:00 +00:00
|
|
|
#import "RCTEventDispatcher.h"
|
2015-10-15 01:52:55 +00:00
|
|
|
|
|
|
|
@import RealmReact;
|
|
|
|
|
2015-10-15 23:18:58 +00:00
|
|
|
extern void JSGlobalContextSetIncludesNativeCallStackWhenReportingExceptions(JSGlobalContextRef ctx, bool includesNativeCallStack);
|
2015-11-30 21:48:43 +00:00
|
|
|
extern NSMutableArray *RCTGetModuleClasses(void);
|
2015-10-15 23:18:58 +00:00
|
|
|
|
2016-01-06 23:24:00 +00:00
|
|
|
@interface RCTBridge ()
|
|
|
|
+ (instancetype)currentBridge;
|
|
|
|
- (void)setUp;
|
|
|
|
@end
|
|
|
|
|
2015-10-15 01:52:55 +00:00
|
|
|
@interface RealmReactTests : RealmJSTests
|
|
|
|
@end
|
|
|
|
|
2015-10-23 21:12:35 +00:00
|
|
|
@interface RealmReactChromeTests : RealmReactTests
|
|
|
|
@end
|
|
|
|
|
2015-10-15 01:52:55 +00:00
|
|
|
@implementation RealmReactTests
|
|
|
|
|
2015-11-02 21:37:37 +00:00
|
|
|
+ (void)load {
|
2015-11-30 21:48:43 +00:00
|
|
|
NSMutableArray *moduleClasses = RCTGetModuleClasses();
|
|
|
|
[moduleClasses removeObject:[RCTDevMenu class]];
|
2015-11-02 21:37:37 +00:00
|
|
|
}
|
|
|
|
|
2015-10-23 21:12:35 +00:00
|
|
|
+ (Class)executorClass {
|
2016-01-06 23:24:00 +00:00
|
|
|
return NSClassFromString(@"RCTJSCExecutor");
|
2015-10-23 21:12:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSString *)classNameSuffix {
|
|
|
|
return @"";
|
|
|
|
}
|
2015-10-15 23:18:58 +00:00
|
|
|
|
2016-01-06 23:24:00 +00:00
|
|
|
+ (RCTBridge *)currentBridge {
|
2015-10-23 21:12:35 +00:00
|
|
|
Class executorClass = [self executorClass];
|
2015-11-02 23:20:45 +00:00
|
|
|
if (!executorClass) {
|
|
|
|
return nil;
|
|
|
|
}
|
2015-10-15 23:18:58 +00:00
|
|
|
|
2016-01-06 23:24:00 +00:00
|
|
|
RCTBridge *bridge = [RCTBridge currentBridge];
|
|
|
|
if (!bridge.valid) {
|
|
|
|
[self waitForNotification:RCTJavaScriptDidLoadNotification];
|
|
|
|
bridge = [RCTBridge currentBridge];
|
2015-10-23 21:12:35 +00:00
|
|
|
}
|
|
|
|
|
2016-01-06 23:24:00 +00:00
|
|
|
if (bridge.executorClass != executorClass) {
|
|
|
|
bridge.executorClass = executorClass;
|
2015-10-23 21:12:35 +00:00
|
|
|
|
2016-01-06 23:24:00 +00:00
|
|
|
RCTBridge *parentBridge = [bridge valueForKey:@"parentBridge"];
|
|
|
|
[parentBridge invalidate];
|
|
|
|
[parentBridge setUp];
|
2015-10-23 21:12:35 +00:00
|
|
|
|
2016-01-06 23:24:00 +00:00
|
|
|
return [self currentBridge];
|
2015-10-23 21:12:35 +00:00
|
|
|
}
|
|
|
|
|
2016-01-06 23:24:00 +00:00
|
|
|
return bridge;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (id<RCTJavaScriptExecutor>)currentExecutor {
|
|
|
|
return [[self currentBridge] valueForKey:@"javaScriptExecutor"];
|
2015-10-23 21:12:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
+ (XCTestSuite *)defaultTestSuite {
|
2015-11-02 23:20:45 +00:00
|
|
|
XCTestSuite *suite = [super defaultTestSuite];
|
2015-10-23 21:12:35 +00:00
|
|
|
id<RCTJavaScriptExecutor> executor = [self currentExecutor];
|
2015-10-15 23:18:58 +00:00
|
|
|
|
2015-11-02 23:20:45 +00:00
|
|
|
// The executor may be nil if the executorClass was not found (i.e. release build).
|
|
|
|
if (!executor) {
|
|
|
|
return suite;
|
|
|
|
}
|
|
|
|
|
2015-10-15 23:18:58 +00:00
|
|
|
// FIXME: Remove this nonsense once the crashes go away when a test fails!
|
2015-10-23 21:12:35 +00:00
|
|
|
JSGlobalContextRef ctx = RealmReactGetJSGlobalContextForExecutor(executor, false);
|
2015-10-15 23:18:58 +00:00
|
|
|
if (ctx) {
|
|
|
|
JSGlobalContextSetIncludesNativeCallStackWhenReportingExceptions(ctx, false);
|
|
|
|
}
|
2015-10-15 18:34:24 +00:00
|
|
|
|
2016-01-06 23:24:00 +00:00
|
|
|
NSDictionary *testCaseNames = [self waitForEvent:@"realm-test-names"];
|
|
|
|
if (!testCaseNames.count) {
|
|
|
|
NSLog(@"ERROR: No test names were provided by the JS");
|
2015-10-15 18:34:24 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2015-10-23 21:12:35 +00:00
|
|
|
NSString *nameSuffix = [self classNameSuffix];
|
|
|
|
if (nameSuffix.length) {
|
|
|
|
NSMutableDictionary *renamedTestCaseNames = [[NSMutableDictionary alloc] init];
|
|
|
|
for (NSString *name in testCaseNames) {
|
|
|
|
renamedTestCaseNames[[name stringByAppendingString:nameSuffix]] = testCaseNames[name];
|
|
|
|
}
|
|
|
|
testCaseNames = renamedTestCaseNames;
|
|
|
|
}
|
2015-10-15 18:34:24 +00:00
|
|
|
|
|
|
|
for (XCTestSuite *testSuite in [self testSuitesFromDictionary:testCaseNames]) {
|
|
|
|
[suite addTest:testSuite];
|
|
|
|
}
|
|
|
|
|
|
|
|
return suite;
|
2015-10-15 01:52:55 +00:00
|
|
|
}
|
|
|
|
|
2015-10-15 23:18:58 +00:00
|
|
|
+ (NSNotification *)waitForNotification:(NSString *)notificationName {
|
2015-10-15 18:34:24 +00:00
|
|
|
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
|
2015-10-22 09:49:15 +00:00
|
|
|
__block BOOL condition = NO;
|
2015-10-15 23:18:58 +00:00
|
|
|
__block NSNotification *notification;
|
2015-10-15 01:52:55 +00:00
|
|
|
|
2015-10-15 18:34:24 +00:00
|
|
|
id token = [nc addObserverForName:notificationName object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
|
2015-10-22 09:49:15 +00:00
|
|
|
condition = YES;
|
2015-10-15 23:18:58 +00:00
|
|
|
notification = note;
|
2015-10-15 18:34:24 +00:00
|
|
|
}];
|
|
|
|
|
2015-10-22 09:49:15 +00:00
|
|
|
[self waitForCondition:&condition];
|
|
|
|
[nc removeObserver:token];
|
|
|
|
|
|
|
|
return notification;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (void)waitForCondition:(BOOL *)condition {
|
|
|
|
NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
|
|
|
|
|
|
|
|
while (!*condition) {
|
2015-10-15 18:34:24 +00:00
|
|
|
@autoreleasepool {
|
|
|
|
[runLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-06 23:24:00 +00:00
|
|
|
+ (id)waitForEvent:(NSString *)eventName {
|
|
|
|
__weak RealmReact *realmModule = [[self currentBridge] moduleForClass:[RealmReact class]];
|
|
|
|
assert(realmModule);
|
2015-10-15 01:52:55 +00:00
|
|
|
|
2015-10-22 09:49:15 +00:00
|
|
|
__block BOOL condition = NO;
|
2015-10-15 18:34:24 +00:00
|
|
|
__block id result;
|
2016-01-06 23:24:00 +00:00
|
|
|
__block RealmReactEventHandler handler;
|
2015-10-15 18:34:24 +00:00
|
|
|
|
2016-01-06 23:24:00 +00:00
|
|
|
__block __weak RealmReactEventHandler weakHandler = handler = ^(id object) {
|
|
|
|
[realmModule removeListenerForEvent:eventName handler:weakHandler];
|
2015-10-23 21:13:38 +00:00
|
|
|
|
2016-01-06 23:24:00 +00:00
|
|
|
condition = YES;
|
|
|
|
result = object;
|
|
|
|
};
|
2015-10-22 09:49:15 +00:00
|
|
|
|
2016-01-06 23:24:00 +00:00
|
|
|
[realmModule addListenerForEvent:eventName handler:handler];
|
2015-10-15 10:12:28 +00:00
|
|
|
|
2015-10-22 09:49:15 +00:00
|
|
|
[self waitForCondition:&condition];
|
2015-10-15 18:34:24 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)invokeMethod:(NSString *)method {
|
2015-10-23 21:12:35 +00:00
|
|
|
NSString *module = NSStringFromClass(self.class);
|
|
|
|
NSString *suffix = [self.class classNameSuffix];
|
|
|
|
|
|
|
|
if (suffix.length && [module hasSuffix:suffix]) {
|
|
|
|
module = [module substringToIndex:(module.length - suffix.length)];
|
|
|
|
}
|
|
|
|
|
2016-01-06 23:24:00 +00:00
|
|
|
RCTBridge *bridge = [self.class currentBridge];
|
|
|
|
[bridge.eventDispatcher sendAppEventWithName:@"realm-run-test" body:@{@"suite": module, @"name": method}];
|
2015-10-15 18:34:24 +00:00
|
|
|
|
2016-01-06 23:24:00 +00:00
|
|
|
id error = [self.class waitForEvent:@"realm-test-finished"];
|
2015-10-15 18:34:24 +00:00
|
|
|
if (error) {
|
2016-01-06 23:24:00 +00:00
|
|
|
[self recordFailureWithDescription:[error description] inFile:@(__FILE__) atLine:__LINE__ expected:YES];
|
2015-10-15 18:34:24 +00:00
|
|
|
}
|
2015-10-15 01:52:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
2015-10-23 21:12:35 +00:00
|
|
|
|
|
|
|
@implementation RealmReactChromeTests
|
|
|
|
|
|
|
|
+ (Class)executorClass {
|
|
|
|
return NSClassFromString(@"RCTWebSocketExecutor");
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSString *)classNameSuffix {
|
|
|
|
return @"_Chrome";
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|