Skip to content

Commit 009d39f

Browse files
Merge branch 'feat/support-code-push' into snapshot-d-resolve-crashes-11
2 parents f83cd54 + 7088d0d commit 009d39f

File tree

13 files changed

+303
-8
lines changed

13 files changed

+303
-8
lines changed

android/src/main/java/com/instabug/reactlibrary/RNInstabug.java

Lines changed: 114 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
public class RNInstabug {
1919

2020
private static RNInstabug instance;
21-
21+
2222
private RNInstabug() {}
2323

2424

@@ -133,4 +133,117 @@ public void setBaseUrlForDeprecationLogs() {
133133
e.printStackTrace();
134134
}
135135
}
136+
137+
public static class Builder {
138+
139+
/**
140+
* Application instance to initialize Instabug.
141+
*/
142+
private Application application;
143+
144+
/**
145+
* The application token obtained from the Instabug dashboard.
146+
*/
147+
private String applicationToken;
148+
149+
/**
150+
* The level of detail in logs that you want to print.
151+
*/
152+
private int logLevel = LogLevel.ERROR;
153+
154+
/**
155+
* The Code Push version to be used for all reports.
156+
*/
157+
private String codePushVersion;
158+
159+
/**
160+
* The events that trigger the SDK's user interface.
161+
*/
162+
private InstabugInvocationEvent[] invocationEvents;
163+
164+
165+
/**
166+
* Initialize Instabug SDK with application token
167+
*
168+
* @param application Application object for initialization of library
169+
* @param applicationToken The app's identifying token, available on your dashboard.
170+
*/
171+
public Builder(Application application, String applicationToken) {
172+
this.application = application;
173+
this.applicationToken = applicationToken;
174+
}
175+
176+
/**
177+
* Initialize Instabug SDK with application token and invocation trigger events
178+
*
179+
* @param application Application object for initialization of library
180+
* @param applicationToken The app's identifying token, available on your dashboard.
181+
* @param invocationEvents The events that trigger the SDK's user interface.
182+
* <p>Choose from the available events listed in {@link InstabugInvocationEvent}.</p>
183+
*/
184+
public Builder(Application application, String applicationToken, InstabugInvocationEvent... invocationEvents) {
185+
this.application = application;
186+
this.applicationToken = applicationToken;
187+
this.invocationEvents = invocationEvents;
188+
}
189+
190+
/**
191+
* Sets the filtering level for printed SDK logs.
192+
*
193+
* @param logLevel The log filtering level to be set.
194+
* Choose from {@link LogLevel} constants:
195+
* {@link LogLevel#NONE}, {@link LogLevel#ERROR}, {@link LogLevel#DEBUG}, or {@link LogLevel#VERBOSE}.
196+
* <p>Default level is {@link LogLevel#ERROR}.</p>
197+
*/
198+
public Builder setLogLevel(int logLevel) {
199+
this.logLevel = logLevel;
200+
return this;
201+
}
202+
203+
/**
204+
* Sets Code Push version to be used for all reports.
205+
*
206+
* @param codePushVersion the Code Push version to work with.
207+
*/
208+
public Builder setCodePushVersion(String codePushVersion) {
209+
this.codePushVersion = codePushVersion;
210+
return this;
211+
}
212+
213+
/**
214+
* Sets the invocation triggering events for the SDK's user interface
215+
*
216+
* @param invocationEvents The events that trigger the SDK's user interface.
217+
* Choose from the available events listed in {@link InstabugInvocationEvent}.
218+
*/
219+
public Builder setInvocationEvents(InstabugInvocationEvent... invocationEvents) {
220+
this.invocationEvents = invocationEvents;
221+
return this;
222+
}
223+
224+
/**
225+
* Builds the Instabug instance with the provided configurations.
226+
*/
227+
public void build() {
228+
try {
229+
RNInstabug.getInstance().setBaseUrlForDeprecationLogs();
230+
RNInstabug.getInstance().setCurrentPlatform();
231+
232+
Instabug.Builder instabugBuilder = new Instabug.Builder(application, applicationToken)
233+
.setInvocationEvents(invocationEvents)
234+
.setSdkDebugLogsLevel(logLevel);
235+
236+
if (codePushVersion != null) {
237+
instabugBuilder.setCodePushVersion(codePushVersion);
238+
}
239+
240+
instabugBuilder.build();
241+
242+
// Temporarily disabling APM hot launches
243+
APM.setHotAppLaunchEnabled(false);
244+
} catch (Exception e) {
245+
e.printStackTrace();
246+
}
247+
}
248+
}
136249
}

android/src/main/java/com/instabug/reactlibrary/RNInstabugReactnativeModule.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
import java.util.Locale;
5151
import java.util.Map;
5252

53+
import javax.annotation.Nullable;
54+
5355

5456
/**
5557
* The type Rn instabug reactnative module.
@@ -117,9 +119,16 @@ public void run() {
117119
* Initializes the SDK.
118120
* @param token The token that identifies the app. You can find it on your dashboard.
119121
* @param invocationEventValues The events that invoke the SDK's UI.
122+
* @param logLevel The level of detail in logs that you want to print.
123+
* @param codePushVersion The Code Push version to be used for all reports.
120124
*/
121125
@ReactMethod
122-
public void init(final String token, final ReadableArray invocationEventValues, final String logLevel) {
126+
public void init(
127+
final String token,
128+
final ReadableArray invocationEventValues,
129+
final String logLevel,
130+
@Nullable final String codePushVersion
131+
) {
123132
MainThreadHandler.runOnMainThread(new Runnable() {
124133
@Override
125134
public void run() {
@@ -130,7 +139,14 @@ public void run() {
130139

131140
final Application application = (Application) reactContext.getApplicationContext();
132141

133-
RNInstabug.getInstance().init(application, token, parsedLogLevel, invocationEvents);
142+
RNInstabug.Builder builder = new RNInstabug.Builder(application, token)
143+
.setInvocationEvents(invocationEvents)
144+
.setLogLevel(parsedLogLevel);
145+
146+
if(codePushVersion != null) {
147+
builder.setCodePushVersion(codePushVersion);
148+
}
149+
builder.build();
134150
}
135151
});
136152
}

android/src/test/java/com/instabug/reactlibrary/RNInstabugTest.java

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import static org.mockito.Mockito.mockStatic;
1111
import static org.mockito.Mockito.spy;
1212
import static org.mockito.Mockito.verify;
13+
import static org.mockito.Mockito.verifyNoMoreInteractions;
1314
import static org.mockito.Mockito.when;
1415

1516
import android.app.Application;
@@ -112,6 +113,127 @@ public void testSetDeprecationBaseUrl() {
112113

113114
reflected.verify(() -> MockReflected.setBaseUrl(any()));
114115
}
116+
117+
@Test
118+
public void testBuildWithoutLogLevelShouldSetLogLevelToError() {
119+
final String token = "fde....";
120+
121+
122+
MockedConstruction<Instabug.Builder> mInstabugBuilder = mockConstruction(Instabug.Builder.class, (mock, context) -> {
123+
String actualToken = (String) context.arguments().get(1);
124+
// Initializes Instabug with the correct token
125+
assertEquals(token, actualToken);
126+
when(mock.setSdkDebugLogsLevel(anyInt())).thenReturn(mock);
127+
when(mock.setInvocationEvents(any())).thenReturn(mock);
128+
});
129+
130+
new RNInstabug.Builder(mContext, token)
131+
.build();
132+
133+
Instabug.Builder builder = mInstabugBuilder.constructed().get(0);
134+
135+
verify(builder).setSdkDebugLogsLevel(LogLevel.ERROR);
136+
verify(builder).setInvocationEvents(null);
137+
verify(builder).build();
138+
139+
140+
// verify(sut).setBaseUrlForDeprecationLogs();
141+
// verify(sut).setCurrentPlatform();
142+
143+
144+
mInstabugBuilder.close();
145+
}
146+
147+
@Test
148+
public void testBuildWithVerboseLogLevel() {
149+
final String token = "fde....";
150+
final int logLevel = LogLevel.VERBOSE;
151+
152+
153+
MockedConstruction<Instabug.Builder> mInstabugBuilder = mockConstruction(Instabug.Builder.class, (mock, context) -> {
154+
String actualToken = (String) context.arguments().get(1);
155+
// Initializes Instabug with the correct token
156+
assertEquals(token, actualToken);
157+
when(mock.setSdkDebugLogsLevel(anyInt())).thenReturn(mock);
158+
when(mock.setInvocationEvents(any())).thenReturn(mock);
159+
});
160+
161+
new RNInstabug.Builder(mContext, token)
162+
.setLogLevel(logLevel)
163+
.build();
164+
165+
Instabug.Builder builder = mInstabugBuilder.constructed().get(0);
166+
167+
verify(builder).setSdkDebugLogsLevel(logLevel);
168+
verify(builder).setInvocationEvents(null);
169+
verify(builder).build();
170+
171+
172+
mInstabugBuilder.close();
173+
}
174+
175+
@Test
176+
public void testBuildWithInvocationEvents() {
177+
final InstabugInvocationEvent[] invocationEvents = new InstabugInvocationEvent[]{InstabugInvocationEvent.FLOATING_BUTTON};
178+
final String token = "fde....";
179+
180+
181+
MockedConstruction<Instabug.Builder> mInstabugBuilder = mockConstruction(Instabug.Builder.class, (mock, context) -> {
182+
String actualToken = (String) context.arguments().get(1);
183+
// Initializes Instabug with the correct token
184+
assertEquals(token, actualToken);
185+
when(mock.setSdkDebugLogsLevel(anyInt())).thenReturn(mock);
186+
when(mock.setInvocationEvents(any())).thenReturn(mock);
187+
});
188+
189+
new RNInstabug.Builder(mContext, token)
190+
.setInvocationEvents(invocationEvents)
191+
.build();
192+
193+
Instabug.Builder builder = mInstabugBuilder.constructed().get(0);
194+
195+
verify(builder).setSdkDebugLogsLevel(LogLevel.ERROR);
196+
verify(builder).setInvocationEvents(invocationEvents);
197+
verify(builder).build();
198+
199+
200+
mInstabugBuilder.close();
201+
}
202+
203+
@Test
204+
public void testBuildWithCodePushVersion() {
205+
final InstabugInvocationEvent[] invocationEvents = new InstabugInvocationEvent[]{InstabugInvocationEvent.FLOATING_BUTTON};
206+
final String token = "fde....";
207+
final String codePushVersion = "1.0.0(1)";
208+
209+
210+
MockedConstruction<Instabug.Builder> mInstabugBuilder = mockConstruction(Instabug.Builder.class, (mock, context) -> {
211+
String actualToken = (String) context.arguments().get(1);
212+
// Initializes Instabug with the correct token
213+
assertEquals(token, actualToken);
214+
when(mock.setSdkDebugLogsLevel(anyInt())).thenReturn(mock);
215+
when(mock.setInvocationEvents(any())).thenReturn(mock);
216+
when(mock.setCodePushVersion(any())).thenReturn(mock);
217+
});
218+
219+
new RNInstabug.Builder(mContext, token)
220+
.setLogLevel(LogLevel.ERROR)
221+
.setInvocationEvents(null)
222+
.setCodePushVersion(codePushVersion)
223+
.build();
224+
225+
Instabug.Builder builder = mInstabugBuilder.constructed().get(0);
226+
227+
verify(builder).setSdkDebugLogsLevel(LogLevel.ERROR);
228+
verify(builder).setCodePushVersion(codePushVersion);
229+
verify(builder).setInvocationEvents(null);
230+
verify(builder).build();
231+
232+
verifyNoMoreInteractions(sut);
233+
234+
235+
mInstabugBuilder.close();
236+
}
115237
}
116238

117239

examples/default/ios/InstabugTests/InstabugSampleTests.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,12 @@ - (void)testSetEnabled {
6767
- (void)testInit {
6868
IBGInvocationEvent floatingButtonInvocationEvent = IBGInvocationEventFloatingButton;
6969
NSString *appToken = @"app_token";
70+
NSString *codePushVersion = @"1.0.0(1)";
7071
NSArray *invocationEvents = [NSArray arrayWithObjects:[NSNumber numberWithInteger:floatingButtonInvocationEvent], nil];
7172
IBGSDKDebugLogsLevel sdkDebugLogsLevel = IBGSDKDebugLogsLevelDebug;
7273

73-
[self.instabugBridge init:appToken invocationEvents:invocationEvents debugLogsLevel:sdkDebugLogsLevel];
74+
[self.instabugBridge init:appToken invocationEvents:invocationEvents debugLogsLevel:sdkDebugLogsLevel codePushVersion:codePushVersion];
75+
OCMVerify([Instabug setCodePushVersion:codePushVersion]);
7476

7577
OCMVerify([self.mRNInstabug initWithToken:appToken invocationEvents:floatingButtonInvocationEvent debugLogsLevel:sdkDebugLogsLevel]);
7678
}

examples/default/ios/InstabugTests/RNInstabugTests.m

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,11 @@ - (void)testInitWithLogsLevel {
6868
OCMVerify([self.mIBGNetworkLogger setEnabled:YES]);
6969
}
7070

71+
- (void) testSetCodePushVersion {
72+
NSString *codePushVersion = @"1.0.0(1)";
73+
[RNInstabug setCodePushVersion:codePushVersion];
74+
75+
OCMVerify([self.mInstabug setCodePushVersion:codePushVersion]);
76+
}
77+
7178
@end

ios/RNInstabug/InstabugReactBridge.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
- (void)setEnabled:(BOOL)isEnabled;
2929

30-
- (void)init:(NSString *)token invocationEvents:(NSArray *)invocationEventsArray debugLogsLevel:(IBGSDKDebugLogsLevel)sdkDebugLogsLevel;
30+
- (void)init:(NSString *)token invocationEvents:(NSArray *)invocationEventsArray debugLogsLevel:(IBGSDKDebugLogsLevel)sdkDebugLogsLevel codePushVersion:(NSString *)codePushVersion;
3131

3232
- (void)setUserData:(NSString *)userData;
3333

ios/RNInstabug/InstabugReactBridge.m

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,21 @@ - (dispatch_queue_t)methodQueue {
3737
Instabug.enabled = isEnabled;
3838
}
3939

40-
RCT_EXPORT_METHOD(init:(NSString *)token invocationEvents:(NSArray*)invocationEventsArray debugLogsLevel:(IBGSDKDebugLogsLevel)sdkDebugLogsLevel) {
40+
RCT_EXPORT_METHOD(init:(NSString *)token
41+
invocationEvents:(NSArray *)invocationEventsArray
42+
debugLogsLevel:(IBGSDKDebugLogsLevel)sdkDebugLogsLevel
43+
codePushVersion:(NSString *)codePushVersion) {
4144
IBGInvocationEvent invocationEvents = 0;
4245

4346
for (NSNumber *boxedValue in invocationEventsArray) {
4447
invocationEvents |= [boxedValue intValue];
4548
}
4649

47-
[RNInstabug initWithToken:token invocationEvents:invocationEvents debugLogsLevel:sdkDebugLogsLevel];
50+
[Instabug setCodePushVersion:codePushVersion];
51+
52+
[RNInstabug initWithToken:token
53+
invocationEvents:invocationEvents
54+
debugLogsLevel:sdkDebugLogsLevel];
4855
}
4956

5057
RCT_EXPORT_METHOD(setReproStepsConfig:(IBGUserStepsMode)bugMode :(IBGUserStepsMode)crashMode:(IBGUserStepsMode)sessionReplayMode) {

ios/RNInstabug/RNInstabug.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,17 @@
88
+ (void)initWithToken:(NSString *)token invocationEvents:(IBGInvocationEvent)invocationEvents debugLogsLevel:(IBGSDKDebugLogsLevel)debugLogsLevel;
99
+ (void)initWithToken:(NSString *)token invocationEvents:(IBGInvocationEvent)invocationEvents;
1010

11+
/**
12+
@brief Set codePush version before starting the SDK.
13+
14+
@discussion Sets Code Push version to be used for all reports.
15+
should be called from `-[UIApplicationDelegate application:didFinishLaunchingWithOptions:]`
16+
and before `startWithToken`.
17+
18+
@param codePushVersion the Code Push version to be used for all reports.
19+
*/
20+
+ (void)setCodePushVersion:(NSString *)codePushVersion;
21+
1122
@end
1223

1324
#endif /* RNInstabug_h */

ios/RNInstabug/RNInstabug.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ + (void)initWithToken:(NSString *)token
4949
[self initWithToken:token invocationEvents:invocationEvents];
5050
}
5151

52+
+ (void)setCodePushVersion:(NSString *)codePushVersion {
53+
[Instabug setCodePushVersion:codePushVersion];
54+
}
55+
5256
// Note: This function is used to bridge IBGNSLog with RCTLogFunction.
5357
// This log function should not be used externally and is only an implementation detail.
5458
void RNIBGLog(IBGLogLevel logLevel, NSString *format, ...) {

0 commit comments

Comments
 (0)