Search in sources :

Example 1 with AppCenterFuture

use of com.microsoft.appcenter.utils.async.AppCenterFuture 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 2 with AppCenterFuture

use of com.microsoft.appcenter.utils.async.AppCenterFuture 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)

Aggregations

Context (android.content.Context)2 AppCenterHandler (com.microsoft.appcenter.AppCenterHandler)2 Exception (com.microsoft.appcenter.crashes.ingestion.models.Exception)2 ManagedErrorLog (com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog)2 AppCenterFuture (com.microsoft.appcenter.utils.async.AppCenterFuture)2 StorageHelper (com.microsoft.appcenter.utils.storage.StorageHelper)2 IOException (java.io.IOException)2 JSONException (org.json.JSONException)2 Before (org.junit.Before)2 Matchers.anyBoolean (org.mockito.Matchers.anyBoolean)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Answer (org.mockito.stubbing.Answer)2 PowerMockito.doAnswer (org.powermock.api.mockito.PowerMockito.doAnswer)2 File (java.io.File)1