diff --git a/collect_app/src/main/java/org/odk/collect/android/backgroundwork/SyncFormsTaskSpec.java b/collect_app/src/main/java/org/odk/collect/android/backgroundwork/SyncFormsTaskSpec.java index 9a363ae8631..a711471555e 100644 --- a/collect_app/src/main/java/org/odk/collect/android/backgroundwork/SyncFormsTaskSpec.java +++ b/collect_app/src/main/java/org/odk/collect/android/backgroundwork/SyncFormsTaskSpec.java @@ -47,9 +47,10 @@ public Supplier getTask(@NotNull Context context) { try { serverFormsSynchronizer.synchronize(); syncStatusRepository.finishSync(null); + notifier.onSync(null); } catch (FormApiException e) { syncStatusRepository.finishSync(e); - notifier.onSyncFailure(e); + notifier.onSync(e); } } diff --git a/collect_app/src/main/java/org/odk/collect/android/formmanagement/BlankFormsListViewModel.java b/collect_app/src/main/java/org/odk/collect/android/formmanagement/BlankFormsListViewModel.java index c7910917764..4c5b7d40e77 100644 --- a/collect_app/src/main/java/org/odk/collect/android/formmanagement/BlankFormsListViewModel.java +++ b/collect_app/src/main/java/org/odk/collect/android/formmanagement/BlankFormsListViewModel.java @@ -73,9 +73,10 @@ public void syncWithServer() { try { serverFormsSynchronizer.synchronize(); syncRepository.finishSync(null); + notifier.onSync(null); } catch (FormApiException e) { syncRepository.finishSync(e); - notifier.onSyncFailure(e); + notifier.onSync(e); } return null; diff --git a/collect_app/src/main/java/org/odk/collect/android/notifications/NotificationManagerNotifier.java b/collect_app/src/main/java/org/odk/collect/android/notifications/NotificationManagerNotifier.java index 75e3b98fb2d..31071af4ddd 100644 --- a/collect_app/src/main/java/org/odk/collect/android/notifications/NotificationManagerNotifier.java +++ b/collect_app/src/main/java/org/odk/collect/android/notifications/NotificationManagerNotifier.java @@ -22,6 +22,8 @@ import java.util.Locale; import java.util.Map; +import javax.annotation.Nullable; + import static android.content.Context.NOTIFICATION_SERVICE; import static org.odk.collect.android.activities.FormDownloadListActivity.DISPLAY_ONLY_UPDATED_FORMS; import static org.odk.collect.android.utilities.ApplicationConstants.RequestCodes.FORMS_DOWNLOADED_NOTIFICATION; @@ -82,19 +84,23 @@ public void onUpdatesDownloaded(HashMap result) { } @Override - public void onSyncFailure(FormApiException exception) { - Intent intent = new Intent(application, FillBlankFormActivity.class); - PendingIntent contentIntent = PendingIntent.getActivity(application, FORM_SYNC_NOTIFICATION_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT); - - Resources localizedResources = getLocalizedResources(application); - showNotification( - application, - notificationManager, - localizedResources.getString(R.string.form_update_error), - new FormApiExceptionMapper(localizedResources).getMessage(exception), - contentIntent, - FORM_SYNC_NOTIFICATION_ID - ); + public void onSync(@Nullable FormApiException exception) { + if (exception != null) { + Intent intent = new Intent(application, FillBlankFormActivity.class); + PendingIntent contentIntent = PendingIntent.getActivity(application, FORM_SYNC_NOTIFICATION_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT); + + Resources localizedResources = getLocalizedResources(application); + showNotification( + application, + notificationManager, + localizedResources.getString(R.string.form_update_error), + new FormApiExceptionMapper(localizedResources).getMessage(exception), + contentIntent, + FORM_SYNC_NOTIFICATION_ID + ); + } else { + notificationManager.cancel(FORM_SYNC_NOTIFICATION_ID); + } } @Override diff --git a/collect_app/src/main/java/org/odk/collect/android/notifications/Notifier.java b/collect_app/src/main/java/org/odk/collect/android/notifications/Notifier.java index bfc1f55d39c..1d26bea7e5a 100644 --- a/collect_app/src/main/java/org/odk/collect/android/notifications/Notifier.java +++ b/collect_app/src/main/java/org/odk/collect/android/notifications/Notifier.java @@ -11,7 +11,7 @@ public interface Notifier { void onUpdatesDownloaded(HashMap result); - void onSyncFailure(FormApiException exception); + void onSync(FormApiException exception); void onSubmission(boolean failure, String message); } diff --git a/collect_app/src/test/java/org/odk/collect/android/formmanagement/SyncFormsTaskSpecTest.java b/collect_app/src/test/java/org/odk/collect/android/backgroundwork/SyncFormsTaskSpecTest.java similarity index 92% rename from collect_app/src/test/java/org/odk/collect/android/formmanagement/SyncFormsTaskSpecTest.java rename to collect_app/src/test/java/org/odk/collect/android/backgroundwork/SyncFormsTaskSpecTest.java index 477e728d33c..6d7fc55bae7 100644 --- a/collect_app/src/test/java/org/odk/collect/android/formmanagement/SyncFormsTaskSpecTest.java +++ b/collect_app/src/test/java/org/odk/collect/android/backgroundwork/SyncFormsTaskSpecTest.java @@ -1,4 +1,4 @@ -package org.odk.collect.android.formmanagement; +package org.odk.collect.android.backgroundwork; import android.app.Application; @@ -8,8 +8,8 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.InOrder; -import org.odk.collect.android.backgroundwork.ChangeLock; -import org.odk.collect.android.backgroundwork.SyncFormsTaskSpec; +import org.odk.collect.android.formmanagement.FormDownloader; +import org.odk.collect.android.formmanagement.ServerFormsDetailsFetcher; import org.odk.collect.android.formmanagement.matchexactly.ServerFormsSynchronizer; import org.odk.collect.android.formmanagement.matchexactly.SyncStatusRepository; import org.odk.collect.android.forms.FormsRepository; @@ -64,7 +64,7 @@ public Notifier providesNotifier(Application application) { } @Test - public void setsRepositoryToSyncing_runsSync_thenSetsRepositoryToNotSyncing() throws Exception { + public void setsRepositoryToSyncing_runsSync_thenSetsRepositoryToNotSyncingAndNotifies() throws Exception { InOrder inOrder = inOrder(syncStatusRepository, serverFormsSynchronizer); SyncFormsTaskSpec taskSpec = new SyncFormsTaskSpec(); @@ -74,6 +74,8 @@ public void setsRepositoryToSyncing_runsSync_thenSetsRepositoryToNotSyncing() th inOrder.verify(syncStatusRepository).startSync(); inOrder.verify(serverFormsSynchronizer).synchronize(); inOrder.verify(syncStatusRepository).finishSync(null); + + verify(notifier).onSync(null); } @Test @@ -89,7 +91,8 @@ public void whenSynchronizingFails_setsRepositoryToNotSyncingAndNotifiesWithErro inOrder.verify(syncStatusRepository).startSync(); inOrder.verify(serverFormsSynchronizer).synchronize(); inOrder.verify(syncStatusRepository).finishSync(exception); - verify(notifier).onSyncFailure(exception); + + verify(notifier).onSync(exception); } @Test diff --git a/collect_app/src/test/java/org/odk/collect/android/formmanagement/BlankFormsListViewModelTest.java b/collect_app/src/test/java/org/odk/collect/android/formmanagement/BlankFormsListViewModelTest.java index 9e953b79848..0cf09be7826 100644 --- a/collect_app/src/test/java/org/odk/collect/android/formmanagement/BlankFormsListViewModelTest.java +++ b/collect_app/src/test/java/org/odk/collect/android/formmanagement/BlankFormsListViewModelTest.java @@ -81,12 +81,14 @@ public void syncWithServer_startsSyncOnRepository() { @Test public void syncWithServer_whenTaskFinishes_finishesSyncOnRepository() { FakeScheduler fakeScheduler = new FakeScheduler(); + Notifier notifier = mock(Notifier.class); - BlankFormsListViewModel viewModel = new BlankFormsListViewModel(mock(Application.class), fakeScheduler, syncRepository, mock(ServerFormsSynchronizer.class), mock(PreferencesProvider.class), mock(Notifier.class), changeLock); + BlankFormsListViewModel viewModel = new BlankFormsListViewModel(mock(Application.class), fakeScheduler, syncRepository, mock(ServerFormsSynchronizer.class), mock(PreferencesProvider.class), notifier, changeLock); viewModel.syncWithServer(); fakeScheduler.runBackground(); verify(syncRepository).finishSync(null); + verify(notifier).onSync(null); } @Test @@ -103,7 +105,7 @@ public void syncWithServer_whenThereIsAnError_finishesSyncOnRepositoryWithFailur fakeScheduler.runBackground(); verify(syncRepository).finishSync(exception); - verify(notifier).onSyncFailure(exception); + verify(notifier).onSync(exception); } @Test diff --git a/collect_app/src/test/java/org/odk/collect/android/notifications/NotificationManagerNotifierTest.java b/collect_app/src/test/java/org/odk/collect/android/notifications/NotificationManagerNotifierTest.java new file mode 100644 index 00000000000..09289a2e3d8 --- /dev/null +++ b/collect_app/src/test/java/org/odk/collect/android/notifications/NotificationManagerNotifierTest.java @@ -0,0 +1,34 @@ +package org.odk.collect.android.notifications; + +import android.app.Application; +import android.app.NotificationManager; +import android.content.Context; + +import androidx.test.core.app.ApplicationProvider; +import androidx.test.ext.junit.runners.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.odk.collect.android.openrosa.api.FormApiException; +import org.robolectric.shadows.ShadowNotificationManager; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; +import static org.robolectric.Shadows.shadowOf; + +@RunWith(AndroidJUnit4.class) +public class NotificationManagerNotifierTest { + + @Test + public void onSync_whenExceptionNull_clearsNotification() { + Application context = ApplicationProvider.getApplicationContext(); + ShadowNotificationManager shadowNotificationManager = shadowOf((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE)); + NotificationManagerNotifier notifier = new NotificationManagerNotifier(context); + + notifier.onSync(new FormApiException(FormApiException.Type.FETCH_ERROR)); + assertThat(shadowNotificationManager.getAllNotifications().size(), is(1)); + + notifier.onSync(null); + assertThat(shadowNotificationManager.getAllNotifications().size(), is(0)); + } +} \ No newline at end of file