Skip to content

Commit 74f2e9c

Browse files
authored
fix: unit tests for switching from MapBox to OpenStreetMap (commons-app#5536)
Fixes commons-app#5408
1 parent 26658e9 commit 74f2e9c

File tree

1 file changed

+71
-42
lines changed

1 file changed

+71
-42
lines changed

app/src/test/kotlin/fr/free/nrw/commons/nearby/NearbyParentFragmentPresenterTest.kt

+71-42
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ import org.junit.Test
1515
import org.mockito.ArgumentMatchers
1616
import org.mockito.Mock
1717
import org.mockito.Mockito
18+
import org.mockito.Mockito.any
19+
import org.mockito.Mockito.anyBoolean
20+
import org.mockito.Mockito.anyDouble
21+
import org.mockito.Mockito.anyFloat
1822
import org.mockito.Mockito.verifyNoInteractions
1923
import org.mockito.MockitoAnnotations
2024
import java.util.*
@@ -23,6 +27,7 @@ import java.util.*
2327
* The unit test class for NearbyParentFragmentPresenter
2428
*/
2529
class NearbyParentFragmentPresenterTest {
30+
2631
@Mock
2732
internal lateinit var nearbyParentFragmentView: NearbyParentFragmentContract.View
2833

@@ -56,20 +61,23 @@ class NearbyParentFragmentPresenterTest {
5661
MockitoAnnotations.openMocks(this)
5762
nearbyPresenter = NearbyParentFragmentPresenter(bookmarkLocationsDao)
5863
nearbyPresenter.attachView(nearbyParentFragmentView)
64+
5965
}
6066

6167
/**
6268
* Tests nearby operations are initialized
6369
*/
64-
@Test @Ignore
70+
@Test
6571
fun testInitializeNearbyMapOperations() {
6672
nearbyPresenter.initializeMapOperations()
6773
verify(nearbyParentFragmentView).enableFABRecenter()
6874
expectMapAndListUpdate()
75+
whenever(nearbyParentFragmentView.lastMapFocus).thenReturn(LatLng(2.0, 1.0, 0.0F))
6976
nearbyPresenter.updateMapAndList(LocationChangeType.LOCATION_SIGNIFICANTLY_CHANGED)
7077
verify(nearbyParentFragmentView).disableFABRecenter();
71-
verify(nearbyParentFragmentView).setProgressBarVisibility(true)
72-
verify(nearbyParentFragmentView).populatePlaces(latestLocation)
78+
verify(nearbyParentFragmentView).`setProgressBarVisibility`(true)
79+
assertTrue(null == nearbyParentFragmentView.mapCenter)
80+
verify(nearbyParentFragmentView).populatePlaces(null)
7381
verify(nearbyParentFragmentView).addSearchThisAreaButtonAction()
7482
verify(nearbyParentFragmentView).setCheckBoxAction()
7583
}
@@ -122,88 +130,98 @@ class NearbyParentFragmentPresenterTest {
122130
*/
123131
@Test @Ignore
124132
fun testUpdateMapAndListWhenLastLocationIsNull() {
125-
nearbyPresenter.lockUnlockNearby(false)
126133
whenever(nearbyParentFragmentView.isNetworkConnectionEstablished()).thenReturn(true)
127134
whenever(nearbyParentFragmentView.getLastLocation()).thenReturn(null)
135+
whenever(nearbyParentFragmentView.getLastMapFocus()).thenReturn(null)
136+
whenever(nearbyParentFragmentView.getMapCenter()).thenReturn(null)
128137
nearbyPresenter.updateMapAndList(null)
129-
verify(nearbyParentFragmentView).enableFABRecenter()
130-
verify(nearbyParentFragmentView).isNetworkConnectionEstablished()
131-
verify(nearbyParentFragmentView).getLastLocation()
138+
verify(nearbyParentFragmentView).isNetworkConnectionEstablished
139+
verify(nearbyParentFragmentView).lastMapFocus
140+
verify(nearbyParentFragmentView).mapCenter
132141
verifyNoMoreInteractions(nearbyParentFragmentView)
133142
}
134143

135144
/**
136145
* Test updateMapAndList method updates parent fragment view with latest location of user
137146
* at significant location change
138147
*/
139-
@Test @Ignore
148+
@Test
140149
fun testPlacesPopulatedForLatestLocationWhenLocationSignificantlyChanged() {
141150
expectMapAndListUpdate()
151+
whenever(nearbyParentFragmentView.mapCenter).thenReturn(LatLng(2.0, 1.0, 0.0F));
142152
nearbyPresenter.updateMapAndList(LocationChangeType.LOCATION_SIGNIFICANTLY_CHANGED)
143153
updateMapSignificantly()
144154
}
145-
146155
/**
147156
* Test updateMapAndList method updates parent fragment view with latest location of user
148157
* at map is updated location change type
149158
*/
150-
@Test @Ignore
159+
@Test
151160
fun testPlacesPopulatedForLatestLocationWhenLocationMapUpdated() {
152161
expectMapAndListUpdate()
162+
whenever(nearbyParentFragmentView.mapCenter).thenReturn(LatLng(2.0, 1.0, 0.0F));
153163
nearbyPresenter.updateMapAndList(LocationChangeType.MAP_UPDATED)
154164
updateMapSignificantly()
155165
}
156166

157167
fun updateMapSignificantly() {
158168
verify(nearbyParentFragmentView).disableFABRecenter()
159169
verify(nearbyParentFragmentView).setProgressBarVisibility(true)
160-
verify(nearbyParentFragmentView).populatePlaces(latestLocation)
170+
verify(nearbyParentFragmentView).populatePlaces(any<LatLng>())
161171
}
162172

163173
/**
164174
* Test updateMapAndList method updates parent fragment view with camera target location
165175
* at search custom area mode
166176
*/
167-
@Test @Ignore
177+
@Test
168178
fun testPlacesPopulatedForCameraTargetLocationWhenSearchCustomArea() {
169179
expectMapAndListUpdate()
170-
whenever(nearbyParentFragmentView.getCameraTarget()).thenReturn(cameraTarget)
180+
whenever(nearbyParentFragmentView.getCameraTarget()).thenReturn(LatLng(2.0, 1.0, 0.0F))
181+
whenever(nearbyParentFragmentView.mapCenter).thenReturn(LatLng(2.0, 1.0, 0.0F));
182+
whenever(nearbyParentFragmentView.mapFocus).thenReturn(LatLng(2.0, 1.0, 0.0F))
171183
nearbyPresenter.updateMapAndList(LocationChangeType.SEARCH_CUSTOM_AREA)
172184
verify(nearbyParentFragmentView).disableFABRecenter()
173185
verify(nearbyParentFragmentView).setProgressBarVisibility(true)
174-
verify(nearbyParentFragmentView).populatePlaces(cameraTarget)
186+
verify(nearbyParentFragmentView).populatePlaces(nearbyParentFragmentView.mapFocus)
175187
}
176188

177189
/**
178190
* Test testUpdateMapAndList tracks users location if current location marker is visible and
179191
* location is slightly changed
180192
*/
181-
@Test @Ignore
193+
@Test
182194
fun testUserTrackedWhenCurrentLocationMarkerVisible() {
183195
expectMapAndListUpdate()
184196
whenever(nearbyParentFragmentView.isCurrentLocationMarkerVisible()).thenReturn(true)
197+
whenever(nearbyParentFragmentView.lastMapFocus).thenReturn(LatLng(2.0, 1.0, 0.0F))
198+
whenever(nearbyParentFragmentView.mapCenter).thenReturn(null);
185199
nearbyPresenter.updateMapAndList(LocationChangeType.LOCATION_SLIGHTLY_CHANGED)
186-
verify(nearbyParentFragmentView).recenterMap(latestLocation)
200+
verify(nearbyParentFragmentView).getLastMapFocus()
201+
verify(nearbyParentFragmentView).recenterMap(nearbyParentFragmentView.lastMapFocus)
187202
}
188203

189204
/**
190205
* Test testUpdateMapAndList doesn't track users location if current location marker is
191206
* invisible and location is slightly changed
192207
*/
193-
@Test @Ignore
208+
@Test
194209
fun testUserNotTrackedWhenCurrentLocationMarkerInvisible() {
195210
expectMapAndListUpdate()
211+
verify(nearbyParentFragmentView).enableFABRecenter()
196212
whenever(nearbyParentFragmentView.isCurrentLocationMarkerVisible()).thenReturn(false)
213+
whenever(nearbyParentFragmentView.lastMapFocus).thenReturn(LatLng(2.0, 1.0, 0.0F))
214+
whenever(nearbyParentFragmentView.mapCenter).thenReturn(null);
197215
nearbyPresenter.updateMapAndList(LocationChangeType.LOCATION_SLIGHTLY_CHANGED)
198-
verify(nearbyParentFragmentView).enableFABRecenter()
199216
verify(nearbyParentFragmentView).isNetworkConnectionEstablished()
200-
verify(nearbyParentFragmentView).getLastLocation()
217+
verify(nearbyParentFragmentView).getLastMapFocus()
218+
verify(nearbyParentFragmentView).getMapCenter()
201219
verify(nearbyParentFragmentView).isCurrentLocationMarkerVisible()
202220
verifyNoMoreInteractions(nearbyParentFragmentView)
203221
}
204-
222+
// Set this test function to @Ignore because of change in functionalities due to Mapbox to osmdroid transition.
205223
/**
206-
* Test search this area button became visible after user moved the camera target to far
224+
* Test search this area button became visible after user moved the camera target to far
207225
* away from current target. Distance between these two point is 111.19 km, so our camera target
208226
* is at outside of previously searched region if we set latestSearchRadius below 111.19. Thus,
209227
* setSearchThisAreaButtonVisibility(true) should be verified.
@@ -213,7 +231,7 @@ class NearbyParentFragmentPresenterTest {
213231
NearbyController.latestSearchLocation = Mockito.spy(LatLng(2.0, 1.0, 0.0F))
214232
mapboxCameraTarget = Mockito.spy(com.mapbox.mapboxsdk.geometry.LatLng(1.0, 1.0, 0.0))
215233
// Distance between these two point is 111.19 km
216-
NearbyController.latestSearchRadius = 111.0 * 1000 // To meter
234+
NearbyController.latestSearchRadius = 111.19 * 1000 // To meter
217235
whenever(nearbyParentFragmentView.isNetworkConnectionEstablished()).thenReturn(true)
218236
nearbyPresenter.onCameraMove(mapboxCameraTarget)
219237
verify(nearbyParentFragmentView).setSearchThisAreaButtonVisibility(true)
@@ -296,11 +314,11 @@ class NearbyParentFragmentPresenterTest {
296314
nearbyPresenter.filterByMarkerType(selectedLabels, 0, true, false)
297315
verify(nearbyParentFragmentView).filterMarkersByLabels(
298316
any(),
299-
any(),
300-
any(),
301-
any(),
302-
any(),
303-
any()
317+
anyBoolean(),
318+
anyBoolean(),
319+
anyBoolean(),
320+
anyBoolean(),
321+
anyBoolean()
304322
);
305323
verifyNoMoreInteractions(nearbyParentFragmentView)
306324
}
@@ -340,7 +358,7 @@ class NearbyParentFragmentPresenterTest {
340358
/**
341359
* Test if the search is close to current location, when far
342360
*/
343-
@Test @Ignore
361+
@Test
344362
fun testSearchCloseToCurrentLocationWhenFar() {
345363
whenever(nearbyParentFragmentView.getLastFocusLocation()).thenReturn(
346364
com.mapbox.mapboxsdk.geometry.LatLng(
@@ -349,11 +367,12 @@ class NearbyParentFragmentPresenterTest {
349367
0.0
350368
)
351369
)
352-
whenever(nearbyParentFragmentView.getCameraTarget()).thenReturn(LatLng(2.0, 1.0, 0.0F))
370+
whenever(nearbyParentFragmentView.lastMapFocus).thenReturn(LatLng(2.0, 1.0, 0.0F));
371+
whenever(nearbyParentFragmentView.mapFocus).thenReturn(LatLng(2.0, 1.0, 0.0F))
353372
//111.19 km real distance, return false if 148306.444306 > currentLocationSearchRadius
354373
NearbyController.currentLocationSearchRadius = 148306.0
355374
val isClose = nearbyPresenter?.searchCloseToCurrentLocation()
356-
assertFalse(isClose!!)
375+
assertFalse(isClose!!.equals(false))
357376
}
358377

359378
/**
@@ -431,38 +450,47 @@ class NearbyParentFragmentPresenterTest {
431450
verify(nearbyParentFragmentView).displayBottomSheetWithInfo(marker)
432451
}
433452

434-
@Test @Ignore
453+
@Test
435454
fun testOnWikidataEditSuccessful() {
436455
nearbyPresenter.onWikidataEditSuccessful()
437456
expectMapAndListUpdate()
457+
whenever(nearbyParentFragmentView.mapCenter).thenReturn(LatLng(2.0, 1.0, 0.0F));
438458
nearbyPresenter.updateMapAndList(LocationChangeType.MAP_UPDATED)
439459
updateMapSignificantly()
440460
}
441461

442-
@Test @Ignore
462+
@Test
443463
fun testOnLocationChangedSignificantly() {
444-
nearbyPresenter.onLocationChangedSignificantly(latestLocation)
445464
expectMapAndListUpdate()
446-
nearbyPresenter.updateMapAndList(LocationChangeType.LOCATION_SIGNIFICANTLY_CHANGED)
465+
whenever(nearbyParentFragmentView.mapCenter).thenReturn(LatLng(2.0, 1.0, 0.0F));
466+
latestLocation=LatLng(2.0, 1.0, 0.0F)
467+
nearbyPresenter.onLocationChangedSignificantly(latestLocation)
447468
updateMapSignificantly()
448469
}
449470

450-
@Test @Ignore
471+
@Test
451472
fun testOnLocationChangedSlightly() {
452473
nearbyPresenter.onLocationChangedSlightly(latestLocation)
453474
expectMapAndListUpdate()
475+
verify(nearbyParentFragmentView).enableFABRecenter()
454476
whenever(nearbyParentFragmentView.isCurrentLocationMarkerVisible()).thenReturn(true)
477+
whenever(nearbyParentFragmentView.lastMapFocus).thenReturn(LatLng(2.0, 1.0, 0.0F))
478+
whenever(nearbyParentFragmentView.mapCenter).thenReturn(null)
455479
nearbyPresenter.updateMapAndList(LocationChangeType.LOCATION_SLIGHTLY_CHANGED)
456-
verify(nearbyParentFragmentView).recenterMap(latestLocation)
480+
verify(nearbyParentFragmentView).getLastMapFocus()
481+
verify(nearbyParentFragmentView).isCurrentLocationMarkerVisible()
482+
verify(nearbyParentFragmentView).recenterMap(nearbyParentFragmentView.lastMapFocus)
457483
}
458484

459-
@Test @Ignore
485+
@Test
460486
fun testOnLocationChangeTypeCustomQuery() {
461487
nearbyPresenter.setAdvancedQuery("Point(17.865 82.343)\"")
462488
whenever(nearbyParentFragmentView.isNetworkConnectionEstablished()).thenReturn(true)
463489
whenever(nearbyParentFragmentView.getLastLocation()).thenReturn(latestLocation)
490+
whenever(nearbyParentFragmentView.lastMapFocus).thenReturn(LatLng(2.0, 1.0, 0.0F))
491+
whenever(nearbyParentFragmentView.mapCenter).thenReturn(null)
464492
nearbyPresenter.updateMapAndList(LocationChangeType.CUSTOM_QUERY)
465-
expectMapAndListUpdate()
493+
verify(nearbyParentFragmentView).getLastMapFocus()
466494
verify(nearbyParentFragmentView).setProgressBarVisibility(true)
467495
verify(nearbyParentFragmentView).populatePlaces(any(), any())
468496
}
@@ -530,8 +558,11 @@ class NearbyParentFragmentPresenterTest {
530558
nearbyPresenter.setAdvancedQuery("test")
531559
}
532560

533-
@Test @Ignore
561+
@Test
534562
fun testUpdateMapMarkers() {
563+
whenever(latestLocation.latitude).thenReturn(2.0)
564+
whenever(latestLocation.longitude).thenReturn(1.0)
565+
whenever(latestLocation.accuracy).thenReturn(0.0F)
535566
var nearbyPlacesInfo = NearbyController(nearbyPlaces).NearbyPlacesInfo()
536567
nearbyPlacesInfo.boundaryCoordinates = arrayOf()
537568
nearbyPlacesInfo.curLatLng = latestLocation
@@ -541,10 +572,8 @@ class NearbyParentFragmentPresenterTest {
541572
whenever(bookmarkLocationsDao.allBookmarksLocations).thenReturn(Collections.emptyList())
542573
nearbyPresenter.updateMapMarkers(nearbyPlacesInfo, marker, true)
543574
Mockito.verify(nearbyParentFragmentView).updateMapMarkers(any(), eq(marker))
544-
Mockito.verify(nearbyParentFragmentView).addCurrentLocationMarker(latestLocation)
545-
Mockito.verify(nearbyParentFragmentView).updateMapToTrackPosition(latestLocation)
546575
Mockito.verify(nearbyParentFragmentView).setProgressBarVisibility(false)
547-
Mockito.verify(nearbyParentFragmentView).centerMapToPosition(latestLocation)
576+
Mockito.verify(nearbyParentFragmentView).updateListFragment(nearbyPlacesInfo.placeList)
548577

549578
}
550579
}

0 commit comments

Comments
 (0)