Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add network diagnostics callback #1119

Draft
wants to merge 13 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ jobs:
sync_generated_files:
macos:
xcode: 13.4.1
resource_class: macos.m1.medium.gen1
steps:
- advanced-checkout/shallow-checkout
- install_node_modules
Expand All @@ -161,6 +162,7 @@ jobs:
test_ios:
macos:
xcode: 13.4.1
resource_class: macos.m1.medium.gen1
working_directory: ~/project/examples/default
environment:
INSTABUG_SOURCEMAPS_UPLOAD_DISABLE: true
Expand Down Expand Up @@ -192,7 +194,7 @@ jobs:
e2e_ios:
macos:
xcode: 13.4.1
resource_class: macos.x86.medium.gen2
resource_class: macos.m1.medium.gen1
environment:
INSTABUG_SOURCEMAPS_UPLOAD_DISABLE: true
steps:
Expand Down Expand Up @@ -321,6 +323,7 @@ jobs:
publish:
macos:
xcode: 13.4.1
resource_class: macos.m1.medium.gen1
working_directory: '~'
steps:
- advanced-checkout/shallow-checkout
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,6 @@ android/keystores/debug.keystore

# Vscode local history
.history/

# .idea run configurations
/.run/*
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 @@ -7,6 +7,8 @@
import android.net.Uri;
import android.view.View;

import androidx.annotation.NonNull;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.Promise;
Expand All @@ -32,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 @@ -42,6 +45,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 @@ -144,7 +148,11 @@ public void run() {
.setLogLevel(parsedLogLevel);

if(codePushVersion != null) {
builder.setCodePushVersion(codePushVersion);
if(Instabug.isBuilt()) {
Instabug.setCodePushVersion(codePushVersion);
} else {
builder.setCodePushVersion(codePushVersion);
}
}
builder.build();
}
Expand Down Expand Up @@ -1024,6 +1032,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 @@ -483,28 +499,17 @@ public void tearDown() {
}

@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 @@ -554,4 +559,33 @@ public void tearDown() {
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 @@ -408,4 +409,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
2 changes: 1 addition & 1 deletion examples/default/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -795,4 +795,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: d169b4508f413ff5d69cdf38428960e0828e6282

COCOAPODS: 1.11.3
COCOAPODS: 1.12.0
Loading