Skip to content

Commit 0b17401

Browse files
authored
[MOB-10611] Add new APIs (#830)
* Add `Instabug.setEnabled` API * Add `Instabug.setEnabled` unit test * Deprecate `Instabug.enable` and `Instabug.disable` * Add `setDisclaimerText` API * Add `setDisclaimerText` unit test * Add `setCommentMinimumCharacterCount` API * Add `setCommentMinimumCharacterCount` unit test * Update CHANGELOG.md * Add `setDisclaimerText` native tests * Add `setEnabled` native tests * Add `setCommentMinimumCharacterCount` native tests
1 parent 130db30 commit 0b17401

17 files changed

+236
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
## Unreleased
22

33
- Adds first-class TypeScript support.
4+
- Adds BugReporting.setDisclaimerText API
5+
- Adds BugReporting.setCommentMinimumCharacterCount API
6+
- Deprecates Instabug.enable and Instabug.disable APIs in favour of a new API Instabug.setEnabled, which works on both platforms
47
- Fixes a compilation error on Android projects without buildToolsVersion property set.
58

69
## 11.3.0 (2022-10-11)

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,5 +366,45 @@ public void run() {
366366
});
367367
}
368368

369+
/**
370+
* Adds a disclaimer text within the bug reporting form, which can include hyperlinked text.
371+
* @param text String text.
372+
*/
373+
@ReactMethod
374+
public void setDisclaimerText(final String text){
375+
MainThreadHandler.runOnMainThread(new Runnable() {
376+
@Override
377+
public void run() {
378+
BugReporting.setDisclaimerText(text);
379+
}
380+
});
381+
}
382+
383+
/**
384+
* Sets a minimum number of characters as a requirement for the comments field in the different report types.
385+
* @param limit int number of characters.
386+
* @param reportTypes (Optional) Array of reportType. If it's not passed, the limit will apply to all report types.
387+
*/
388+
@ReactMethod
389+
public void setCommentMinimumCharacterCount(final int limit, final ReadableArray reportTypes){
390+
MainThreadHandler.runOnMainThread(new Runnable() {
391+
@SuppressLint("WrongConstant")
392+
@Override
393+
public void run() {
394+
try {
395+
final ArrayList<String> keys = ArrayUtil.parseReadableArrayOfStrings(reportTypes);
396+
final ArrayList<Integer> types = ArgsRegistry.reportTypes.getAll(keys);
397+
398+
final int[] typesInts = new int[types.size()];
399+
for (int i = 0; i < types.size(); i++) {
400+
typesInts[i] = types.get(i);
401+
}
369402

403+
BugReporting.setCommentMinimumCharacterCount(limit, typesInts);
404+
} catch (Exception e) {
405+
e.printStackTrace();
406+
}
407+
}
408+
});
409+
}
370410
}

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,27 @@ public RNInstabugReactnativeModule(ReactApplicationContext reactContext) {
105105
public String getName() {
106106
return "Instabug";
107107
}
108+
109+
/**
110+
* Enables or disables Instabug functionality.
111+
* @param isEnabled A boolean to enable/disable Instabug.
112+
*/
113+
@ReactMethod
114+
public void setEnabled(final boolean isEnabled) {
115+
MainThreadHandler.runOnMainThread(new Runnable() {
116+
@Override
117+
public void run() {
118+
try {
119+
if(isEnabled)
120+
Instabug.enable();
121+
else
122+
Instabug.disable();
123+
} catch (Exception e) {
124+
e.printStackTrace();
125+
}
126+
}
127+
});
128+
}
108129

109130
/**
110131
* Starts the SDK.

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,4 +335,36 @@ public Object answer(InvocationOnMock invocation) {
335335
BugReporting.show(reportTypeArgs.get(reportTypeKeys[0]));
336336
}
337337

338+
@Test
339+
public void givenArgs$setDisclaimerText_whenQuery_shouldCallNativeApiWithArgs() {
340+
// given
341+
String text = "This is a disclaimer text!";
342+
343+
// when
344+
bugReportingModule.setDisclaimerText(text);
345+
346+
// then
347+
verify(BugReporting.class, VerificationModeFactory.times(1));
348+
349+
BugReporting.setDisclaimerText(text);
350+
}
351+
352+
@Test
353+
public void givenArgs$setCommentMinimumCharacterCount_whenQuery_thenShouldCallNativeApiWithArgs() {
354+
// given
355+
final int count = 20;
356+
final Map<String, Integer> args = ArgsRegistry.reportTypes;
357+
final String[] keysArray = args.keySet().toArray(new String[0]);
358+
JavaOnlyArray actualArray = new JavaOnlyArray();
359+
actualArray.pushString(keysArray[0]);
360+
361+
// when
362+
bugReportingModule.setCommentMinimumCharacterCount(count, actualArray);
363+
364+
// then
365+
verify(BugReporting.class, VerificationModeFactory.times(1));
366+
int type1 = args.get(keysArray[0]);
367+
368+
BugReporting.setCommentMinimumCharacterCount(count, type1);
369+
}
338370
}

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,29 @@ public void tearDown() {
9393

9494
/********Instabug*********/
9595

96+
97+
@Test
98+
public void givenTrue$setEnabled_whenQuery_thenShouldCallNativeApi() {
99+
// given
100+
101+
// when
102+
rnModule.setEnabled(true);
103+
// then
104+
verify(Instabug.class, times(1));
105+
Instabug.enable();
106+
}
107+
108+
@Test
109+
public void givenFalse$setEnabled_whenQuery_thenShouldCallNativeApi() {
110+
// given
111+
112+
// when
113+
rnModule.setEnabled(false);
114+
// then
115+
verify(Instabug.class, times(1));
116+
Instabug.disable();
117+
}
118+
96119
@Test
97120
public void givenBoolean$setDebugEnabled_whenQuery_thenShouldCallNativeApi() {
98121
// when

example/ios/InstabugSampleTests/InstabugBugReportingTests.m

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,5 +177,27 @@ - (void) testgivenBoolean$setViewHierarchyEnabled_whenQuery_thenShouldCallNative
177177
XCTAssertTrue(IBGBugReporting.shouldCaptureViewHierarchy);
178178
}
179179

180+
- (void) testSetDisclaimerText {
181+
id mock = OCMClassMock([IBGBugReporting class]);
182+
NSString *text = @"This is a disclaimer text!";
183+
184+
OCMStub([mock setDisclaimerText:text]);
185+
[self.instabugBridge setDisclaimerText:text];
186+
OCMVerify([mock setDisclaimerText:text]);
187+
}
188+
189+
- (void) testSetCommentMinimumCharacterCount {
190+
id mock = OCMClassMock([IBGBugReporting class]);
191+
NSNumber *limit = [NSNumber numberWithInt:20];
192+
NSArray *reportTypesArr = [NSArray arrayWithObjects: @(IBGReportTypeBug), nil];
193+
IBGBugReportingReportType reportTypes = 0;
194+
for (NSNumber *reportType in reportTypesArr) {
195+
reportTypes |= [reportType intValue];
196+
}
197+
OCMStub([mock setCommentMinimumCharacterCountForReportTypes:reportTypes withLimit:limit.intValue]);
198+
[self.instabugBridge setCommentMinimumCharacterCount:limit reportTypes:reportTypesArr];
199+
OCMVerify([mock setCommentMinimumCharacterCountForReportTypes:reportTypes withLimit:limit.intValue]);
200+
}
201+
180202
@end
181203

example/ios/InstabugSampleTests/InstabugSampleTests.m

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ - (void)setUp {
5252
+------------------------------------------------------------------------+
5353
*/
5454

55+
- (void)testSetEnabled {
56+
id mock = OCMClassMock([Instabug class]);
57+
BOOL isEnabled = true;
58+
59+
OCMStub([mock setEnabled:isEnabled]);
60+
[self.instabugBridge setEnabled:isEnabled];
61+
OCMVerify([mock setEnabled:isEnabled]);
62+
}
63+
5564
- (void)testStart {
5665
id<InstabugCPTestProtocol> mock = OCMClassMock([Instabug class]);
5766
IBGInvocationEvent floatingButtonInvocationEvent = IBGInvocationEventFloatingButton;

ios/RNInstabug/InstabugBugReportingBridge.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,8 @@
4747

4848
- (void)setViewHierarchyEnabled:(BOOL)viewHirearchyEnabled;
4949

50+
- (void)setDisclaimerText:(NSString *)text;
51+
52+
- (void)setCommentMinimumCharacterCount:(NSNumber *)limit reportTypes:(NSArray *)reportTypes;
53+
5054
@end

ios/RNInstabug/InstabugBugReportingBridge.m

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,24 @@ - (void) showBugReportingWithReportTypeAndOptionsHelper:(NSArray*)args {
201201
IBGBugReporting.shakingThresholdForiPad = iPadShakingThreshold;
202202
}
203203

204+
RCT_EXPORT_METHOD(setDisclaimerText:(NSString*)text) {
205+
[IBGBugReporting setDisclaimerText:text];
206+
}
207+
208+
RCT_EXPORT_METHOD(setCommentMinimumCharacterCount:(nonnull NSNumber *)limit reportTypes:(NSArray *)reportTypes) {
209+
IBGBugReportingReportType parsedReportTypes = 0;
210+
211+
if (![reportTypes count]) {
212+
parsedReportTypes = @(IBGBugReportingReportTypeBug).integerValue | @(IBGBugReportingReportTypeFeedback).integerValue | @(IBGBugReportingReportTypeQuestion).integerValue;
213+
}
214+
else {
215+
for (NSNumber *reportType in reportTypes) {
216+
parsedReportTypes |= [reportType intValue];
217+
}
218+
}
219+
220+
[IBGBugReporting setCommentMinimumCharacterCountForReportTypes:parsedReportTypes withLimit:limit.intValue];
221+
}
204222

205223
@synthesize description;
206224

ios/RNInstabug/InstabugReactBridge.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
+------------------------------------------------------------------------+
2626
*/
2727

28+
- (void)setEnabled:(BOOL)isEnabled;
29+
2830
- (void)start:(NSString *)token invocationEvents:(NSArray *)invocationEventsArray;
2931

3032
- (void)setUserData:(NSString *)userData;

0 commit comments

Comments
 (0)