Search in sources :

Example 1 with DeviceExtension

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

the class PropertyConfiguratorTest method collectDeviceId.

@Test
public void collectDeviceId() {
    CommonSchemaLog log = new CommonSchemaEventLog();
    log.setExt(new Extensions());
    log.getExt().setDevice(new DeviceExtension());
    /* Mock context. */
    mockStatic(Secure.class);
    when(Secure.getString(any(ContentResolver.class), anyString())).thenReturn("mockDeviceId");
    /* Get property configurator and collect device ID. */
    PropertyConfigurator pc = Analytics.getTransmissionTarget("test").getPropertyConfigurator();
    pc.collectDeviceId();
    /* Simulate what the pipeline does to convert from App Center to Common Schema. */
    log.addTransmissionTarget("test");
    log.setTag(Analytics.getTransmissionTarget("test"));
    pc.onPreparingLog(log, "groupName");
    /* Assert device ID is collected. */
    assertEquals("a:mockDeviceId", log.getExt().getDevice().getLocalId());
}
Also used : 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) DeviceExtension(com.microsoft.appcenter.ingestion.models.one.DeviceExtension) ContentResolver(android.content.ContentResolver) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 2 with DeviceExtension

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

the class PropertyConfiguratorTest method collectDeviceIdSavedWhenDisabled.

@Test
public void collectDeviceIdSavedWhenDisabled() {
    CommonSchemaLog log = new CommonSchemaEventLog();
    log.setExt(new Extensions());
    log.getExt().setDevice(new DeviceExtension());
    /* Mock context. */
    mockStatic(Secure.class);
    when(Secure.getString(any(ContentResolver.class), anyString())).thenReturn("mockDeviceId");
    /* Disable Analytics. */
    Analytics.setEnabled(false).get();
    /* Get property configurator and collect device ID. */
    PropertyConfigurator pc = Analytics.getTransmissionTarget("test").getPropertyConfigurator();
    pc.collectDeviceId();
    /* Simulate what the pipeline does to convert from App Center to Common Schema. */
    log.addTransmissionTarget("test");
    log.setTag(Analytics.getTransmissionTarget("test"));
    /* Enable and simulate log preparing. */
    Analytics.setEnabled(true).get();
    pc.onPreparingLog(log, "groupName");
    /* Assert device ID is collected. */
    assertEquals("a:mockDeviceId", log.getExt().getDevice().getLocalId());
}
Also used : 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) DeviceExtension(com.microsoft.appcenter.ingestion.models.one.DeviceExtension) ContentResolver(android.content.ContentResolver) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 3 with DeviceExtension

use of com.microsoft.appcenter.ingestion.models.one.DeviceExtension 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 4 with DeviceExtension

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

the class PropertyConfigurator method onPreparingLog.

/**
 * Override or inherit common schema properties while preparing log.
 *
 * @param log       A log.
 * @param groupName The group name.
 */
@Override
public void onPreparingLog(@NonNull Log log, @NonNull String groupName) {
    if (shouldOverridePartAProperties(log)) {
        AppExtension app = ((CommonSchemaLog) log).getExt().getApp();
        UserExtension user = ((CommonSchemaLog) log).getExt().getUser();
        DeviceExtension device = ((CommonSchemaLog) log).getExt().getDevice();
        /* Override app name if not null, else use the name of the nearest parent. */
        if (mAppName != null) {
            app.setName(mAppName);
        } else {
            for (AnalyticsTransmissionTarget target = mTransmissionTarget.mParentTarget; target != null; target = target.mParentTarget) {
                String parentAppName = target.getPropertyConfigurator().getAppName();
                if (parentAppName != null) {
                    app.setName(parentAppName);
                    break;
                }
            }
        }
        /* Override app version if not null, else use the version of the nearest parent. */
        if (mAppVersion != null) {
            app.setVer(mAppVersion);
        } else {
            for (AnalyticsTransmissionTarget target = mTransmissionTarget.mParentTarget; target != null; target = target.mParentTarget) {
                String parentAppVersion = target.getPropertyConfigurator().getAppVersion();
                if (parentAppVersion != null) {
                    app.setVer(parentAppVersion);
                    break;
                }
            }
        }
        /* Override app locale if not null, else use the locale of the nearest parent. */
        if (mAppLocale != null) {
            app.setLocale(mAppLocale);
        } else {
            for (AnalyticsTransmissionTarget target = mTransmissionTarget.mParentTarget; target != null; target = target.mParentTarget) {
                String parentAppLocale = target.getPropertyConfigurator().getAppLocale();
                if (parentAppLocale != null) {
                    app.setLocale(parentAppLocale);
                    break;
                }
            }
        }
        /* Override userId if not null, else use the userId of the nearest parent. */
        if (mUserId != null) {
            user.setLocalId(mUserId);
        } else {
            for (AnalyticsTransmissionTarget target = mTransmissionTarget.mParentTarget; target != null; target = target.mParentTarget) {
                String parentUserId = target.getPropertyConfigurator().getUserId();
                if (parentUserId != null) {
                    user.setLocalId(parentUserId);
                    break;
                }
            }
        }
        /* Fill out the device id if it has been collected. */
        if (mDeviceIdEnabled) {
            /* Get device identifier, Secure class already has an in memory cache. */
            @SuppressLint("HardwareIds") String androidId = Secure.getString(mTransmissionTarget.mContext.getContentResolver(), Secure.ANDROID_ID);
            device.setLocalId(ANDROID_DEVICE_ID_PREFIX + androidId);
        }
    }
}
Also used : AppExtension(com.microsoft.appcenter.ingestion.models.one.AppExtension) UserExtension(com.microsoft.appcenter.ingestion.models.one.UserExtension) SuppressLint(android.annotation.SuppressLint) DeviceExtension(com.microsoft.appcenter.ingestion.models.one.DeviceExtension)

Aggregations

DeviceExtension (com.microsoft.appcenter.ingestion.models.one.DeviceExtension)4 ContentResolver (android.content.ContentResolver)3 CommonSchemaEventLog (com.microsoft.appcenter.analytics.ingestion.models.one.CommonSchemaEventLog)3 CommonSchemaLog (com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog)3 Extensions (com.microsoft.appcenter.ingestion.models.one.Extensions)3 Test (org.junit.Test)3 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 AppExtension (com.microsoft.appcenter.ingestion.models.one.AppExtension)2 UserExtension (com.microsoft.appcenter.ingestion.models.one.UserExtension)2 SuppressLint (android.annotation.SuppressLint)1 Context (android.content.Context)1 AppCenterHandler (com.microsoft.appcenter.AppCenterHandler)1 LinkedList (java.util.LinkedList)1 Mockito.doAnswer (org.mockito.Mockito.doAnswer)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 Answer (org.mockito.stubbing.Answer)1