use of com.microsoft.azure.mobile.channel.Channel in project mobile-center-sdk-android by Microsoft.
the class AnalyticsTest method startSessionAfterUserApprovalWeakReference.
@Test
public void startSessionAfterUserApprovalWeakReference() {
/*
* Disable analytics while in background to set up the initial condition
* simulating the optin use case.
*/
Analytics analytics = Analytics.getInstance();
Channel channel = mock(Channel.class);
analytics.onStarted(mock(Context.class), "", channel);
Analytics.setEnabled(false);
/* App in foreground: no log yet, we are disabled. */
analytics.onActivityResumed(new Activity());
System.gc();
verify(channel, never()).enqueue(any(Log.class), eq(analytics.getGroupName()));
/* Enable: start session not sent retroactively, weak reference lost. */
Analytics.setEnabled(true);
verify(channel, never()).enqueue(any(Log.class), eq(analytics.getGroupName()));
}
use of com.microsoft.azure.mobile.channel.Channel in project mobile-center-sdk-android by Microsoft.
the class CrashesAndroidTest method wrapperSdkOverrideLog.
@Test
public void wrapperSdkOverrideLog() throws InterruptedException {
Thread.UncaughtExceptionHandler uncaughtExceptionHandler = mock(Thread.UncaughtExceptionHandler.class);
Thread.setDefaultUncaughtExceptionHandler(uncaughtExceptionHandler);
Channel channel = mock(Channel.class);
Crashes.getInstance().onStarted(sContext, "", channel);
Crashes.WrapperSdkListener wrapperSdkListener = mock(Crashes.WrapperSdkListener.class);
Crashes.getInstance().setWrapperSdkListener(wrapperSdkListener);
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
ManagedErrorLog errorLog = (ManagedErrorLog) invocationOnMock.getArguments()[0];
errorLog.setErrorThreadName("ReplacedErrorThreadName");
WrapperSdkExceptionManager.saveWrapperSdkErrorLog(errorLog);
return null;
}
}).when(wrapperSdkListener).onCrashCaptured(notNull(ManagedErrorLog.class));
final RuntimeException exception = new RuntimeException("mock");
final Thread thread = new Thread() {
@Override
public void run() {
throw exception;
}
};
thread.start();
thread.join();
verify(wrapperSdkListener).onCrashCaptured(notNull(ManagedErrorLog.class));
Crashes.unsetInstance();
Crashes.getInstance().onStarted(sContext, "", channel);
waitForCrashesHandlerTasksToComplete();
Crashes.getLastSessionCrashReport(new ResultCallback<ErrorReport>() {
@Override
public void onResult(ErrorReport errorReport) {
assertNotNull(errorReport);
assertEquals("ReplacedErrorThreadName", errorReport.getThreadName());
}
});
}
use of com.microsoft.azure.mobile.channel.Channel in project mobile-center-sdk-android by Microsoft.
the class CrashesTest method queuePendingCrashesAlwaysSend.
@Test
public void queuePendingCrashesAlwaysSend() throws IOException, ClassNotFoundException, JSONException {
Context mockContext = mock(Context.class);
Channel mockChannel = mock(Channel.class);
ErrorAttachmentLog mockAttachment = mock(ErrorAttachmentLog.class);
when(mockAttachment.getId()).thenReturn(UUID.randomUUID());
when(mockAttachment.getErrorId()).thenReturn(UUID.randomUUID());
when(mockAttachment.getContentType()).thenReturn("");
when(mockAttachment.getFileName()).thenReturn("");
when(mockAttachment.getData()).thenReturn(new byte[0]);
when(mockAttachment.isValid()).thenReturn(true);
List<ErrorAttachmentLog> errorAttachmentLogList = Arrays.asList(mockAttachment, mockAttachment);
ErrorReport report = new ErrorReport();
mockStatic(ErrorLogHelper.class);
when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[] { mock(File.class) });
when(ErrorLogHelper.getStoredThrowableFile(any(UUID.class))).thenReturn(mock(File.class));
when(ErrorLogHelper.getErrorReportFromErrorLog(any(ManagedErrorLog.class), any(Throwable.class))).thenReturn(report);
when(StorageHelper.InternalStorage.read(any(File.class))).thenReturn("");
when(StorageHelper.InternalStorage.readObject(any(File.class))).thenReturn(new RuntimeException());
when(StorageHelper.PreferencesStorage.getBoolean(eq(Crashes.PREF_KEY_ALWAYS_SEND), anyBoolean())).thenReturn(true);
CrashesListener mockListener = mock(CrashesListener.class);
when(mockListener.shouldProcess(report)).thenReturn(true);
when(mockListener.shouldProcess(report)).thenReturn(true);
when(mockListener.shouldAwaitUserConfirmation()).thenReturn(false);
when(mockListener.getErrorAttachments(report)).thenReturn(errorAttachmentLogList);
Crashes crashes = Crashes.getInstance();
LogSerializer logSerializer = mock(LogSerializer.class);
when(logSerializer.deserializeLog(anyString())).thenReturn(mErrorLog);
crashes.setLogSerializer(logSerializer);
crashes.setInstanceListener(mockListener);
crashes.onStarted(mockContext, "", mockChannel);
verify(mockListener).shouldProcess(report);
verify(mockListener, never()).shouldAwaitUserConfirmation();
verify(mockListener).getErrorAttachments(report);
verify(mockChannel).enqueue(argThat(new ArgumentMatcher<Log>() {
@Override
public boolean matches(Object log) {
return log.equals(mErrorLog);
}
}), eq(crashes.getGroupName()));
verify(mockChannel, times(errorAttachmentLogList.size())).enqueue(mockAttachment, crashes.getGroupName());
}
use of com.microsoft.azure.mobile.channel.Channel in project mobile-center-sdk-android by Microsoft.
the class CrashesTest method setEnabled.
@Test
public void setEnabled() {
/* Setup mock. */
Crashes crashes = Crashes.getInstance();
mockStatic(ErrorLogHelper.class);
Channel mockChannel = mock(Channel.class);
File dir = mock(File.class);
File file1 = mock(File.class);
File file2 = mock(File.class);
when(ErrorLogHelper.getErrorStorageDirectory()).thenReturn(dir);
when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[0]);
when(dir.listFiles()).thenReturn(new File[] { file1, file2 });
/* Test. */
assertTrue(Crashes.isEnabled());
Crashes.setEnabled(true);
assertTrue(Crashes.isEnabled());
verify(mMockLooper, never()).quit();
assertTrue(crashes.getInitializeTimestamp() > 0);
Crashes.setEnabled(false);
assertFalse(Crashes.isEnabled());
verify(mMockLooper).quit();
crashes.onStarted(mock(Context.class), "", mockChannel);
verify(mockChannel).clear(crashes.getGroupName());
verify(mockChannel).removeGroup(eq(crashes.getGroupName()));
assertEquals(crashes.getInitializeTimestamp(), -1);
assertFalse(Thread.getDefaultUncaughtExceptionHandler() instanceof UncaughtExceptionHandler);
assertFalse(verify(file1).delete());
assertFalse(verify(file2).delete());
Crashes.trackException(EXCEPTION);
verifyNoMoreInteractions(mockChannel);
/* Enable back, testing double calls. */
Crashes.setEnabled(true);
assertTrue(Crashes.isEnabled());
assertTrue(crashes.getInitializeTimestamp() > 0);
assertTrue(Thread.getDefaultUncaughtExceptionHandler() instanceof UncaughtExceptionHandler);
Crashes.setEnabled(true);
assertTrue(Crashes.isEnabled());
verify(mockChannel).addGroup(eq(crashes.getGroupName()), anyInt(), anyInt(), anyInt(), any(Channel.GroupListener.class));
Crashes.trackException(EXCEPTION);
verify(mockChannel, times(1)).enqueue(any(ManagedErrorLog.class), eq(crashes.getGroupName()));
}
use of com.microsoft.azure.mobile.channel.Channel in project mobile-center-sdk-android by Microsoft.
the class CrashesTest method trackException.
@Test
public void trackException() {
/* Track exception test. */
Crashes crashes = Crashes.getInstance();
Channel mockChannel = mock(Channel.class);
crashes.onStarted(mock(Context.class), "", mockChannel);
Crashes.trackException(EXCEPTION);
verify(mockChannel).enqueue(argThat(new ArgumentMatcher<Log>() {
@Override
public boolean matches(Object item) {
return item instanceof ManagedErrorLog && EXCEPTION.getMessage().equals(((ManagedErrorLog) item).getException().getMessage());
}
}), eq(crashes.getGroupName()));
ManagedErrorLog mockLog = mock(ManagedErrorLog.class);
when(mockLog.getFatal()).thenReturn(false);
CrashesListener mockListener = mock(CrashesListener.class);
crashes.setInstanceListener(mockListener);
/* Crashes callback test for trackException. */
crashes.getChannelListener().onBeforeSending(mockLog);
verify(mockListener, never()).onBeforeSending(any(ErrorReport.class));
crashes.getChannelListener().onSuccess(mockLog);
verify(mockListener, never()).onSendingSucceeded(any(ErrorReport.class));
crashes.getChannelListener().onFailure(mockLog, EXCEPTION);
verify(mockListener, never()).onSendingFailed(any(ErrorReport.class), eq(EXCEPTION));
ErrorAttachmentLog attachmentLog = mock(ErrorAttachmentLog.class);
crashes.getChannelListener().onBeforeSending(attachmentLog);
verify(mockListener, never()).onBeforeSending(any(ErrorReport.class));
crashes.getChannelListener().onSuccess(attachmentLog);
verify(mockListener, never()).onSendingSucceeded(any(ErrorReport.class));
crashes.getChannelListener().onFailure(attachmentLog, EXCEPTION);
verify(mockListener, never()).onSendingFailed(any(ErrorReport.class), eq(EXCEPTION));
}
Aggregations