use of com.microsoft.appcenter.crashes.model.ErrorReport in project mobile-center-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. */
String stackTrace = "Sample stack trace";
ErrorReport report = ErrorLogHelper.getErrorReportFromErrorLog(errorLog, stackTrace);
assertNotNull(report);
assertEquals(errorLog.getId().toString(), report.getId());
assertEquals(errorLog.getErrorThreadName(), report.getThreadName());
assertEquals(stackTrace, report.getStackTrace());
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 CrashesAndroidTest method getLastSessionCrashReport.
@Test
public void getLastSessionCrashReport() 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 Error exception = generateStackOverflowError();
assertTrue(exception.getStackTrace().length > ErrorLogHelper.FRAME_LIMIT);
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);
assertTrue(Crashes.hasCrashedInLastSession().get());
}
use of com.microsoft.appcenter.crashes.model.ErrorReport in project mobile-center-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 mobile-center-sdk-android by Microsoft.
the class CrashesTest method buildErrorReportError.
@Test
public void buildErrorReportError() throws IOException, ClassNotFoundException {
mockStatic(ErrorLogHelper.class);
when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[] { mock(File.class) });
when(ErrorLogHelper.getNewMinidumpFiles()).thenReturn(new File[0]);
File throwableFile = mock(File.class);
when(throwableFile.length()).thenReturn(1L);
when(ErrorLogHelper.getStoredThrowableFile(any(UUID.class))).thenReturn(throwableFile);
when(ErrorLogHelper.getErrorReportFromErrorLog(any(ManagedErrorLog.class), any(Throwable.class))).thenReturn(null);
Exception classNotFoundException = mock(ClassNotFoundException.class);
Exception ioException = mock(IOException.class);
when(StorageHelper.InternalStorage.readObject(any(File.class))).thenThrow(classNotFoundException).thenThrow(ioException);
Crashes crashes = Crashes.getInstance();
ErrorReport report = crashes.buildErrorReport(mErrorLog);
assertNull(report);
report = crashes.buildErrorReport(mErrorLog);
assertNull(report);
verifyStatic();
AppCenterLog.error(eq(Crashes.LOG_TAG), anyString(), eq(classNotFoundException));
verifyStatic();
AppCenterLog.error(eq(Crashes.LOG_TAG), anyString(), eq(ioException));
}
use of com.microsoft.appcenter.crashes.model.ErrorReport in project mobile-center-sdk-android by Microsoft.
the class CrashesTest method sendMoreThan2ErrorAttachments.
@Test
public void sendMoreThan2ErrorAttachments() throws IOException, ClassNotFoundException, JSONException {
int MAX_ATTACHMENT_PER_CRASH = 2;
int numOfAttachments = MAX_ATTACHMENT_PER_CRASH + 1;
ArrayList<ErrorAttachmentLog> errorAttachmentLogs = new ArrayList<>(3);
for (int i = 0; i < numOfAttachments; ++i) {
ErrorAttachmentLog log = mock(ErrorAttachmentLog.class);
when(log.isValid()).thenReturn(true);
errorAttachmentLogs.add(log);
}
CrashesListener listener = mock(CrashesListener.class);
when(listener.shouldProcess(any(ErrorReport.class))).thenReturn(true);
when(listener.getErrorAttachments(any(ErrorReport.class))).thenReturn(errorAttachmentLogs);
ManagedErrorLog log = mock(ManagedErrorLog.class);
when(log.getId()).thenReturn(UUID.randomUUID());
LogSerializer logSerializer = mock(LogSerializer.class);
when(logSerializer.deserializeLog(anyString())).thenReturn(log);
mockStatic(ErrorLogHelper.class);
when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[] { mock(File.class) });
when(ErrorLogHelper.getNewMinidumpFiles()).thenReturn(new File[0]);
when(ErrorLogHelper.getStoredThrowableFile(any(UUID.class))).thenReturn(mock(File.class));
when(ErrorLogHelper.getErrorReportFromErrorLog(any(ManagedErrorLog.class), any(Throwable.class))).thenReturn(new ErrorReport());
when(StorageHelper.InternalStorage.read(any(File.class))).thenReturn("");
when(StorageHelper.InternalStorage.readObject(any(File.class))).thenReturn(mock(Throwable.class));
Crashes crashes = Crashes.getInstance();
crashes.setInstanceListener(listener);
crashes.setLogSerializer(logSerializer);
crashes.onStarting(mAppCenterHandler);
crashes.onStarted(mock(Context.class), "", mock(Channel.class));
String expectedMessage = "A limit of " + MAX_ATTACHMENT_PER_CRASH + " attachments per error report might be enforced by server.";
PowerMockito.verifyStatic();
AppCenterLog.warn(Crashes.LOG_TAG, expectedMessage);
}
Aggregations