@Override public void run() { function.accept(mResult); } });
@Override public void run() { /* No need to synchronize anymore as consumers cannot be modified anymore. */ for (AppCenterConsumer<T> function : mConsumers) { function.accept(value); } mConsumers = null; } });
@Test public void getLastSessionCrashReportWithMultipleListenersAndResultIsNullBeforeInit() { mockStatic(ErrorLogHelper.class); when(ErrorLogHelper.getLastErrorLogFile()).thenReturn(null); when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[0]); when(ErrorLogHelper.getNewMinidumpFiles()).thenReturn(new File[0]); @SuppressWarnings("unchecked") AppCenterConsumer<ErrorReport> callback = (AppCenterConsumer<ErrorReport>) mock(AppCenterConsumer.class); /* Call twice for multiple callbacks before initialize. */ Crashes.getLastSessionCrashReport().thenAccept(callback); Crashes.getLastSessionCrashReport().thenAccept(callback); Crashes.getInstance().onStarted(mock(Context.class), mock(Channel.class), "", null, true); assertFalse(Crashes.hasCrashedInLastSession().get()); verify(callback, times(2)).accept(null); }
@Test @PrepareForTest(HandlerUtils.class) public void multipleCallbacks() { mockStatic(HandlerUtils.class); doAnswer(new Answer<Void>() { @Override public Void answer(InvocationOnMock invocation) { ((Runnable) invocation.getArguments()[0]).run(); return null; } }).when(HandlerUtils.class); HandlerUtils.runOnUiThread(any(Runnable.class)); DefaultAppCenterFuture<Integer> future = new DefaultAppCenterFuture<>(); @SuppressWarnings("unchecked") AppCenterConsumer<Integer> function = mock(AppCenterConsumer.class); future.thenAccept(function); future.thenAccept(function); future.complete(1); verify(function, times(2)).accept(1); /* Works also after completion. */ future.thenAccept(function); future.thenAccept(function); verify(function, times(4)).accept(1); } }
AppCenterConsumer<ErrorReport> beforeCallback = (AppCenterConsumer<ErrorReport>) mock(AppCenterConsumer.class); Crashes.getLastSessionCrashReport().thenAccept(beforeCallback); verify(beforeCallback).accept(null); future.thenAccept(afterCallback); ArgumentCaptor<ErrorReport> errorReportCaptor = ArgumentCaptor.forClass(ErrorReport.class); verify(afterCallback, times(2)).accept(errorReportCaptor.capture()); assertEquals(errorReportCaptor.getAllValues().get(0), errorReportCaptor.getAllValues().get(1)); ErrorReport result = errorReportCaptor.getValue();