Change RealmReactTests to run with both executors

A new approach was needed for this. The RCTDevMenu is disabled so that it doesn't interfere with us changing the executor class. A suffix is added to the name of test cases that are run in Chrome.
This commit is contained in:
Scott Kyle 2015-10-23 14:12:35 -07:00
parent 2fe1f0d26e
commit e7b05d2dc6
2 changed files with 79 additions and 36 deletions

View File

@ -19,30 +19,8 @@
#import "AppDelegate.h"
#import "RCTRootView.h"
static NSString * const RealmReactEnableChromeDebuggingKey = @"RealmReactEnableChromeDebugging";
static NSString * const RCTDevMenuKey = @"RCTDevMenu";
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
// Check if this default is explicitly set, otherwise just leave the settings as is.
if ([defaults objectForKey:RealmReactEnableChromeDebuggingKey]) {
NSMutableDictionary *settings = [([defaults dictionaryForKey:RCTDevMenuKey] ?: @{}) mutableCopy];
NSMutableDictionary *domain = [[defaults volatileDomainForName:NSArgumentDomain] mutableCopy];
settings[@"executorClass"] = [defaults boolForKey:RealmReactEnableChromeDebuggingKey] ? @"RCTWebSocketExecutor" : @"RCTContextExecutor";
domain[RCTDevMenuKey] = settings;
// Re-register the arguments domain (highest precedent and volatile) with our new overridden settings.
[defaults removeVolatileDomainForName:NSArgumentDomain];
[defaults setVolatileDomain:domain forName:NSArgumentDomain];
}
return YES;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSURL *jsCodeLocation;

View File

@ -19,34 +19,71 @@
#import "RealmJSTests.h"
#import "Base/RCTJavaScriptExecutor.h"
#import "Base/RCTBridge.h"
#import "Modules/RCTDevMenu.h"
@import ObjectiveC;
@import RealmReact;
extern void JSGlobalContextSetIncludesNativeCallStackWhenReportingExceptions(JSGlobalContextRef ctx, bool includesNativeCallStack);
static id<RCTJavaScriptExecutor> s_currentJavaScriptExecutor;
@interface RealmReactTests : RealmJSTests
@end
@interface RealmReactChromeTests : RealmReactTests
@end
@implementation RealmReactTests
+ (XCTestSuite *)defaultTestSuite {
NSNotification *notification = [self waitForNotification:RCTJavaScriptDidLoadNotification];
RCTBridge *bridge = notification.userInfo[@"bridge"];
+ (void)load {
// Swap the [RCTDevMenu init] method with [NSObject init] in order to disable RCTDevMenu completely.
IMP init = class_getMethodImplementation([NSObject class], @selector(init));
class_replaceMethod([RCTDevMenu class], @selector(init), init, NULL);
}
if (!bridge) {
NSLog(@"No RCTBridge provided by RCTJavaScriptDidLoadNotification");
+ (Class)executorClass {
return NSClassFromString(@"RCTContextExecutor");
}
+ (NSString *)classNameSuffix {
return @"";
}
+ (id<RCTJavaScriptExecutor>)currentExecutor {
Class executorClass = [self executorClass];
if (!executorClass) {
NSLog(@"%@: Executor class not found", self);
exit(1);
}
// Wait for the RCTDevMenu to handle this notification and act upon it.
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
static RCTBridge *s_bridge;
if (!s_bridge.valid) {
NSNotification *notification = [self waitForNotification:RCTJavaScriptDidLoadNotification];;
s_bridge = notification.userInfo[@"bridge"];
s_currentJavaScriptExecutor = [bridge valueForKey:@"javaScriptExecutor"];
if (!s_bridge) {
NSLog(@"No RCTBridge provided by RCTJavaScriptDidLoadNotification");
exit(1);
}
}
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 {
id<RCTJavaScriptExecutor> executor = [self currentExecutor];
// FIXME: Remove this nonsense once the crashes go away when a test fails!
JSGlobalContextRef ctx = RealmReactGetJSGlobalContextForExecutor(s_currentJavaScriptExecutor, false);
JSGlobalContextRef ctx = RealmReactGetJSGlobalContextForExecutor(executor, false);
if (ctx) {
JSGlobalContextSetIncludesNativeCallStackWhenReportingExceptions(ctx, false);
}
@ -59,8 +96,16 @@ static id<RCTJavaScriptExecutor> s_currentJavaScriptExecutor;
exit(1);
}
XCTestSuite *suite = [super defaultTestSuite];
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;
}
XCTestSuite *suite = [super defaultTestSuite];
for (XCTestSuite *testSuite in [self testSuitesFromDictionary:testCaseNames]) {
[suite addTest:testSuite];
}
@ -95,12 +140,13 @@ static id<RCTJavaScriptExecutor> s_currentJavaScriptExecutor;
}
+ (id)invokeMethod:(NSString *)method inModule:(NSString *)module error:(NSError * __strong *)outError {
id<RCTJavaScriptExecutor> executor = [self currentExecutor];
module = [NSString stringWithFormat:@"realm-tests/%@.js", module];
__block BOOL condition = NO;
__block id result;
[s_currentJavaScriptExecutor executeJSCall:module method:method arguments:@[] callback:^(id json, NSError *error) {
[executor executeJSCall:module method:method arguments:@[] callback:^(id json, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
condition = YES;
result = json;
@ -117,8 +163,15 @@ static id<RCTJavaScriptExecutor> s_currentJavaScriptExecutor;
}
- (void)invokeMethod:(NSString *)method {
NSString *module = NSStringFromClass(self.class);
NSString *suffix = [self.class classNameSuffix];
if (suffix.length && [module hasSuffix:suffix]) {
module = [module substringToIndex:(module.length - suffix.length)];
}
NSError *error;
[self.class invokeMethod:method inModule:NSStringFromClass(self.class) error:&error];
[self.class invokeMethod:method inModule:module error:&error];
if (error) {
// TODO: Parse and use localizedFailureReason info once we can source map the failure location in JS.
@ -127,3 +180,15 @@ static id<RCTJavaScriptExecutor> s_currentJavaScriptExecutor;
}
@end
@implementation RealmReactChromeTests
+ (Class)executorClass {
return NSClassFromString(@"RCTWebSocketExecutor");
}
+ (NSString *)classNameSuffix {
return @"_Chrome";
}
@end