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"
|
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
|
|
|
|
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 {
|
|
|
|
return NSClassFromString(@"RCTContextExecutor");
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSString *)classNameSuffix {
|
|
|
|
return @"";
|
|
|
|
}
|
2015-10-15 23:18:58 +00:00
|
|
|
|
2015-10-23 21:12:35 +00:00
|
|
|
+ (id<RCTJavaScriptExecutor>)currentExecutor {
|
|
|
|
Class executorClass = [self executorClass];
|
2015-11-02 23:20:45 +00:00
|
|
|
if (!executorClass) {
|
|
|
|
return nil;
|
|
|
|
}
|
2015-10-15 23:18:58 +00:00
|
|
|
|
2015-10-23 21:12:35 +00:00
|
|
|
static RCTBridge *s_bridge;
|
2015-10-26 20:25:15 +00:00
|
|
|
if (!s_bridge) {
|
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:RCTReloadNotification object:nil];
|
|
|
|
}
|
|
|
|
|
2015-10-23 21:12:35 +00:00
|
|
|
if (!s_bridge.valid) {
|
2015-11-02 21:37:37 +00:00
|
|
|
NSNotification *notification = [self waitForNotification:RCTJavaScriptDidLoadNotification];
|
|
|
|
s_bridge = notification.userInfo[@"bridge"];
|
|
|
|
assert(s_bridge);
|
2015-10-23 21:12:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (s_bridge.executorClass != executorClass) {
|
|
|
|
s_bridge.executorClass = executorClass;
|
|
|
|
[s_bridge reload];
|
|
|
|
|
|
|
|
// The [RCTBridge reload] method does a dispatch_async that we must run before trying again.
|
|
|
|
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
|
|
|
|
|
|
|
|
return [self currentExecutor];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [s_bridge valueForKey:@"javaScriptExecutor"];
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (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
|
|
|
|
|
|
|
NSError *error;
|
2015-12-31 00:47:16 +00:00
|
|
|
NSDictionary *testCaseNames = [self invokeMethod:@"getTestNames" arguments:nil error:&error];
|
2015-10-15 18:34:24 +00:00
|
|
|
|
|
|
|
if (error || !testCaseNames.count) {
|
|
|
|
NSLog(@"Error from calling getTestNames() - %@", error ?: @"None returned");
|
|
|
|
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]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-31 00:47:16 +00:00
|
|
|
+ (id)invokeMethod:(NSString *)method arguments:(NSArray *)arguments error:(NSError * __strong *)outError {
|
2015-10-23 21:12:35 +00:00
|
|
|
id<RCTJavaScriptExecutor> executor = [self currentExecutor];
|
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;
|
|
|
|
|
2015-12-31 00:47:16 +00:00
|
|
|
[executor executeJSCall:@"realm-tests/index.js" method:method arguments:(arguments ?: @[]) callback:^(id json, NSError *error) {
|
2015-10-23 21:13:38 +00:00
|
|
|
// The React Native debuggerWorker.js very bizarrely returns an array five empty arrays to signify an error.
|
|
|
|
if ([json isKindOfClass:[NSArray class]] && [json isEqualToArray:@[@[], @[], @[], @[], @[]]]) {
|
|
|
|
json = nil;
|
|
|
|
|
|
|
|
if (!error) {
|
|
|
|
error = [NSError errorWithDomain:@"JS" code:1 userInfo:@{NSLocalizedDescriptionKey: @"unknown JS error"}];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-22 09:49:15 +00:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
condition = YES;
|
|
|
|
result = json;
|
|
|
|
|
|
|
|
if (error && outError) {
|
|
|
|
*outError = error;
|
|
|
|
}
|
|
|
|
});
|
2015-10-15 01:52:55 +00:00
|
|
|
}];
|
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)];
|
|
|
|
}
|
|
|
|
|
2015-10-15 18:34:24 +00:00
|
|
|
NSError *error;
|
2015-12-31 00:47:16 +00:00
|
|
|
[self.class invokeMethod:@"runTest" arguments:@[module, method] error:&error];
|
2015-10-15 18:34:24 +00:00
|
|
|
|
|
|
|
if (error) {
|
|
|
|
// TODO: Parse and use localizedFailureReason info once we can source map the failure location in JS.
|
|
|
|
[self recordFailureWithDescription:error.localizedDescription inFile:@(__FILE__) atLine:__LINE__ expected:YES];
|
|
|
|
}
|
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
|