Search in sources :

Example 6 with AppCenterHandler

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

the class PropertyConfiguratorTest method overridingPartAIsNotRetroactive.

@Test
public void overridingPartAIsNotRetroactive() {
    /* Mock deviceId. */
    mockStatic(Secure.class);
    when(Secure.getString(any(ContentResolver.class), anyString())).thenReturn("mockDeviceId");
    /* 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);
    final LinkedList<Runnable> backgroundCommands = new LinkedList<>();
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) {
            backgroundCommands.add((Runnable) invocation.getArguments()[0]);
            return null;
        }
    }).when(handler).post(notNull(Runnable.class), any(Runnable.class));
    analytics.onStarting(handler);
    analytics.onStarted(mock(Context.class), mChannel, null, null, true);
    /* Create a target. */
    AnalyticsTransmissionTarget target = Analytics.getTransmissionTarget("test");
    /* Init target background code now. */
    backgroundCommands.removeFirst().run();
    /* Simulate a track event call with no property. */
    CommonSchemaLog logBeforeSetProperty = new CommonSchemaEventLog();
    logBeforeSetProperty.setExt(new Extensions());
    logBeforeSetProperty.getExt().setApp(new AppExtension());
    logBeforeSetProperty.getExt().setUser(new UserExtension());
    logBeforeSetProperty.getExt().setDevice(new DeviceExtension());
    /* Simulate what the pipeline does to convert from App Center to Common Schema. */
    logBeforeSetProperty.addTransmissionTarget("test");
    logBeforeSetProperty.setTag(target);
    /* Set all common properties that should not be set retroactively before first log persisted. */
    target.getPropertyConfigurator().setAppVersion("appVersion");
    target.getPropertyConfigurator().setAppLocale("appLocale");
    target.getPropertyConfigurator().setAppName("appName");
    target.getPropertyConfigurator().setUserId("c:alice");
    target.getPropertyConfigurator().collectDeviceId();
    /* Run background commands in order: first prepare first log, then all the set property calls. */
    target.getPropertyConfigurator().onPreparingLog(logBeforeSetProperty, "groupName");
    for (Runnable command : backgroundCommands) {
        command.run();
    }
    /* Simulate the second track event call. */
    CommonSchemaLog logAfterSetProperty = new CommonSchemaEventLog();
    logAfterSetProperty.setExt(new Extensions());
    logAfterSetProperty.getExt().setApp(new AppExtension());
    logAfterSetProperty.getExt().setUser(new UserExtension());
    logAfterSetProperty.getExt().setDevice(new DeviceExtension());
    /* Simulate what the pipeline does to convert from App Center to Common Schema. */
    logAfterSetProperty.addTransmissionTarget("test");
    logAfterSetProperty.setTag(target);
    target.getPropertyConfigurator().onPreparingLog(logAfterSetProperty, "groupName");
    /* Check first log has no property. */
    assertNull(logBeforeSetProperty.getExt().getApp().getVer());
    assertNull(logBeforeSetProperty.getExt().getApp().getLocale());
    assertNull(logBeforeSetProperty.getExt().getApp().getName());
    assertNull(logBeforeSetProperty.getExt().getUser().getLocalId());
    assertNull(logBeforeSetProperty.getExt().getDevice().getLocalId());
    /* Check second log has all of them. */
    assertEquals("appVersion", logAfterSetProperty.getExt().getApp().getVer());
    assertEquals("appLocale", logAfterSetProperty.getExt().getApp().getLocale());
    assertEquals("appName", logAfterSetProperty.getExt().getApp().getName());
    assertEquals("c:alice", logAfterSetProperty.getExt().getUser().getLocalId());
    assertEquals("a:mockDeviceId", logAfterSetProperty.getExt().getDevice().getLocalId());
}
Also used : Context(android.content.Context) AppExtension(com.microsoft.appcenter.ingestion.models.one.AppExtension) Extensions(com.microsoft.appcenter.ingestion.models.one.Extensions) DeviceExtension(com.microsoft.appcenter.ingestion.models.one.DeviceExtension) LinkedList(java.util.LinkedList) ContentResolver(android.content.ContentResolver) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Answer(org.mockito.stubbing.Answer) AppCenterHandler(com.microsoft.appcenter.AppCenterHandler) InvocationOnMock(org.mockito.invocation.InvocationOnMock) CommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog) UserExtension(com.microsoft.appcenter.ingestion.models.one.UserExtension) CommonSchemaEventLog(com.microsoft.appcenter.analytics.ingestion.models.one.CommonSchemaEventLog) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 7 with AppCenterHandler

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

the class UncaughtExceptionHandlerTest method setUp.

@Before
public void setUp() {
    Crashes.unsetInstance();
    mockStatic(AppCenter.class);
    mockStatic(AppCenterLog.class);
    mockStatic(SystemClock.class);
    mockStatic(FileManager.class);
    mockStatic(SharedPreferencesManager.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(SharedPreferencesManager.getBoolean(CRASHES_ENABLED_KEY, true)).thenReturn(true);
    /* Then simulate further changes to state. */
    PowerMockito.doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) {
            /* Whenever the new state is persisted, make further calls return the new state. */
            boolean enabled = (Boolean) invocation.getArguments()[1];
            Mockito.when(SharedPreferencesManager.getBoolean(CRASHES_ENABLED_KEY, true)).thenReturn(enabled);
            return null;
        }
    }).when(SharedPreferencesManager.class);
    SharedPreferencesManager.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());
    when(errorLogMock.getException()).thenReturn(new com.microsoft.appcenter.crashes.ingestion.models.Exception());
    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) {
            ((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) Exception(com.microsoft.appcenter.crashes.ingestion.models.Exception) 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) Matchers.anyBoolean(org.mockito.Matchers.anyBoolean) File(java.io.File) Before(org.junit.Before)

Example 8 with AppCenterHandler

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

the class AnalyticsTransmissionTargetTest method registerCallbackWhenDisabledWorks.

@Test
public void registerCallbackWhenDisabledWorks() {
    /* Simulate disabling and background thread. */
    Analytics analytics = Analytics.getInstance();
    AppCenterHandler handler = mock(AppCenterHandler.class);
    ArgumentCaptor<Runnable> backgroundRunnable = ArgumentCaptor.forClass(Runnable.class);
    ArgumentCaptor<Runnable> disabledRunnable = ArgumentCaptor.forClass(Runnable.class);
    doNothing().when(handler).post(backgroundRunnable.capture(), disabledRunnable.capture());
    analytics.onStarting(handler);
    analytics.onStarted(mock(Context.class), mChannel, null, "test", true);
    /* Disable. */
    Analytics.setEnabled(false);
    backgroundRunnable.getValue().run();
    /* Add authentication provider while disabled. */
    AuthenticationProvider.TokenProvider tokenProvider = mock(AuthenticationProvider.TokenProvider.class);
    AuthenticationProvider authenticationProvider = spy(new AuthenticationProvider(AuthenticationProvider.Type.MSA_COMPACT, "key1", tokenProvider));
    AnalyticsTransmissionTarget.addAuthenticationProvider(authenticationProvider);
    /* Unlock command. */
    disabledRunnable.getValue().run();
    /* Verify update while disabled. */
    assertEquals(authenticationProvider, AnalyticsTransmissionTarget.sAuthenticationProvider);
    verify(authenticationProvider).acquireTokenAsync();
    /* Enable. */
    Analytics.setEnabled(true);
    disabledRunnable.getValue().run();
    /* Call prepare log. */
    ProtocolExtension protocol = new ProtocolExtension();
    Extensions ext = new Extensions();
    ext.setProtocol(protocol);
    CommonSchemaLog log = new CommonSchemaEventLog();
    log.setExt(ext);
    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) CommonSchemaEventLog(com.microsoft.appcenter.analytics.ingestion.models.one.CommonSchemaEventLog) Extensions(com.microsoft.appcenter.ingestion.models.one.Extensions) Test(org.junit.Test)

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