Search in sources :

Example 1 with AnalyticsListener

use of com.microsoft.azure.mobile.analytics.channel.AnalyticsListener in project mobile-center-sdk-android by Microsoft.

the class AnalyticsAndroidTest method testAnalyticsListener.

@Test
public void testAnalyticsListener() {
    AnalyticsListener analyticsListener = mock(AnalyticsListener.class);
    Analytics.setListener(analyticsListener);
    Channel channel = mock(Channel.class);
    Analytics.getInstance().onStarted(sContext, "", channel);
    Analytics.trackEvent("event");
    /* First process: enqueue log but network is down... */
    final EventLog log = new EventLog();
    log.setId(randomUUID());
    log.setName("name");
    Analytics.unsetInstance();
    Analytics.setListener(analyticsListener);
    verify(channel).enqueue(any(Log.class), anyString());
    verifyNoMoreInteractions(analyticsListener);
    /* Second process: sending succeeds. */
    final AtomicReference<Channel.GroupListener> groupListener = new AtomicReference<>();
    channel = mock(Channel.class);
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            Channel.GroupListener listener = (Channel.GroupListener) invocationOnMock.getArguments()[4];
            groupListener.set(listener);
            listener.onBeforeSending(log);
            return null;
        }
    }).when(channel).addGroup(anyString(), anyInt(), anyInt(), anyInt(), any(Channel.GroupListener.class));
    Analytics.unsetInstance();
    Analytics.setListener(analyticsListener);
    Analytics.getInstance().onStarted(sContext, "", channel);
    assertNotNull(groupListener.get());
    groupListener.get().onSuccess(log);
    verify(channel, never()).enqueue(any(Log.class), anyString());
    verify(analyticsListener).onBeforeSending(any(EventLog.class));
    verify(analyticsListener).onSendingSucceeded(any(EventLog.class));
    verifyNoMoreInteractions(analyticsListener);
}
Also used : Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) AnalyticsListener(com.microsoft.azure.mobile.analytics.channel.AnalyticsListener) MobileCenterLog(com.microsoft.azure.mobile.utils.MobileCenterLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) EventLog(com.microsoft.azure.mobile.analytics.ingestion.models.EventLog) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Channel(com.microsoft.azure.mobile.channel.Channel) EventLog(com.microsoft.azure.mobile.analytics.ingestion.models.EventLog) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 2 with AnalyticsListener

use of com.microsoft.azure.mobile.analytics.channel.AnalyticsListener 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 3 with AnalyticsListener

use of com.microsoft.azure.mobile.analytics.channel.AnalyticsListener in project mobile-center-sdk-android by Microsoft.

the class MainActivity method getAnalyticsListener.

private AnalyticsListener getAnalyticsListener() {
    return new AnalyticsListener() {

        @Override
        public void onBeforeSending(com.microsoft.azure.mobile.ingestion.models.Log log) {
            if (log instanceof EventLog) {
                Toast.makeText(MainActivity.this, R.string.event_before_sending, Toast.LENGTH_SHORT).show();
            } else if (log instanceof PageLog) {
                Toast.makeText(MainActivity.this, R.string.page_before_sending, Toast.LENGTH_SHORT).show();
            }
            analyticsIdlingResource.increment();
        }

        @Override
        public void onSendingFailed(com.microsoft.azure.mobile.ingestion.models.Log log, Exception e) {
            String message = null;
            if (log instanceof EventLog) {
                message = getString(R.string.event_sent_failed);
            } else if (log instanceof PageLog) {
                message = getString(R.string.page_sent_failed);
            }
            if (message != null) {
                message = String.format("%s\nException: %s", message, e.toString());
                Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();
            }
            analyticsIdlingResource.decrement();
        }

        @Override
        public void onSendingSucceeded(com.microsoft.azure.mobile.ingestion.models.Log log) {
            String message = null;
            if (log instanceof EventLog) {
                message = String.format("%s\nName: %s", getString(R.string.event_sent_succeeded), ((EventLog) log).getName());
            } else if (log instanceof PageLog) {
                message = String.format("%s\nName: %s", getString(R.string.page_sent_succeeded), ((PageLog) log).getName());
            }
            if (message != null) {
                if (((LogWithProperties) log).getProperties() != null) {
                    message += String.format("\nProperties: %s", new JSONObject(((LogWithProperties) log).getProperties()).toString());
                }
                Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();
            }
            analyticsIdlingResource.decrement();
        }
    };
}
Also used : AnalyticsListener(com.microsoft.azure.mobile.analytics.channel.AnalyticsListener) JSONObject(org.json.JSONObject) LogWithProperties(com.microsoft.azure.mobile.ingestion.models.LogWithProperties) PageLog(com.microsoft.azure.mobile.analytics.ingestion.models.PageLog) Log(android.util.Log) MobileCenterLog(com.microsoft.azure.mobile.utils.MobileCenterLog) EventLog(com.microsoft.azure.mobile.analytics.ingestion.models.EventLog) PageLog(com.microsoft.azure.mobile.analytics.ingestion.models.PageLog) EventLog(com.microsoft.azure.mobile.analytics.ingestion.models.EventLog)

Example 4 with AnalyticsListener

use of com.microsoft.azure.mobile.analytics.channel.AnalyticsListener in project mobile-center-sdk-android by Microsoft.

the class AnalyticsTest method testAnalyticsListenerNull.

@Test
public void testAnalyticsListenerNull() {
    AnalyticsListener analyticsListener = mock(AnalyticsListener.class);
    Analytics.setListener(analyticsListener);
    Analytics.setListener(null);
    final EventLog testEventLog = new EventLog();
    testEventLog.setId(UUID.randomUUID());
    testEventLog.setName("name");
    final Exception testException = new Exception("test exception message");
    Channel.GroupListener listener = Analytics.getInstance().getChannelListener();
    listener.onBeforeSending(testEventLog);
    listener.onSuccess(testEventLog);
    listener.onFailure(testEventLog, testException);
    verify(analyticsListener, never()).onBeforeSending(any(EventLog.class));
    verify(analyticsListener, never()).onSendingSucceeded(any(EventLog.class));
    verify(analyticsListener, never()).onSendingFailed(any(EventLog.class), any(Exception.class));
}
Also used : AnalyticsListener(com.microsoft.azure.mobile.analytics.channel.AnalyticsListener) 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)

Aggregations

AnalyticsListener (com.microsoft.azure.mobile.analytics.channel.AnalyticsListener)4 EventLog (com.microsoft.azure.mobile.analytics.ingestion.models.EventLog)4 Channel (com.microsoft.azure.mobile.channel.Channel)3 MobileCenterLog (com.microsoft.azure.mobile.utils.MobileCenterLog)3 Test (org.junit.Test)3 PageLog (com.microsoft.azure.mobile.analytics.ingestion.models.PageLog)2 Log (com.microsoft.azure.mobile.ingestion.models.Log)2 IOException (java.io.IOException)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 Log (android.util.Log)1 StartSessionLog (com.microsoft.azure.mobile.analytics.ingestion.models.StartSessionLog)1 LogWithProperties (com.microsoft.azure.mobile.ingestion.models.LogWithProperties)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 JSONObject (org.json.JSONObject)1 Mockito.doAnswer (org.mockito.Mockito.doAnswer)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 Answer (org.mockito.stubbing.Answer)1