Skip to content

Commit

Permalink
Prevent NavigationNotification update after unregistered
Browse files Browse the repository at this point in the history
  • Loading branch information
danesfeder committed Jul 18, 2018
1 parent f52ebe7 commit ba02ce6
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
class NavigationNotificationProvider {

private NavigationNotification navigationNotification;
private boolean isUpdating = true;

NavigationNotificationProvider(Context context, MapboxNavigation mapboxNavigation) {
navigationNotification = buildNotificationFrom(context, mapboxNavigation);
Expand All @@ -18,14 +19,17 @@ NavigationNotification retrieveNotification() {
}

void updateNavigationNotification(RouteProgress routeProgress) {
navigationNotification.updateNotification(routeProgress);
if (isUpdating) {
navigationNotification.updateNotification(routeProgress);
}
}

void unregisterNotificationReceiver(Context context) {
void shutdown(Context context) {
if (navigationNotification instanceof MapboxNavigationNotification) {
((MapboxNavigationNotification) navigationNotification).unregisterReceiver(context);
}
navigationNotification = null;
isUpdating = false;
}

private NavigationNotification buildNotificationFrom(Context context, MapboxNavigation mapboxNavigation) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ void startNavigation(MapboxNavigation mapboxNavigation) {
void endNavigation() {
routeFetcher.clearListeners();
locationProvider.removeLocationEngineListener();
notificationProvider.unregisterNotificationReceiver(getApplication());
notificationProvider.shutdown(getApplication());
thread.quit();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.mapbox.services.android.navigation.v5.navigation;

import android.content.Context;
import android.support.annotation.NonNull;

import com.mapbox.services.android.navigation.v5.navigation.notification.NavigationNotification;
import com.mapbox.services.android.navigation.v5.routeprogress.RouteProgress;

import org.junit.Test;

import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;

public class NavigationNotificationProviderTest {

@Test
public void updateNavigationNotification() {
NavigationNotification notification = mock(NavigationNotification.class);
MapboxNavigation mapboxNavigation = buildNavigationWithNotificationOptions(notification);
Context context = mock(Context.class);
NavigationNotificationProvider provider = new NavigationNotificationProvider(context, mapboxNavigation);

RouteProgress routeProgress = mock(RouteProgress.class);
provider.updateNavigationNotification(routeProgress);

verify(notification).updateNotification(eq(routeProgress));
}

@Test
public void updateNavigationNotification_doesNotUpdateAfterShutdown() {
NavigationNotification notification = mock(NavigationNotification.class);
MapboxNavigation mapboxNavigation = buildNavigationWithNotificationOptions(notification);
Context context = mock(Context.class);
NavigationNotificationProvider provider = new NavigationNotificationProvider(context, mapboxNavigation);

provider.shutdown(context);
provider.updateNavigationNotification(mock(RouteProgress.class));

verifyZeroInteractions(notification);
}

@NonNull
private MapboxNavigation buildNavigationWithNotificationOptions(NavigationNotification notification) {
MapboxNavigation mapboxNavigation = mock(MapboxNavigation.class);
MapboxNavigationOptions options = mock(MapboxNavigationOptions.class);
when(options.navigationNotification()).thenReturn(notification);
when(mapboxNavigation.options()).thenReturn(options);
return mapboxNavigation;
}
}

0 comments on commit ba02ce6

Please sign in to comment.