use of com.microsoft.appcenter.crashes.model.ErrorReport in project AppCenter-SDK-Android by Microsoft.
the class CrashesTest method testNativeCrashLog.
private ManagedErrorLog testNativeCrashLog(long appStartTime, long crashTime, boolean correlateSession) throws Exception {
/* Setup mock for a crash in disk. */
File minidumpFile = mock(File.class);
when(minidumpFile.getName()).thenReturn("mockFile");
when(minidumpFile.lastModified()).thenReturn(crashTime);
mockStatic(SessionContext.class);
SessionContext sessionContext = mock(SessionContext.class);
when(SessionContext.getInstance()).thenReturn(sessionContext);
if (correlateSession) {
SessionContext.SessionInfo sessionInfo = mock(SessionContext.SessionInfo.class);
when(sessionContext.getSessionAt(crashTime)).thenReturn(sessionInfo);
when(sessionInfo.getAppLaunchTimestamp()).thenReturn(appStartTime);
}
mockStatic(DeviceInfoHelper.class);
when(DeviceInfoHelper.getDeviceInfo(any(Context.class))).thenReturn(mock(Device.class));
ErrorReport report = new ErrorReport();
mockStatic(ErrorLogHelper.class);
when(ErrorLogHelper.getLastErrorLogFile()).thenReturn(mock(File.class));
when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[] { mock(File.class) });
when(ErrorLogHelper.getNewMinidumpFiles()).thenReturn(new File[] { minidumpFile });
File pendingDir = mock(File.class);
Whitebox.setInternalState(pendingDir, "path", "");
when(ErrorLogHelper.getPendingMinidumpDirectory()).thenReturn(pendingDir);
when(ErrorLogHelper.getStoredThrowableFile(any(UUID.class))).thenReturn(mock(File.class));
when(ErrorLogHelper.getErrorReportFromErrorLog(any(ManagedErrorLog.class), any(Throwable.class))).thenReturn(report);
when(StorageHelper.InternalStorage.read(any(File.class))).thenReturn("");
when(StorageHelper.InternalStorage.readObject(any(File.class))).thenReturn(new NativeException());
LogSerializer logSerializer = mock(LogSerializer.class);
ArgumentCaptor<Log> log = ArgumentCaptor.forClass(Log.class);
when(logSerializer.serializeLog(log.capture())).thenReturn("{}");
when(logSerializer.deserializeLog(anyString())).thenAnswer(new Answer<ManagedErrorLog>() {
@Override
public ManagedErrorLog answer(InvocationOnMock invocation) throws Throwable {
ManagedErrorLog log = mock(ManagedErrorLog.class);
when(log.getId()).thenReturn(UUID.randomUUID());
return log;
}
});
/* Start crashes. */
Crashes crashes = Crashes.getInstance();
crashes.setLogSerializer(logSerializer);
crashes.onStarting(mAppCenterHandler);
crashes.onStarted(mock(Context.class), "", mock(Channel.class));
/* Verify timestamps on the crash log. */
assertTrue(Crashes.hasCrashedInLastSession().get());
assertTrue(log.getValue() instanceof ManagedErrorLog);
return (ManagedErrorLog) log.getValue();
}
use of com.microsoft.appcenter.crashes.model.ErrorReport in project AppCenter-SDK-Android by Microsoft.
the class CrashesTest method manuallyReportNullList.
@Test
public void manuallyReportNullList() 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(UUIDUtils.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.getStoredThrowableFile(any(UUID.class))).thenReturn(mock(File.class));
when(ErrorLogHelper.getErrorReportFromErrorLog(any(ManagedErrorLog.class), any(Throwable.class))).thenReturn(report1).thenReturn(report2);
when(StorageHelper.InternalStorage.read(any(File.class))).thenReturn("");
when(StorageHelper.InternalStorage.readObject(any(File.class))).thenReturn(new RuntimeException());
LogSerializer logSerializer = mock(LogSerializer.class);
when(logSerializer.deserializeLog(anyString())).thenAnswer(new Answer<ManagedErrorLog>() {
@Override
public ManagedErrorLog answer(InvocationOnMock invocation) throws Throwable {
ManagedErrorLog log = mock(ManagedErrorLog.class);
when(log.getId()).thenReturn(UUID.randomUUID());
return log;
}
});
Crashes crashes = Crashes.getInstance();
crashes.setLogSerializer(logSerializer);
/* Set manual processing. */
WrapperSdkExceptionManager.setAutomaticProcessing(false);
/* Start crashes. */
crashes.onStarting(mAppCenterHandler);
crashes.onStarted(mockContext, "", mockChannel);
/* No log queued. */
verify(mockChannel, never()).enqueue(any(Log.class), eq(crashes.getGroupName()));
/* 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());
/* Send nothing using null. */
assertFalse(WrapperSdkExceptionManager.sendCrashReportsOrAwaitUserConfirmation(null).get());
/* No log sent. */
verify(mockChannel, never()).enqueue(any(ManagedErrorLog.class), eq(crashes.getGroupName()));
}
use of com.microsoft.appcenter.crashes.model.ErrorReport in project AppCenter-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);
}
use of com.microsoft.appcenter.crashes.model.ErrorReport in project AppCenter-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 AppCenter-SDK-Android by Microsoft.
the class CrashesTest method buildErrorReport.
@Test
public void buildErrorReport() throws IOException, ClassNotFoundException {
ErrorReport errorReport = ErrorLogHelper.getErrorReportFromErrorLog(mErrorLog, EXCEPTION);
mockStatic(ErrorLogHelper.class);
when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[] { mock(File.class) });
File throwableFile = mock(File.class);
when(throwableFile.length()).thenReturn(1L);
when(ErrorLogHelper.getStoredThrowableFile(any(UUID.class))).thenReturn(throwableFile).thenReturn(null);
when(ErrorLogHelper.getErrorReportFromErrorLog(mErrorLog, EXCEPTION)).thenReturn(errorReport);
when(StorageHelper.InternalStorage.readObject(any(File.class))).thenReturn(EXCEPTION);
Crashes crashes = Crashes.getInstance();
ErrorReport report = crashes.buildErrorReport(mErrorLog);
assertErrorEquals(mErrorLog, report);
mErrorLog.setId(UUIDUtils.randomUUID());
report = crashes.buildErrorReport(mErrorLog);
assertNull(report);
}
Aggregations