Skip to content

Commit c7ecf92

Browse files
chore: adjust changelog entry for supporting user steps
Jira ID: MOB-13966
1 parent 5576338 commit c7ecf92

File tree

14 files changed

+221
-20
lines changed

14 files changed

+221
-20
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
### Added
66

77
- Support user identification using ID ([#1115](https://github.com/Instabug/Instabug-React-Native/pull/1115)).
8-
- Support button detection and label extraction for repro steps ([#1109](https://github.com/Instabug/Instabug-React-Native/pull/1109)).
8+
- Add support for user steps on Android ([#1109](https://github.com/Instabug/Instabug-React-Native/pull/1109)).
99

1010
### Changed
1111

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

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
final class Constants {
44
final static String IBG_PRE_INVOCATION_HANDLER = "IBGpreInvocationHandler";
55
final static String IBG_POST_INVOCATION_HANDLER = "IBGpostInvocationHandler";
6+
final static String IBG_NETWORK_DIAGNOSTICS_HANDLER = "IBGNetworkDiagnosticsHandler";
67

78
final static String IBG_ON_SHOW_SURVEY_HANDLER = "IBGWillShowSurvey";
89
final static String IBG_ON_DISMISS_SURVEY_HANDLER = "IBGDidDismissSurvey";

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

+36
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import android.view.View;
99

1010
import androidx.annotation.UiThread;
11+
import androidx.annotation.NonNull;
1112

1213
import com.facebook.react.bridge.Arguments;
1314
import com.facebook.react.bridge.Callback;
@@ -33,6 +34,7 @@
3334
import com.instabug.library.logging.InstabugLog;
3435
import com.instabug.library.model.NetworkLog;
3536
import com.instabug.library.model.Report;
37+
import com.instabug.library.networkDiagnostics.model.NetworkDiagnosticsCallback;
3638
import com.instabug.library.ui.onboarding.WelcomeMessage;
3739
import com.instabug.reactlibrary.utils.ArrayUtil;
3840
import com.instabug.reactlibrary.utils.EventEmitterModule;
@@ -44,6 +46,7 @@
4446
import org.json.JSONTokener;
4547

4648
import java.io.File;
49+
import java.lang.reflect.InvocationTargetException;
4750
import java.lang.reflect.Method;
4851
import java.util.ArrayList;
4952
import java.util.Arrays;
@@ -1042,6 +1045,39 @@ public void run() {
10421045
});
10431046
}
10441047

1048+
@ReactMethod
1049+
public void setOnNetworkDiagnosticsHandler() {
1050+
MainThreadHandler.runOnMainThread(new Runnable() {
1051+
@Override
1052+
public void run() {
1053+
try {
1054+
Method method = getMethod(Class.forName("com.instabug.library.Instabug"), "setNetworkDiagnosticsCallback", NetworkDiagnosticsCallback.class);
1055+
1056+
if (method != null) {
1057+
method.invoke(null, new NetworkDiagnosticsCallback() {
1058+
@Override
1059+
public void onReady(@NonNull String date, int totalRequestCount, int failureCount) {
1060+
try {
1061+
WritableMap params = Arguments.createMap();
1062+
params.putString("date", date);
1063+
params.putInt("totalRequestCount", totalRequestCount);
1064+
params.putInt("failureCount", failureCount);
1065+
1066+
sendEvent(Constants.IBG_NETWORK_DIAGNOSTICS_HANDLER, params);
1067+
} catch (Exception e) {
1068+
e.printStackTrace();
1069+
}
1070+
}
1071+
});
1072+
}
1073+
} catch (ClassNotFoundException | IllegalAccessException |
1074+
InvocationTargetException e) {
1075+
e.printStackTrace();
1076+
}
1077+
}
1078+
});
1079+
}
1080+
10451081
/**
10461082
* Map between the exported JS constant and the arg key in {@link ArgsRegistry}.
10471083
* The constant name and the arg key should match to be able to resolve the

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

+51-17
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.facebook.react.bridge.JavaOnlyArray;
1010
import com.facebook.react.bridge.JavaOnlyMap;
1111
import com.facebook.react.bridge.Promise;
12+
import com.facebook.react.bridge.ReactApplicationContext;
1213
import com.facebook.react.bridge.WritableArray;
1314
import com.facebook.react.bridge.WritableMap;
1415
import com.instabug.library.Feature;
@@ -19,7 +20,10 @@
1920
import com.instabug.library.ReproConfigurations;
2021
import com.instabug.library.ReproMode;
2122
import com.instabug.library.internal.module.InstabugLocale;
23+
import com.instabug.library.networkDiagnostics.model.NetworkDiagnosticsCallback;
2224
import com.instabug.library.ui.onboarding.WelcomeMessage;
25+
import com.instabug.reactlibrary.util.GlobalMocks;
26+
import com.instabug.reactlibrary.util.MockReflected;
2327
import com.instabug.reactlibrary.utils.MainThreadHandler;
2428

2529
import org.junit.After;
@@ -37,6 +41,7 @@
3741
import java.lang.reflect.Field;
3842
import java.lang.reflect.Method;
3943
import java.util.ArrayList;
44+
import java.util.Date;
4045
import java.util.HashMap;
4146
import java.util.List;
4247
import java.util.Locale;
@@ -45,18 +50,21 @@
4550
import java.util.concurrent.Executors;
4651
import java.util.concurrent.ScheduledExecutorService;
4752

53+
import static com.instabug.reactlibrary.util.GlobalMocks.reflected;
4854
import static org.mockito.ArgumentMatchers.anyInt;
4955
import static org.mockito.Matchers.any;
5056
import static org.mockito.Mockito.mock;
5157
import static org.mockito.Mockito.mockConstruction;
5258
import static org.mockito.Mockito.mockStatic;
59+
import static org.mockito.Mockito.spy;
5360
import static org.mockito.Mockito.times;
5461
import static org.mockito.Mockito.verify;
5562
import static org.mockito.Mockito.when;
5663

5764

5865
public class RNInstabugReactnativeModuleTest {
59-
private RNInstabugReactnativeModule rnModule = new RNInstabugReactnativeModule(null);
66+
private RNInstabugReactnativeModule rnModule;
67+
private ReactApplicationContext mReactContext = mock(ReactApplicationContext.class);
6068

6169
private final static ScheduledExecutorService mainThread = Executors.newSingleThreadScheduledExecutor();
6270

@@ -66,7 +74,9 @@ public class RNInstabugReactnativeModuleTest {
6674
private MockedStatic <Instabug> mockInstabug;
6775

6876
@Before
69-
public void mockMainThreadHandler() throws Exception {
77+
public void setUp() throws Exception {
78+
rnModule = spy(new RNInstabugReactnativeModule(mReactContext));
79+
7080
// Mock static functions
7181
mockInstabug = mockStatic(Instabug.class);
7282
mockLooper = mockStatic(Looper.class);
@@ -86,13 +96,19 @@ public Boolean answer(InvocationOnMock invocation) throws Throwable {
8696
};
8797
Mockito.doAnswer(handlerPostAnswer).when(MainThreadHandler.class);
8898
MainThreadHandler.runOnMainThread(any(Runnable.class));
99+
100+
// Set up global mocks
101+
GlobalMocks.setUp();
89102
}
90103
@After
91104
public void tearDown() {
92105
// Remove static mocks
93106
mockLooper.close();
94107
mockMainThreadHandler.close();
95108
mockInstabug.close();
109+
110+
// Remove global mocks
111+
GlobalMocks.close();
96112
}
97113

98114
/********Instabug*********/
@@ -496,28 +512,17 @@ public void testIdentifyUserWithId() {
496512
}
497513

498514
@Test
499-
public void givenString$reportCurrentViewChange_whenQuery_thenShouldCallNativeApiWithString() throws Exception {
500-
// when
515+
public void testReportCurrentViewChange() {
501516
rnModule.reportCurrentViewChange("screen");
502-
Method privateStringMethod = getMethod(Class.forName("com.instabug.library.Instabug"), "reportCurrentViewChange", String.class);
503-
privateStringMethod.setAccessible(true);
504517

505-
// then
506-
verify(Instabug.class, VerificationModeFactory.times(1));
507-
privateStringMethod.invoke("reportCurrentViewChange","screen");
518+
reflected.verify(() -> MockReflected.reportCurrentViewChange("screen"), times(1));
508519
}
509520

510521
@Test
511-
public void givenString$reportScreenChange_whenQuery_thenShouldCallNativeApiWithString() throws Exception {
512-
// when
522+
public void testReportScreenChange() {
513523
rnModule.reportScreenChange("screen");
514-
Method privateStringMethod = getMethod(Class.forName("com.instabug.library.Instabug"), "reportScreenChange", Bitmap.class, String.class);
515-
privateStringMethod.setAccessible(true);
516-
517-
// then
518-
verify(Instabug.class, VerificationModeFactory.times(1));
519-
privateStringMethod.invoke("reportScreenChange", null,"screen");
520524

525+
reflected.verify(() -> MockReflected.reportScreenChange(null, "screen"), times(1));
521526
}
522527

523528
@Test
@@ -567,4 +572,33 @@ public void testIdentifyUserWithId() {
567572
verify(Instabug.class,times(1));
568573
Instabug.clearAllExperiments();
569574
}
575+
576+
@Test
577+
public void testSetOnNetworkDiagnosticsHandler() {
578+
String date = new Date().toString();
579+
int successOrderCount = 2;
580+
int failureCount = 1;
581+
582+
MockedStatic<Arguments> mockArgument = mockStatic(Arguments.class);
583+
mockArgument.when(Arguments::createMap).thenReturn(new JavaOnlyMap());
584+
585+
reflected
586+
.when(() -> MockReflected.setNetworkDiagnosticsCallback(any(NetworkDiagnosticsCallback.class)))
587+
.thenAnswer((InvocationOnMock invocation) -> {
588+
NetworkDiagnosticsCallback callback = invocation.getArgument(0);
589+
callback.onReady(date, successOrderCount, failureCount);
590+
return null;
591+
});
592+
593+
rnModule.setOnNetworkDiagnosticsHandler();
594+
595+
WritableMap params = new JavaOnlyMap();
596+
params.putString("date", date);
597+
params.putInt("totalRequestCount", successOrderCount);
598+
params.putInt("failureCount", failureCount);
599+
600+
verify(rnModule).sendEvent(Constants.IBG_NETWORK_DIAGNOSTICS_HANDLER, params);
601+
602+
mockArgument.close();
603+
}
570604
}

android/src/test/java/com/instabug/reactlibrary/util/GlobalMocks.java

+26
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package com.instabug.reactlibrary.util;
22

3+
import static com.instabug.reactlibrary.utils.InstabugUtil.getMethod;
34
import static org.mockito.Mockito.mockStatic;
45

6+
import android.graphics.Bitmap;
57
import android.util.Log;
68

9+
import com.instabug.library.networkDiagnostics.model.NetworkDiagnosticsCallback;
710
import com.instabug.reactlibrary.utils.InstabugUtil;
811

912
import org.mockito.MockedStatic;
@@ -37,6 +40,29 @@ public static void setUp() throws NoSuchMethodException {
3740
reflection
3841
.when(() -> InstabugUtil.getMethod(Class.forName("com.instabug.library.util.InstabugDeprecationLogger"), "setBaseUrl", String.class))
3942
.thenReturn(mSetBaseUrl);
43+
44+
// setNetworkDiagnosticsCallback mock
45+
Method mSetNetworkDiagnosticsCallback = MockReflected.class.getDeclaredMethod("setNetworkDiagnosticsCallback", NetworkDiagnosticsCallback.class);
46+
mSetNetworkDiagnosticsCallback.setAccessible(true);
47+
reflection
48+
.when(() -> InstabugUtil.getMethod(Class.forName("com.instabug.library.Instabug"), "setNetworkDiagnosticsCallback", NetworkDiagnosticsCallback.class))
49+
.thenReturn(mSetNetworkDiagnosticsCallback);
50+
51+
// reportCurrentViewChange mock
52+
Method mReportCurrentViewChange = MockReflected.class.getDeclaredMethod("reportCurrentViewChange", String.class);
53+
mReportCurrentViewChange.setAccessible(true);
54+
55+
reflection
56+
.when(() -> InstabugUtil.getMethod(Class.forName("com.instabug.library.Instabug"), "reportCurrentViewChange", String.class))
57+
.thenReturn(mReportCurrentViewChange);
58+
59+
// reportScreenChange mock
60+
Method mReportScreenChange = MockReflected.class.getDeclaredMethod("reportScreenChange", Bitmap.class, String.class);
61+
mReportScreenChange.setAccessible(true);
62+
63+
reflection
64+
.when(() -> InstabugUtil.getMethod(Class.forName("com.instabug.library.Instabug"), "reportScreenChange", Bitmap.class, String.class))
65+
.thenReturn(mReportScreenChange);
4066
}
4167

4268
public static void close() {

android/src/test/java/com/instabug/reactlibrary/util/MockReflected.java

+23
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
package com.instabug.reactlibrary.util;
22

3+
import static com.instabug.reactlibrary.utils.InstabugUtil.getMethod;
4+
5+
import android.graphics.Bitmap;
6+
7+
import com.instabug.library.networkDiagnostics.model.NetworkDiagnosticsCallback;
8+
9+
import java.lang.reflect.Method;
10+
311
/**
412
* Includes fake implementations of methods called by reflection.
513
* Used to verify whether or not a private methods was called.
@@ -16,4 +24,19 @@ public static void setCurrentPlatform(int platform) {}
1624
* Instabug.util.InstabugDeprecationLogger.setBaseUrl
1725
*/
1826
public static void setBaseUrl(String baseUrl) {}
27+
28+
/**
29+
* com.instabug.library.Instabug.setNetworkDiagnosticsCallback
30+
*/
31+
public static void setNetworkDiagnosticsCallback(NetworkDiagnosticsCallback callback) {}
32+
33+
/**
34+
* com.instabug.library.Instabug.reportCurrentViewChange
35+
*/
36+
public static void reportCurrentViewChange(String currentView) {}
37+
38+
/**
39+
* com.instabug.library.Instabug.reportScreenChange
40+
*/
41+
public static void reportScreenChange(Bitmap screenshot, String screen) {}
1942
}

examples/default/ios/InstabugTests/InstabugSampleTests.m

+23-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#import <Instabug/IBGTypes.h>
1414
#import "IBGConstants.h"
1515
#import "RNInstabug.h"
16+
#import "Instabug+CP.h"
1617

1718
@protocol InstabugCPTestProtocol <NSObject>
1819
/**
@@ -45,7 +46,7 @@ @implementation InstabugSampleTests
4546

4647
- (void)setUp {
4748
// Put setup code here. This method is called before the invocation of each test method in the class.
48-
self.instabugBridge = [[InstabugReactBridge alloc] init];
49+
self.instabugBridge = OCMPartialMock([[InstabugReactBridge alloc] init]);
4950
self.mRNInstabug = OCMClassMock([RNInstabug class]);
5051
}
5152

@@ -420,4 +421,25 @@ - (void)testClearAllExperiments {
420421
OCMVerify([mock clearAllExperiments]);
421422
}
422423

424+
- (void)testSetOnNetworkDiagnosticsHandler {
425+
id mInstabug = OCMClassMock([Instabug class]);
426+
NSString* date = @"1/2/2024";
427+
NSInteger totalRequestCount = 10;
428+
NSInteger failureCount = 8;
429+
430+
NSDictionary *expected = @{
431+
@"date": date,
432+
@"totalRequestCount": @(totalRequestCount),
433+
@"failureCount": @(failureCount)
434+
};
435+
436+
OCMStub([mInstabug setWillSendNetworkDiagnosticsHandler:([OCMArg invokeBlockWithArgs:date, OCMOCK_VALUE(totalRequestCount), OCMOCK_VALUE(failureCount), nil])]);
437+
438+
OCMStub([self.instabugBridge sendEventWithName:[OCMArg any] body:[OCMArg any]]);
439+
440+
[self.instabugBridge setOnNetworkDiagnosticsHandler];
441+
442+
OCMVerify([self.instabugBridge sendEventWithName:@"IBGNetworkDiagnosticsHandler" body:expected]);
443+
}
444+
423445
@end

ios/RNInstabug/InstabugReactBridge.h

+1
Original file line numberDiff line numberDiff line change
@@ -112,5 +112,6 @@
112112
- (void)addExperiments:(NSArray *)experiments;
113113
- (void)removeExperiments:(NSArray *)experiments;
114114
- (void)clearAllExperiments;
115+
- (void)setOnNetworkDiagnosticsHandler;
115116

116117
@end

ios/RNInstabug/InstabugReactBridge.m

+17-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#import <os/log.h>
1616
#import <React/RCTUIManager.h>
1717
#import "RNInstabug.h"
18+
#import "Util/Instabug+CP.h"
1819

1920
@interface Instabug (PrivateWillSendAPI)
2021
+ (void)setWillSendReportHandler_private:(void(^)(IBGReport *report, void(^reportCompletionHandler)(IBGReport *)))willSendReportHandler_private;
@@ -23,7 +24,10 @@ + (void)setWillSendReportHandler_private:(void(^)(IBGReport *report, void(^repor
2324
@implementation InstabugReactBridge
2425

2526
- (NSArray<NSString *> *)supportedEvents {
26-
return @[@"IBGpreSendingHandler"];
27+
return @[
28+
@"IBGpreSendingHandler",
29+
@"IBGNetworkDiagnosticsHandler"
30+
];
2731
}
2832

2933
RCT_EXPORT_MODULE(Instabug)
@@ -378,6 +382,18 @@ - (dispatch_queue_t)methodQueue {
378382
[Instabug clearAllExperiments];
379383
}
380384

385+
RCT_EXPORT_METHOD(setOnNetworkDiagnosticsHandler) {
386+
[Instabug setWillSendNetworkDiagnosticsHandler:^(NSString *date, NSInteger totalRequestCount, NSInteger failureCount) {
387+
NSDictionary *params = @{
388+
@"date": date,
389+
@"totalRequestCount": @(totalRequestCount),
390+
@"failureCount": @(failureCount)
391+
};
392+
393+
[self sendEventWithName:@"IBGNetworkDiagnosticsHandler" body:params];
394+
}];
395+
}
396+
381397
- (NSDictionary *)constantsToExport {
382398
return ArgsRegistry.getAll;
383399
}

0 commit comments

Comments
 (0)