2015-10-15 18:34:24 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Copyright 2015 Realm Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
2015-10-15 01:52:55 +00:00
|
|
|
|
|
|
|
#import "RealmJSTests.h"
|
2015-10-15 10:12:28 +00:00
|
|
|
#import "Base/RCTJavaScriptExecutor.h"
|
|
|
|
#import "Base/RCTBridge.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);
|
|
|
|
|
|
|
|
static id<RCTJavaScriptExecutor> s_currentJavaScriptExecutor;
|
|
|
|
|
2015-10-15 01:52:55 +00:00
|
|
|
@interface RealmReactTests : RealmJSTests
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation RealmReactTests
|
|
|
|
|
2015-10-15 18:34:24 +00:00
|
|
|
+ (XCTestSuite *)defaultTestSuite {
|
2015-10-15 23:18:58 +00:00
|
|
|
NSNotification *notification = [self waitForNotification:RCTJavaScriptDidLoadNotification];
|
|
|
|
RCTBridge *bridge = notification.userInfo[@"bridge"];
|
|
|
|
|
|
|
|
if (!bridge) {
|
|
|
|
NSLog(@"No RCTBridge provided by RCTJavaScriptDidLoadNotification");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
s_currentJavaScriptExecutor = [bridge valueForKey:@"javaScriptExecutor"];
|
|
|
|
|
|
|
|
// FIXME: Remove this nonsense once the crashes go away when a test fails!
|
2015-10-19 18:28:50 +00:00
|
|
|
JSGlobalContextRef ctx = RealmReactGetJSGlobalContextForExecutor(s_currentJavaScriptExecutor, false);
|
2015-10-15 23:18:58 +00:00
|
|
|
if (ctx) {
|
|
|
|
JSGlobalContextSetIncludesNativeCallStackWhenReportingExceptions(ctx, false);
|
|
|
|
}
|
2015-10-15 18:34:24 +00:00
|
|
|
|
|
|
|
NSError *error;
|
|
|
|
NSDictionary *testCaseNames = [self invokeMethod:@"getTestNames" inModule:@"index" error:&error];
|
|
|
|
|
|
|
|
if (error || !testCaseNames.count) {
|
|
|
|
NSLog(@"Error from calling getTestNames() - %@", error ?: @"None returned");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
XCTestSuite *suite = [super defaultTestSuite];
|
|
|
|
|
|
|
|
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
|
|
|
NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
|
|
|
|
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
|
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-15 23:18:58 +00:00
|
|
|
notification = note;
|
2015-10-15 18:34:24 +00:00
|
|
|
}];
|
|
|
|
|
2015-10-15 23:18:58 +00:00
|
|
|
while (!notification) {
|
2015-10-15 18:34:24 +00:00
|
|
|
@autoreleasepool {
|
|
|
|
[runLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[nc removeObserver:token];
|
2015-10-15 23:18:58 +00:00
|
|
|
return notification;
|
2015-10-15 18:34:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
+ (id)invokeMethod:(NSString *)method inModule:(NSString *)module error:(NSError * __strong *)outError {
|
|
|
|
module = [NSString stringWithFormat:@"realm-tests/%@.js", module];
|
2015-10-15 01:52:55 +00:00
|
|
|
|
2015-10-15 18:34:24 +00:00
|
|
|
dispatch_group_t group = dispatch_group_create();
|
|
|
|
__block id result;
|
|
|
|
|
|
|
|
dispatch_group_enter(group);
|
2015-10-15 10:12:28 +00:00
|
|
|
|
2015-10-15 23:18:58 +00:00
|
|
|
[s_currentJavaScriptExecutor executeJSCall:module method:method arguments:@[] callback:^(id json, NSError *error) {
|
2015-10-15 18:34:24 +00:00
|
|
|
result = json;
|
|
|
|
|
|
|
|
if (error && outError) {
|
|
|
|
*outError = error;
|
|
|
|
}
|
|
|
|
|
2015-10-15 10:12:28 +00:00
|
|
|
dispatch_group_leave(group);
|
2015-10-15 01:52:55 +00:00
|
|
|
}];
|
2015-10-15 10:12:28 +00:00
|
|
|
|
|
|
|
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
|
2015-10-15 18:34:24 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)invokeMethod:(NSString *)method {
|
|
|
|
NSError *error;
|
|
|
|
[self.class invokeMethod:method inModule:NSStringFromClass(self.class) error:&error];
|
|
|
|
|
|
|
|
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
|