Skip to content

Commit

Permalink
feat: add network diagnostics callback
Browse files Browse the repository at this point in the history
Jira ID: MOB-13680
  • Loading branch information
abdelhamid-f-nasser committed Feb 15, 2024
1 parent 4355b9a commit 4bf62c2
Show file tree
Hide file tree
Showing 13 changed files with 220 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
final class Constants {
final static String IBG_PRE_INVOCATION_HANDLER = "IBGpreInvocationHandler";
final static String IBG_POST_INVOCATION_HANDLER = "IBGpostInvocationHandler";
final static String IBG_NETWORK_DIAGNOSTICS_HANDLER = "IBGNetworkDiagnosticsHandler";

final static String IBG_ON_SHOW_SURVEY_HANDLER = "IBGWillShowSurvey";
final static String IBG_ON_DISMISS_SURVEY_HANDLER = "IBGDidDismissSurvey";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.view.View;

import androidx.annotation.UiThread;
import androidx.annotation.NonNull;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Callback;
Expand All @@ -33,6 +34,7 @@
import com.instabug.library.logging.InstabugLog;
import com.instabug.library.model.NetworkLog;
import com.instabug.library.model.Report;
import com.instabug.library.networkDiagnostics.model.NetworkDiagnosticsCallback;
import com.instabug.library.ui.onboarding.WelcomeMessage;
import com.instabug.reactlibrary.utils.ArrayUtil;
import com.instabug.reactlibrary.utils.EventEmitterModule;
Expand All @@ -44,6 +46,7 @@
import org.json.JSONTokener;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -1042,6 +1045,39 @@ public void run() {
});
}

@ReactMethod
public void setOnNetworkDiagnosticsHandler() {
MainThreadHandler.runOnMainThread(new Runnable() {
@Override
public void run() {
try {
Method method = getMethod(Class.forName("com.instabug.library.Instabug"), "setNetworkDiagnosticsCallback", NetworkDiagnosticsCallback.class);

if (method != null) {
method.invoke(null, new NetworkDiagnosticsCallback() {
@Override
public void onReady(@NonNull String date, int totalRequestCount, int failureCount) {
try {
WritableMap params = Arguments.createMap();
params.putString("date", date);
params.putInt("totalRequestCount", totalRequestCount);
params.putInt("failureCount", failureCount);

sendEvent(Constants.IBG_NETWORK_DIAGNOSTICS_HANDLER, params);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
} catch (ClassNotFoundException | IllegalAccessException |
InvocationTargetException e) {
e.printStackTrace();
}
}
});
}

/**
* Map between the exported JS constant and the arg key in {@link ArgsRegistry}.
* The constant name and the arg key should match to be able to resolve the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.facebook.react.bridge.JavaOnlyArray;
import com.facebook.react.bridge.JavaOnlyMap;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.bridge.WritableMap;
import com.instabug.library.Feature;
Expand All @@ -19,7 +20,10 @@
import com.instabug.library.ReproConfigurations;
import com.instabug.library.ReproMode;
import com.instabug.library.internal.module.InstabugLocale;
import com.instabug.library.networkDiagnostics.model.NetworkDiagnosticsCallback;
import com.instabug.library.ui.onboarding.WelcomeMessage;
import com.instabug.reactlibrary.util.GlobalMocks;
import com.instabug.reactlibrary.util.MockReflected;
import com.instabug.reactlibrary.utils.MainThreadHandler;

import org.junit.After;
Expand All @@ -37,6 +41,7 @@
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
Expand All @@ -45,18 +50,21 @@
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

import static com.instabug.reactlibrary.util.GlobalMocks.reflected;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockConstruction;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;


public class RNInstabugReactnativeModuleTest {
private RNInstabugReactnativeModule rnModule = new RNInstabugReactnativeModule(null);
private RNInstabugReactnativeModule rnModule;
private ReactApplicationContext mReactContext = mock(ReactApplicationContext.class);

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

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

@Before
public void mockMainThreadHandler() throws Exception {
public void setUp() throws Exception {
rnModule = spy(new RNInstabugReactnativeModule(mReactContext));

// Mock static functions
mockInstabug = mockStatic(Instabug.class);
mockLooper = mockStatic(Looper.class);
Expand All @@ -86,13 +96,19 @@ public Boolean answer(InvocationOnMock invocation) throws Throwable {
};
Mockito.doAnswer(handlerPostAnswer).when(MainThreadHandler.class);
MainThreadHandler.runOnMainThread(any(Runnable.class));

// Set up global mocks
GlobalMocks.setUp();
}
@After
public void tearDown() {
// Remove static mocks
mockLooper.close();
mockMainThreadHandler.close();
mockInstabug.close();

// Remove global mocks
GlobalMocks.close();
}

/********Instabug*********/
Expand Down Expand Up @@ -496,28 +512,17 @@ public void testIdentifyUserWithId() {
}

@Test
public void givenString$reportCurrentViewChange_whenQuery_thenShouldCallNativeApiWithString() throws Exception {
// when
public void testReportCurrentViewChange() {
rnModule.reportCurrentViewChange("screen");
Method privateStringMethod = getMethod(Class.forName("com.instabug.library.Instabug"), "reportCurrentViewChange", String.class);
privateStringMethod.setAccessible(true);

// then
verify(Instabug.class, VerificationModeFactory.times(1));
privateStringMethod.invoke("reportCurrentViewChange","screen");
reflected.verify(() -> MockReflected.reportCurrentViewChange("screen"), times(1));
}

@Test
public void givenString$reportScreenChange_whenQuery_thenShouldCallNativeApiWithString() throws Exception {
// when
public void testReportScreenChange() {
rnModule.reportScreenChange("screen");
Method privateStringMethod = getMethod(Class.forName("com.instabug.library.Instabug"), "reportScreenChange", Bitmap.class, String.class);
privateStringMethod.setAccessible(true);

// then
verify(Instabug.class, VerificationModeFactory.times(1));
privateStringMethod.invoke("reportScreenChange", null,"screen");

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

@Test
Expand Down Expand Up @@ -567,4 +572,33 @@ public void testIdentifyUserWithId() {
verify(Instabug.class,times(1));
Instabug.clearAllExperiments();
}

@Test
public void testSetOnNetworkDiagnosticsHandler() {
String date = new Date().toString();
int successOrderCount = 2;
int failureCount = 1;

MockedStatic<Arguments> mockArgument = mockStatic(Arguments.class);
mockArgument.when(Arguments::createMap).thenReturn(new JavaOnlyMap());

reflected
.when(() -> MockReflected.setNetworkDiagnosticsCallback(any(NetworkDiagnosticsCallback.class)))
.thenAnswer((InvocationOnMock invocation) -> {
NetworkDiagnosticsCallback callback = invocation.getArgument(0);
callback.onReady(date, successOrderCount, failureCount);
return null;
});

rnModule.setOnNetworkDiagnosticsHandler();

WritableMap params = new JavaOnlyMap();
params.putString("date", date);
params.putInt("totalRequestCount", successOrderCount);
params.putInt("failureCount", failureCount);

verify(rnModule).sendEvent(Constants.IBG_NETWORK_DIAGNOSTICS_HANDLER, params);

mockArgument.close();
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.instabug.reactlibrary.util;

import static com.instabug.reactlibrary.utils.InstabugUtil.getMethod;
import static org.mockito.Mockito.mockStatic;

import android.graphics.Bitmap;
import android.util.Log;

import com.instabug.library.networkDiagnostics.model.NetworkDiagnosticsCallback;
import com.instabug.reactlibrary.utils.InstabugUtil;

import org.mockito.MockedStatic;
Expand Down Expand Up @@ -37,6 +40,29 @@ public static void setUp() throws NoSuchMethodException {
reflection
.when(() -> InstabugUtil.getMethod(Class.forName("com.instabug.library.util.InstabugDeprecationLogger"), "setBaseUrl", String.class))
.thenReturn(mSetBaseUrl);

// setNetworkDiagnosticsCallback mock
Method mSetNetworkDiagnosticsCallback = MockReflected.class.getDeclaredMethod("setNetworkDiagnosticsCallback", NetworkDiagnosticsCallback.class);
mSetNetworkDiagnosticsCallback.setAccessible(true);
reflection
.when(() -> InstabugUtil.getMethod(Class.forName("com.instabug.library.Instabug"), "setNetworkDiagnosticsCallback", NetworkDiagnosticsCallback.class))
.thenReturn(mSetNetworkDiagnosticsCallback);

// reportCurrentViewChange mock
Method mReportCurrentViewChange = MockReflected.class.getDeclaredMethod("reportCurrentViewChange", String.class);
mReportCurrentViewChange.setAccessible(true);

reflection
.when(() -> InstabugUtil.getMethod(Class.forName("com.instabug.library.Instabug"), "reportCurrentViewChange", String.class))
.thenReturn(mReportCurrentViewChange);

// reportScreenChange mock
Method mReportScreenChange = MockReflected.class.getDeclaredMethod("reportScreenChange", Bitmap.class, String.class);
mReportScreenChange.setAccessible(true);

reflection
.when(() -> InstabugUtil.getMethod(Class.forName("com.instabug.library.Instabug"), "reportScreenChange", Bitmap.class, String.class))
.thenReturn(mReportScreenChange);
}

public static void close() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
package com.instabug.reactlibrary.util;

import static com.instabug.reactlibrary.utils.InstabugUtil.getMethod;

import android.graphics.Bitmap;

import com.instabug.library.networkDiagnostics.model.NetworkDiagnosticsCallback;

import java.lang.reflect.Method;

/**
* Includes fake implementations of methods called by reflection.
* Used to verify whether or not a private methods was called.
Expand All @@ -16,4 +24,19 @@ public static void setCurrentPlatform(int platform) {}
* Instabug.util.InstabugDeprecationLogger.setBaseUrl
*/
public static void setBaseUrl(String baseUrl) {}

/**
* com.instabug.library.Instabug.setNetworkDiagnosticsCallback
*/
public static void setNetworkDiagnosticsCallback(NetworkDiagnosticsCallback callback) {}

/**
* com.instabug.library.Instabug.reportCurrentViewChange
*/
public static void reportCurrentViewChange(String currentView) {}

/**
* com.instabug.library.Instabug.reportScreenChange
*/
public static void reportScreenChange(Bitmap screenshot, String screen) {}
}
24 changes: 23 additions & 1 deletion examples/default/ios/InstabugTests/InstabugSampleTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#import <Instabug/IBGTypes.h>
#import "IBGConstants.h"
#import "RNInstabug.h"
#import "Instabug+CP.h"

@protocol InstabugCPTestProtocol <NSObject>
/**
Expand Down Expand Up @@ -45,7 +46,7 @@ @implementation InstabugSampleTests

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

Expand Down Expand Up @@ -420,4 +421,25 @@ - (void)testClearAllExperiments {
OCMVerify([mock clearAllExperiments]);
}

- (void)testSetOnNetworkDiagnosticsHandler {
id mInstabug = OCMClassMock([Instabug class]);
NSString* date = @"1/2/2024";
NSInteger totalRequestCount = 10;
NSInteger failureCount = 8;

NSDictionary *expected = @{
@"date": date,
@"totalRequestCount": @(totalRequestCount),
@"failureCount": @(failureCount)
};

OCMStub([mInstabug setWillSendNetworkDiagnosticsHandler:([OCMArg invokeBlockWithArgs:date, OCMOCK_VALUE(totalRequestCount), OCMOCK_VALUE(failureCount), nil])]);

OCMStub([self.instabugBridge sendEventWithName:[OCMArg any] body:[OCMArg any]]);

[self.instabugBridge setOnNetworkDiagnosticsHandler];

OCMVerify([self.instabugBridge sendEventWithName:@"IBGNetworkDiagnosticsHandler" body:expected]);
}

@end
1 change: 1 addition & 0 deletions ios/RNInstabug/InstabugReactBridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,6 @@
- (void)addExperiments:(NSArray *)experiments;
- (void)removeExperiments:(NSArray *)experiments;
- (void)clearAllExperiments;
- (void)setOnNetworkDiagnosticsHandler;

@end
18 changes: 17 additions & 1 deletion ios/RNInstabug/InstabugReactBridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#import <os/log.h>
#import <React/RCTUIManager.h>
#import "RNInstabug.h"
#import "Util/Instabug+CP.h"

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

- (NSArray<NSString *> *)supportedEvents {
return @[@"IBGpreSendingHandler"];
return @[
@"IBGpreSendingHandler",
@"IBGNetworkDiagnosticsHandler"
];
}

RCT_EXPORT_MODULE(Instabug)
Expand Down Expand Up @@ -378,6 +382,18 @@ - (dispatch_queue_t)methodQueue {
[Instabug clearAllExperiments];
}

RCT_EXPORT_METHOD(setOnNetworkDiagnosticsHandler) {
[Instabug setWillSendNetworkDiagnosticsHandler:^(NSString *date, NSInteger totalRequestCount, NSInteger failureCount) {
NSDictionary *params = @{
@"date": date,
@"totalRequestCount": @(totalRequestCount),
@"failureCount": @(failureCount)
};

[self sendEventWithName:@"IBGNetworkDiagnosticsHandler" body:params];
}];
}

- (NSDictionary *)constantsToExport {
return ArgsRegistry.getAll;
}
Expand Down
1 change: 1 addition & 0 deletions ios/RNInstabug/Util/Instabug+CP.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ NS_ASSUME_NONNULL_BEGIN
@interface Instabug (CP)

+ (void)setCurrentPlatform:(IBGPlatform)platform;
+ (void)setWillSendNetworkDiagnosticsHandler:(void (^_Nullable)(NSString*, NSInteger, NSInteger))willSendNetworkDiagnosticsHandler;

@end

Expand Down
Loading

0 comments on commit 4bf62c2

Please sign in to comment.