Search in sources :

Example 1 with ResultCallback

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());
        }
    });
}
Also used : Context(android.content.Context) ResultCallback(com.microsoft.azure.mobile.ResultCallback) Device(com.microsoft.azure.mobile.ingestion.models.Device) Channel(com.microsoft.azure.mobile.channel.Channel) LogSerializer(com.microsoft.azure.mobile.ingestion.models.json.LogSerializer) Matchers.anyString(org.mockito.Matchers.anyString) Date(java.util.Date) ErrorReport(com.microsoft.azure.mobile.crashes.model.ErrorReport) ManagedErrorLog(com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog) InvocationOnMock(org.mockito.invocation.InvocationOnMock) UUID(java.util.UUID) File(java.io.File) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 2 with ResultCallback

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);
                            }
                        }
                    });
                }
            });
        }
    }
}
Also used : ErrorReport(com.microsoft.azure.mobile.crashes.model.ErrorReport) ResultCallback(com.microsoft.azure.mobile.ResultCallback) ManagedErrorLog(com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog) Iterator(java.util.Iterator) JSONException(org.json.JSONException) CountDownLatch(java.util.concurrent.CountDownLatch) File(java.io.File)

Example 3 with ResultCallback

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));
}
Also used : ErrorReport(com.microsoft.azure.mobile.crashes.model.ErrorReport) Context(android.content.Context) ResultCallback(com.microsoft.azure.mobile.ResultCallback) ManagedErrorLog(com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog) Channel(com.microsoft.azure.mobile.channel.Channel) JSONException(org.json.JSONException) LogSerializer(com.microsoft.azure.mobile.ingestion.models.json.LogSerializer) File(java.io.File) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

ResultCallback (com.microsoft.azure.mobile.ResultCallback)3 ManagedErrorLog (com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog)3 ErrorReport (com.microsoft.azure.mobile.crashes.model.ErrorReport)3 File (java.io.File)3 Context (android.content.Context)2 Channel (com.microsoft.azure.mobile.channel.Channel)2 LogSerializer (com.microsoft.azure.mobile.ingestion.models.json.LogSerializer)2 JSONException (org.json.JSONException)2 Test (org.junit.Test)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 Device (com.microsoft.azure.mobile.ingestion.models.Device)1 Date (java.util.Date)1 Iterator (java.util.Iterator)1 UUID (java.util.UUID)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Matchers.anyString (org.mockito.Matchers.anyString)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1