Search in sources :

Example 6 with Log

use of com.microsoft.azure.mobile.ingestion.models.Log in project mobile-center-sdk-android by Microsoft.

the class AnalyticsTest method testGetChannelListener.

@Test
public void testGetChannelListener() throws IOException, ClassNotFoundException {
    final EventLog testEventLog = new EventLog();
    testEventLog.setId(UUID.randomUUID());
    testEventLog.setName("name");
    final Exception testException = new Exception("test exception message");
    Analytics.setListener(new AnalyticsListener() {

        @Override
        public void onBeforeSending(Log log) {
            assertEquals(log, testEventLog);
        }

        @Override
        public void onSendingSucceeded(Log log) {
            assertEquals(log, testEventLog);
        }

        @Override
        public void onSendingFailed(Log log, Exception e) {
            assertEquals(log, testEventLog);
            assertEquals(e, testException);
        }
    });
    Channel.GroupListener listener = Analytics.getInstance().getChannelListener();
    listener.onBeforeSending(testEventLog);
    listener.onSuccess(testEventLog);
    listener.onFailure(testEventLog, testException);
}
Also used : AnalyticsListener(com.microsoft.azure.mobile.analytics.channel.AnalyticsListener) MobileCenterLog(com.microsoft.azure.mobile.utils.MobileCenterLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) PageLog(com.microsoft.azure.mobile.analytics.ingestion.models.PageLog) StartSessionLog(com.microsoft.azure.mobile.analytics.ingestion.models.StartSessionLog) EventLog(com.microsoft.azure.mobile.analytics.ingestion.models.EventLog) EventLog(com.microsoft.azure.mobile.analytics.ingestion.models.EventLog) Channel(com.microsoft.azure.mobile.channel.Channel) IOException(java.io.IOException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 7 with Log

use of com.microsoft.azure.mobile.ingestion.models.Log in project mobile-center-sdk-android by Microsoft.

the class AnalyticsTest method testTrackPage.

@Test
public void testTrackPage() {
    Analytics analytics = Analytics.getInstance();
    Channel channel = mock(Channel.class);
    analytics.onStarted(mock(Context.class), "", channel);
    Analytics.trackPage(null, null);
    verify(channel, never()).enqueue(any(Log.class), anyString());
    reset(channel);
    Analytics.trackPage("", null);
    verify(channel, never()).enqueue(any(Log.class), anyString());
    reset(channel);
    Analytics.trackPage(" ", null);
    verify(channel, times(1)).enqueue(any(Log.class), anyString());
    reset(channel);
    Analytics.trackPage(generateString(257, '*'), null);
    verify(channel, never()).enqueue(any(Log.class), anyString());
    reset(channel);
    Analytics.trackPage(generateString(256, '*'), null);
    verify(channel, times(1)).enqueue(any(Log.class), anyString());
    reset(channel);
    Analytics.trackPage("pageName", new HashMap<String, String>() {

        {
            put(null, null);
            put("", null);
            put(generateString(65, '*'), null);
            put("1", null);
            put("2", generateString(65, '*'));
        }
    });
    verify(channel, times(1)).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object item) {
            if (item instanceof PageLog) {
                PageLog pageLog = (PageLog) item;
                return pageLog.getProperties().size() == 0;
            }
            return false;
        }
    }), anyString());
    reset(channel);
    final String validMapItem = "valid";
    Analytics.trackPage("pageName", new HashMap<String, String>() {

        {
            for (int i = 0; i < 10; i++) {
                put(validMapItem + i, validMapItem);
            }
        }
    });
    verify(channel, times(1)).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object item) {
            if (item instanceof PageLog) {
                PageLog pageLog = (PageLog) item;
                return pageLog.getProperties().size() == 5;
            }
            return false;
        }
    }), anyString());
}
Also used : Context(android.content.Context) MobileCenterLog(com.microsoft.azure.mobile.utils.MobileCenterLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) PageLog(com.microsoft.azure.mobile.analytics.ingestion.models.PageLog) StartSessionLog(com.microsoft.azure.mobile.analytics.ingestion.models.StartSessionLog) EventLog(com.microsoft.azure.mobile.analytics.ingestion.models.EventLog) PageLog(com.microsoft.azure.mobile.analytics.ingestion.models.PageLog) Channel(com.microsoft.azure.mobile.channel.Channel) ArgumentMatcher(org.mockito.ArgumentMatcher) Matchers.anyString(org.mockito.Matchers.anyString) TestUtils.generateString(com.microsoft.azure.mobile.test.TestUtils.generateString) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 8 with Log

use of com.microsoft.azure.mobile.ingestion.models.Log in project mobile-center-sdk-android by Microsoft.

the class SessionTrackerTest method ignoreStartService.

@Test
public void ignoreStartService() {
    Log startServiceLog = spy(new StartServiceLog());
    mSessionTracker.onEnqueuingLog(startServiceLog, TEST_GROUP);
    verify(mChannel, never()).enqueue(any(Log.class), anyString());
    verify(startServiceLog, never()).setSid(any(UUID.class));
}
Also used : StartServiceLog(com.microsoft.azure.mobile.ingestion.models.StartServiceLog) StartSessionLog(com.microsoft.azure.mobile.analytics.ingestion.models.StartSessionLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) EventLog(com.microsoft.azure.mobile.analytics.ingestion.models.EventLog) StartServiceLog(com.microsoft.azure.mobile.ingestion.models.StartServiceLog) UUID(java.util.UUID) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 9 with Log

use of com.microsoft.azure.mobile.ingestion.models.Log 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.azure.mobile.analytics.ingestion.models.StartSessionLog) StartServiceLog(com.microsoft.azure.mobile.ingestion.models.StartServiceLog) StartSessionLog(com.microsoft.azure.mobile.analytics.ingestion.models.StartSessionLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) EventLog(com.microsoft.azure.mobile.analytics.ingestion.models.EventLog) UUID(java.util.UUID) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 10 with Log

use of com.microsoft.azure.mobile.ingestion.models.Log in project mobile-center-sdk-android by Microsoft.

the class SessionTrackerTest method longSessionStartingFromBackground.

@Test
public void longSessionStartingFromBackground() {
    /* Application is in background, send a log, verify decoration. */
    UUID firstSid;
    long firstSessionTime = mMockTime;
    UUID expectedSid;
    StartSessionLog expectedStartSessionLog = new StartSessionLog();
    {
        Log log = newEvent();
        mSessionTracker.onEnqueuingLog(log, TEST_GROUP);
        mSessionTracker.onEnqueuingLog(expectedStartSessionLog, TEST_GROUP);
        assertNotNull(log.getSid());
        firstSid = expectedSid = log.getSid();
        expectedStartSessionLog.setSid(expectedSid);
        verify(mChannel).enqueue(expectedStartSessionLog, TEST_GROUP);
    }
    /* Verify session reused for second log. */
    {
        Log log = newEvent();
        mSessionTracker.onEnqueuingLog(log, TEST_GROUP);
        mSessionTracker.onEnqueuingLog(expectedStartSessionLog, TEST_GROUP);
        assertEquals(expectedSid, log.getSid());
        verify(mChannel).enqueue(expectedStartSessionLog, TEST_GROUP);
    }
    /* No usage from background for a long time: new session. */
    {
        spendTime(30000);
        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);
    }
    /* App comes to foreground and sends a log, still session. */
    {
        mSessionTracker.onActivityResumed();
        Log log = newEvent();
        mSessionTracker.onEnqueuingLog(log, TEST_GROUP);
        mSessionTracker.onEnqueuingLog(expectedStartSessionLog, TEST_GROUP);
        assertEquals(expectedSid, log.getSid());
        verify(mChannel).enqueue(expectedStartSessionLog, TEST_GROUP);
    }
    /* We are in foreground, even after timeout a log is still in session. */
    {
        spendTime(30000);
        Log log = newEvent();
        mSessionTracker.onEnqueuingLog(log, TEST_GROUP);
        mSessionTracker.onEnqueuingLog(expectedStartSessionLog, TEST_GROUP);
        assertEquals(expectedSid, log.getSid());
        verify(mChannel).enqueue(expectedStartSessionLog, TEST_GROUP);
    }
    /* Switch to another activity and send a log, still session. */
    {
        spendTime(2);
        mSessionTracker.onActivityPaused();
        spendTime(2);
        mSessionTracker.onActivityResumed();
        Log log = newEvent();
        mSessionTracker.onEnqueuingLog(log, TEST_GROUP);
        mSessionTracker.onEnqueuingLog(expectedStartSessionLog, TEST_GROUP);
        assertEquals(expectedSid, log.getSid());
        verify(mChannel).enqueue(expectedStartSessionLog, TEST_GROUP);
    }
    /* We are in foreground, even after timeout a log is still in session. */
    {
        spendTime(30000);
        Log log = newEvent();
        mSessionTracker.onEnqueuingLog(log, TEST_GROUP);
        mSessionTracker.onEnqueuingLog(expectedStartSessionLog, TEST_GROUP);
        assertEquals(expectedSid, log.getSid());
        verify(mChannel).enqueue(expectedStartSessionLog, TEST_GROUP);
    }
    /* Background for a short time and send log: still in session. */
    {
        spendTime(2);
        mSessionTracker.onActivityPaused();
        spendTime(2);
        Log log = newEvent();
        mSessionTracker.onEnqueuingLog(log, TEST_GROUP);
        mSessionTracker.onEnqueuingLog(expectedStartSessionLog, TEST_GROUP);
        assertEquals(expectedSid, log.getSid());
        verify(mChannel).enqueue(expectedStartSessionLog, TEST_GROUP);
    }
    /* Background for a long time but correlating a log to first session: should not trigger new session. */
    {
        Log log = newEvent();
        log.setToffset(firstSessionTime + 20);
        mSessionTracker.onEnqueuingLog(log, TEST_GROUP);
        mSessionTracker.onEnqueuingLog(expectedStartSessionLog, TEST_GROUP);
        assertEquals(firstSid, log.getSid());
        verify(mChannel).enqueue(expectedStartSessionLog, TEST_GROUP);
    }
    /* Background for a long time and coming back to foreground: new session. */
    {
        spendTime(30000);
        mSessionTracker.onActivityResumed();
        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);
    }
    /* Background for a long time sending a log: new session. */
    {
        mSessionTracker.onActivityPaused();
        spendTime(30000);
        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);
    }
}
Also used : StartSessionLog(com.microsoft.azure.mobile.analytics.ingestion.models.StartSessionLog) StartServiceLog(com.microsoft.azure.mobile.ingestion.models.StartServiceLog) StartSessionLog(com.microsoft.azure.mobile.analytics.ingestion.models.StartSessionLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) EventLog(com.microsoft.azure.mobile.analytics.ingestion.models.EventLog) UUID(java.util.UUID) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

Log (com.microsoft.azure.mobile.ingestion.models.Log)59 Test (org.junit.Test)44 MobileCenterLog (com.microsoft.azure.mobile.utils.MobileCenterLog)37 ArrayList (java.util.ArrayList)24 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)20 Context (android.content.Context)18 UUID (java.util.UUID)18 EventLog (com.microsoft.azure.mobile.analytics.ingestion.models.EventLog)13 LogSerializer (com.microsoft.azure.mobile.ingestion.models.json.LogSerializer)13 StartSessionLog (com.microsoft.azure.mobile.analytics.ingestion.models.StartSessionLog)12 LogContainer (com.microsoft.azure.mobile.ingestion.models.LogContainer)12 DefaultLogSerializer (com.microsoft.azure.mobile.ingestion.models.json.DefaultLogSerializer)11 Persistence (com.microsoft.azure.mobile.persistence.Persistence)11 Matchers.anyString (org.mockito.Matchers.anyString)10 IOException (java.io.IOException)9 Channel (com.microsoft.azure.mobile.channel.Channel)8 IngestionHttp (com.microsoft.azure.mobile.ingestion.IngestionHttp)8 StartServiceLog (com.microsoft.azure.mobile.ingestion.models.StartServiceLog)8 JSONException (org.json.JSONException)8 MediumTest (android.support.test.filters.MediumTest)7