use of com.microsoft.appcenter.crashes.model.ErrorReport in project AppCenter-SDK-Android by Microsoft.
the class CrashesTest method crashTest.
/**
* Crash and sending report test.
* <p>
* We can't truly restart application in tests, so some kind of crashes can't be tested by this method.
* Out of memory or stack overflow - crash the test process;
* UI states errors - problems with restart activity;
* <p>
* Also to avoid flakiness, please setup your test environment
* (https://google.github.io/android-testing-support-library/docs/espresso/setup/index.html#setup-your-test-environment).
* On your device, under Settings->Developer options disable the following 3 settings:
* - Window animation scale
* - Transition animation scale
* - Animator duration scale
*
* @param titleId Title string resource to find list item.
* @throws InterruptedException If the current thread is interrupted.
*/
@SuppressWarnings("ThrowableResultOfMethodCallIgnored")
private void crashTest(@StringRes int titleId) throws InterruptedException {
/* Crash. */
onView(allOf(withChild(withText(R.string.title_crashes)), withChild(withText(R.string.description_crashes)))).perform(click());
CrashFailureHandler failureHandler = new CrashFailureHandler();
onCrash(titleId).withFailureHandler(failureHandler).perform(click());
/* Check error report. */
assertTrue(Crashes.hasCrashedInLastSession().get());
ErrorReport errorReport = Crashes.getLastSessionCrashReport().get();
assertNotNull(errorReport);
assertNotNull(errorReport.getId());
assertEquals(mContext.getMainLooper().getThread().getName(), errorReport.getThreadName());
assertThat("AppStartTime", new Date().getTime() - errorReport.getAppStartTime().getTime(), lessThan(60000L));
assertThat("AppErrorTime", new Date().getTime() - errorReport.getAppErrorTime().getTime(), lessThan(10000L));
assertNotNull(errorReport.getDevice());
assertEquals(failureHandler.uncaughtException.getClass(), errorReport.getThrowable().getClass());
assertEquals(failureHandler.uncaughtException.getMessage(), errorReport.getThrowable().getMessage());
assertArrayEquals(failureHandler.uncaughtException.getStackTrace(), errorReport.getThrowable().getStackTrace());
/* Send report. */
waitFor(onView(withText(R.string.crash_confirmation_dialog_send_button)).inRoot(isDialog()), 1000).perform(click());
/* Check toasts. */
waitFor(onToast(mActivityTestRule.getActivity(), withText(R.string.crash_before_sending)), CHECK_DELAY).check(matches(isDisplayed()));
onView(isRoot()).perform(waitFor(CHECK_DELAY));
waitFor(onToast(mActivityTestRule.getActivity(), anyOf(withContainsText(R.string.crash_sent_succeeded), withText(R.string.crash_sent_failed))), TOAST_DELAY).check(matches(isDisplayed()));
onView(isRoot()).perform(waitFor(TOAST_DELAY));
}
use of com.microsoft.appcenter.crashes.model.ErrorReport in project AppCenter-SDK-Android by Microsoft.
the class ErrorLogHelper method getErrorReportFromErrorLog.
@NonNull
public static ErrorReport getErrorReportFromErrorLog(@NonNull ManagedErrorLog log, Throwable throwable) {
ErrorReport report = new ErrorReport();
report.setId(log.getId().toString());
report.setThreadName(log.getErrorThreadName());
report.setThrowable(throwable);
report.setAppStartTime(log.getAppLaunchTimestamp());
report.setAppErrorTime(log.getTimestamp());
report.setDevice(log.getDevice());
return report;
}
use of com.microsoft.appcenter.crashes.model.ErrorReport in project AppCenter-SDK-Android by Microsoft.
the class ErrorLogHelperTest method getErrorReportFromErrorLog.
@Test
public void getErrorReportFromErrorLog() throws java.lang.Exception {
/* Mock base. */
Context mockContext = mock(Context.class);
when(Process.myPid()).thenReturn(123);
/* Mock device. */
Device mockDevice = mock(Device.class);
when(DeviceInfoHelper.getDeviceInfo(any(Context.class))).thenReturn(mockDevice);
/* Mock process name. */
ActivityManager activityManager = mock(ActivityManager.class);
RunningAppProcessInfo runningAppProcessInfo = new RunningAppProcessInfo(null, 0, null);
runningAppProcessInfo.pid = 123;
runningAppProcessInfo.processName = "right.process";
when(mockContext.getSystemService(Context.ACTIVITY_SERVICE)).thenReturn(activityManager);
when(activityManager.getRunningAppProcesses()).thenReturn(Arrays.asList(mock(RunningAppProcessInfo.class), runningAppProcessInfo));
/* Mock architecture. */
TestUtils.setInternalState(Build.VERSION.class, "SDK_INT", 23);
TestUtils.setInternalState(Build.class, "SUPPORTED_ABIS", new String[] { "armeabi-v7a", "arm" });
/* Create an error log. */
ManagedErrorLog errorLog = ErrorLogHelper.createErrorLog(mockContext, java.lang.Thread.currentThread(), new RuntimeException(new TestCrashException()), java.lang.Thread.getAllStackTraces(), 900);
assertNotNull(errorLog);
/* Test. */
Throwable throwable = new RuntimeException();
ErrorReport report = ErrorLogHelper.getErrorReportFromErrorLog(errorLog, throwable);
assertNotNull(report);
assertEquals(errorLog.getId().toString(), report.getId());
assertEquals(errorLog.getErrorThreadName(), report.getThreadName());
assertEquals(throwable, report.getThrowable());
assertEquals(errorLog.getAppLaunchTimestamp(), report.getAppStartTime());
assertEquals(errorLog.getTimestamp(), report.getAppErrorTime());
assertEquals(errorLog.getDevice(), report.getDevice());
}
use of com.microsoft.appcenter.crashes.model.ErrorReport in project mobile-center-sdk-android by Microsoft.
the class MainActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sSharedPreferences = getSharedPreferences("Sasquatch", Context.MODE_PRIVATE);
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll().build());
/* Set custom log URL if one was configured in settings. */
String startType = sSharedPreferences.getString(APPCENTER_START_TYPE, StartType.APP_SECRET.toString());
String logUrl = sSharedPreferences.getString(LOG_URL_KEY, getLogUrl(this, startType));
if (!TextUtils.isEmpty(logUrl)) {
AppCenter.setLogUrl(logUrl);
}
/* Set listeners. */
AnalyticsPrivateHelper.setListener(getAnalyticsListener());
Crashes.setListener(getCrashesListener());
Distribute.setListener(new SasquatchDistributeListener());
/* Set distribute urls. */
String installUrl = getString(R.string.install_url);
if (!TextUtils.isEmpty(installUrl)) {
Distribute.setInstallUrl(installUrl);
}
String apiUrl = getString(R.string.api_url);
if (!TextUtils.isEmpty(apiUrl)) {
Distribute.setApiUrl(apiUrl);
}
/* Set crash attachments. */
AttachmentsUtil.getInstance();
/* Set max storage size. */
setMaxStorageSize();
/* Set debug enabled for distribute. */
setDistributeEnabledForDebuggableBuild();
/* Start App Center. */
startAppCenter(getApplication(), startType);
/* Set user id. */
String userId = sSharedPreferences.getString(USER_ID_KEY, null);
if (userId != null) {
setUserId(userId);
}
/* Attach NDK Crash Handler after SDK is initialized. */
Crashes.getMinidumpDirectory().thenAccept(new AppCenterConsumer<String>() {
@Override
public void accept(String path) {
/* Path is null when Crashes is disabled. */
if (path != null) {
setupNativeCrashesListener(path);
}
}
});
/* Use some App Center getters. */
AppCenter.getInstallId().thenAccept(new AppCenterConsumer<UUID>() {
@Override
public void accept(UUID uuid) {
Log.i(LOG_TAG, "InstallId=" + uuid);
}
});
/* Print last crash. */
Crashes.hasCrashedInLastSession().thenAccept(new AppCenterConsumer<Boolean>() {
@Override
public void accept(Boolean crashed) {
Log.i(LOG_TAG, "Crashes.hasCrashedInLastSession=" + crashed);
}
});
Crashes.getLastSessionCrashReport().thenAccept(new AppCenterConsumer<ErrorReport>() {
@Override
public void accept(ErrorReport data) {
if (data != null) {
Log.i(LOG_TAG, "Crashes.getLastSessionCrashReport().getStackTrace()=" + data.getStackTrace());
}
}
});
/* Populate UI. */
((TextView) findViewById(R.id.package_name)).setText(String.format(getString(R.string.sdk_source_format), getPackageName().substring(getPackageName().lastIndexOf(".") + 1)));
TestFeatures.initialize(this);
ListView listView = findViewById(R.id.list);
listView.setAdapter(new TestFeaturesListAdapter(TestFeatures.getAvailableControls()));
listView.setOnItemClickListener(TestFeatures.getOnItemClickListener());
/* Restore the MSA authentication callback. */
String msaUserId = sSharedPreferences.getString(MSA_TOKEN_KEY, null);
String refreshToken = sSharedPreferences.getString(MSA_REFRESH_TOKEN_KEY, null);
String refreshTokenScope = sSharedPreferences.getString(MSA_REFRESH_TOKEN_SCOPE_KEY, null);
int rawAuthType = sSharedPreferences.getInt(MSA_AUTH_TYPE_KEY, 0);
if (msaUserId != null && refreshToken != null && refreshTokenScope != null) {
AuthenticationProvider.Type mAuthType = AuthenticationProvider.Type.values()[rawAuthType];
MSAAuthenticationProvider tokenProvider = MSAAuthenticationProvider.getInstance(refreshToken, refreshTokenScope, this);
AuthenticationProvider provider = new AuthenticationProvider(mAuthType, msaUserId, tokenProvider);
AnalyticsTransmissionTarget.addAuthenticationProvider(provider);
}
}
use of com.microsoft.appcenter.crashes.model.ErrorReport in project mobile-center-sdk-android by Microsoft.
the class CrashesAndroidTest method getLastSessionCrashReportSimpleException.
@Test
public void getLastSessionCrashReportSimpleException() throws Exception {
/* Null before start. */
Crashes.unsetInstance();
assertNull(Crashes.getLastSessionCrashReport().get());
assertFalse(Crashes.hasCrashedInLastSession().get());
/* Crash on 1st process. */
Thread.UncaughtExceptionHandler uncaughtExceptionHandler = mock(Thread.UncaughtExceptionHandler.class);
Thread.setDefaultUncaughtExceptionHandler(uncaughtExceptionHandler);
startFresh(null);
assertNull(Crashes.getLastSessionCrashReport().get());
assertFalse(Crashes.hasCrashedInLastSession().get());
final RuntimeException exception = new IllegalArgumentException();
final Thread thread = new Thread() {
@Override
public void run() {
throw exception;
}
};
thread.start();
thread.join();
/* Get last session crash on 2nd process. */
startFresh(null);
ErrorReport errorReport = Crashes.getLastSessionCrashReport().get();
assertNotNull(errorReport);
assertNotNull(errorReport.getStackTrace());
assertTrue(Crashes.hasCrashedInLastSession().get());
/* Disable SDK, that will clear the report. */
Crashes.setEnabled(false).get();
errorReport = Crashes.getLastSessionCrashReport().get();
assertNull(errorReport);
/* The report must not be restored after re-enabling. */
Crashes.setEnabled(true).get();
errorReport = Crashes.getLastSessionCrashReport().get();
assertNull(errorReport);
}
Aggregations