mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-27 04:43:54 +01:00
Merge pull request #673 from tobias47n9e/bugfix/nearby-crash
Fix crash of nearby list when the places list is null
This commit is contained in:
commit
0112a09a70
2 changed files with 67 additions and 4 deletions
|
|
@ -0,0 +1,57 @@
|
||||||
|
package fr.free.nrw.commons;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.support.test.InstrumentationRegistry;
|
||||||
|
import android.support.test.runner.AndroidJUnit4;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import fr.free.nrw.commons.location.LatLng;
|
||||||
|
import fr.free.nrw.commons.nearby.NearbyBaseMarker;
|
||||||
|
import fr.free.nrw.commons.nearby.NearbyController;
|
||||||
|
import fr.free.nrw.commons.nearby.Place;
|
||||||
|
|
||||||
|
import static org.hamcrest.CoreMatchers.is;
|
||||||
|
|
||||||
|
@RunWith(AndroidJUnit4.class)
|
||||||
|
public class NearbyControllerTest {
|
||||||
|
private Context instrumentationContext;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setup() {
|
||||||
|
instrumentationContext = InstrumentationRegistry.getContext();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test public void testNullAttractions() {
|
||||||
|
LatLng location = new LatLng(0, 0);
|
||||||
|
|
||||||
|
List<NearbyBaseMarker> options =
|
||||||
|
NearbyController.loadAttractionsFromLocationToBaseMarkerOptions(
|
||||||
|
location,
|
||||||
|
null,
|
||||||
|
instrumentationContext
|
||||||
|
);
|
||||||
|
|
||||||
|
Assert.assertThat(options.size(), is(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test public void testEmptyList() {
|
||||||
|
LatLng location = new LatLng(0, 0);
|
||||||
|
List<Place> emptyList = new ArrayList<>();
|
||||||
|
|
||||||
|
List<NearbyBaseMarker> options =
|
||||||
|
NearbyController.loadAttractionsFromLocationToBaseMarkerOptions(
|
||||||
|
location,
|
||||||
|
emptyList,
|
||||||
|
instrumentationContext
|
||||||
|
);
|
||||||
|
|
||||||
|
Assert.assertThat(options.size(), is(0));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -84,14 +84,20 @@ public class NearbyController {
|
||||||
*Loads attractions from location for map view, we need to return BaseMarkerOption data type.
|
*Loads attractions from location for map view, we need to return BaseMarkerOption data type.
|
||||||
* @param curLatLng users current location
|
* @param curLatLng users current location
|
||||||
* @param placeList list of nearby places in Place data type
|
* @param placeList list of nearby places in Place data type
|
||||||
* @return BaseMarkerOprions list that holds nearby places
|
* @return BaseMarkerOptions list that holds nearby places
|
||||||
*/
|
*/
|
||||||
public static List<NearbyBaseMarker> loadAttractionsFromLocationToBaseMarkerOptions(
|
public static List<NearbyBaseMarker> loadAttractionsFromLocationToBaseMarkerOptions(
|
||||||
LatLng curLatLng,
|
LatLng curLatLng,
|
||||||
List<Place> placeList,
|
List<Place> placeList,
|
||||||
Context context) {
|
Context context) {
|
||||||
List<NearbyBaseMarker> baseMarkerOptionses = new ArrayList<>();
|
List<NearbyBaseMarker> baseMarkerOptions = new ArrayList<>();
|
||||||
|
|
||||||
|
if (placeList == null) {
|
||||||
|
return baseMarkerOptions;
|
||||||
|
}
|
||||||
|
|
||||||
placeList = placeList.subList(0, Math.min(placeList.size(), MAX_RESULTS));
|
placeList = placeList.subList(0, Math.min(placeList.size(), MAX_RESULTS));
|
||||||
|
|
||||||
for (Place place: placeList) {
|
for (Place place: placeList) {
|
||||||
String distance = formatDistanceBetween(curLatLng, place.location);
|
String distance = formatDistanceBetween(curLatLng, place.location);
|
||||||
place.setDistance(distance);
|
place.setDistance(distance);
|
||||||
|
|
@ -108,8 +114,8 @@ public class NearbyController {
|
||||||
nearbyBaseMarker.place(place);
|
nearbyBaseMarker.place(place);
|
||||||
nearbyBaseMarker.icon(icon);
|
nearbyBaseMarker.icon(icon);
|
||||||
|
|
||||||
baseMarkerOptionses.add(nearbyBaseMarker);
|
baseMarkerOptions.add(nearbyBaseMarker);
|
||||||
}
|
}
|
||||||
return baseMarkerOptionses;
|
return baseMarkerOptions;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue