use of com.microsoft.appcenter.crashes.model.ErrorReport in project mobile-center-sdk-android by Microsoft.
the class CrashesTest method minidumpFilePathNull.
@Test
public void minidumpFilePathNull() throws Exception {
/* Set up mock for the crash. */
final com.microsoft.appcenter.crashes.ingestion.models.Exception exception = mock(com.microsoft.appcenter.crashes.ingestion.models.Exception.class);
DefaultLogSerializer defaultLogSerializer = mock(DefaultLogSerializer.class);
mock(ErrorAttachmentLog.class);
mockStatic(ErrorLogHelper.class);
mockStatic(ErrorAttachmentLog.class);
ErrorReport errorReport = new ErrorReport();
Device device = new Device();
device.setWrapperSdkName(WRAPPER_SDK_NAME_NDK);
errorReport.setDevice(device);
when(ErrorLogHelper.getErrorReportFromErrorLog(any(ManagedErrorLog.class), anyString())).thenReturn(errorReport);
whenNew(DefaultLogSerializer.class).withAnyArguments().thenReturn(defaultLogSerializer);
whenNew(com.microsoft.appcenter.crashes.ingestion.models.Exception.class).withAnyArguments().thenReturn(exception);
when(exception.getMinidumpFilePath()).thenReturn(null);
when(exception.getType()).thenReturn(MINIDUMP_FILE);
when(exception.getMessage()).thenReturn("Error message");
when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[] { mock(File.class), mock(File.class) });
when(ErrorLogHelper.getNewMinidumpFiles()).thenReturn(new File[0]);
when(FileManager.read(any(File.class))).thenReturn("");
String jsonCrash = "{}";
LogSerializer logSerializer = mock(LogSerializer.class);
when(logSerializer.deserializeLog(anyString(), anyString())).thenAnswer(new Answer<ManagedErrorLog>() {
@Override
public ManagedErrorLog answer(InvocationOnMock invocation) {
ManagedErrorLog log = mock(ManagedErrorLog.class);
when(log.getId()).thenReturn(UUID.randomUUID());
when(log.getException()).thenReturn(exception);
return log;
}
});
when(logSerializer.serializeLog(any(Log.class))).thenReturn(jsonCrash);
when(SharedPreferencesManager.getBoolean(CRASHES_ENABLED_KEY, true)).thenReturn(true);
ErrorAttachmentLog errorAttachmentLog = mock(ErrorAttachmentLog.class);
whenNew(ErrorAttachmentLog.class).withAnyArguments().thenReturn(errorAttachmentLog);
/* Start crashes. */
Crashes crashes = Crashes.getInstance();
crashes.setLogSerializer(logSerializer);
crashes.onStarting(mAppCenterHandler);
crashes.onStarted(mock(Context.class), mock(Channel.class), "secret-app-mock", null, true);
/*
* Verify that attachmentWithBinary doesn't get called if minidump is missing.
* This scenario used to crash before, so if the test succeeds that also tests the crash is fixed.
*/
verifyStatic(never());
attachmentWithBinary(new byte[] { anyByte() }, anyString(), anyString());
}
use of com.microsoft.appcenter.crashes.model.ErrorReport in project mobile-center-sdk-android by Microsoft.
the class CrashesTest method queuePendingCrashesShouldProcess.
@Test
public void queuePendingCrashesShouldProcess() throws JSONException {
/* Setup mock. */
Context mockContext = mock(Context.class);
Channel mockChannel = mock(Channel.class);
ErrorReport report = new ErrorReport();
mockStatic(ErrorLogHelper.class);
when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[] { mock(File.class) });
when(ErrorLogHelper.getNewMinidumpFiles()).thenReturn(new File[0]);
when(ErrorLogHelper.getErrorReportFromErrorLog(any(ManagedErrorLog.class), anyString())).thenReturn(report);
when(FileManager.read(any(File.class))).thenReturn("");
CrashesListener mockListener = mock(CrashesListener.class);
when(mockListener.shouldProcess(report)).thenReturn(true);
when(mockListener.shouldAwaitUserConfirmation()).thenReturn(false);
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);
ErrorAttachmentLog mockEmptyAttachment = mock(ErrorAttachmentLog.class);
final int skipAttachmentLogsCount = 2;
List<ErrorAttachmentLog> errorAttachmentLogList = Arrays.asList(mockAttachment, mockAttachment, mockEmptyAttachment, null);
when(mockListener.getErrorAttachments(report)).thenReturn(errorAttachmentLogList);
LogSerializer logSerializer = mock(LogSerializer.class);
when(logSerializer.deserializeLog(anyString(), anyString())).thenReturn(mErrorLog);
Crashes crashes = Crashes.getInstance();
crashes.setLogSerializer(logSerializer);
crashes.setInstanceListener(mockListener);
crashes.onStarting(mAppCenterHandler);
crashes.onStarted(mockContext, mockChannel, "", null, true);
/* Test. */
verify(mockListener).shouldProcess(report);
verify(mockListener).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()), eq(CRITICAL));
verify(mockChannel, times(errorAttachmentLogList.size() - skipAttachmentLogsCount)).enqueue(mockAttachment, crashes.getGroupName(), DEFAULTS);
}
use of com.microsoft.appcenter.crashes.model.ErrorReport in project mobile-center-sdk-android by Microsoft.
the class CrashesTest method discardHugeErrorAttachments.
@Test
public void discardHugeErrorAttachments() throws JSONException {
/* Prepare a big (too big) attachment and a small one. */
ArrayList<ErrorAttachmentLog> errorAttachmentLogs = new ArrayList<>(2);
ErrorAttachmentLog binaryAttachment = attachmentWithBinary(new byte[7 * 1024 * 1024 + 1], "earth.png", "image/png");
errorAttachmentLogs.add(binaryAttachment);
ErrorAttachmentLog textAttachment = ErrorAttachmentLog.attachmentWithText("hello", "log.txt");
errorAttachmentLogs.add(textAttachment);
/* Set up callbacks. */
CrashesListener listener = mock(CrashesListener.class);
when(listener.shouldProcess(any(ErrorReport.class))).thenReturn(true);
when(listener.getErrorAttachments(any(ErrorReport.class))).thenReturn(errorAttachmentLogs);
/* Mock a crash log to process. */
com.microsoft.appcenter.crashes.ingestion.models.Exception mockException = new com.microsoft.appcenter.crashes.ingestion.models.Exception();
mockException.setType("type");
mockException.setMessage("message");
ManagedErrorLog log = mock(ManagedErrorLog.class);
when(log.getId()).thenReturn(UUID.randomUUID());
when(log.getException()).thenReturn(mockException);
LogSerializer logSerializer = mock(LogSerializer.class);
when(logSerializer.deserializeLog(anyString(), anyString())).thenReturn(log);
mockStatic(ErrorLogHelper.class);
when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[] { mock(File.class) });
when(ErrorLogHelper.getNewMinidumpFiles()).thenReturn(new File[0]);
when(ErrorLogHelper.getErrorReportFromErrorLog(any(ManagedErrorLog.class), anyString())).thenReturn(new ErrorReport());
when(FileManager.read(any(File.class))).thenReturn("");
/* Mock starting crashes so that attachments are processed. */
Crashes crashes = Crashes.getInstance();
crashes.setInstanceListener(listener);
crashes.setLogSerializer(logSerializer);
crashes.onStarting(mAppCenterHandler);
Channel channel = mock(Channel.class);
crashes.onStarted(mock(Context.class), channel, "", null, true);
/* Check we send only the text attachment as the binary is too big. */
verify(channel).enqueue(textAttachment, crashes.getGroupName(), NORMAL);
verify(channel, never()).enqueue(eq(binaryAttachment), anyString(), anyInt());
}
use of com.microsoft.appcenter.crashes.model.ErrorReport in project mobile-center-sdk-android by Microsoft.
the class CrashesTest method queuePendingCrashesShouldNotProcess.
@Test
public void queuePendingCrashesShouldNotProcess() throws JSONException {
Context mockContext = mock(Context.class);
Channel mockChannel = mock(Channel.class);
ErrorReport report = new ErrorReport();
mockStatic(ErrorLogHelper.class);
when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[] { mock(File.class) });
when(ErrorLogHelper.getNewMinidumpFiles()).thenReturn(new File[0]);
when(ErrorLogHelper.getErrorReportFromErrorLog(any(ManagedErrorLog.class), anyString())).thenReturn(report);
when(FileManager.read(any(File.class))).thenReturn("");
CrashesListener mockListener = mock(CrashesListener.class);
when(mockListener.shouldProcess(report)).thenReturn(false);
Crashes crashes = Crashes.getInstance();
LogSerializer logSerializer = mock(LogSerializer.class);
when(logSerializer.deserializeLog(anyString(), anyString())).thenReturn(mErrorLog);
crashes.setLogSerializer(logSerializer);
crashes.setInstanceListener(mockListener);
crashes.onStarting(mAppCenterHandler);
crashes.onStarted(mockContext, mockChannel, "", null, true);
verify(mockListener).shouldProcess(report);
verify(mockListener, never()).shouldAwaitUserConfirmation();
verify(mockListener, never()).getErrorAttachments(report);
verify(mockChannel, never()).enqueue(any(Log.class), eq(crashes.getGroupName()), anyInt());
}
use of com.microsoft.appcenter.crashes.model.ErrorReport in project mobile-center-sdk-android by Microsoft.
the class CrashesTest method minidumpStoredWithOldSDK.
@Test
public void minidumpStoredWithOldSDK() throws Exception {
/* Set up mock for the crash. */
final com.microsoft.appcenter.crashes.ingestion.models.Exception exception = mock(com.microsoft.appcenter.crashes.ingestion.models.Exception.class);
DefaultLogSerializer defaultLogSerializer = mock(DefaultLogSerializer.class);
mock(ErrorAttachmentLog.class);
mockStatic(ErrorLogHelper.class);
mockStatic(ErrorAttachmentLog.class);
ErrorReport errorReport = new ErrorReport();
Device device = new Device();
device.setWrapperSdkName(WRAPPER_SDK_NAME_NDK);
errorReport.setDevice(device);
when(ErrorLogHelper.getErrorReportFromErrorLog(any(ManagedErrorLog.class), anyString())).thenReturn(errorReport);
whenNew(DefaultLogSerializer.class).withAnyArguments().thenReturn(defaultLogSerializer);
whenNew(com.microsoft.appcenter.crashes.ingestion.models.Exception.class).withAnyArguments().thenReturn(exception);
when(exception.getStackTrace()).thenReturn("some minidump");
when(exception.getType()).thenReturn(MINIDUMP_FILE);
when(exception.getMessage()).thenReturn("message");
/* This mocks we already processed minidump to convert to pending regular crash report as that would be the case if migrating data from older SDK. */
when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[] { mock(File.class) });
when(ErrorLogHelper.getNewMinidumpFiles()).thenReturn(new File[0]);
when(FileManager.read(any(File.class))).thenReturn("");
String jsonCrash = "{}";
LogSerializer logSerializer = mock(LogSerializer.class);
when(logSerializer.deserializeLog(anyString(), anyString())).thenAnswer(new Answer<ManagedErrorLog>() {
@Override
public ManagedErrorLog answer(InvocationOnMock invocation) {
ManagedErrorLog log = mock(ManagedErrorLog.class);
when(log.getId()).thenReturn(UUID.randomUUID());
when(log.getException()).thenReturn(exception);
return log;
}
});
when(logSerializer.serializeLog(any(Log.class))).thenReturn(jsonCrash);
when(SharedPreferencesManager.getBoolean(CRASHES_ENABLED_KEY, true)).thenReturn(true);
ErrorAttachmentLog errorAttachmentLog = mock(ErrorAttachmentLog.class);
whenNew(ErrorAttachmentLog.class).withAnyArguments().thenReturn(errorAttachmentLog);
/* Start crashes. */
Crashes crashes = Crashes.getInstance();
crashes.setLogSerializer(logSerializer);
crashes.onStarting(mAppCenterHandler);
crashes.onStarted(mock(Context.class), mock(Channel.class), "secret-app-mock", null, true);
/* Verify that attachmentWithBinary does get sent. */
verifyStatic();
attachmentWithBinary(new byte[] { anyByte() }, anyString(), anyString());
/* Verify temporary field erased. */
verify(exception, times(1)).setStackTrace(null);
}
Aggregations