use of com.microsoft.azure.mobile.ResultCallback in project mobile-center-sdk-android by Microsoft.
the class CrashesTest method crashInLastSession.
@Test
public void crashInLastSession() throws JSONException, IOException, ClassNotFoundException {
final int tOffset = 10;
final long appLaunchTOffset = 100L;
final ManagedErrorLog errorLog = new ManagedErrorLog();
errorLog.setId(UUIDUtils.randomUUID());
errorLog.setErrorThreadName(Thread.currentThread().getName());
errorLog.setToffset(tOffset);
errorLog.setAppLaunchTOffset(appLaunchTOffset);
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);
/* This callback will be called after Crashes service is initialized. */
final ResultCallback<ErrorReport> callback = new ResultCallback<ErrorReport>() {
@Override
public void onResult(ErrorReport data) {
assertNotNull(data);
assertEquals(errorReport, data);
}
};
mockStatic(ErrorLogHelper.class);
File lastErrorLogFile = errorStorageDirectory.newFile("last-error-log.json");
when(ErrorLogHelper.getLastErrorLogFile()).thenReturn(lastErrorLogFile);
when(ErrorLogHelper.getStoredThrowableFile(any(UUID.class))).thenReturn(errorStorageDirectory.newFile());
when(ErrorLogHelper.getErrorReportFromErrorLog(errorLog, throwable)).thenReturn(errorReport);
when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[] { lastErrorLogFile });
when(StorageHelper.InternalStorage.read(any(File.class))).thenAnswer(new Answer<String>() {
@Override
public String answer(InvocationOnMock invocation) throws Throwable {
/* Call twice for multiple listeners during initialize. */
Crashes.getLastSessionCrashReport(callback);
Crashes.getLastSessionCrashReport(callback);
return "";
}
});
when(StorageHelper.InternalStorage.readObject(any(File.class))).thenReturn(throwable);
Crashes.getInstance().setLogSerializer(logSerializer);
assertFalse(Crashes.hasCrashedInLastSession());
/*
* Last session error is only fetched upon initialization: enabled and channel ready.
* Here the service is enabled by default but we are waiting channel to be ready, simulate that.
*/
assertTrue(Crashes.isEnabled());
Crashes.getInstance().onStarted(mock(Context.class), "", mock(Channel.class));
assertTrue(Crashes.hasCrashedInLastSession());
Crashes.getLastSessionCrashReport(new ResultCallback<ErrorReport>() {
@Override
public void onResult(ErrorReport errorReport) {
assertNotNull(errorReport);
assertEquals(errorLog.getId().toString(), errorReport.getId());
assertEquals(errorLog.getErrorThreadName(), errorReport.getThreadName());
assertEquals(new Date(tOffset - appLaunchTOffset), errorReport.getAppStartTime());
assertEquals(new Date(tOffset), errorReport.getAppErrorTime());
assertNotNull(errorReport.getDevice());
assertEquals(throwable, errorReport.getThrowable());
}
});
}
use of com.microsoft.azure.mobile.ResultCallback in project mobile-center-sdk-android by Microsoft.
the class Crashes method initialize.
private void initialize() {
boolean enabled = isInstanceEnabled();
mInitializeTimestamp = enabled ? SystemClock.elapsedRealtime() : -1;
if (!enabled) {
if (mUncaughtExceptionHandler != null) {
mUncaughtExceptionHandler.unregister();
mUncaughtExceptionHandler = null;
}
} else if (mContext != null && mUncaughtExceptionHandler == null) {
mUncaughtExceptionHandler = new UncaughtExceptionHandler();
mUncaughtExceptionHandler.register();
final File logFile = ErrorLogHelper.getLastErrorLogFile();
if (logFile != null) {
MobileCenterLog.debug(LOG_TAG, "Processing crash report for the last session.");
mCountDownLatch = new CountDownLatch(1);
mHandler.post(new Runnable() {
@Override
public void run() {
String logFileContents = StorageHelper.InternalStorage.read(logFile);
if (logFileContents == null)
MobileCenterLog.error(LOG_TAG, "Error reading last session error log.");
else {
try {
ManagedErrorLog log = (ManagedErrorLog) mLogSerializer.deserializeLog(logFileContents);
mLastSessionErrorReport = buildErrorReport(log);
MobileCenterLog.debug(LOG_TAG, "Processed crash report for the last session.");
} catch (JSONException e) {
MobileCenterLog.error(LOG_TAG, "Error parsing last session error log.", e);
}
}
mCountDownLatch.countDown();
HandlerUtils.runOnUiThread(new Runnable() {
@Override
public void run() {
/* Call callbacks for getInstanceLastSessionCrashReport(ResultCallback) . */
for (Iterator<ResultCallback<ErrorReport>> iterator = mLastCrashErrorReportCallbacks.iterator(); iterator.hasNext(); ) {
ResultCallback<ErrorReport> callback = iterator.next();
iterator.remove();
callback.onResult(mLastSessionErrorReport);
}
}
});
}
});
}
}
}
use of com.microsoft.azure.mobile.ResultCallback in project mobile-center-sdk-android by Microsoft.
the class CrashesTest method crashInLastSessionError.
@Test
public void crashInLastSessionError() throws JSONException, IOException, ClassNotFoundException {
LogSerializer logSerializer = mock(LogSerializer.class);
when(logSerializer.deserializeLog(anyString())).thenReturn(mock(ManagedErrorLog.class));
mockStatic(ErrorLogHelper.class);
File lastErrorLogFile = errorStorageDirectory.newFile("last-error-log.json");
when(ErrorLogHelper.getLastErrorLogFile()).thenReturn(lastErrorLogFile);
when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[] { lastErrorLogFile });
when(StorageHelper.InternalStorage.read(any(File.class))).thenReturn("");
Crashes.getInstance().setLogSerializer(logSerializer);
assertFalse(Crashes.hasCrashedInLastSession());
JSONException jsonException = new JSONException("Fake JSON exception");
when(logSerializer.deserializeLog(anyString())).thenThrow(jsonException);
ResultCallback<ErrorReport> callback = new ResultCallback<ErrorReport>() {
@Override
public void onResult(ErrorReport data) {
assertNull(data);
}
};
/*
* Last session error is only fetched upon initialization: enabled and channel ready.
* Here the service is enabled by default but we are waiting channel to be ready, simulate that.
*/
assertTrue(Crashes.isEnabled());
Crashes.getLastSessionCrashReport(callback);
Crashes.getInstance().onStarted(mock(Context.class), "", mock(Channel.class));
assertFalse(Crashes.hasCrashedInLastSession());
Crashes.getLastSessionCrashReport(callback);
/*
* De-serializing fails twice: processing the log from last time as part of the bulk processing.
* And loading that same file for exposing it in getLastErrorReport.
*/
verifyStatic(times(2));
MobileCenterLog.error(eq(Crashes.LOG_TAG), anyString(), eq(jsonException));
}
Aggregations