mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 20:33:53 +01:00
Add some more tests for NearbyController (#4778)
This commit is contained in:
parent
15d70bbe0a
commit
d77ae8e582
2 changed files with 325 additions and 41 deletions
|
|
@ -1,18 +1,19 @@
|
|||
package fr.free.nrw.commons.nearby;
|
||||
|
||||
import static fr.free.nrw.commons.utils.LengthUtils.computeDistanceBetween;
|
||||
import static fr.free.nrw.commons.utils.LengthUtils.formatDistanceBetween;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.Resources;
|
||||
import android.graphics.Bitmap;
|
||||
|
||||
import androidx.annotation.MainThread;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.vectordrawable.graphics.drawable.VectorDrawableCompat;
|
||||
|
||||
import com.mapbox.mapboxsdk.annotations.IconFactory;
|
||||
import com.mapbox.mapboxsdk.annotations.Marker;
|
||||
|
||||
import io.reactivex.Observable;
|
||||
import java.io.IOException;
|
||||
import fr.free.nrw.commons.R;
|
||||
import fr.free.nrw.commons.location.LatLng;
|
||||
import fr.free.nrw.commons.utils.UiUtils;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
|
|
@ -20,17 +21,9 @@ import java.util.List;
|
|||
import java.util.ListIterator;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import fr.free.nrw.commons.R;
|
||||
import fr.free.nrw.commons.location.LatLng;
|
||||
import fr.free.nrw.commons.utils.UiUtils;
|
||||
import timber.log.Timber;
|
||||
|
||||
import static fr.free.nrw.commons.utils.LengthUtils.computeDistanceBetween;
|
||||
import static fr.free.nrw.commons.utils.LengthUtils.formatDistanceBetween;
|
||||
|
||||
public class NearbyController {
|
||||
private static final int MAX_RESULTS = 1000;
|
||||
private final NearbyPlaces nearbyPlaces;
|
||||
|
|
@ -127,11 +120,8 @@ public class NearbyController {
|
|||
}
|
||||
|
||||
|
||||
return nearbyPlacesInfo;
|
||||
}
|
||||
else {
|
||||
return nearbyPlacesInfo;
|
||||
}
|
||||
return nearbyPlacesInfo;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -151,24 +141,6 @@ public class NearbyController {
|
|||
checkingAroundCurrentLocation, shouldQueryForMonuments, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads attractions from location for list view, we need to return Place data type.
|
||||
*
|
||||
* @param curLatLng users current location
|
||||
* @param placeList list of nearby places in Place data type
|
||||
* @return Place list that holds nearby places
|
||||
*/
|
||||
public static List<Place> loadAttractionsFromLocationToPlaces(
|
||||
LatLng curLatLng,
|
||||
List<Place> placeList) {
|
||||
placeList = placeList.subList(0, Math.min(placeList.size(), MAX_RESULTS));
|
||||
for (Place place : placeList) {
|
||||
String distance = formatDistanceBetween(curLatLng, place.location);
|
||||
place.setDistance(distance);
|
||||
}
|
||||
return placeList;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads attractions from location for map view, we need to return BaseMarkerOption data type.
|
||||
*
|
||||
|
|
|
|||
|
|
@ -1,37 +1,61 @@
|
|||
package fr.free.nrw.commons.nearby
|
||||
|
||||
import android.content.Context
|
||||
import com.nhaarman.mockitokotlin2.any
|
||||
import com.nhaarman.mockitokotlin2.anyOrNull
|
||||
import com.nhaarman.mockitokotlin2.eq
|
||||
import fr.free.nrw.commons.TestCommonsApplication
|
||||
import fr.free.nrw.commons.location.LatLng
|
||||
import fr.free.nrw.commons.nearby.NearbyController.loadAttractionsFromLocationToBaseMarkerOptions
|
||||
import fr.free.nrw.commons.nearby.NearbyController.updateMarkerLabelListBookmark
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.runner.RunWith
|
||||
import org.mockito.Mock
|
||||
import org.mockito.Mockito
|
||||
import org.mockito.Mockito.`when`
|
||||
import org.mockito.MockitoAnnotations
|
||||
import org.powermock.reflect.Whitebox
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.RuntimeEnvironment
|
||||
import org.robolectric.annotation.Config
|
||||
import org.robolectric.annotation.LooperMode
|
||||
import java.util.*
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [21], application = TestCommonsApplication::class)
|
||||
@LooperMode(LooperMode.Mode.PAUSED)
|
||||
class NearbyControllerTest {
|
||||
|
||||
@Mock
|
||||
private lateinit var place: Place
|
||||
|
||||
@Mock
|
||||
private lateinit var nearbyPlaces: NearbyPlaces
|
||||
|
||||
@Mock
|
||||
lateinit var searchLatLong: LatLng
|
||||
private lateinit var searchLatLong: LatLng
|
||||
|
||||
@Mock
|
||||
lateinit var currentLatLng: LatLng
|
||||
var customQuery: String = "test"
|
||||
private lateinit var currentLatLng: LatLng
|
||||
|
||||
|
||||
private lateinit var context: Context
|
||||
private var customQuery: String = "test"
|
||||
private lateinit var nearbyController: NearbyController
|
||||
private lateinit var markerPlaceGroup: MarkerPlaceGroup
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
MockitoAnnotations.initMocks(this)
|
||||
nearbyController = NearbyController(nearbyPlaces)
|
||||
context = RuntimeEnvironment.application.applicationContext
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testLoadAttractionsForLocationTest() {
|
||||
Mockito.`when`(nearbyPlaces.radiusExpander(any(), any(), any(), any(), any()))
|
||||
`when`(nearbyPlaces.radiusExpander(any(), any(), any(), any(), any()))
|
||||
.thenReturn(Collections.emptyList())
|
||||
nearbyController.loadAttractionsFromLocation(
|
||||
searchLatLong,
|
||||
|
|
@ -52,7 +76,7 @@ class NearbyControllerTest {
|
|||
|
||||
@Test
|
||||
fun testLoadAttractionsForLocationTestNoQuery() {
|
||||
Mockito.`when`(nearbyPlaces.radiusExpander(any(), any(), any(), any(), anyOrNull()))
|
||||
`when`(nearbyPlaces.radiusExpander(any(), any(), any(), any(), anyOrNull()))
|
||||
.thenReturn(Collections.emptyList())
|
||||
nearbyController.loadAttractionsFromLocation(
|
||||
searchLatLong,
|
||||
|
|
@ -70,5 +94,293 @@ class NearbyControllerTest {
|
|||
)
|
||||
}
|
||||
|
||||
fun <T> any(type: Class<T>): T = Mockito.any<T>(type)
|
||||
@Test
|
||||
fun testLoadAttractionsFromLocationCaseNull() {
|
||||
assertEquals(
|
||||
nearbyController.loadAttractionsFromLocation(
|
||||
currentLatLng,
|
||||
null,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
customQuery
|
||||
), null
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testLoadAttractionsFromLocationCase1() {
|
||||
val place1 = Place(
|
||||
"en",
|
||||
"placeName",
|
||||
Label.FOREST,
|
||||
"placeDescription",
|
||||
LatLng(0.0, 0.0, 1.0F),
|
||||
"placeCategory",
|
||||
Sitelinks.Builder().build(),
|
||||
"picName",
|
||||
false
|
||||
)
|
||||
val place2 = Place(
|
||||
"en",
|
||||
"placeName",
|
||||
Label.FOREST,
|
||||
"placeDescription",
|
||||
LatLng(-40.69, -74.04, 1.0F),
|
||||
"placeCategory",
|
||||
Sitelinks.Builder().build(),
|
||||
"picName",
|
||||
false
|
||||
)
|
||||
`when`(
|
||||
nearbyPlaces.radiusExpander(
|
||||
searchLatLong, Locale.getDefault().language, false,
|
||||
false, customQuery
|
||||
)
|
||||
).thenReturn(mutableListOf(place1, place2))
|
||||
val result = nearbyController.loadAttractionsFromLocation(
|
||||
currentLatLng,
|
||||
searchLatLong,
|
||||
false,
|
||||
true,
|
||||
false,
|
||||
customQuery
|
||||
)
|
||||
assertEquals(result.curLatLng, currentLatLng)
|
||||
assertEquals(result.searchLatLng, searchLatLong)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testLoadAttractionsFromLocationCase2() {
|
||||
val place1 = Place(
|
||||
"en",
|
||||
"placeName",
|
||||
Label.FOREST,
|
||||
"placeDescription",
|
||||
LatLng(0.0, 0.0, 1.0F),
|
||||
"placeCategory",
|
||||
Sitelinks.Builder().build(),
|
||||
"picName",
|
||||
false
|
||||
)
|
||||
val place2 = Place(
|
||||
"en",
|
||||
"placeName",
|
||||
Label.FOREST,
|
||||
"placeDescription",
|
||||
LatLng(40.69, -74.04, 1.0F),
|
||||
"placeCategory",
|
||||
Sitelinks.Builder().build(),
|
||||
"picName",
|
||||
false
|
||||
)
|
||||
`when`(
|
||||
nearbyPlaces.radiusExpander(
|
||||
searchLatLong, Locale.getDefault().language, false,
|
||||
false, customQuery
|
||||
)
|
||||
).thenReturn(mutableListOf(place1, place2))
|
||||
val result = nearbyController.loadAttractionsFromLocation(
|
||||
currentLatLng,
|
||||
searchLatLong,
|
||||
false,
|
||||
true,
|
||||
false,
|
||||
customQuery
|
||||
)
|
||||
assertEquals(result.curLatLng, currentLatLng)
|
||||
assertEquals(result.searchLatLng, searchLatLong)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testLoadAttractionsFromLocationCase3() {
|
||||
val place1 = Place(
|
||||
"en",
|
||||
"placeName",
|
||||
Label.FOREST,
|
||||
"placeDescription",
|
||||
LatLng(0.0, 0.0, 1.0F),
|
||||
"placeCategory",
|
||||
Sitelinks.Builder().build(),
|
||||
"picName",
|
||||
false
|
||||
)
|
||||
val place2 = Place(
|
||||
"en",
|
||||
"placeName",
|
||||
Label.FOREST,
|
||||
"placeDescription",
|
||||
LatLng(40.69, 74.04, 1.0F),
|
||||
"placeCategory",
|
||||
Sitelinks.Builder().build(),
|
||||
"picName",
|
||||
false
|
||||
)
|
||||
`when`(
|
||||
nearbyPlaces.radiusExpander(
|
||||
searchLatLong, Locale.getDefault().language, false,
|
||||
false, customQuery
|
||||
)
|
||||
).thenReturn(mutableListOf(place1, place2))
|
||||
val result = nearbyController.loadAttractionsFromLocation(
|
||||
currentLatLng,
|
||||
searchLatLong,
|
||||
false,
|
||||
true,
|
||||
false,
|
||||
customQuery
|
||||
)
|
||||
assertEquals(result.curLatLng, currentLatLng)
|
||||
assertEquals(result.searchLatLng, searchLatLong)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testLoadAttractionsFromLocationToBaseMarkerOptionsCaseNull() {
|
||||
assertEquals(
|
||||
loadAttractionsFromLocationToBaseMarkerOptions(
|
||||
currentLatLng,
|
||||
null,
|
||||
context,
|
||||
mutableListOf(place)
|
||||
), listOf<NearbyBaseMarker>()
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testLoadAttractionsFromLocationToBaseMarkerOptionsCaseIsMonument() {
|
||||
place = Place(
|
||||
"en",
|
||||
"placeName",
|
||||
Label.FOREST,
|
||||
"placeDescription",
|
||||
currentLatLng,
|
||||
"placeCategory",
|
||||
Sitelinks.Builder().build(),
|
||||
"picName",
|
||||
false
|
||||
)
|
||||
place.isMonument = true
|
||||
`when`(currentLatLng.latitude).thenReturn(0.0)
|
||||
`when`(currentLatLng.longitude).thenReturn(0.0)
|
||||
assertEquals(
|
||||
loadAttractionsFromLocationToBaseMarkerOptions(
|
||||
currentLatLng,
|
||||
listOf(place),
|
||||
context,
|
||||
mutableListOf(place)
|
||||
)[0].place, place
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testLoadAttractionsFromLocationToBaseMarkerOptionsCaseIsNotMonumentPicNotEmpty() {
|
||||
place = Place(
|
||||
"en",
|
||||
"placeName",
|
||||
Label.FOREST,
|
||||
"placeDescription",
|
||||
currentLatLng,
|
||||
"placeCategory",
|
||||
Sitelinks.Builder().build(),
|
||||
"picName",
|
||||
false
|
||||
)
|
||||
place.isMonument = false
|
||||
`when`(currentLatLng.latitude).thenReturn(0.0)
|
||||
`when`(currentLatLng.longitude).thenReturn(0.0)
|
||||
assertEquals(
|
||||
loadAttractionsFromLocationToBaseMarkerOptions(
|
||||
currentLatLng,
|
||||
listOf(place),
|
||||
context,
|
||||
mutableListOf(place)
|
||||
)[0].place, place
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testLoadAttractionsFromLocationToBaseMarkerOptionsCaseIsNotMonumentPicEmptyPlaceDoesNotExists() {
|
||||
place = Place(
|
||||
"en",
|
||||
"placeName",
|
||||
Label.FOREST,
|
||||
"placeDescription",
|
||||
currentLatLng,
|
||||
"placeCategory",
|
||||
Sitelinks.Builder().build(),
|
||||
"",
|
||||
false
|
||||
)
|
||||
place.isMonument = false
|
||||
`when`(currentLatLng.latitude).thenReturn(0.0)
|
||||
`when`(currentLatLng.longitude).thenReturn(0.0)
|
||||
assertEquals(
|
||||
loadAttractionsFromLocationToBaseMarkerOptions(
|
||||
currentLatLng,
|
||||
listOf(place),
|
||||
context,
|
||||
mutableListOf(place)
|
||||
)[0].place, place
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testLoadAttractionsFromLocationToBaseMarkerOptionsCaseIsNotMonumentPicEmptyPlaceExists() {
|
||||
place = Place(
|
||||
"en",
|
||||
"placeName",
|
||||
Label.FOREST,
|
||||
"placeDescription",
|
||||
currentLatLng,
|
||||
"placeCategory",
|
||||
Sitelinks.Builder().build(),
|
||||
"",
|
||||
true
|
||||
)
|
||||
place.isMonument = false
|
||||
`when`(currentLatLng.latitude).thenReturn(0.0)
|
||||
`when`(currentLatLng.longitude).thenReturn(0.0)
|
||||
assertEquals(
|
||||
loadAttractionsFromLocationToBaseMarkerOptions(
|
||||
currentLatLng,
|
||||
listOf(place),
|
||||
context,
|
||||
mutableListOf(place)
|
||||
)[0].place, place
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testUpdateMarkerLabelListBookmarkCaseTrue() {
|
||||
markerPlaceGroup = MarkerPlaceGroup(null, true, place)
|
||||
`when`(place.wikiDataEntityId).thenReturn("someString")
|
||||
val list = mutableListOf(markerPlaceGroup)
|
||||
Whitebox.setInternalState(
|
||||
NearbyController::class.java,
|
||||
"markerLabelList",
|
||||
list
|
||||
)
|
||||
updateMarkerLabelListBookmark(place, false)
|
||||
assertEquals(list[0].isBookmarked, false)
|
||||
assertEquals(list[0].place, place)
|
||||
assertEquals(list[0].marker, null)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testUpdateMarkerLabelListBookmarkCaseFalse() {
|
||||
markerPlaceGroup = MarkerPlaceGroup(null, false, place)
|
||||
`when`(place.wikiDataEntityId).thenReturn("someString")
|
||||
val list = mutableListOf(markerPlaceGroup)
|
||||
Whitebox.setInternalState(
|
||||
NearbyController::class.java,
|
||||
"markerLabelList",
|
||||
list
|
||||
)
|
||||
updateMarkerLabelListBookmark(place, true)
|
||||
assertEquals(list[0].isBookmarked, true)
|
||||
assertEquals(list[0].place, place)
|
||||
assertEquals(list[0].marker, null)
|
||||
}
|
||||
|
||||
fun <T> any(type: Class<T>): T = Mockito.any(type)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue