Search in sources :

Example 1 with AppCenterHandler

use of com.microsoft.appcenter.AppCenterHandler in project AppCenter-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 AppCenterHandler

use of com.microsoft.appcenter.AppCenterHandler in project AppCenter-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 3 with AppCenterHandler

use of com.microsoft.appcenter.AppCenterHandler in project mobile-center-sdk-android by Microsoft.

the class AnalyticsTransmissionTargetTest method addTicketToLogBeforeStart.

@Test
public void addTicketToLogBeforeStart() {
    /* Simulate not started. */
    Analytics.unsetInstance();
    when(AppCenter.isConfigured()).thenReturn(false);
    /* No actions are prepared without authentication provider. */
    CommonSchemaLog log = new CommonSchemaEventLog();
    AnalyticsTransmissionTarget.getChannelListener().onPreparingLog(log, "test");
    /* Add authentication provider before start. */
    AuthenticationProvider.TokenProvider tokenProvider = mock(AuthenticationProvider.TokenProvider.class);
    AuthenticationProvider authenticationProvider = spy(new AuthenticationProvider(AuthenticationProvider.Type.MSA_COMPACT, "key1", tokenProvider));
    AnalyticsTransmissionTarget.addAuthenticationProvider(authenticationProvider);
    assertEquals(authenticationProvider, AnalyticsTransmissionTarget.sAuthenticationProvider);
    verify(authenticationProvider).acquireTokenAsync();
    /* Start analytics. */
    when(AppCenter.isConfigured()).thenReturn(true);
    Analytics analytics = Analytics.getInstance();
    AppCenterHandler handler = mock(AppCenterHandler.class);
    ArgumentCaptor<Runnable> normalRunnable = ArgumentCaptor.forClass(Runnable.class);
    ArgumentCaptor<Runnable> disabledRunnable = ArgumentCaptor.forClass(Runnable.class);
    doNothing().when(handler).post(normalRunnable.capture(), disabledRunnable.capture());
    analytics.onStarting(handler);
    analytics.onStarted(mock(Context.class), mChannel, null, null, false);
    /* No actions are prepared with no CommonSchemaLog. */
    AnalyticsTransmissionTarget.getChannelListener().onPreparingLog(mock(Log.class), "test");
    verify(authenticationProvider, never()).checkTokenExpiry();
    /* Call prepare log. */
    final ProtocolExtension protocol = new ProtocolExtension();
    log.setExt(new Extensions() {

        {
            setProtocol(protocol);
        }
    });
    AnalyticsTransmissionTarget.getChannelListener().onPreparingLog(log, "test");
    /* Verify log. */
    assertEquals(Collections.singletonList(authenticationProvider.getTicketKeyHash()), protocol.getTicketKeys());
    /* And that we check expiry. */
    verify(authenticationProvider).checkTokenExpiry();
}
Also used : Context(android.content.Context) AppCenterHandler(com.microsoft.appcenter.AppCenterHandler) ProtocolExtension(com.microsoft.appcenter.ingestion.models.one.ProtocolExtension) CommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog) EventLog(com.microsoft.appcenter.analytics.ingestion.models.EventLog) Log(com.microsoft.appcenter.ingestion.models.Log) AppCenterLog(com.microsoft.appcenter.utils.AppCenterLog) CommonSchemaEventLog(com.microsoft.appcenter.analytics.ingestion.models.one.CommonSchemaEventLog) CommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog) CommonSchemaEventLog(com.microsoft.appcenter.analytics.ingestion.models.one.CommonSchemaEventLog) Extensions(com.microsoft.appcenter.ingestion.models.one.Extensions) Test(org.junit.Test)

Example 4 with AppCenterHandler

use of com.microsoft.appcenter.AppCenterHandler in project mobile-center-sdk-android by Microsoft.

the class AnalyticsTransmissionTargetTest method updateAuthProviderAndLog.

@Test
public void updateAuthProviderAndLog() {
    /* When we enqueue app center log from track event. */
    final List<CommonSchemaLog> sentLogs = new ArrayList<>();
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) {
            ProtocolExtension protocol = new ProtocolExtension();
            Extensions ext = new Extensions();
            ext.setProtocol(protocol);
            CommonSchemaLog log = new CommonSchemaEventLog();
            log.setExt(ext);
            sentLogs.add(log);
            /* Call the listener after conversion of common schema for authentication decoration. */
            AnalyticsTransmissionTarget.getChannelListener().onPreparingLog(log, "test");
            return null;
        }
    }).when(mChannel).enqueue(any(Log.class), anyString(), anyInt());
    /* Start analytics and simulate background thread handler (we hold the thread command and run it in the test). */
    Analytics analytics = Analytics.getInstance();
    AppCenterHandler handler = mock(AppCenterHandler.class);
    ArgumentCaptor<Runnable> backgroundRunnable = ArgumentCaptor.forClass(Runnable.class);
    doNothing().when(handler).post(backgroundRunnable.capture(), any(Runnable.class));
    analytics.onStarting(handler);
    analytics.onStarted(mock(Context.class), mChannel, null, "test", true);
    /* Add first authentication provider. */
    AuthenticationProvider.TokenProvider tokenProvider1 = mock(AuthenticationProvider.TokenProvider.class);
    AuthenticationProvider authenticationProvider1 = spy(new AuthenticationProvider(AuthenticationProvider.Type.MSA_COMPACT, "key1", tokenProvider1));
    AnalyticsTransmissionTarget.addAuthenticationProvider(authenticationProvider1);
    /* Check provider updated in background thread only when AppCenter is configured/started. */
    assertNull(AnalyticsTransmissionTarget.sAuthenticationProvider);
    verify(authenticationProvider1, never()).acquireTokenAsync();
    assertNotNull(backgroundRunnable.getValue());
    /* Run background thread. */
    backgroundRunnable.getValue().run();
    /* Check update. */
    assertEquals(authenticationProvider1, AnalyticsTransmissionTarget.sAuthenticationProvider);
    verify(authenticationProvider1).acquireTokenAsync();
    /* Track an event. */
    Analytics.trackEvent("test1");
    Runnable trackEvent1Command = backgroundRunnable.getValue();
    /* Update authentication provider before the commands run and track a second event. */
    AuthenticationProvider.TokenProvider tokenProvider2 = mock(AuthenticationProvider.TokenProvider.class);
    AuthenticationProvider authenticationProvider2 = spy(new AuthenticationProvider(AuthenticationProvider.Type.MSA_COMPACT, "key2", tokenProvider2));
    AnalyticsTransmissionTarget.addAuthenticationProvider(authenticationProvider2);
    Runnable addAuthProvider2Command = backgroundRunnable.getValue();
    Analytics.trackEvent("test2");
    Runnable trackEvent2Command = backgroundRunnable.getValue();
    /* Simulate background thread doing everything in a sequence. */
    trackEvent1Command.run();
    addAuthProvider2Command.run();
    trackEvent2Command.run();
    /* Verify first log has first ticket. */
    assertEquals(Collections.singletonList(authenticationProvider1.getTicketKeyHash()), sentLogs.get(0).getExt().getProtocol().getTicketKeys());
    /* And that we checked expiry. */
    verify(authenticationProvider1).checkTokenExpiry();
    /* Verify second log has the second ticket. */
    assertEquals(Collections.singletonList(authenticationProvider2.getTicketKeyHash()), sentLogs.get(1).getExt().getProtocol().getTicketKeys());
    /* And that we checked expiry. */
    verify(authenticationProvider2).checkTokenExpiry();
}
Also used : Context(android.content.Context) ProtocolExtension(com.microsoft.appcenter.ingestion.models.one.ProtocolExtension) CommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog) EventLog(com.microsoft.appcenter.analytics.ingestion.models.EventLog) Log(com.microsoft.appcenter.ingestion.models.Log) AppCenterLog(com.microsoft.appcenter.utils.AppCenterLog) CommonSchemaEventLog(com.microsoft.appcenter.analytics.ingestion.models.one.CommonSchemaEventLog) ArrayList(java.util.ArrayList) Extensions(com.microsoft.appcenter.ingestion.models.one.Extensions) AppCenterHandler(com.microsoft.appcenter.AppCenterHandler) InvocationOnMock(org.mockito.invocation.InvocationOnMock) CommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog) CommonSchemaEventLog(com.microsoft.appcenter.analytics.ingestion.models.one.CommonSchemaEventLog) Test(org.junit.Test)

Example 5 with AppCenterHandler

use of com.microsoft.appcenter.AppCenterHandler in project mobile-center-sdk-android by Microsoft.

the class WrapperSdkExceptionManagerTest method setUp.

@Before
public void setUp() {
    Crashes.unsetInstance();
    mockStatic(AppCenter.class);
    mockStatic(FileManager.class);
    mockStatic(SharedPreferencesManager.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(errorLogMock.getException()).thenReturn(new com.microsoft.appcenter.crashes.ingestion.models.Exception());
    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(SharedPreferencesManager.getBoolean(CRASHES_ENABLED_KEY, true)).thenReturn(true);
    /* Mock handlers. */
    mockStatic(HandlerUtils.class);
    Answer<Void> runNow = new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) {
            ((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) Exception(com.microsoft.appcenter.crashes.ingestion.models.Exception) JSONException(org.json.JSONException) IOException(java.io.IOException) PowerMockito.doAnswer(org.powermock.api.mockito.PowerMockito.doAnswer) Answer(org.mockito.stubbing.Answer) AppCenterHandler(com.microsoft.appcenter.AppCenterHandler) ManagedErrorLog(com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Matchers.anyBoolean(org.mockito.Matchers.anyBoolean) Before(org.junit.Before)

Aggregations

Context (android.content.Context)8 AppCenterHandler (com.microsoft.appcenter.AppCenterHandler)8 InvocationOnMock (org.mockito.invocation.InvocationOnMock)6 Answer (org.mockito.stubbing.Answer)5 CommonSchemaEventLog (com.microsoft.appcenter.analytics.ingestion.models.one.CommonSchemaEventLog)4 Exception (com.microsoft.appcenter.crashes.ingestion.models.Exception)4 ManagedErrorLog (com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog)4 CommonSchemaLog (com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog)4 Extensions (com.microsoft.appcenter.ingestion.models.one.Extensions)4 AppCenterFuture (com.microsoft.appcenter.utils.async.AppCenterFuture)4 IOException (java.io.IOException)4 JSONException (org.json.JSONException)4 Before (org.junit.Before)4 Test (org.junit.Test)4 Matchers.anyBoolean (org.mockito.Matchers.anyBoolean)4 PowerMockito.doAnswer (org.powermock.api.mockito.PowerMockito.doAnswer)4 ProtocolExtension (com.microsoft.appcenter.ingestion.models.one.ProtocolExtension)3 EventLog (com.microsoft.appcenter.analytics.ingestion.models.EventLog)2 Log (com.microsoft.appcenter.ingestion.models.Log)2 AppCenterLog (com.microsoft.appcenter.utils.AppCenterLog)2