use of com.microsoft.appcenter.utils.async.AppCenterConsumer in project mobile-center-sdk-android by Microsoft.
the class CrashesTest method crashInLastSession.
@Test
public void crashInLastSession() throws JSONException, IOException, ClassNotFoundException {
final ManagedErrorLog errorLog = new ManagedErrorLog();
errorLog.setId(UUIDUtils.randomUUID());
errorLog.setErrorThreadName(Thread.currentThread().getName());
Date logTimestamp = new Date(10);
errorLog.setTimestamp(logTimestamp);
Date appLaunchTimestamp = new Date(100L);
errorLog.setAppLaunchTimestamp(appLaunchTimestamp);
errorLog.setDevice(mock(Device.class));
LogSerializer logSerializer = mock(LogSerializer.class);
when(logSerializer.deserializeLog(anyString())).thenReturn(errorLog);
final Throwable throwable = mock(Throwable.class);
final ErrorReport errorReport = ErrorLogHelper.getErrorReportFromErrorLog(errorLog, throwable);
mockStatic(ErrorLogHelper.class);
File lastErrorLogFile = errorStorageDirectory.newFile("last-error-log.json");
when(ErrorLogHelper.getLastErrorLogFile()).thenReturn(lastErrorLogFile);
File throwableFile = errorStorageDirectory.newFile();
new FileWriter(throwableFile).append("fake_data").close();
when(ErrorLogHelper.getStoredThrowableFile(any(UUID.class))).thenReturn(throwableFile);
when(ErrorLogHelper.getErrorReportFromErrorLog(errorLog, throwable)).thenReturn(errorReport);
when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[] { lastErrorLogFile });
when(ErrorLogHelper.getNewMinidumpFiles()).thenReturn(new File[0]);
when(StorageHelper.InternalStorage.read(any(File.class))).thenReturn("");
when(StorageHelper.InternalStorage.readObject(any(File.class))).thenReturn(throwable);
Crashes crashes = Crashes.getInstance();
crashes.setLogSerializer(logSerializer);
assertFalse(Crashes.hasCrashedInLastSession().get());
/*
* Last session error is only fetched upon initialization: enabled and channel ready.
* Here the service is disabled by default until stated, we are waiting channel to be ready, simulate that.
*/
assertFalse(Crashes.isEnabled().get());
assertFalse(Crashes.hasCrashedInLastSession().get());
@SuppressWarnings("unchecked") AppCenterConsumer<ErrorReport> beforeCallback = (AppCenterConsumer<ErrorReport>) mock(AppCenterConsumer.class);
Crashes.getLastSessionCrashReport().thenAccept(beforeCallback);
verify(beforeCallback).accept(null);
crashes.onStarting(mAppCenterHandler);
crashes.onStarted(mock(Context.class), "", mock(Channel.class));
assertTrue(Crashes.isEnabled().get());
assertTrue(Crashes.hasCrashedInLastSession().get());
/* Test with 2 callbacks and check result is the same for both callbacks. */
@SuppressWarnings("unchecked") AppCenterConsumer<ErrorReport> afterCallback = (AppCenterConsumer<ErrorReport>) mock(AppCenterConsumer.class);
AppCenterFuture<ErrorReport> future = Crashes.getLastSessionCrashReport();
future.thenAccept(afterCallback);
future.thenAccept(afterCallback);
ArgumentCaptor<ErrorReport> errorReportCaptor = ArgumentCaptor.forClass(ErrorReport.class);
verify(afterCallback, times(2)).accept(errorReportCaptor.capture());
assertEquals(errorReportCaptor.getAllValues().get(0), errorReportCaptor.getAllValues().get(1));
ErrorReport result = errorReportCaptor.getValue();
assertNotNull(result);
assertEquals(errorLog.getId().toString(), result.getId());
assertEquals(errorLog.getErrorThreadName(), result.getThreadName());
assertEquals(appLaunchTimestamp, result.getAppStartTime());
assertEquals(logTimestamp, result.getAppErrorTime());
assertNotNull(result.getDevice());
assertEquals(throwable, result.getThrowable());
}
Aggregations