@Test public void syncWithServer_whenTaskFinishes_setsResultToTrue() { FakeScheduler fakeScheduler = new FakeScheduler(); BlankFormsListViewModel viewModel = new BlankFormsListViewModel(mock(Application.class), fakeScheduler, syncRepository, mock(ServerFormsSynchronizer.class), preferencesProvider, mock(Notifier.class), changeLock, analytics); LiveData<Boolean> result = viewModel.syncWithServer(); fakeScheduler.runBackground(); assertThat(result.getValue(), is(true)); }
public LiveData<Boolean> syncWithServer() { logManualSync(); MutableLiveData<Boolean> result = new MutableLiveData<>(); changeLock.withLock(acquiredLock -> { if (acquiredLock) { syncRepository.startSync(); scheduler.immediate(() -> { FormApiException exception = null; try { serverFormsSynchronizer.synchronize(); } catch (FormApiException e) { exception = e; } return exception; }, exception -> { if (exception == null) { syncRepository.finishSync(null); notifier.onSync(null); analytics.logEvent(AnalyticsEvents.MATCH_EXACTLY_SYNC_COMPLETED, "Success"); result.setValue(true); } else { syncRepository.finishSync(exception); notifier.onSync(exception); analytics.logEvent(AnalyticsEvents.MATCH_EXACTLY_SYNC_COMPLETED, exception.getType().toString()); result.setValue(false); } }); } return null; }); return result; }
BlankFormsListViewModel extends ViewModel { public LiveData<Boolean> syncWithServer() { logManualSync(); MutableLiveData<Boolean> result = new MutableLiveData<>(); changeLock.withLock(acquiredLock -> { if (acquiredLock) { syncRepository.startSync(); scheduler.immediate(() -> { FormApiException exception = null; try { serverFormsSynchronizer.synchronize(); } catch (FormApiException e) { exception = e; } return exception; }, exception -> { if (exception == null) { syncRepository.finishSync(null); notifier.onSync(null); analytics.logEvent(AnalyticsEvents.MATCH_EXACTLY_SYNC_COMPLETED, "Success"); result.setValue(true); } else { syncRepository.finishSync(exception); notifier.onSync(exception); analytics.logEvent(AnalyticsEvents.MATCH_EXACTLY_SYNC_COMPLETED, exception.getType().toString()); result.setValue(false); } }); } return null; }); return result; } }
BlankFormsListViewModel extends ViewModel { public LiveData<Boolean> syncWithServer() { logManualSync(); MutableLiveData<Boolean> result = new MutableLiveData<>(); changeLock.withLock(acquiredLock -> { if (acquiredLock) { syncRepository.startSync(); scheduler.immediate(() -> { FormApiException exception = null; try { serverFormsSynchronizer.synchronize(); } catch (FormApiException e) { exception = e; } return exception; }, exception -> { if (exception == null) { syncRepository.finishSync(null); notifier.onSync(null); analytics.logEvent(AnalyticsEvents.MATCH_EXACTLY_SYNC_COMPLETED, "Success"); result.setValue(true); } else { syncRepository.finishSync(exception); notifier.onSync(exception); analytics.logEvent(AnalyticsEvents.MATCH_EXACTLY_SYNC_COMPLETED, exception.getType().toString()); result.setValue(false); } }); } return null; }); return result; } BlankFormsListViewModel(Application application, Scheduler scheduler, SyncStatusRepository syncRepository, ServerFormsSynchronizer serverFormsSynchronizer, PreferencesProvider preferencesProvider, Notifier notifier, ChangeLock changeLock, Analytics analytics); }
BlankFormsListViewModel extends ViewModel { public LiveData<Boolean> syncWithServer() { logManualSync(); MutableLiveData<Boolean> result = new MutableLiveData<>(); changeLock.withLock(acquiredLock -> { if (acquiredLock) { syncRepository.startSync(); scheduler.immediate(() -> { FormApiException exception = null; try { serverFormsSynchronizer.synchronize(); } catch (FormApiException e) { exception = e; } return exception; }, exception -> { if (exception == null) { syncRepository.finishSync(null); notifier.onSync(null); analytics.logEvent(AnalyticsEvents.MATCH_EXACTLY_SYNC_COMPLETED, "Success"); result.setValue(true); } else { syncRepository.finishSync(exception); notifier.onSync(exception); analytics.logEvent(AnalyticsEvents.MATCH_EXACTLY_SYNC_COMPLETED, exception.getType().toString()); result.setValue(false); } }); } return null; }); return result; } BlankFormsListViewModel(Application application, Scheduler scheduler, SyncStatusRepository syncRepository, ServerFormsSynchronizer serverFormsSynchronizer, PreferencesProvider preferencesProvider, Notifier notifier, ChangeLock changeLock, Analytics analytics); boolean isMatchExactlyEnabled(); LiveData<Boolean> isSyncing(); LiveData<Boolean> isOutOfSync(); LiveData<Boolean> isAuthenticationRequired(); LiveData<Boolean> syncWithServer(); }
BlankFormsListViewModel extends ViewModel { public LiveData<Boolean> syncWithServer() { logManualSync(); MutableLiveData<Boolean> result = new MutableLiveData<>(); changeLock.withLock(acquiredLock -> { if (acquiredLock) { syncRepository.startSync(); scheduler.immediate(() -> { FormApiException exception = null; try { serverFormsSynchronizer.synchronize(); } catch (FormApiException e) { exception = e; } return exception; }, exception -> { if (exception == null) { syncRepository.finishSync(null); notifier.onSync(null); analytics.logEvent(AnalyticsEvents.MATCH_EXACTLY_SYNC_COMPLETED, "Success"); result.setValue(true); } else { syncRepository.finishSync(exception); notifier.onSync(exception); analytics.logEvent(AnalyticsEvents.MATCH_EXACTLY_SYNC_COMPLETED, exception.getType().toString()); result.setValue(false); } }); } return null; }); return result; } BlankFormsListViewModel(Application application, Scheduler scheduler, SyncStatusRepository syncRepository, ServerFormsSynchronizer serverFormsSynchronizer, PreferencesProvider preferencesProvider, Notifier notifier, ChangeLock changeLock, Analytics analytics); boolean isMatchExactlyEnabled(); LiveData<Boolean> isSyncing(); LiveData<Boolean> isOutOfSync(); LiveData<Boolean> isAuthenticationRequired(); LiveData<Boolean> syncWithServer();  }