Search in sources :

Example 16 with ManagedErrorLog

use of com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog in project mobile-center-sdk-android by Microsoft.

the class CrashesTest method minidumpAppLaunchTimestampFromSessionContextNotFound.

@Test
@PrepareForTest({ SessionContext.class, DeviceInfoHelper.class })
public void minidumpAppLaunchTimestampFromSessionContextNotFound() throws Exception {
    long appStartTime = 99L;
    long crashTime = 123L;
    ManagedErrorLog crashLog = testNativeCrashLog(appStartTime, crashTime, false);
    /* Verify we fall back to crash time for app start time. */
    assertEquals(new Date(crashTime), crashLog.getTimestamp());
    assertEquals(new Date(crashTime), crashLog.getAppLaunchTimestamp());
}
Also used : ManagedErrorLog(com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog) Date(java.util.Date) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 17 with ManagedErrorLog

use of com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog 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)

Example 18 with ManagedErrorLog

use of com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog 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());
}
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 19 with ManagedErrorLog

use of com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog in project mobile-center-sdk-android by Microsoft.

the class ErrorLogHelperTest method createErrorLogFailOver.

@Test
public void createErrorLogFailOver() throws java.lang.Exception {
    /* Mock base. */
    Context mockContext = mock(Context.class);
    when(Process.myPid()).thenReturn(123);
    Date logTimestamp = new Date(1000L);
    whenNew(Date.class).withNoArguments().thenReturn(logTimestamp);
    whenNew(Date.class).withArguments(anyLong()).thenAnswer(new Answer<Date>() {

        @Override
        public Date answer(InvocationOnMock invocation) throws Throwable {
            return new Date((Long) invocation.getArguments()[0]);
        }
    });
    /* Mock device. */
    when(DeviceInfoHelper.getDeviceInfo(any(Context.class))).thenThrow(new DeviceInfoHelper.DeviceInfoException("mock", new PackageManager.NameNotFoundException()));
    /* Mock architecture. */
    TestUtils.setInternalState(Build.VERSION.class, "SDK_INT", 15);
    TestUtils.setInternalState(Build.class, "CPU_ABI", "armeabi-v7a");
    /* Test. */
    long launchTimeStamp = 2000;
    ManagedErrorLog errorLog = ErrorLogHelper.createErrorLog(mockContext, java.lang.Thread.currentThread(), new java.lang.Exception(), java.lang.Thread.getAllStackTraces(), launchTimeStamp);
    assertNotNull(errorLog);
    assertNotNull(errorLog.getId());
    assertEquals(logTimestamp, errorLog.getTimestamp());
    assertNull(errorLog.getDevice());
    assertEquals(Integer.valueOf(123), errorLog.getProcessId());
    assertNull(errorLog.getProcessName());
    assertNull(errorLog.getParentProcessId());
    assertNull(errorLog.getParentProcessName());
    assertEquals("armeabi-v7a", errorLog.getArchitecture());
    assertEquals((Long) java.lang.Thread.currentThread().getId(), errorLog.getErrorThreadId());
    assertEquals(java.lang.Thread.currentThread().getName(), errorLog.getErrorThreadName());
    assertEquals(Boolean.TRUE, errorLog.getFatal());
    assertEquals(launchTimeStamp, errorLog.getAppLaunchTimestamp().getTime());
}
Also used : Context(android.content.Context) DeviceInfoHelper(com.microsoft.appcenter.utils.DeviceInfoHelper) Date(java.util.Date) ManagedErrorLog(com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Build(android.os.Build) Matchers.anyLong(org.mockito.Matchers.anyLong) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

ManagedErrorLog (com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog)19 Test (org.junit.Test)12 Context (android.content.Context)10 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)10 ErrorReport (com.microsoft.appcenter.crashes.model.ErrorReport)9 File (java.io.File)9 InvocationOnMock (org.mockito.invocation.InvocationOnMock)9 Date (java.util.Date)8 SessionContext (com.microsoft.appcenter.SessionContext)6 Channel (com.microsoft.appcenter.channel.Channel)6 ErrorAttachmentLog (com.microsoft.appcenter.crashes.ingestion.models.ErrorAttachmentLog)6 UUID (java.util.UUID)6 Log (com.microsoft.appcenter.ingestion.models.Log)5 LogSerializer (com.microsoft.appcenter.ingestion.models.json.LogSerializer)5 Exception (com.microsoft.appcenter.crashes.ingestion.models.Exception)4 Device (com.microsoft.appcenter.ingestion.models.Device)4 IOException (java.io.IOException)4 JSONException (org.json.JSONException)4 Answer (org.mockito.stubbing.Answer)4 ActivityManager (android.app.ActivityManager)3