2016-02-18 19:59:34 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Copyright 2016 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-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"
|
2016-02-15 22:22:40 +00:00
|
|
|
#import "RCTJavaScriptLoader.h"
|
|
|
|
#import "RCTLog.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
|
|
|
|
|
2016-03-22 16:54:06 +00:00
|
|
|
@interface RCTDevMenuDisabler : RCTDevMenu
|
|
|
|
@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
|
|
|
|
|
2016-03-22 16:54:06 +00:00
|
|
|
|
|
|
|
@implementation RCTDevMenuDisabler
|
2015-10-15 01:52:55 +00:00
|
|
|
|
2015-11-02 21:37:37 +00:00
|
|
|
+ (void)load {
|
2016-03-22 16:54:06 +00:00
|
|
|
// +[RCTDevMenu load] is guaranteed to have been called since it's the superclass.
|
|
|
|
// We remove it since it interferes with us fully controlling the executor class.
|
2015-11-30 21:48:43 +00:00
|
|
|
NSMutableArray *moduleClasses = RCTGetModuleClasses();
|
|
|
|
[moduleClasses removeObject:[RCTDevMenu class]];
|
2016-03-22 16:54:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
@implementation RealmReactTests
|
2016-02-15 22:22:40 +00:00
|
|
|
|
2016-03-22 16:54:06 +00:00
|
|
|
+ (void)load {
|
2016-02-15 22:22:40 +00:00
|
|
|
RCTAddLogFunction(^(RCTLogLevel level, RCTLogSource source, NSString *fileName, NSNumber *lineNumber, NSString *message) {
|
|
|
|
NSAssert(level < RCTLogLevelError, RCTFormatLog(nil, level, fileName, lineNumber, message));
|
|
|
|
});
|
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"];
|
2016-02-15 22:22:40 +00:00
|
|
|
NSAssert(testCaseNames.count, @"No test names were provided by the JS");
|
2015-10-15 18:34:24 +00:00
|
|
|
|
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
|
|
|
}];
|
|
|
|
|
2016-02-15 22:22:40 +00:00
|
|
|
@try {
|
|
|
|
[self waitForCondition:&condition description:notificationName];
|
|
|
|
} @finally {
|
|
|
|
[nc removeObserver:token];
|
|
|
|
}
|
2015-10-22 09:49:15 +00:00
|
|
|
|
|
|
|
return notification;
|
|
|
|
}
|
|
|
|
|
2016-02-15 22:22:40 +00:00
|
|
|
+ (void)waitForCondition:(BOOL *)condition description:(NSString *)description {
|
2015-10-22 09:49:15 +00:00
|
|
|
NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
|
2016-02-16 03:23:59 +00:00
|
|
|
NSDate *timeout = [NSDate dateWithTimeIntervalSinceNow:30.0];
|
2015-10-22 09:49:15 +00:00
|
|
|
|
|
|
|
while (!*condition) {
|
2016-02-15 22:22:40 +00:00
|
|
|
if ([timeout timeIntervalSinceNow] < 0) {
|
|
|
|
@throw [NSException exceptionWithName:@"ConditionTimeout"
|
|
|
|
reason:[NSString stringWithFormat:@"Timed out waiting for: %@", description]
|
|
|
|
userInfo:nil];
|
|
|
|
}
|
|
|
|
|
2015-10-15 18:34:24 +00:00
|
|
|
@autoreleasepool {
|
2016-02-15 22:22:40 +00:00
|
|
|
[runLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
|
|
|
|
[runLoop runMode:NSRunLoopCommonModes beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
|
|
|
|
[NSThread sleepForTimeInterval:0.01]; // Bad things may happen without some sleep.
|
2015-10-15 18:34:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-06 23:24:00 +00:00
|
|
|
+ (id)waitForEvent:(NSString *)eventName {
|
|
|
|
__weak RealmReact *realmModule = [[self currentBridge] moduleForClass:[RealmReact class]];
|
2016-02-15 22:22:40 +00:00
|
|
|
NSAssert(realmModule, @"RealmReact module not found");
|
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
|
|
|
|
2016-02-15 22:22:40 +00:00
|
|
|
[self waitForCondition:&condition description:eventName];
|
2015-10-15 18:34:24 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-02-15 22:22:40 +00:00
|
|
|
- (void)invokeTest {
|
|
|
|
RCTLogFunction logFunction = RCTGetLogFunction();
|
|
|
|
|
|
|
|
// Fail when React Native logs an error.
|
|
|
|
RCTSetLogFunction(^(RCTLogLevel level, RCTLogSource source, NSString *fileName, NSNumber *lineNumber, NSString *message) {
|
|
|
|
RCTDefaultLogFunction(level, source, fileName, lineNumber, message);
|
|
|
|
|
|
|
|
if (level >= RCTLogLevelError) {
|
|
|
|
NSString *type = (source == RCTLogSourceJavaScript) ? @"JS" : @"Native";
|
|
|
|
XCTFail(@"%@ Error: %@", type, RCTFormatLog(nil, level, fileName, lineNumber, message));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
[super invokeTest];
|
|
|
|
|
|
|
|
RCTSetLogFunction(logFunction);
|
|
|
|
}
|
|
|
|
|
2015-10-15 18:34:24 +00:00
|
|
|
- (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-02-15 22:22:40 +00:00
|
|
|
id error;
|
|
|
|
@try {
|
|
|
|
error = [self.class waitForEvent:@"realm-test-finished"];
|
|
|
|
} @catch (id exception) {
|
|
|
|
error = exception;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2016-03-22 16:54:06 +00:00
|
|
|
|
2015-10-23 21:12:35 +00:00
|
|
|
@implementation RealmReactChromeTests
|
|
|
|
|
|
|
|
+ (Class)executorClass {
|
|
|
|
return NSClassFromString(@"RCTWebSocketExecutor");
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSString *)classNameSuffix {
|
|
|
|
return @"_Chrome";
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|