Search in sources :

Example 1 with ErrorReport

use of com.microsoft.appcenter.crashes.model.ErrorReport in project AppCenter-SDK-Android by Microsoft.

the class CrashesAndroidTest method getLastSessionCrashReport.

@Test
public void getLastSessionCrashReport() 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);
    assertTrue(Crashes.hasCrashedInLastSession().get());
}
Also used : ErrorReport(com.microsoft.appcenter.crashes.model.ErrorReport) Test(org.junit.Test)

Example 2 with ErrorReport

use of com.microsoft.appcenter.crashes.model.ErrorReport in project AppCenter-SDK-Android by Microsoft.

the class CrashesAndroidTest method getLastSessionCrashReportNative.

@Test
public void getLastSessionCrashReportNative() throws Exception {
    /* Null before start. */
    Crashes.unsetInstance();
    assertNull(Crashes.getLastSessionCrashReport().get());
    assertFalse(Crashes.hasCrashedInLastSession().get());
    assertNull(Crashes.getMinidumpDirectory().get());
    /* Simulate we have a minidump. */
    File newMinidumpDirectory = ErrorLogHelper.getNewMinidumpDirectory();
    File minidumpFile = new File(newMinidumpDirectory, "minidump.dmp");
    StorageHelper.InternalStorage.write(minidumpFile, "mock minidump");
    /* Start crashes now. */
    startFresh(null);
    /* We can access directory now. */
    assertEquals(newMinidumpDirectory.getAbsolutePath(), Crashes.getMinidumpDirectory().get());
    ErrorReport errorReport = Crashes.getLastSessionCrashReport().get();
    assertNotNull(errorReport);
    assertTrue(Crashes.hasCrashedInLastSession().get());
    assertTrue(errorReport.getThrowable() instanceof NativeException);
    /* File has been deleted. */
    assertFalse(minidumpFile.exists());
    /* After restart, it's processed. */
    Crashes.unsetInstance();
    startFresh(null);
    assertNull(Crashes.getLastSessionCrashReport().get());
    assertFalse(Crashes.hasCrashedInLastSession().get());
}
Also used : ErrorReport(com.microsoft.appcenter.crashes.model.ErrorReport) File(java.io.File) NativeException(com.microsoft.appcenter.crashes.model.NativeException) Test(org.junit.Test)

Example 3 with ErrorReport

use of com.microsoft.appcenter.crashes.model.ErrorReport in project AppCenter-SDK-Android by Microsoft.

the class Crashes method processPendingErrors.

private void processPendingErrors() {
    for (File logFile : ErrorLogHelper.getStoredErrorLogFiles()) {
        AppCenterLog.debug(LOG_TAG, "Process pending error file: " + logFile);
        String logfileContents = StorageHelper.InternalStorage.read(logFile);
        if (logfileContents != null) {
            try {
                ManagedErrorLog log = (ManagedErrorLog) mLogSerializer.deserializeLog(logfileContents);
                UUID id = log.getId();
                ErrorReport report = buildErrorReport(log);
                if (report == null) {
                    removeAllStoredErrorLogFiles(id);
                } else if (!mAutomaticProcessing || mCrashesListener.shouldProcess(report)) {
                    if (!mAutomaticProcessing) {
                        AppCenterLog.debug(LOG_TAG, "CrashesListener.shouldProcess returned true, continue processing log: " + id.toString());
                    }
                    mUnprocessedErrorReports.put(id, mErrorReportCache.get(id));
                } else {
                    AppCenterLog.debug(LOG_TAG, "CrashesListener.shouldProcess returned false, clean up and ignore log: " + id.toString());
                    removeAllStoredErrorLogFiles(id);
                }
            } catch (JSONException e) {
                AppCenterLog.error(LOG_TAG, "Error parsing error log", e);
            }
        }
    }
    /* If automatic processing is enabled. */
    if (mAutomaticProcessing) {
        /* Proceed to check if user confirmation is needed. */
        sendCrashReportsOrAwaitUserConfirmation();
    }
}
Also used : ErrorReport(com.microsoft.appcenter.crashes.model.ErrorReport) ManagedErrorLog(com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog) JSONException(org.json.JSONException) UUID(java.util.UUID) File(java.io.File)

Example 4 with ErrorReport

use of com.microsoft.appcenter.crashes.model.ErrorReport in project AppCenter-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());
}
Also used : Context(android.content.Context) SessionContext(com.microsoft.appcenter.SessionContext) Device(com.microsoft.appcenter.ingestion.models.Device) FileWriter(java.io.FileWriter) Channel(com.microsoft.appcenter.channel.Channel) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) Date(java.util.Date) ErrorReport(com.microsoft.appcenter.crashes.model.ErrorReport) ManagedErrorLog(com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog) AppCenterConsumer(com.microsoft.appcenter.utils.async.AppCenterConsumer) UUID(java.util.UUID) File(java.io.File) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 5 with ErrorReport

use of com.microsoft.appcenter.crashes.model.ErrorReport in project AppCenter-SDK-Android by Microsoft.

the class CrashesTest method queuePendingCrashesAlwaysSend.

@Test
public void queuePendingCrashesAlwaysSend() throws IOException, ClassNotFoundException, JSONException {
    Context mockContext = mock(Context.class);
    Channel mockChannel = mock(Channel.class);
    ErrorAttachmentLog mockAttachment = mock(ErrorAttachmentLog.class);
    when(mockAttachment.getId()).thenReturn(UUID.randomUUID());
    when(mockAttachment.getErrorId()).thenReturn(UUID.randomUUID());
    when(mockAttachment.getContentType()).thenReturn("");
    when(mockAttachment.getFileName()).thenReturn("");
    when(mockAttachment.getData()).thenReturn(new byte[0]);
    when(mockAttachment.isValid()).thenReturn(true);
    List<ErrorAttachmentLog> errorAttachmentLogList = Arrays.asList(mockAttachment, mockAttachment);
    ErrorReport report = new ErrorReport();
    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(report);
    when(StorageHelper.InternalStorage.read(any(File.class))).thenReturn("");
    when(StorageHelper.InternalStorage.readObject(any(File.class))).thenReturn(new RuntimeException());
    when(StorageHelper.PreferencesStorage.getBoolean(eq(Crashes.PREF_KEY_ALWAYS_SEND), anyBoolean())).thenReturn(true);
    CrashesListener mockListener = mock(CrashesListener.class);
    when(mockListener.shouldProcess(report)).thenReturn(true);
    when(mockListener.shouldProcess(report)).thenReturn(true);
    when(mockListener.shouldAwaitUserConfirmation()).thenReturn(false);
    when(mockListener.getErrorAttachments(report)).thenReturn(errorAttachmentLogList);
    Crashes crashes = Crashes.getInstance();
    LogSerializer logSerializer = mock(LogSerializer.class);
    when(logSerializer.deserializeLog(anyString())).thenReturn(mErrorLog);
    crashes.setLogSerializer(logSerializer);
    crashes.setInstanceListener(mockListener);
    crashes.onStarting(mAppCenterHandler);
    crashes.onStarted(mockContext, "", mockChannel);
    verify(mockListener).shouldProcess(report);
    verify(mockListener, never()).shouldAwaitUserConfirmation();
    verify(mockListener).getErrorAttachments(report);
    verify(mockChannel).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object log) {
            return log.equals(mErrorLog);
        }
    }), eq(crashes.getGroupName()));
    verify(mockChannel, times(errorAttachmentLogList.size())).enqueue(mockAttachment, crashes.getGroupName());
}
Also used : Context(android.content.Context) SessionContext(com.microsoft.appcenter.SessionContext) Channel(com.microsoft.appcenter.channel.Channel) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) ErrorReport(com.microsoft.appcenter.crashes.model.ErrorReport) ManagedErrorLog(com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog) ArgumentMatcher(org.mockito.ArgumentMatcher) ErrorAttachmentLog(com.microsoft.appcenter.crashes.ingestion.models.ErrorAttachmentLog) UUID(java.util.UUID) File(java.io.File) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

ErrorReport (com.microsoft.appcenter.crashes.model.ErrorReport)54 Test (org.junit.Test)40 File (java.io.File)36 ManagedErrorLog (com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog)31 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)31 Context (android.content.Context)26 Channel (com.microsoft.appcenter.channel.Channel)25 LogSerializer (com.microsoft.appcenter.ingestion.models.json.LogSerializer)22 UUID (java.util.UUID)21 ErrorAttachmentLog (com.microsoft.appcenter.crashes.ingestion.models.ErrorAttachmentLog)18 Device (com.microsoft.appcenter.ingestion.models.Device)12 DefaultLogSerializer (com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer)12 SessionContext (com.microsoft.appcenter.utils.context.SessionContext)12 SessionContext (com.microsoft.appcenter.SessionContext)11 Log (com.microsoft.appcenter.ingestion.models.Log)11 HandledErrorLog (com.microsoft.appcenter.crashes.ingestion.models.HandledErrorLog)10 AppCenterLog (com.microsoft.appcenter.utils.AppCenterLog)10 JSONException (org.json.JSONException)10 InvocationOnMock (org.mockito.invocation.InvocationOnMock)10 TestCrashException (com.microsoft.appcenter.crashes.model.TestCrashException)9