use of com.microsoft.azure.mobile.channel.Channel 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.channel.Channel in project mobile-center-sdk-android by Microsoft.
the class CrashesAndroidTest method getLastSessionCrashReport.
@Test
public void getLastSessionCrashReport() throws InterruptedException {
/* Crash on 1st process. */
Thread.UncaughtExceptionHandler uncaughtExceptionHandler = mock(Thread.UncaughtExceptionHandler.class);
Thread.setDefaultUncaughtExceptionHandler(uncaughtExceptionHandler);
Channel channel = mock(Channel.class);
Crashes.getInstance().onStarted(sContext, "", channel);
final Error exception = generateStackOverflowError();
assertTrue(exception.getStackTrace().length > ErrorLogHelper.FRAME_LIMIT);
final Thread thread = new Thread() {
@Override
public void run() {
throw exception;
}
};
thread.start();
thread.join();
/* Get last session crash on 2nd process. */
Crashes.unsetInstance();
Crashes.getInstance().onStarted(sContext, "", channel);
assertNotNull(Crashes.getLastSessionCrashReport());
/* Try to get last session crash after Crashes service completed processing. */
assertNotNull(Crashes.getLastSessionCrashReport());
}
use of com.microsoft.azure.mobile.channel.Channel in project mobile-center-sdk-android by Microsoft.
the class CrashesAndroidTest method setEnabledWhileAlreadyEnabledShouldNotDuplicateCrashReport.
@Test
public void setEnabledWhileAlreadyEnabledShouldNotDuplicateCrashReport() throws InterruptedException {
/* Test the fix of the duplicate crash sending bug. */
android.util.Log.i(TAG, "Process 1");
Thread.UncaughtExceptionHandler uncaughtExceptionHandler = mock(Thread.UncaughtExceptionHandler.class);
Thread.setDefaultUncaughtExceptionHandler(uncaughtExceptionHandler);
Channel channel = mock(Channel.class);
Crashes.getInstance().onStarted(sContext, "", channel);
Crashes.setEnabled(true);
final RuntimeException exception = new RuntimeException();
final Thread thread = new Thread() {
@Override
public void run() {
throw exception;
}
};
thread.start();
thread.join();
verify(uncaughtExceptionHandler).uncaughtException(thread, exception);
/* Check there are only 2 files: the throwable and the json one. */
assertEquals(2, ErrorLogHelper.getErrorStorageDirectory().listFiles().length);
}
use of com.microsoft.azure.mobile.channel.Channel in project mobile-center-sdk-android by Microsoft.
the class CrashesAndroidTest method testNoDuplicateCallbacksOrSending.
@Test
public void testNoDuplicateCallbacksOrSending() throws InterruptedException {
/* Crash on 1st process. */
assertFalse(Crashes.hasCrashedInLastSession());
android.util.Log.i(TAG, "Process 1");
Thread.UncaughtExceptionHandler uncaughtExceptionHandler = mock(Thread.UncaughtExceptionHandler.class);
Thread.setDefaultUncaughtExceptionHandler(uncaughtExceptionHandler);
Channel channel = mock(Channel.class);
Crashes.getInstance().onStarted(sContext, "", channel);
CrashesListener crashesListener = mock(CrashesListener.class);
when(crashesListener.shouldProcess(any(ErrorReport.class))).thenReturn(true);
when(crashesListener.shouldAwaitUserConfirmation()).thenReturn(true);
Crashes.setListener(crashesListener);
final Error exception = generateStackOverflowError();
assertTrue(exception.getStackTrace().length > ErrorLogHelper.FRAME_LIMIT);
final Thread thread = new Thread() {
@Override
public void run() {
throw exception;
}
};
thread.start();
thread.join();
assertEquals(ErrorLogHelper.FRAME_LIMIT, exception.getStackTrace().length);
verify(uncaughtExceptionHandler).uncaughtException(thread, exception);
assertEquals(2, ErrorLogHelper.getErrorStorageDirectory().listFiles().length);
verifyZeroInteractions(crashesListener);
/* Second process: enqueue log but network is down... */
android.util.Log.i(TAG, "Process 2");
final AtomicReference<Log> log = new AtomicReference<>();
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
log.set((Log) invocationOnMock.getArguments()[0]);
return null;
}
}).when(channel).enqueue(any(Log.class), anyString());
Crashes.unsetInstance();
Crashes.setListener(crashesListener);
Crashes.getInstance().onStarted(sContext, "", channel);
waitForCrashesHandlerTasksToComplete();
/* Check last session error report. */
assertTrue(Crashes.hasCrashedInLastSession());
Crashes.getLastSessionCrashReport(new ResultCallback<ErrorReport>() {
@Override
public void onResult(ErrorReport errorReport) {
assertNotNull(errorReport);
Throwable lastThrowable = errorReport.getThrowable();
assertTrue(lastThrowable instanceof StackOverflowError);
assertEquals(ErrorLogHelper.FRAME_LIMIT, lastThrowable.getStackTrace().length);
}
});
/* Waiting user confirmation so no log sent yet. */
verify(channel, never()).enqueue(any(Log.class), anyString());
assertEquals(2, ErrorLogHelper.getErrorStorageDirectory().listFiles().length);
verify(crashesListener).shouldProcess(any(ErrorReport.class));
verify(crashesListener).shouldAwaitUserConfirmation();
verifyNoMoreInteractions(crashesListener);
/* Confirm to resume processing. */
Crashes.notifyUserConfirmation(Crashes.ALWAYS_SEND);
verify(channel).enqueue(any(Log.class), anyString());
assertNotNull(log.get());
assertEquals(1, ErrorLogHelper.getErrorStorageDirectory().listFiles().length);
verify(crashesListener).getErrorAttachments(any(ErrorReport.class));
verifyNoMoreInteractions(crashesListener);
/* Third process: sending succeeds. */
android.util.Log.i(TAG, "Process 3");
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.get());
return null;
}
}).when(channel).addGroup(anyString(), anyInt(), anyInt(), anyInt(), any(Channel.GroupListener.class));
Crashes.unsetInstance();
Crashes.setListener(crashesListener);
Crashes.getInstance().onStarted(sContext, "", channel);
waitForCrashesHandlerTasksToComplete();
assertFalse(Crashes.hasCrashedInLastSession());
Crashes.getLastSessionCrashReport(new ResultCallback<ErrorReport>() {
@Override
public void onResult(ErrorReport errorReport) {
assertNull(errorReport);
}
});
assertNotNull(groupListener.get());
groupListener.get().onSuccess(log.get());
waitForCrashesHandlerTasksToComplete();
assertEquals(0, ErrorLogHelper.getErrorStorageDirectory().listFiles().length);
verify(channel, never()).enqueue(any(Log.class), anyString());
verify(crashesListener).onBeforeSending(any(ErrorReport.class));
verify(crashesListener).onSendingSucceeded(any(ErrorReport.class));
verifyNoMoreInteractions(crashesListener);
/* Verify log was truncated to 256 frames. */
assertTrue(log.get() instanceof ManagedErrorLog);
ManagedErrorLog errorLog = (ManagedErrorLog) log.get();
assertNotNull(errorLog.getException());
assertNotNull(errorLog.getException().getFrames());
assertEquals(ErrorLogHelper.FRAME_LIMIT, errorLog.getException().getFrames().size());
}
use of com.microsoft.azure.mobile.channel.Channel in project mobile-center-sdk-android by Microsoft.
the class AnalyticsTest method testTrackEvent.
@Test
public void testTrackEvent() {
Analytics analytics = Analytics.getInstance();
Channel channel = mock(Channel.class);
analytics.onStarted(mock(Context.class), "", channel);
Analytics.trackEvent(null, null);
verify(channel, never()).enqueue(any(Log.class), anyString());
reset(channel);
Analytics.trackEvent("", null);
verify(channel, never()).enqueue(any(Log.class), anyString());
reset(channel);
Analytics.trackEvent(" ", null);
verify(channel, times(1)).enqueue(any(Log.class), anyString());
reset(channel);
Analytics.trackEvent(generateString(257, '*'), null);
verify(channel, never()).enqueue(any(Log.class), anyString());
reset(channel);
Analytics.trackEvent(generateString(256, '*'), null);
verify(channel, times(1)).enqueue(any(Log.class), anyString());
reset(channel);
Analytics.trackEvent("eventName", 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 EventLog) {
EventLog eventLog = (EventLog) item;
return eventLog.getProperties().size() == 0;
}
return false;
}
}), anyString());
reset(channel);
final String validMapItem = "valid";
Analytics.trackEvent("eventName", 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 EventLog) {
EventLog eventLog = (EventLog) item;
return eventLog.getProperties().size() == 5;
}
return false;
}
}), anyString());
}
Aggregations