-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ios): add native-side init API for startup crashes (#1056)
Jira ID: MOB-13246
- Loading branch information
1 parent
f880e94
commit d8e4b0d
Showing
8 changed files
with
201 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#import <XCTest/XCTest.h> | ||
#import "OCMock/OCMock.h" | ||
#import "Instabug/Instabug.h" | ||
#import "Instabug/IBGSurvey.h" | ||
#import <Instabug/IBGTypes.h> | ||
#import "RNInstabug.h" | ||
#import "RNInstabug/Instabug+CP.h" | ||
#import "RNInstabug/IBGNetworkLogger+CP.h" | ||
#import "IBGConstants.h" | ||
|
||
@interface RNInstabugTests : XCTestCase | ||
|
||
@property (nonatomic, strong) id mInstabug; | ||
@property (nonatomic, strong) id mIBGNetworkLogger; | ||
|
||
@end | ||
|
||
// Expose the `reset` API on RNInstabug to allow multiple calls to `initWithToken`. | ||
@interface RNInstabug (Test) | ||
+ (void)reset; | ||
@end | ||
|
||
@implementation RNInstabugTests | ||
|
||
- (void)setUp { | ||
self.mInstabug = OCMClassMock([Instabug class]); | ||
self.mIBGNetworkLogger = OCMClassMock([IBGNetworkLogger class]); | ||
|
||
[RNInstabug reset]; | ||
} | ||
|
||
- (void)testInitWithoutLogsLevel { | ||
NSString *token = @"app-token"; | ||
IBGInvocationEvent invocationEvents = IBGInvocationEventFloatingButton | IBGInvocationEventShake; | ||
|
||
[RNInstabug initWithToken:token invocationEvents:invocationEvents]; | ||
|
||
OCMVerify([self.mInstabug startWithToken:token invocationEvents:invocationEvents]); | ||
OCMVerify([self.mInstabug setCurrentPlatform:IBGPlatformReactNative]); | ||
OCMVerify([self.mIBGNetworkLogger disableAutomaticCapturingOfNetworkLogs]); | ||
OCMVerify([self.mIBGNetworkLogger setEnabled:YES]); | ||
} | ||
|
||
- (void)testInitStartsOnce { | ||
NSString *token = @"app-token"; | ||
IBGInvocationEvent invocationEvents = IBGInvocationEventFloatingButton | IBGInvocationEventShake; | ||
|
||
// Call init twice to check that only 1 call to native methods happens. | ||
[RNInstabug initWithToken:token invocationEvents:invocationEvents]; | ||
[RNInstabug initWithToken:token invocationEvents:invocationEvents]; | ||
|
||
OCMVerify(times(1), [self.mInstabug startWithToken:token invocationEvents:invocationEvents]); | ||
OCMVerify(times(1), [self.mInstabug setCurrentPlatform:IBGPlatformReactNative]); | ||
OCMVerify(times(1), [self.mIBGNetworkLogger disableAutomaticCapturingOfNetworkLogs]); | ||
OCMVerify(times(1), [self.mIBGNetworkLogger setEnabled:YES]); | ||
} | ||
|
||
- (void)testInitWithLogsLevel { | ||
NSString *token = @"app-token"; | ||
IBGInvocationEvent invocationEvents = IBGInvocationEventFloatingButton | IBGInvocationEventShake; | ||
IBGSDKDebugLogsLevel debugLogsLevel = IBGSDKDebugLogsLevelDebug; | ||
|
||
[RNInstabug initWithToken:token invocationEvents:invocationEvents debugLogsLevel:debugLogsLevel]; | ||
|
||
OCMVerify([self.mInstabug startWithToken:token invocationEvents:invocationEvents]); | ||
OCMVerify([self.mInstabug setCurrentPlatform:IBGPlatformReactNative]); | ||
OCMVerify([self.mIBGNetworkLogger disableAutomaticCapturingOfNetworkLogs]); | ||
OCMVerify([self.mIBGNetworkLogger setEnabled:YES]); | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#ifndef RNInstabug_h | ||
#define RNInstabug_h | ||
|
||
#import <Instabug/Instabug.h> | ||
|
||
@interface RNInstabug : NSObject | ||
|
||
+ (void)initWithToken:(NSString *)token invocationEvents:(IBGInvocationEvent)invocationEvents debugLogsLevel:(IBGSDKDebugLogsLevel)debugLogsLevel; | ||
+ (void)initWithToken:(NSString *)token invocationEvents:(IBGInvocationEvent)invocationEvents; | ||
|
||
@end | ||
|
||
#endif /* RNInstabug_h */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
#import <Instabug/Instabug.h> | ||
#import <React/RCTLog.h> | ||
#import "RNInstabug.h" | ||
#import "Util/IBGNetworkLogger+CP.h" | ||
#import "Util/Instabug+CP.h" | ||
|
||
@implementation RNInstabug | ||
|
||
static BOOL didInit = NO; | ||
|
||
/// Resets `didInit` allowing re-initialization, it should not be added to the header file and is there for testing purposes. | ||
+ (void)reset { | ||
didInit = NO; | ||
} | ||
|
||
+ (void)initWithToken:(NSString *)token invocationEvents:(IBGInvocationEvent)invocationEvents { | ||
// Initialization is performed only once to avoid unexpected behavior. | ||
if (didInit) { | ||
NSLog(@"IBG-RN: Skipped iOS SDK re-initialization"); | ||
return; | ||
} | ||
|
||
didInit = YES; | ||
|
||
[Instabug setCurrentPlatform:IBGPlatformReactNative]; | ||
|
||
// Disable automatic network logging in the iOS SDK to avoid duplicate network logs coming | ||
// from both the iOS and React Native SDKs | ||
[IBGNetworkLogger disableAutomaticCapturingOfNetworkLogs]; | ||
|
||
[Instabug startWithToken:token invocationEvents:invocationEvents]; | ||
|
||
// Setup automatic capturing of JavaScript console logs | ||
RCTAddLogFunction(InstabugReactLogFunction); | ||
RCTSetLogThreshold(RCTLogLevelInfo); | ||
|
||
// Even though automatic network logging is disabled in the iOS SDK, the network logger itself | ||
// is still needed since network logs captured by the React Native SDK need to be logged through it | ||
IBGNetworkLogger.enabled = YES; | ||
|
||
// Temporarily disabling APM hot launches | ||
IBGAPM.hotAppLaunchEnabled = NO; | ||
} | ||
|
||
+ (void)initWithToken:(NSString *)token | ||
invocationEvents:(IBGInvocationEvent)invocationEvents | ||
debugLogsLevel:(IBGSDKDebugLogsLevel)debugLogsLevel { | ||
[Instabug setSdkDebugLogsLevel:debugLogsLevel]; | ||
[self initWithToken:token invocationEvents:invocationEvents]; | ||
} | ||
|
||
// Note: This function is used to bridge IBGNSLog with RCTLogFunction. | ||
// This log function should not be used externally and is only an implementation detail. | ||
void RNIBGLog(IBGLogLevel logLevel, NSString *format, ...) { | ||
va_list arg_list; | ||
va_start(arg_list, format); | ||
IBGNSLogWithLevel(format, arg_list, logLevel); | ||
va_end(arg_list); | ||
} | ||
|
||
RCTLogFunction InstabugReactLogFunction = ^(RCTLogLevel level, | ||
__unused RCTLogSource source, | ||
NSString *fileName, | ||
NSNumber *lineNumber, | ||
NSString *message) | ||
{ | ||
NSString *formatString = @"Instabug - REACT LOG: %@"; | ||
NSString *log = RCTFormatLog([NSDate date], level, fileName, lineNumber, message); | ||
|
||
switch(level) { | ||
case RCTLogLevelTrace: | ||
RNIBGLog(IBGLogLevelVerbose, formatString, log); | ||
break; | ||
case RCTLogLevelInfo: | ||
RNIBGLog(IBGLogLevelInfo, formatString, log); | ||
break; | ||
case RCTLogLevelWarning: | ||
RNIBGLog(IBGLogLevelWarning, formatString, log); | ||
break; | ||
case RCTLogLevelError: | ||
RNIBGLog(IBGLogLevelError, formatString, log); | ||
break; | ||
case RCTLogLevelFatal: | ||
RNIBGLog(IBGLogLevelError, formatString, log); | ||
break; | ||
} | ||
}; | ||
|
||
@end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#import <Instabug/Instabug.h> | ||
#import <Instabug/IBGTypes.h> | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
@interface Instabug (CP) | ||
|
||
+ (void)setCurrentPlatform:(IBGPlatform)platform; | ||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END |