Search in sources :

Example 6 with ManagedErrorLog

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

the class CrashesTest method minidumpAppLaunchTimestampFromSessionContextInFuture.

@Test
@PrepareForTest({ SessionContext.class, DeviceInfoHelper.class })
public void minidumpAppLaunchTimestampFromSessionContextInFuture() throws Exception {
    long appStartTime = 101L;
    long crashTime = 100L;
    ManagedErrorLog crashLog = testNativeCrashLog(appStartTime, crashTime, true);
    /* 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 7 with ManagedErrorLog

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

the class UncaughtExceptionHandlerTest method setUp.

@Before
public void setUp() throws java.lang.Exception {
    Crashes.unsetInstance();
    mockStatic(AppCenter.class);
    mockStatic(AppCenterLog.class);
    mockStatic(SystemClock.class);
    mockStatic(StorageHelper.PreferencesStorage.class);
    mockStatic(StorageHelper.InternalStorage.class);
    mockStatic(ErrorLogHelper.class);
    mockStatic(DeviceInfoHelper.class);
    mockStatic(System.class);
    @SuppressWarnings("unchecked") AppCenterFuture<Boolean> future = (AppCenterFuture<Boolean>) mock(AppCenterFuture.class);
    when(AppCenter.isEnabled()).thenReturn(future);
    when(future.get()).thenReturn(true);
    when(StorageHelper.PreferencesStorage.getBoolean(CRASHES_ENABLED_KEY, true)).thenReturn(true);
    /* Then simulate further changes to state. */
    PowerMockito.doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            /* Whenever the new state is persisted, make further calls return the new state. */
            boolean enabled = (Boolean) invocation.getArguments()[1];
            Mockito.when(StorageHelper.PreferencesStorage.getBoolean(CRASHES_ENABLED_KEY, true)).thenReturn(enabled);
            return null;
        }
    }).when(StorageHelper.PreferencesStorage.class);
    StorageHelper.PreferencesStorage.putBoolean(eq(CRASHES_ENABLED_KEY), anyBoolean());
    ManagedErrorLog errorLogMock = mock(ManagedErrorLog.class);
    when(ErrorLogHelper.getErrorStorageDirectory()).thenReturn(new File("."));
    when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[0]);
    when(ErrorLogHelper.createErrorLog(any(Context.class), any(Thread.class), any(Exception.class), Matchers.<Map<Thread, StackTraceElement[]>>any(), anyLong(), anyBoolean())).thenReturn(errorLogMock);
    when(errorLogMock.getId()).thenReturn(UUID.randomUUID());
    mDefaultExceptionHandler = mock(Thread.UncaughtExceptionHandler.class);
    Thread.setDefaultUncaughtExceptionHandler(mDefaultExceptionHandler);
    mExceptionHandler = new UncaughtExceptionHandler();
    /* Mock handlers. */
    mockStatic(HandlerUtils.class);
    Answer<Void> runNow = new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            ((Runnable) invocation.getArguments()[0]).run();
            return null;
        }
    };
    doAnswer(runNow).when(HandlerUtils.class);
    HandlerUtils.runOnUiThread(any(Runnable.class));
    AppCenterHandler handler = mock(AppCenterHandler.class);
    Crashes.getInstance().onStarting(handler);
    doAnswer(runNow).when(handler).post(any(Runnable.class), any(Runnable.class));
}
Also used : Context(android.content.Context) AppCenterFuture(com.microsoft.appcenter.utils.async.AppCenterFuture) Exception(com.microsoft.appcenter.crashes.ingestion.models.Exception) JSONException(org.json.JSONException) IOException(java.io.IOException) Answer(org.mockito.stubbing.Answer) PowerMockito.doAnswer(org.powermock.api.mockito.PowerMockito.doAnswer) AppCenterHandler(com.microsoft.appcenter.AppCenterHandler) ManagedErrorLog(com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog) InvocationOnMock(org.mockito.invocation.InvocationOnMock) StorageHelper(com.microsoft.appcenter.utils.storage.StorageHelper) Matchers.anyBoolean(org.mockito.Matchers.anyBoolean) File(java.io.File) Before(org.junit.Before)

Example 8 with ManagedErrorLog

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

the class WrapperSdkExceptionManagerTest method setUp.

@Before
public void setUp() {
    Crashes.unsetInstance();
    mockStatic(AppCenter.class);
    mockStatic(StorageHelper.PreferencesStorage.class);
    mockStatic(StorageHelper.InternalStorage.class);
    mockStatic(AppCenterLog.class);
    mockStatic(ErrorLogHelper.class);
    when(ErrorLogHelper.getErrorStorageDirectory()).thenReturn(errorStorageDirectory.getRoot());
    ManagedErrorLog errorLogMock = mock(ManagedErrorLog.class);
    when(errorLogMock.getId()).thenReturn(UUID.randomUUID());
    when(ErrorLogHelper.createErrorLog(any(Context.class), any(Thread.class), any(Exception.class), Matchers.<Map<Thread, StackTraceElement[]>>any(), anyLong(), anyBoolean())).thenReturn(errorLogMock);
    @SuppressWarnings("unchecked") AppCenterFuture<Boolean> future = (AppCenterFuture<Boolean>) mock(AppCenterFuture.class);
    when(AppCenter.isEnabled()).thenReturn(future);
    when(future.get()).thenReturn(true);
    when(StorageHelper.PreferencesStorage.getBoolean(CRASHES_ENABLED_KEY, true)).thenReturn(true);
    /* Mock handlers. */
    mockStatic(HandlerUtils.class);
    Answer<Void> runNow = new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            ((Runnable) invocation.getArguments()[0]).run();
            return null;
        }
    };
    doAnswer(runNow).when(HandlerUtils.class);
    HandlerUtils.runOnUiThread(any(Runnable.class));
    AppCenterHandler handler = mock(AppCenterHandler.class);
    Crashes.getInstance().onStarting(handler);
    doAnswer(runNow).when(handler).post(any(Runnable.class), any(Runnable.class));
}
Also used : Context(android.content.Context) AppCenterFuture(com.microsoft.appcenter.utils.async.AppCenterFuture) Exception(com.microsoft.appcenter.crashes.ingestion.models.Exception) JSONException(org.json.JSONException) IOException(java.io.IOException) Answer(org.mockito.stubbing.Answer) PowerMockito.doAnswer(org.powermock.api.mockito.PowerMockito.doAnswer) AppCenterHandler(com.microsoft.appcenter.AppCenterHandler) ManagedErrorLog(com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog) InvocationOnMock(org.mockito.invocation.InvocationOnMock) StorageHelper(com.microsoft.appcenter.utils.storage.StorageHelper) Matchers.anyBoolean(org.mockito.Matchers.anyBoolean) Before(org.junit.Before)

Example 9 with ManagedErrorLog

use of com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog 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. */
    Throwable throwable = new RuntimeException();
    ErrorReport report = ErrorLogHelper.getErrorReportFromErrorLog(errorLog, throwable);
    assertNotNull(report);
    assertEquals(errorLog.getId().toString(), report.getId());
    assertEquals(errorLog.getErrorThreadName(), report.getThreadName());
    assertEquals(throwable, report.getThrowable());
    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) ActivityManager(android.app.ActivityManager) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 10 with ManagedErrorLog

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

the class ErrorLogHelperTest method createErrorLog.

@Test
public void createErrorLog() throws java.lang.Exception {
    /* Dummy coverage of utils class. */
    new ErrorLogHelper();
    /* 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. */
    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" });
    /* Test. */
    long launchTimeStamp = 2000;
    ManagedErrorLog errorLog = ErrorLogHelper.createErrorLog(mockContext, java.lang.Thread.currentThread(), new RuntimeException(new IOException(new TestCrashException())), java.lang.Thread.getAllStackTraces(), launchTimeStamp);
    assertNotNull(errorLog);
    assertNotNull(errorLog.getId());
    assertEquals(logTimestamp, errorLog.getTimestamp());
    assertEquals(mockDevice, errorLog.getDevice());
    assertEquals(Integer.valueOf(123), errorLog.getProcessId());
    assertEquals("right.process", 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());
    /* Check first exception. */
    Exception topException = errorLog.getException();
    sanityCheck(topException);
    assertEquals(RuntimeException.class.getName(), topException.getType());
    assertNotNull(topException.getMessage());
    assertNotNull(topException.getInnerExceptions());
    assertEquals(1, topException.getInnerExceptions().size());
    /* Check second exception. */
    Exception middleException = topException.getInnerExceptions().get(0);
    sanityCheck(middleException);
    assertEquals(IOException.class.getName(), middleException.getType());
    assertNotNull(middleException.getInnerExceptions());
    assertEquals(1, middleException.getInnerExceptions().size());
    /* Check third exception. */
    Exception rootCauseException = middleException.getInnerExceptions().get(0);
    sanityCheck(rootCauseException);
    assertEquals(TestCrashException.class.getName(), rootCauseException.getType());
    assertNotNull(rootCauseException.getMessage());
    assertNull(rootCauseException.getInnerExceptions());
    /* Check threads. */
    assertNotNull(errorLog.getThreads());
    assertEquals(java.lang.Thread.getAllStackTraces().size(), errorLog.getThreads().size());
    for (Thread thread : errorLog.getThreads()) {
        assertNotNull(thread);
        assertTrue(thread.getId() > 0);
        assertNotNull(thread.getName());
        assertNotNull(thread.getFrames());
        for (StackFrame frame : thread.getFrames()) {
            assertNotNull(frame);
            assertNotNull(frame.getClassName());
            assertNotNull(frame.getMethodName());
        }
    }
}
Also used : Context(android.content.Context) TestCrashException(com.microsoft.appcenter.crashes.model.TestCrashException) Device(com.microsoft.appcenter.ingestion.models.Device) IOException(java.io.IOException) ActivityManager(android.app.ActivityManager) Date(java.util.Date) Exception(com.microsoft.appcenter.crashes.ingestion.models.Exception) TestCrashException(com.microsoft.appcenter.crashes.model.TestCrashException) IOException(java.io.IOException) Thread(com.microsoft.appcenter.crashes.ingestion.models.Thread) RunningAppProcessInfo(android.app.ActivityManager.RunningAppProcessInfo) ManagedErrorLog(com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Build(android.os.Build) StackFrame(com.microsoft.appcenter.crashes.ingestion.models.StackFrame) 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