Search in sources :

Example 21 with ErrorReport

use of com.microsoft.appcenter.crashes.model.ErrorReport in project mobile-center-sdk-android by Microsoft.

the class ErrorLogHelperTest method getErrorReportFromErrorLog.

@Test
public void getErrorReportFromErrorLog() throws java.lang.Exception {
    /* Mock base. */
    Context mockContext = mock(Context.class);
    when(Process.myPid()).thenReturn(123);
    /* Mock device. */
    Device mockDevice = mock(Device.class);
    when(DeviceInfoHelper.getDeviceInfo(any(Context.class))).thenReturn(mockDevice);
    /* Mock process name. */
    ActivityManager activityManager = mock(ActivityManager.class);
    RunningAppProcessInfo runningAppProcessInfo = new RunningAppProcessInfo(null, 0, null);
    runningAppProcessInfo.pid = 123;
    runningAppProcessInfo.processName = "right.process";
    when(mockContext.getSystemService(Context.ACTIVITY_SERVICE)).thenReturn(activityManager);
    when(activityManager.getRunningAppProcesses()).thenReturn(Arrays.asList(mock(RunningAppProcessInfo.class), runningAppProcessInfo));
    /* Mock architecture. */
    TestUtils.setInternalState(Build.VERSION.class, "SDK_INT", 23);
    TestUtils.setInternalState(Build.class, "SUPPORTED_ABIS", new String[] { "armeabi-v7a", "arm" });
    /* Create an error log. */
    ManagedErrorLog errorLog = ErrorLogHelper.createErrorLog(mockContext, java.lang.Thread.currentThread(), new RuntimeException(new TestCrashException()), java.lang.Thread.getAllStackTraces(), 900);
    assertNotNull(errorLog);
    /* Test. */
    String stackTrace = "Sample stack trace";
    ErrorReport report = ErrorLogHelper.getErrorReportFromErrorLog(errorLog, stackTrace);
    assertNotNull(report);
    assertEquals(errorLog.getId().toString(), report.getId());
    assertEquals(errorLog.getErrorThreadName(), report.getThreadName());
    assertEquals(stackTrace, report.getStackTrace());
    assertEquals(errorLog.getAppLaunchTimestamp(), report.getAppStartTime());
    assertEquals(errorLog.getTimestamp(), report.getAppErrorTime());
    assertEquals(errorLog.getDevice(), report.getDevice());
}
Also used : Context(android.content.Context) ErrorReport(com.microsoft.appcenter.crashes.model.ErrorReport) TestCrashException(com.microsoft.appcenter.crashes.model.TestCrashException) RunningAppProcessInfo(android.app.ActivityManager.RunningAppProcessInfo) ManagedErrorLog(com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog) Device(com.microsoft.appcenter.ingestion.models.Device) Build(android.os.Build) Matchers.anyString(org.mockito.Matchers.anyString) TestUtils.generateString(com.microsoft.appcenter.test.TestUtils.generateString) ActivityManager(android.app.ActivityManager) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 22 with ErrorReport

use of com.microsoft.appcenter.crashes.model.ErrorReport in project mobile-center-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 23 with ErrorReport

use of com.microsoft.appcenter.crashes.model.ErrorReport in project mobile-center-sdk-android by Microsoft.

the class ErrorLogHelper method getErrorReportFromErrorLog.

@NonNull
public static ErrorReport getErrorReportFromErrorLog(@NonNull ManagedErrorLog log, Throwable throwable) {
    ErrorReport report = new ErrorReport();
    report.setId(log.getId().toString());
    report.setThreadName(log.getErrorThreadName());
    report.setThrowable(throwable);
    report.setAppStartTime(log.getAppLaunchTimestamp());
    report.setAppErrorTime(log.getTimestamp());
    report.setDevice(log.getDevice());
    return report;
}
Also used : ErrorReport(com.microsoft.appcenter.crashes.model.ErrorReport) NonNull(android.support.annotation.NonNull)

Example 24 with ErrorReport

use of com.microsoft.appcenter.crashes.model.ErrorReport in project mobile-center-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));
}
Also used : ErrorReport(com.microsoft.appcenter.crashes.model.ErrorReport) ManagedErrorLog(com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog) UUID(java.util.UUID) File(java.io.File) JSONException(org.json.JSONException) NativeException(com.microsoft.appcenter.crashes.model.NativeException) TestCrashException(com.microsoft.appcenter.crashes.model.TestCrashException) IOException(java.io.IOException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 25 with ErrorReport

use of com.microsoft.appcenter.crashes.model.ErrorReport in project mobile-center-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);
}
Also used : Context(android.content.Context) SessionContext(com.microsoft.appcenter.SessionContext) Channel(com.microsoft.appcenter.channel.Channel) ArrayList(java.util.ArrayList) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) Matchers.anyString(org.mockito.Matchers.anyString) TestUtils.generateString(com.microsoft.appcenter.test.TestUtils.generateString) ErrorReport(com.microsoft.appcenter.crashes.model.ErrorReport) ManagedErrorLog(com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog) 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