Search in sources :

Example 6 with StartSessionLog

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

the class AnalyticsTest method disableAutomaticPageTracking.

@Test
public void disableAutomaticPageTracking() {
    Analytics analytics = Analytics.getInstance();
    assertTrue(Analytics.isAutoPageTrackingEnabled());
    Analytics.setAutoPageTrackingEnabled(false);
    assertFalse(Analytics.isAutoPageTrackingEnabled());
    Channel channel = mock(Channel.class);
    analytics.onStarting(mAppCenterHandler);
    analytics.onStarted(mock(Context.class), "", channel);
    analytics.onActivityResumed(new MyActivity());
    verify(channel).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object argument) {
            return argument instanceof StartSessionLog;
        }
    }), anyString());
    verify(channel, never()).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object argument) {
            return argument instanceof PageLog;
        }
    }), anyString());
    Analytics.setAutoPageTrackingEnabled(true);
    assertTrue(Analytics.isAutoPageTrackingEnabled());
    analytics.onActivityResumed(new SomeScreen());
    verify(channel).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object item) {
            if (item instanceof PageLog) {
                PageLog pageLog = (PageLog) item;
                return "SomeScreen".equals(pageLog.getName());
            }
            return false;
        }
    }), eq(analytics.getGroupName()));
}
Also used : Context(android.content.Context) StartSessionLog(com.microsoft.appcenter.analytics.ingestion.models.StartSessionLog) PageLog(com.microsoft.appcenter.analytics.ingestion.models.PageLog) Channel(com.microsoft.appcenter.channel.Channel) ArgumentMatcher(org.mockito.ArgumentMatcher) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 7 with StartSessionLog

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

the class AnalyticsTest method startSessionAfterUserApproval.

@Test
public void startSessionAfterUserApproval() {
    /*
         * Disable analytics while in background to set up the initial condition
         * simulating the opt-in use case.
         */
    Analytics analytics = Analytics.getInstance();
    Channel channel = mock(Channel.class);
    analytics.onStarting(mAppCenterHandler);
    analytics.onStarted(mock(Context.class), "", channel);
    Analytics.setEnabled(false);
    /* App in foreground: no log yet, we are disabled. */
    analytics.onActivityResumed(new Activity());
    verify(channel, never()).enqueue(any(Log.class), eq(analytics.getGroupName()));
    /* Enable: start session sent retroactively. */
    Analytics.setEnabled(true);
    verify(channel).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object argument) {
            return argument instanceof StartSessionLog;
        }
    }), eq(analytics.getGroupName()));
    verify(channel).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object argument) {
            return argument instanceof PageLog;
        }
    }), eq(analytics.getGroupName()));
    /* Go background. */
    analytics.onActivityPaused(new Activity());
    /* Disable/enable: nothing happens on background. */
    Analytics.setEnabled(false);
    Analytics.setEnabled(true);
    /* No additional log. */
    verify(channel, times(2)).enqueue(any(Log.class), eq(analytics.getGroupName()));
}
Also used : Context(android.content.Context) StartSessionLog(com.microsoft.appcenter.analytics.ingestion.models.StartSessionLog) PageLog(com.microsoft.appcenter.analytics.ingestion.models.PageLog) EventLog(com.microsoft.appcenter.analytics.ingestion.models.EventLog) Log(com.microsoft.appcenter.ingestion.models.Log) AppCenterLog(com.microsoft.appcenter.utils.AppCenterLog) StartSessionLog(com.microsoft.appcenter.analytics.ingestion.models.StartSessionLog) PageLog(com.microsoft.appcenter.analytics.ingestion.models.PageLog) Channel(com.microsoft.appcenter.channel.Channel) ArgumentMatcher(org.mockito.ArgumentMatcher) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 8 with StartSessionLog

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

the class SessionTrackerTest method startSessionWithoutLogs.

@Test
public void startSessionWithoutLogs() {
    final AtomicReference<StartSessionLog> startSessionLog = new AtomicReference<>();
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            startSessionLog.set((StartSessionLog) invocation.getArguments()[0]);
            return null;
        }
    }).when(mChannel).enqueue(notNull(StartSessionLog.class), eq(TEST_GROUP));
    /* Go foreground, start session is sent. */
    mSessionTracker.onActivityResumed();
    verify(mChannel, times(1)).enqueue(notNull(StartSessionLog.class), eq(TEST_GROUP));
    assertNotNull(startSessionLog.get());
    UUID sid = startSessionLog.get().getSid();
    assertNotNull(sid);
    /* Change screen after a long time, session reused. */
    spendTime(30000);
    mSessionTracker.onActivityPaused();
    spendTime(1);
    mSessionTracker.onActivityResumed();
    verify(mChannel, times(1)).enqueue(notNull(StartSessionLog.class), eq(TEST_GROUP));
    /* Go background and come back after timeout, second session. */
    spendTime(1);
    mSessionTracker.onActivityPaused();
    spendTime(30000);
    mSessionTracker.onActivityResumed();
    verify(mChannel, times(2)).enqueue(notNull(StartSessionLog.class), eq(TEST_GROUP));
    assertNotEquals(sid, startSessionLog.get().getSid());
}
Also used : Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) StartSessionLog(com.microsoft.appcenter.analytics.ingestion.models.StartSessionLog) InvocationOnMock(org.mockito.invocation.InvocationOnMock) AtomicReference(java.util.concurrent.atomic.AtomicReference) UUID(java.util.UUID) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 9 with StartSessionLog

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

the class SessionTrackerTest method goBackgroundAndComeBackMuchLater.

@Test
public void goBackgroundAndComeBackMuchLater() {
    /* Application is in foreground, send a log, verify decoration with a new session. */
    mSessionTracker.onActivityResumed();
    UUID expectedSid;
    StartSessionLog expectedStartSessionLog = new StartSessionLog();
    {
        Log log = newEvent();
        mSessionTracker.onEnqueuingLog(log, TEST_GROUP);
        mSessionTracker.onEnqueuingLog(expectedStartSessionLog, TEST_GROUP);
        assertNotNull(log.getSid());
        expectedSid = log.getSid();
        expectedStartSessionLog.setSid(expectedSid);
        verify(mChannel).enqueue(expectedStartSessionLog, TEST_GROUP);
    }
    /* Go background. */
    spendTime(1);
    mSessionTracker.onActivityPaused();
    /* Come back after a long time. */
    spendTime(30000);
    mSessionTracker.onActivityResumed();
    /* Send a log again: new session. */
    {
        Log log = newEvent();
        mSessionTracker.onEnqueuingLog(log, TEST_GROUP);
        mSessionTracker.onEnqueuingLog(expectedStartSessionLog, TEST_GROUP);
        assertNotEquals(expectedSid, log.getSid());
        expectedSid = log.getSid();
        expectedStartSessionLog.setSid(expectedSid);
        verify(mChannel).enqueue(expectedStartSessionLog, TEST_GROUP);
    }
    /* In total we sent only 2 session logs. */
    verify(mChannel, times(2)).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object argument) {
            return argument instanceof StartSessionLog;
        }
    }), anyString());
}
Also used : StartSessionLog(com.microsoft.appcenter.analytics.ingestion.models.StartSessionLog) StartSessionLog(com.microsoft.appcenter.analytics.ingestion.models.StartSessionLog) StartServiceLog(com.microsoft.appcenter.ingestion.models.StartServiceLog) EventLog(com.microsoft.appcenter.analytics.ingestion.models.EventLog) Log(com.microsoft.appcenter.ingestion.models.Log) ArgumentMatcher(org.mockito.ArgumentMatcher) UUID(java.util.UUID) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 10 with StartSessionLog

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

the class SessionTrackerTest method stayOnFirstScreenForLong.

@Test
public void stayOnFirstScreenForLong() {
    /* Application is in foreground, send a log, verify decoration with a new session. */
    mSessionTracker.onActivityResumed();
    UUID expectedSid;
    StartSessionLog expectedStartSessionLog = new StartSessionLog();
    {
        Log log = newEvent();
        mSessionTracker.onEnqueuingLog(log, TEST_GROUP);
        mSessionTracker.onEnqueuingLog(expectedStartSessionLog, TEST_GROUP);
        assertNotNull(log.getSid());
        expectedSid = log.getSid();
        expectedStartSessionLog.setSid(expectedSid);
        verify(mChannel).enqueue(expectedStartSessionLog, TEST_GROUP);
    }
    /* Wait a long time. */
    spendTime(30000);
    /* Go to another activity. */
    mSessionTracker.onActivityPaused();
    spendTime(2);
    mSessionTracker.onActivityResumed();
    /* Send a log again: session must be reused. */
    {
        Log log = newEvent();
        mSessionTracker.onEnqueuingLog(log, TEST_GROUP);
        mSessionTracker.onEnqueuingLog(expectedStartSessionLog, TEST_GROUP);
        assertEquals(expectedSid, log.getSid());
        verify(mChannel).enqueue(expectedStartSessionLog, TEST_GROUP);
    }
}
Also used : StartSessionLog(com.microsoft.appcenter.analytics.ingestion.models.StartSessionLog) StartSessionLog(com.microsoft.appcenter.analytics.ingestion.models.StartSessionLog) StartServiceLog(com.microsoft.appcenter.ingestion.models.StartServiceLog) EventLog(com.microsoft.appcenter.analytics.ingestion.models.EventLog) Log(com.microsoft.appcenter.ingestion.models.Log) UUID(java.util.UUID) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

StartSessionLog (com.microsoft.appcenter.analytics.ingestion.models.StartSessionLog)10 Test (org.junit.Test)8 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)7 EventLog (com.microsoft.appcenter.analytics.ingestion.models.EventLog)6 Log (com.microsoft.appcenter.ingestion.models.Log)6 UUID (java.util.UUID)6 StartServiceLog (com.microsoft.appcenter.ingestion.models.StartServiceLog)5 PageLog (com.microsoft.appcenter.analytics.ingestion.models.PageLog)3 Date (java.util.Date)3 ArgumentMatcher (org.mockito.ArgumentMatcher)3 Context (android.content.Context)2 Channel (com.microsoft.appcenter.channel.Channel)2 SessionContext (com.microsoft.appcenter.SessionContext)1 EventLogFactory (com.microsoft.appcenter.analytics.ingestion.models.json.EventLogFactory)1 PageLogFactory (com.microsoft.appcenter.analytics.ingestion.models.json.PageLogFactory)1 StartSessionLogFactory (com.microsoft.appcenter.analytics.ingestion.models.json.StartSessionLogFactory)1 Device (com.microsoft.appcenter.ingestion.models.Device)1 LogContainer (com.microsoft.appcenter.ingestion.models.LogContainer)1 DefaultLogSerializer (com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer)1 LogSerializer (com.microsoft.appcenter.ingestion.models.json.LogSerializer)1