ErrorLogHelper { @NonNull public static ManagedErrorLog createErrorLog(@NonNull Context context, @NonNull final java.lang.Thread thread, @NonNull final Throwable throwable, @NonNull final Map<java.lang.Thread, StackTraceElement[]> allStackTraces, final long initializeTimestamp) { return createErrorLog(context, thread, getModelExceptionFromThrowable(throwable), allStackTraces, initializeTimestamp, true); }  @NonNull static ManagedErrorLog createErrorLog(@NonNull Context context, @NonNull final java.lang.Thread thread, @NonNull final Throwable throwable, @NonNull final Map<java.lang.Thread, StackTraceElement[]> allStackTraces, final long initializeTimestamp); @NonNull static ManagedErrorLog createErrorLog(@NonNull Context context, @NonNull final java.lang.Thread thread, @NonNull final Exception exception, @NonNull final Map<java.lang.Thread, StackTraceElement[]> allStackTraces, final long initializeTimestamp, boolean fatal); @NonNull static synchronized File getErrorStorageDirectory(); @NonNull static synchronized File getNewMinidumpDirectory(); @NonNull static synchronized File getNewMinidumpSubfolder(); @NonNull static synchronized File getNewMinidumpSubfolderWithContextData(Context context); @NonNull static synchronized File getPendingMinidumpDirectory(); @NonNull static File[] getStoredErrorLogFiles(); @NonNull static File[] getNewMinidumpFiles(); @Nullable static Device getStoredDeviceInfo(File logFolder); static void removeStaleMinidumpSubfolders(); static void removeMinidumpFolder(); @Nullable static File getLastErrorLogFile(); @Nullable static File getStoredThrowableFile(@NonNull UUID id); static void removeStoredThrowableFile(@NonNull UUID id); static void removeStoredErrorLogFile(@NonNull UUID id); @NonNull static ErrorReport getErrorReportFromErrorLog(@NonNull ManagedErrorLog log, String stackTrace); @NonNull static Exception getModelExceptionFromThrowable(@NonNull Throwable t); static Map<String, String> validateProperties(Map<String, String> properties, String logType); static void cleanPendingMinidumps(); @NonNull static UUID parseLogFolderUuid(File logFolder); @VisibleForTesting static void clearStaticState(); static final String DEVICE_INFO_FILE; static final String ERROR_LOG_FILE_EXTENSION; static final String THROWABLE_FILE_EXTENSION; @VisibleForTesting
    static final int FRAME_LIMIT; static final int MAX_PROPERTY_ITEM_LENGTH; static final String MINIDUMP_FILE_EXTENSION; }
@Test public void createErrorLog() throws java.lang.Exception { new ErrorLogHelper(); Context mockContext = mock(Context.class); when(Process.myPid()).thenReturn(123); Date logTimestamp = new Date(1000L); whenNew(Date.class).withNoArguments().thenReturn(logTimestamp); whenNew(Date.class).withArguments(anyLong()).thenAnswer(new Answer<Date>() { @Override public Date answer(InvocationOnMock invocation) { return new Date((Long) invocation.getArguments()[0]); } }); Device mockDevice = mock(Device.class); when(DeviceInfoHelper.getDeviceInfo(any(Context.class))).thenReturn(mockDevice); ActivityManager activityManager = mock(ActivityManager.class); RunningAppProcessInfo runningAppProcessInfo = new RunningAppProcessInfo(null, 0, null); runningAppProcessInfo.pid = 123; runningAppProcessInfo.processName = "right.process"; when(mockContext.getSystemService(Context.ACTIVITY_SERVICE)).thenReturn(activityManager); when(activityManager.getRunningAppProcesses()).thenReturn(Arrays.asList(mock(RunningAppProcessInfo.class), runningAppProcessInfo)); TestUtils.setInternalState(Build.VERSION.class, "SDK_INT", 23); TestUtils.setInternalState(Build.class, "SUPPORTED_ABIS", new String[]{"armeabi-v7a", "arm"}); long launchTimeStamp = 2000; ManagedErrorLog errorLog = ErrorLogHelper.createErrorLog(mockContext, java.lang.Thread.currentThread(), new RuntimeException(new IOException(new TestCrashException())), java.lang.Thread.getAllStackTraces(), launchTimeStamp); assertNotNull(errorLog); assertNotNull(errorLog.getId()); assertEquals(logTimestamp, errorLog.getTimestamp()); assertEquals(mockDevice, errorLog.getDevice()); assertEquals(Integer.valueOf(123), errorLog.getProcessId()); assertEquals("right.process", errorLog.getProcessName()); assertNull(errorLog.getParentProcessId()); assertNull(errorLog.getParentProcessName()); assertEquals("armeabi-v7a", errorLog.getArchitecture()); assertEquals((Long) java.lang.Thread.currentThread().getId(), errorLog.getErrorThreadId()); assertEquals(java.lang.Thread.currentThread().getName(), errorLog.getErrorThreadName()); assertEquals(Boolean.TRUE, errorLog.getFatal()); assertEquals(launchTimeStamp, errorLog.getAppLaunchTimestamp().getTime()); Exception topException = errorLog.getException(); sanityCheck(topException); assertEquals(RuntimeException.class.getName(), topException.getType()); assertNotNull(topException.getMessage()); assertNotNull(topException.getInnerExceptions()); assertEquals(1, topException.getInnerExceptions().size()); Exception middleException = topException.getInnerExceptions().get(0); sanityCheck(middleException); assertEquals(IOException.class.getName(), middleException.getType()); assertNotNull(middleException.getInnerExceptions()); assertEquals(1, middleException.getInnerExceptions().size()); Exception rootCauseException = middleException.getInnerExceptions().get(0); sanityCheck(rootCauseException); assertEquals(TestCrashException.class.getName(), rootCauseException.getType()); assertNotNull(rootCauseException.getMessage()); assertNull(rootCauseException.getInnerExceptions()); assertNotNull(errorLog.getThreads()); assertEquals(java.lang.Thread.getAllStackTraces().size(), errorLog.getThreads().size()); for (Thread thread : errorLog.getThreads()) { assertNotNull(thread); assertTrue(thread.getId() > 0); assertNotNull(thread.getName()); assertNotNull(thread.getFrames()); for (StackFrame frame : thread.getFrames()) { assertNotNull(frame); assertNotNull(frame.getClassName()); assertNotNull(frame.getMethodName()); } } }