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);
}
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);
}
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();
}
};
}
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));
}
Aggregations