2015-11-20 11:33:07 +00:00
|
|
|
/**
|
2017-05-06 03:50:47 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
2015-11-20 11:33:07 +00:00
|
|
|
*
|
2017-05-06 03:50:47 +00:00
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
2015-11-20 11:33:07 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import <XCTest/XCTest.h>
|
|
|
|
|
2016-11-24 17:44:51 +00:00
|
|
|
#import <React/RCTAssert.h>
|
|
|
|
#import <React/RCTBridge.h>
|
|
|
|
#import <React/RCTLog.h>
|
2015-11-20 11:33:07 +00:00
|
|
|
|
|
|
|
@interface RCTLoggingTests : XCTestCase
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation RCTLoggingTests
|
|
|
|
{
|
|
|
|
RCTBridge *_bridge;
|
|
|
|
|
|
|
|
dispatch_semaphore_t _logSem;
|
|
|
|
RCTLogLevel _lastLogLevel;
|
|
|
|
RCTLogSource _lastLogSource;
|
|
|
|
NSString *_lastLogMessage;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setUp
|
|
|
|
{
|
2016-05-23 11:36:22 +00:00
|
|
|
NSURL *scriptURL;
|
|
|
|
if (getenv("CI_USE_PACKAGER")) {
|
2016-07-12 12:51:57 +00:00
|
|
|
NSString *app = @"IntegrationTests/IntegrationTestsApp";
|
2016-05-23 11:36:22 +00:00
|
|
|
scriptURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://localhost:8081/%@.bundle?platform=ios&dev=true", app]];
|
|
|
|
} else {
|
|
|
|
scriptURL = [[NSBundle bundleForClass:[RCTBridge class]] URLForResource:@"main" withExtension:@"jsbundle"];
|
|
|
|
}
|
2016-07-18 14:12:19 +00:00
|
|
|
RCTAssert(scriptURL != nil, @"No scriptURL set");
|
2015-11-20 11:33:07 +00:00
|
|
|
|
|
|
|
_bridge = [[RCTBridge alloc] initWithBundleURL:scriptURL moduleProvider:NULL launchOptions:nil];
|
2016-07-12 12:51:57 +00:00
|
|
|
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:60];
|
2015-11-20 11:33:07 +00:00
|
|
|
while (date.timeIntervalSinceNow > 0 && _bridge.loading) {
|
|
|
|
[[NSRunLoop mainRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
|
|
|
|
}
|
|
|
|
XCTAssertFalse(_bridge.loading);
|
|
|
|
|
|
|
|
_logSem = dispatch_semaphore_create(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)tearDown
|
|
|
|
{
|
|
|
|
[_bridge invalidate];
|
|
|
|
_bridge = nil;
|
|
|
|
|
|
|
|
RCTSetLogFunction(RCTDefaultLogFunction);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)testLogging
|
|
|
|
{
|
2016-10-27 08:37:06 +00:00
|
|
|
// First console log call will fire after 2.0 sec, to allow for any initial log messages
|
|
|
|
// that might come in (seeing this in tvOS)
|
|
|
|
[_bridge enqueueJSCall:@"LoggingTestModule.logToConsoleAfterWait" args:@[@"Invoking console.log",@2000]];
|
|
|
|
// Spin native layer for 1.9 sec
|
|
|
|
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1.9]];
|
|
|
|
// Now set the log function to signal the semaphore
|
|
|
|
RCTSetLogFunction(^(RCTLogLevel level, RCTLogSource source, __unused NSString *fileName, __unused NSNumber *lineNumber, NSString *message) {
|
|
|
|
if (source == RCTLogSourceJavaScript) {
|
|
|
|
self->_lastLogLevel = level;
|
|
|
|
self->_lastLogSource = source;
|
|
|
|
self->_lastLogMessage = message;
|
|
|
|
dispatch_semaphore_signal(self->_logSem);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// Wait for console log to signal the semaphore
|
2015-11-20 11:33:07 +00:00
|
|
|
dispatch_semaphore_wait(_logSem, DISPATCH_TIME_FOREVER);
|
|
|
|
|
|
|
|
XCTAssertEqual(_lastLogLevel, RCTLogLevelInfo);
|
|
|
|
XCTAssertEqual(_lastLogSource, RCTLogSourceJavaScript);
|
|
|
|
XCTAssertEqualObjects(_lastLogMessage, @"Invoking console.log");
|
|
|
|
|
|
|
|
[_bridge enqueueJSCall:@"LoggingTestModule.warning" args:@[@"Generating warning"]];
|
|
|
|
dispatch_semaphore_wait(_logSem, DISPATCH_TIME_FOREVER);
|
|
|
|
|
|
|
|
XCTAssertEqual(_lastLogLevel, RCTLogLevelWarning);
|
|
|
|
XCTAssertEqual(_lastLogSource, RCTLogSourceJavaScript);
|
|
|
|
XCTAssertEqualObjects(_lastLogMessage, @"Warning: Generating warning");
|
|
|
|
|
|
|
|
[_bridge enqueueJSCall:@"LoggingTestModule.invariant" args:@[@"Invariant failed"]];
|
|
|
|
dispatch_semaphore_wait(_logSem, DISPATCH_TIME_FOREVER);
|
|
|
|
|
|
|
|
XCTAssertEqual(_lastLogLevel, RCTLogLevelError);
|
|
|
|
XCTAssertEqual(_lastLogSource, RCTLogSourceJavaScript);
|
2015-12-30 19:38:44 +00:00
|
|
|
XCTAssertEqualObjects(_lastLogMessage, @"Invariant failed");
|
2015-11-20 11:33:07 +00:00
|
|
|
|
|
|
|
[_bridge enqueueJSCall:@"LoggingTestModule.logErrorToConsole" args:@[@"Invoking console.error"]];
|
|
|
|
dispatch_semaphore_wait(_logSem, DISPATCH_TIME_FOREVER);
|
|
|
|
|
2016-07-12 12:51:57 +00:00
|
|
|
// For local bundles, we'll first get a warning about symbolication
|
|
|
|
if ([_bridge.bundleURL isFileURL]) {
|
|
|
|
dispatch_semaphore_wait(_logSem, DISPATCH_TIME_FOREVER);
|
|
|
|
}
|
|
|
|
|
2015-11-20 11:33:07 +00:00
|
|
|
XCTAssertEqual(_lastLogLevel, RCTLogLevelError);
|
|
|
|
XCTAssertEqual(_lastLogSource, RCTLogSourceJavaScript);
|
|
|
|
XCTAssertEqualObjects(_lastLogMessage, @"Invoking console.error");
|
|
|
|
|
|
|
|
[_bridge enqueueJSCall:@"LoggingTestModule.throwError" args:@[@"Throwing an error"]];
|
|
|
|
dispatch_semaphore_wait(_logSem, DISPATCH_TIME_FOREVER);
|
|
|
|
|
2016-07-12 12:51:57 +00:00
|
|
|
// For local bundles, we'll first get a warning about symbolication
|
|
|
|
if ([_bridge.bundleURL isFileURL]) {
|
|
|
|
dispatch_semaphore_wait(_logSem, DISPATCH_TIME_FOREVER);
|
|
|
|
}
|
|
|
|
|
2015-11-20 11:33:07 +00:00
|
|
|
XCTAssertEqual(_lastLogLevel, RCTLogLevelError);
|
|
|
|
XCTAssertEqual(_lastLogSource, RCTLogSourceJavaScript);
|
|
|
|
XCTAssertEqualObjects(_lastLogMessage, @"Throwing an error");
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|