use of com.microsoft.appcenter.crashes.model.ErrorReport in project mobile-center-sdk-android by Microsoft.
the class CrashesTest method getThrowableDeprecated.
@Test
@SuppressWarnings("deprecation")
public void getThrowableDeprecated() {
ErrorReport report = new ErrorReport();
assertNull(report.getThrowable());
}
use of com.microsoft.appcenter.crashes.model.ErrorReport in project mobile-center-sdk-android by Microsoft.
the class CrashesTest method checkStacktraceWhenThrowableFileIsEmpty.
@Test
public void checkStacktraceWhenThrowableFileIsEmpty() throws Exception {
String expectedStacktrace = "type: message\n" + "\t at ClassName.MethodName(FileName:1)";
/* Create empty throwable file. */
UUID logId = UUID.randomUUID();
String throwableFileName = logId + ErrorLogHelper.THROWABLE_FILE_EXTENSION;
File errorStorageDirectory = mTemporaryFolder.newFolder("error");
ErrorLogHelper.setErrorLogDirectory(errorStorageDirectory);
File throwableFile = new File(errorStorageDirectory, throwableFileName);
assertTrue(throwableFile.createNewFile());
/* Prepare first frame. */
final StackFrame stackFrame1 = new StackFrame();
stackFrame1.setClassName("ClassName");
stackFrame1.setMethodName("MethodName");
stackFrame1.setFileName("FileName");
stackFrame1.setLineNumber(1);
/* Mock exception value. */
com.microsoft.appcenter.crashes.ingestion.models.Exception mockException = mock(com.microsoft.appcenter.crashes.ingestion.models.Exception.class);
when(mockException.getType()).thenReturn("type");
when(mockException.getMessage()).thenReturn("message");
when(mockException.getFrames()).thenReturn(Arrays.asList(stackFrame1));
/* Mock log. */
ManagedErrorLog mockLog = new ManagedErrorLog();
mockLog.setId(logId);
mockLog.setErrorThreadName("Thread name");
mockLog.setAppLaunchTimestamp(new Date());
mockLog.setTimestamp(new Date());
mockLog.setDevice(new Device());
mockLog.setException(mockException);
/* Build error report. */
Crashes crashes = Crashes.getInstance();
/* Verify that stacktrace is null. */
ErrorReport report = crashes.buildErrorReport(mockLog);
assertEquals(expectedStacktrace, report.getStackTrace());
verifyStatic(never());
FileManager.read(any(File.class));
}
use of com.microsoft.appcenter.crashes.model.ErrorReport in project mobile-center-sdk-android by Microsoft.
the class CrashesTest method manualProcessing.
@Test
public void manualProcessing() throws Exception {
/* Setup mock for a crash in disk. */
Context mockContext = mock(Context.class);
Channel mockChannel = mock(Channel.class);
ErrorReport report1 = new ErrorReport();
report1.setId(UUID.randomUUID().toString());
ErrorReport report2 = new ErrorReport();
mockStatic(ErrorLogHelper.class);
when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[] { mock(File.class), mock(File.class) });
when(ErrorLogHelper.getNewMinidumpFiles()).thenReturn(new File[0]);
when(ErrorLogHelper.getErrorReportFromErrorLog(any(ManagedErrorLog.class), anyString())).thenReturn(report1).thenReturn(report2);
when(FileManager.read(any(File.class))).thenReturn("");
LogSerializer logSerializer = mock(LogSerializer.class);
when(logSerializer.deserializeLog(anyString(), anyString())).thenAnswer(new Answer<ManagedErrorLog>() {
@Override
public ManagedErrorLog answer(InvocationOnMock invocation) {
com.microsoft.appcenter.crashes.ingestion.models.Exception mockException = new com.microsoft.appcenter.crashes.ingestion.models.Exception();
mockException.setType("type");
mockException.setMessage("message");
ManagedErrorLog log = mock(ManagedErrorLog.class);
when(log.getId()).thenReturn(UUID.randomUUID());
when(log.getException()).thenReturn(mockException);
return log;
}
});
Crashes crashes = Crashes.getInstance();
crashes.setLogSerializer(logSerializer);
/* Create listener for user confirmation. */
CrashesListener listener = mock(CrashesListener.class);
Crashes.setListener(listener);
/* Set manual processing. */
WrapperSdkExceptionManager.setAutomaticProcessing(false);
/* Start crashes. */
crashes.onStarting(mAppCenterHandler);
crashes.onStarted(mockContext, mockChannel, "", null, true);
/* No log queued. */
verify(mockChannel, never()).enqueue(any(Log.class), eq(crashes.getGroupName()), anyInt());
/* Get crash reports. */
Collection<ErrorReport> reports = WrapperSdkExceptionManager.getUnprocessedErrorReports().get();
assertNotNull(reports);
assertEquals(2, reports.size());
Iterator<ErrorReport> iterator = reports.iterator();
assertEquals(report1, iterator.next());
assertEquals(report2, iterator.next());
/* Listener not called yet on anything on manual processing. */
verifyZeroInteractions(listener);
/* Send only the first. */
assertFalse(WrapperSdkExceptionManager.sendCrashReportsOrAwaitUserConfirmation(Collections.singletonList(report1.getId())).get());
/* We used manual process function, listener not called. */
verifyZeroInteractions(listener);
/* No log sent until manual user confirmation in that mode (we are not in always send). */
verify(mockChannel, never()).enqueue(any(ManagedErrorLog.class), eq(crashes.getGroupName()), anyInt());
/* Confirm with always send. */
Crashes.notifyUserConfirmation(Crashes.ALWAYS_SEND);
verifyStatic();
SharedPreferencesManager.putBoolean(Crashes.PREF_KEY_ALWAYS_SEND, true);
when(SharedPreferencesManager.getBoolean(eq(Crashes.PREF_KEY_ALWAYS_SEND), anyBoolean())).thenReturn(true);
/* 1 log sent. Other one is filtered. */
verify(mockChannel).enqueue(any(ManagedErrorLog.class), eq(crashes.getGroupName()), eq(CRITICAL));
/* We can send attachments via wrapper instead of using listener (both work but irrelevant to test with listener). */
ErrorAttachmentLog mockAttachment = mock(ErrorAttachmentLog.class);
when(mockAttachment.getId()).thenReturn(UUID.randomUUID());
when(mockAttachment.getData()).thenReturn(new byte[0]);
when(mockAttachment.isValid()).thenReturn(true);
WrapperSdkExceptionManager.sendErrorAttachments(report1.getId(), Collections.singletonList(mockAttachment));
verify(mockChannel).enqueue(eq(mockAttachment), eq(crashes.getGroupName()), eq(DEFAULTS));
/* Send attachment with invalid UUID format for report identifier. */
mockAttachment = mock(ErrorAttachmentLog.class);
when(mockAttachment.getId()).thenReturn(UUID.randomUUID());
when(mockAttachment.getData()).thenReturn(new byte[0]);
when(mockAttachment.isValid()).thenReturn(true);
WrapperSdkExceptionManager.sendErrorAttachments("not-a-uuid", Collections.singletonList(mockAttachment));
verify(mockChannel, never()).enqueue(eq(mockAttachment), eq(crashes.getGroupName()), anyInt());
/* We used manual process function, listener not called and our mock channel does not send events. */
verifyZeroInteractions(listener);
/* Reset instance to test another tine with always send. */
Crashes.unsetInstance();
crashes = Crashes.getInstance();
when(ErrorLogHelper.getErrorReportFromErrorLog(any(ManagedErrorLog.class), anyString())).thenReturn(report1).thenReturn(report2);
WrapperSdkExceptionManager.setAutomaticProcessing(false);
crashes.setLogSerializer(logSerializer);
crashes.onStarting(mAppCenterHandler);
mockChannel = mock(Channel.class);
crashes.onStarted(mockContext, mockChannel, "", null, true);
assertTrue(Crashes.isEnabled().get());
verify(mockChannel, never()).enqueue(any(ManagedErrorLog.class), eq(crashes.getGroupName()), anyInt());
/* Get crash reports, check always sent was returned and sent without confirmation. */
assertTrue(WrapperSdkExceptionManager.sendCrashReportsOrAwaitUserConfirmation(Collections.singletonList(report2.getId())).get());
verify(mockChannel).enqueue(any(ManagedErrorLog.class), eq(crashes.getGroupName()), eq(CRITICAL));
}
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);
}
use of com.microsoft.appcenter.crashes.model.ErrorReport in project mobile-center-sdk-android by Microsoft.
the class CrashesAndroidTest method getLastSessionCrashReportStackOverflowException.
@Test
public void getLastSessionCrashReportStackOverflowException() 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);
assertNotNull(errorReport.getStackTrace());
assertTrue(Crashes.hasCrashedInLastSession().get());
}
Aggregations