Skip to content

Commit

Permalink
Make sure notification cleared when sync success
Browse files Browse the repository at this point in the history
  • Loading branch information
seadowg committed Jul 31, 2020
1 parent eb5f4c1 commit d9b24ed
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ public Supplier<Boolean> 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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -82,19 +84,23 @@ public void onUpdatesDownloaded(HashMap<ServerFormDetails, String> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public interface Notifier {

void onUpdatesDownloaded(HashMap<ServerFormDetails, String> result);

void onSyncFailure(FormApiException exception);
void onSync(FormApiException exception);

void onSubmission(boolean failure, String message);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.odk.collect.android.formmanagement;
package org.odk.collect.android.backgroundwork;

import android.app.Application;

Expand All @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -103,7 +105,7 @@ public void syncWithServer_whenThereIsAnError_finishesSyncOnRepositoryWithFailur
fakeScheduler.runBackground();

verify(syncRepository).finishSync(exception);
verify(notifier).onSyncFailure(exception);
verify(notifier).onSync(exception);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -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));
}
}

0 comments on commit d9b24ed

Please sign in to comment.