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

fix: android showSurvey ANR #430

Draft
wants to merge 3 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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## [Unreleased](https://github.com/Instabug/Instabug-Flutter/compare/v12.5.0...dev)

### Fixed

- Fix an Android issue with `showSurvey` causing ANR when invoked from the main thread. The fix ensures its execution on the background thread by default. ([#454]()).

## [12.7.0](https://github.com/Instabug/Instabug-Flutter/compare/v12.5.0...v12.7.0) (February 15, 2024)

### Added
Expand Down
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ android {
}

dependencies {
api 'com.instabug.library:instabug:12.7.1'
api 'com.instabug.library:instabug:12.5.1.5573368-SNAPSHOT'

testImplementation 'junit:junit:4.13.2'
testImplementation "org.mockito:mockito-inline:3.12.1"
Expand Down
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) {}
}