Skip to content

Commit

Permalink
fix(android): Resolve ANR in showSurvey
Browse files Browse the repository at this point in the history
This fix addresses the ANR issue in the `showSurvey` function on Android. The resolution ensures smoother execution by defaulting it to the background thread.
  • Loading branch information
abdelhamid-f-nasser committed Jan 10, 2024
1 parent cb0cfa7 commit afe12b6
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
package com.instabug.flutter.modules;

import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;

import com.instabug.flutter.generated.SurveysPigeon;
import com.instabug.flutter.util.Reflection;
import com.instabug.flutter.util.ThreadManager;
import com.instabug.library.Feature;
import com.instabug.library.Platform;
import com.instabug.survey.Survey;
import com.instabug.survey.Surveys;
import com.instabug.survey.callbacks.OnDismissCallback;
import com.instabug.survey.callbacks.OnShowCallback;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import io.flutter.plugin.common.BinaryMessenger;

public class SurveysApi implements SurveysPigeon.SurveysHostApi {

private final String TAG = InstabugApi.class.getName();
private final SurveysPigeon.SurveysFlutterApi flutterApi;

public static void init(BinaryMessenger messenger) {
Expand All @@ -42,9 +50,28 @@ public void showSurveyIfAvailable() {
Surveys.showSurveyIfAvailable();
}

/**
* Displays a survey using reflection, eliminating the need to call it on a background thread.
* This variant does not return a boolean indicating the existence of the survey.
* Invoked through reflection.
*/
@VisibleForTesting
public void showSurveyCP(@NonNull String surveyToken) {
try {
Method method = Reflection.getMethod(Class.forName("com.instabug.survey.Surveys"), "showSurveyCP", String.class);
if (method != null) {
method.invoke(null, surveyToken);
} else {
Log.e(TAG, "showSurveyCP was not found by reflection");
}
} catch (Exception e) {
e.printStackTrace();
}
}

@Override
public void showSurvey(@NonNull String surveyToken) {
Surveys.showSurvey(surveyToken);
showSurveyCP(surveyToken);
}

@Override
Expand Down
16 changes: 15 additions & 1 deletion android/src/test/java/com/instabug/flutter/SurveysApiTest.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package com.instabug.flutter;

import static com.instabug.flutter.util.GlobalMocks.reflected;
import static com.instabug.flutter.util.MockResult.makeResult;
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;

import com.instabug.flutter.generated.SurveysPigeon;
import com.instabug.flutter.modules.SurveysApi;
import com.instabug.flutter.util.GlobalMocks;
import com.instabug.flutter.util.MockReflected;
import com.instabug.library.Feature;
import com.instabug.library.Platform;
import com.instabug.survey.Survey;
import com.instabug.survey.Surveys;
import com.instabug.survey.callbacks.OnDismissCallback;
Expand Down Expand Up @@ -82,13 +86,23 @@ public void testShowSurveyIfAvailable() {
mSurveys.verify(Surveys::showSurveyIfAvailable);
}

@Test
public void testShowSurveyCp() {
String token = "survey-token";

api.showSurveyCP(token);

reflected.verify(() -> MockReflected.showSurveyCP(token));
}

@Test
public void testShowSurvey() {
String token = "survey-token";

api.showSurvey(token);

mSurveys.verify(() -> Surveys.showSurvey(token));
reflected.verify(() -> MockReflected.showSurveyCP(token));
mSurveys.verify(() -> Surveys.showSurvey(token), never());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ public static void setUp() throws NoSuchMethodException {

uri = mockStatic(Uri.class);
uri.when(() -> Uri.fromFile(any())).thenReturn(mock(Uri.class));

Method mShowSurveyCP = MockReflected.class.getDeclaredMethod("showSurveyCP", String.class);
mShowSurveyCP.setAccessible(true);
reflection
.when(() -> Reflection.getMethod(Class.forName("com.instabug.survey.Surveys"), "showSurveyCP", String.class))
.thenReturn(mShowSurveyCP);
}

public static void close() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,9 @@ public static void apmNetworkLog(long requestStartTime, long requestDuration, St
* CrashReporting.reportException
*/
public static void crashReportException(JSONObject exception, boolean isHandled) {}

/**
* Surveys.showSurveyCP
*/
public static void showSurveyCP(String surveyToken) {}
}

0 comments on commit afe12b6

Please sign in to comment.