Add nearby presenter unit tests (#3615)

* init the test file

* Add tests to check if searchthisarea button action and checkbox actions are added after initialize test

* Add tests to locked unlocked nearby cases

* Add tests for null cases in updateMapAndList method

* Add test to check which locations are used to populate places, depending to LocationChangeType

* Add tests to test users position is not followed if blue dot (current location marker) is not visible

* Add tests to decide search this area method visibility on camera move

* Add tests for multi filteirng of placetypes

* add tests for single place type selection too

* Add tests to tests search view focus gain and bottom sheet visibilities

* Add tests for SearchCloseToCurrentLocation

* Remove two unneeded getter and setter for isNearbyLocked, use @VisibleForTesting annotation instead

* Add VisibleForTesting annotation to initializeNearbyOperations method so that it will be private by default

* Add missing tests

* Add tests for map updated case and add missing lines those are being tested

* Add some missind method verifications

* Create real latlang objects isntead of spying them

* Use a real presenter object instead of a spy

* Revert nonneeded @VisibleForTest annotations, instead reach via lockUnlock method

* Reduce code repetitions

* Do not call a test from another test method

* Add some more tests

* Fix minor issues
This commit is contained in:
neslihanturan 2020-07-17 12:32:16 +03:00 committed by GitHub
parent 5f7ac19d49
commit 8630ba8025
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -0,0 +1,444 @@
package fr.free.nrw.commons.nearby
import com.mapbox.mapboxsdk.annotations.Marker
import com.nhaarman.mockitokotlin2.*
import fr.free.nrw.commons.bookmarks.locations.BookmarkLocationsDao
import fr.free.nrw.commons.location.LatLng
import fr.free.nrw.commons.location.LocationServiceManager.LocationChangeType
import fr.free.nrw.commons.nearby.contract.NearbyParentFragmentContract
import fr.free.nrw.commons.nearby.presenter.NearbyParentFragmentPresenter
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
import org.mockito.Mock
import org.mockito.Mockito
import org.mockito.MockitoAnnotations
/**
* The unit test class for NearbyParentFragmentPresenter
*/
class NearbyParentFragmentPresenterTest {
@Mock
internal lateinit var nearbyParentFragmentView: NearbyParentFragmentContract.View
@Mock
internal lateinit var bookmarkLocationsDao: BookmarkLocationsDao
@Mock
internal lateinit var latestLocation: LatLng
@Mock
internal lateinit var cameraTarget: LatLng
@Mock
internal lateinit var selectedLabels: List<Label>
@Mock
internal lateinit var marker: Marker
private lateinit var nearbyPresenter: NearbyParentFragmentPresenter
private lateinit var mapboxCameraTarget: com.mapbox.mapboxsdk.geometry.LatLng
/**
* initial setup
*/
@Before
@Throws(Exception::class)
fun setUp() {
MockitoAnnotations.initMocks(this)
nearbyPresenter = NearbyParentFragmentPresenter(bookmarkLocationsDao)
nearbyPresenter.attachView(nearbyParentFragmentView)
}
/**
* Tests nearby operations are initialized
*/
@Test
fun testInitializeNearbyMapOperations() {
nearbyPresenter.initializeMapOperations()
verify(nearbyParentFragmentView).enableFABRecenter()
expectMapAndListUpdate()
nearbyPresenter.updateMapAndList(LocationChangeType.LOCATION_SIGNIFICANTLY_CHANGED)
verify(nearbyParentFragmentView).disableFABRecenter();
verify(nearbyParentFragmentView).setProgressBarVisibility(true)
verify(nearbyParentFragmentView).populatePlaces(latestLocation)
verify(nearbyParentFragmentView).addSearchThisAreaButtonAction()
verify(nearbyParentFragmentView).setCheckBoxAction()
}
/**
* Test lockUnlockNearby method to lock nearby case
*/
@Test
fun testLockUnlockNearbyForLocked() {
nearbyPresenter.lockUnlockNearby(true)
verify(nearbyParentFragmentView).disableFABRecenter()
}
/**
* Test lockUnlockNearby method to unlock nearby case
*/
@Test
fun testLockUnlockNearbyForUnlocked() {
nearbyPresenter.lockUnlockNearby(false)
verify(nearbyParentFragmentView).enableFABRecenter()
}
/**
* Test updateMapAndList method returns with zero interactions when location is locked
*/
@Test
fun testUpdateMapAndListWhenLocationLocked() {
nearbyPresenter.lockUnlockNearby(true)
nearbyPresenter.updateMapAndList(null)
verify(nearbyParentFragmentView).disableFABRecenter()
verifyZeroInteractions(nearbyParentFragmentView)
}
/**
* Test updateMapAndList method returns with zero interactions when network connection
* is not established
*/
@Test
fun testUpdateMapAndListWhenNoNetworkConnection() {
nearbyPresenter.lockUnlockNearby(false)
whenever(nearbyParentFragmentView.isNetworkConnectionEstablished()).thenReturn(false)
nearbyPresenter.updateMapAndList(null)
verify(nearbyParentFragmentView).enableFABRecenter()
verify(nearbyParentFragmentView).isNetworkConnectionEstablished()
verifyNoMoreInteractions(nearbyParentFragmentView)
}
/**
* Test updateMapAndList method returns with zero interactions when last location is null
*/
@Test
fun testUpdateMapAndListWhenLastLocationIsNull() {
nearbyPresenter.lockUnlockNearby(false)
whenever(nearbyParentFragmentView.isNetworkConnectionEstablished()).thenReturn(true)
whenever(nearbyParentFragmentView.getLastLocation()).thenReturn(null)
nearbyPresenter.updateMapAndList(null)
verify(nearbyParentFragmentView).enableFABRecenter()
verify(nearbyParentFragmentView).isNetworkConnectionEstablished()
verify(nearbyParentFragmentView).getLastLocation()
verifyNoMoreInteractions(nearbyParentFragmentView)
}
/**
* Test updateMapAndList method updates parent fragment view with latest location of user
* at significant location change
*/
@Test
fun testPlacesPopulatedForLatestLocationWhenLocationSignificantlyChanged() {
expectMapAndListUpdate()
nearbyPresenter.updateMapAndList(LocationChangeType.LOCATION_SIGNIFICANTLY_CHANGED)
updateMapSignificantly()
}
/**
* Test updateMapAndList method updates parent fragment view with latest location of user
* at map is updated location change type
*/
@Test
fun testPlacesPopulatedForLatestLocationWhenLocationMapUpdated() {
expectMapAndListUpdate()
nearbyPresenter.updateMapAndList(LocationChangeType.MAP_UPDATED)
updateMapSignificantly()
}
fun updateMapSignificantly() {
verify(nearbyParentFragmentView).disableFABRecenter()
verify(nearbyParentFragmentView).setProgressBarVisibility(true)
verify(nearbyParentFragmentView).populatePlaces(latestLocation)
}
/**
* Test updateMapAndList method updates parent fragment view with camera target location
* at search custom area mode
*/
@Test
fun testPlacesPopulatedForCameraTargetLocationWhenSearchCustomArea() {
expectMapAndListUpdate()
whenever(nearbyParentFragmentView.getCameraTarget()).thenReturn(cameraTarget)
nearbyPresenter.updateMapAndList(LocationChangeType.SEARCH_CUSTOM_AREA)
verify(nearbyParentFragmentView).disableFABRecenter()
verify(nearbyParentFragmentView).setProgressBarVisibility(true)
verify(nearbyParentFragmentView).populatePlaces(cameraTarget)
}
/**
* Test testUpdateMapAndList tracks users location if current location marker is visible and
* location is slightly changed
*/
@Test
fun testUserTrackedWhenCurrentLocationMarkerVisible() {
expectMapAndListUpdate()
whenever(nearbyParentFragmentView.isCurrentLocationMarkerVisible()).thenReturn(true)
nearbyPresenter.updateMapAndList(LocationChangeType.LOCATION_SLIGHTLY_CHANGED)
verify(nearbyParentFragmentView).recenterMap(latestLocation)
}
/**
* Test testUpdateMapAndList doesn't track users location if current location marker is
* invisible and location is slightly changed
*/
@Test
fun testUserNotTrackedWhenCurrentLocationMarkerInvisible() {
expectMapAndListUpdate()
whenever(nearbyParentFragmentView.isCurrentLocationMarkerVisible()).thenReturn(false)
nearbyPresenter.updateMapAndList(LocationChangeType.LOCATION_SLIGHTLY_CHANGED)
verify(nearbyParentFragmentView).enableFABRecenter()
verify(nearbyParentFragmentView).isNetworkConnectionEstablished()
verify(nearbyParentFragmentView).getLastLocation()
verify(nearbyParentFragmentView).isCurrentLocationMarkerVisible()
verifyNoMoreInteractions(nearbyParentFragmentView)
}
/**
* Test search this area button became visible after user moved the camera target to far
* away from current target. Distance between these two point is 111.19 km, so our camera target
* is at outside of previously searched region if we set latestSearchRadius below 111.19. Thus,
* setSearchThisAreaButtonVisibility(true) should be verified.
*/
@Test
fun testSearchThisAreaButtonVisibleWhenMoveToFarPosition() {
NearbyController.latestSearchLocation = Mockito.spy(LatLng(2.0,1.0,0.0F))
mapboxCameraTarget = Mockito.spy(com.mapbox.mapboxsdk.geometry.LatLng(1.0,1.0,0.0))
// Distance between these two point is 111.19 km
NearbyController.latestSearchRadius = 111.0*1000 // To meter
whenever(nearbyParentFragmentView.isNetworkConnectionEstablished()).thenReturn(true)
nearbyPresenter.onCameraMove(mapboxCameraTarget)
verify(nearbyParentFragmentView).setSearchThisAreaButtonVisibility(true)
}
/**
* Test search this area button became visible after user moved the camera target to far
* away from current target. Distance between these two point is 111.19 km, so our camera target
* is at inside of previously searched region if we set latestSearchRadius above 111.19. Thus,
* setSearchThisAreaButtonVisibility(false) should be verified.
*/
@Test
fun testSearchThisAreaButtonInvisibleWhenMoveToClosePosition() {
NearbyController.latestSearchLocation = Mockito.spy(LatLng(2.0,1.0,0.0F))
mapboxCameraTarget = Mockito.spy(com.mapbox.mapboxsdk.geometry.LatLng(1.0,1.0,0.0))
// Distance between these two point is 111.19 km
NearbyController.latestSearchRadius = 112.0*1000 // To meter
whenever(nearbyParentFragmentView.isNetworkConnectionEstablished()).thenReturn(true)
nearbyPresenter.onCameraMove(mapboxCameraTarget)
verify(nearbyParentFragmentView).setSearchThisAreaButtonVisibility(false)
}
/**
* Multi selection should overwrite single selection of marker types. Ie. when user choose
*"parks", then they multi select to display all or none, we overwrite previous "park" filter.
*
* We expect zero interaction from view when state is UNKNOWN
*/
@Test
fun testFilterByMarkerTypeMultiSelectUNKNOWN() {
val state = CheckBoxTriStates.UNKNOWN
nearbyPresenter.filterByMarkerType(selectedLabels,state,false,true)
verifyZeroInteractions(nearbyParentFragmentView)
}
/**
* Multi selection should overwrite single selection of marker types. Ie. when user choose
*"parks", then they multi select to display all or none, we overwrite previous "park" filter.
*
* We expect just filterOutAllMarkers and setRecyclerViewAdapterItemsGreyedOut is called when
* the state is UNCHECKED
*/
@Test
fun testFilterByMarkerTypeMultiSelectUNCHECKED() {
val state = CheckBoxTriStates.UNCHECKED
nearbyPresenter.filterByMarkerType(selectedLabels,state,false,true)
verify(nearbyParentFragmentView).filterOutAllMarkers()
verify(nearbyParentFragmentView).setRecyclerViewAdapterItemsGreyedOut()
verifyNoMoreInteractions(nearbyParentFragmentView)
}
/**
* Multi selection should overwrite single selection of marker types. Ie. when user choose
*"parks", then they multi select to display all or none, we overwrite previous "park" filter.
*
* We expect just displayAllMarkers and setRecyclerViewAdapterAllSelected is called when
* the state is CHECKED
*/
@Test
fun testFilterByMarkerTypeMultiSelectCHECKED() {
val state = CheckBoxTriStates.CHECKED
nearbyPresenter.filterByMarkerType(selectedLabels, state, false,true)
verify(nearbyParentFragmentView).displayAllMarkers()
verify(nearbyParentFragmentView).setRecyclerViewAdapterAllSelected()
verifyNoMoreInteractions(nearbyParentFragmentView)
}
/**
* We expect just filterMarkersByLabels is called when filterForAllNoneType is false
*/
@Test
fun testFilterByMarkerTypeSingleSelect() {
nearbyPresenter.filterByMarkerType(selectedLabels, 0, true,false)
verify(nearbyParentFragmentView).filterMarkersByLabels(any(), any(), any(), any(), any());
verifyNoMoreInteractions(nearbyParentFragmentView)
}
/**
* Test if bottom sheet gets hidden after search view gained focus
*/
@Test
fun testSearchViewFocusWhenBottomSheetExpanded() {
whenever(nearbyParentFragmentView.isListBottomSheetExpanded()).thenReturn(true)
nearbyPresenter.searchViewGainedFocus()
verify(nearbyParentFragmentView).hideBottomSheet()
}
/**
* Test if bottom details sheet gets hidden after search view gained focus
*/
@Test
fun testSearchViewFocusWhenDetailsBottomSheetVisible() {
whenever(nearbyParentFragmentView.isListBottomSheetExpanded()).thenReturn(false)
whenever(nearbyParentFragmentView.isDetailsBottomSheetVisible()).thenReturn(true)
nearbyPresenter.searchViewGainedFocus()
verify(nearbyParentFragmentView).hideBottomDetailsSheet()
}
/**
* Test if the search is close to current location, when last location is null we expect it to
* return true
*/
@Test
fun testSearchCloseToCurrentLocationNullLastLocation() {
whenever(nearbyParentFragmentView.getLastFocusLocation()).thenReturn(null)
val isClose = nearbyPresenter?.searchCloseToCurrentLocation()
assertTrue(isClose!!)
}
/**
* Test if the search is close to current location, when far
*/
@Test
fun testSearchCloseToCurrentLocationWhenFar() {
whenever(nearbyParentFragmentView.getLastFocusLocation()).
thenReturn(com.mapbox.mapboxsdk.geometry.LatLng(1.0,1.0,0.0))
whenever(nearbyParentFragmentView.getCameraTarget()).
thenReturn(LatLng(2.0,1.0,0.0F))
//111.19 km real distance, return false if 148306.444306 > currentLocationSearchRadius
NearbyController.currentLocationSearchRadius = 148306.0
val isClose = nearbyPresenter?.searchCloseToCurrentLocation()
assertFalse(isClose!!)
}
/**
* Test if the search is close to current location, when close
*/
@Test
fun testSearchCloseToCurrentLocationWhenClose() {
whenever(nearbyParentFragmentView.getLastFocusLocation()).
thenReturn(com.mapbox.mapboxsdk.geometry.LatLng(1.0,1.0,0.0))
whenever(nearbyParentFragmentView.getCameraTarget()).
thenReturn(LatLng(2.0,1.0,0.0F))
//111.19 km real distance, return false if 148253.333 > currentLocationSearchRadius
NearbyController.currentLocationSearchRadius = 148307.0
val isClose = nearbyPresenter?.searchCloseToCurrentLocation()
assertTrue(isClose!!)
}
fun expectMapAndListUpdate() {
nearbyPresenter.lockUnlockNearby(false)
whenever(nearbyParentFragmentView.isNetworkConnectionEstablished()).thenReturn(true)
whenever(nearbyParentFragmentView.getLastLocation()).thenReturn(latestLocation)
}
@Test
fun testSetActionListeners() {
nearbyPresenter.setActionListeners(any())
verify(nearbyParentFragmentView).setFABPlusAction(any())
verify(nearbyParentFragmentView).setFABRecenterAction(any())
}
@Test
fun testBackButtonClickedWhenBottomSheetExpanded() {
whenever(nearbyParentFragmentView.isListBottomSheetExpanded()).thenReturn(true)
nearbyPresenter.backButtonClicked()
verify(nearbyParentFragmentView).listOptionMenuItemClicked()
}
@Test
fun testBackButtonClickedWhenDetailsBottomSheetVisible() {
whenever(nearbyParentFragmentView.isListBottomSheetExpanded()).thenReturn(false)
whenever(nearbyParentFragmentView.isDetailsBottomSheetVisible()).thenReturn(true)
nearbyPresenter.backButtonClicked()
verify(nearbyParentFragmentView).setBottomSheetDetailsSmaller()
}
@Test
fun testBackButtonClickedWhenNoSheetVisible() {
whenever(nearbyParentFragmentView.isListBottomSheetExpanded()).thenReturn(false)
whenever(nearbyParentFragmentView.isDetailsBottomSheetVisible()).thenReturn(false)
nearbyPresenter.backButtonClicked()
verify(nearbyParentFragmentView).setTabItemContributions()
}
@Test
fun testMarkerUnselected() {
nearbyPresenter.markerUnselected()
verify(nearbyParentFragmentView).hideBottomSheet();
}
@Test
fun testMarkerSelected() {
nearbyPresenter.markerSelected(marker)
verify(nearbyParentFragmentView).displayBottomSheetWithInfo(marker)
}
@Test
fun testOnWikidataEditSuccessful() {
nearbyPresenter.onWikidataEditSuccessful()
expectMapAndListUpdate()
nearbyPresenter.updateMapAndList(LocationChangeType.MAP_UPDATED)
updateMapSignificantly()
}
@Test
fun testOnLocationChangedSignificantly() {
nearbyPresenter.onLocationChangedSignificantly(latestLocation)
expectMapAndListUpdate()
nearbyPresenter.updateMapAndList(LocationChangeType.LOCATION_SIGNIFICANTLY_CHANGED)
updateMapSignificantly()
}
@Test
fun testOnLocationChangedSlightly() {
nearbyPresenter.onLocationChangedSlightly(latestLocation)
expectMapAndListUpdate()
whenever(nearbyParentFragmentView.isCurrentLocationMarkerVisible()).thenReturn(true)
nearbyPresenter.updateMapAndList(LocationChangeType.LOCATION_SLIGHTLY_CHANGED)
verify(nearbyParentFragmentView).recenterMap(latestLocation)
}
@Test
fun testOnCameraMoveWhenSearchLocationNull() {
NearbyController.latestSearchLocation = null
nearbyPresenter.onCameraMove(Mockito.mock(com.mapbox.mapboxsdk.geometry.LatLng::class.java))
verify(nearbyParentFragmentView).setProjectorLatLngBounds()
verify(nearbyParentFragmentView).setSearchThisAreaButtonVisibility(false)
}
@Test
fun testOnCameraMoveWhenNetworkConnectionNotEstablished() {
NearbyController.latestSearchLocation = latestLocation
whenever(nearbyParentFragmentView.isNetworkConnectionEstablished()).thenReturn(false)
nearbyPresenter.onCameraMove(Mockito.mock(com.mapbox.mapboxsdk.geometry.LatLng::class.java))
verify(nearbyParentFragmentView).setProjectorLatLngBounds()
verify(nearbyParentFragmentView).isNetworkConnectionEstablished()
verifyZeroInteractions(nearbyParentFragmentView)
}
@Test
fun testOnCameraMoveWhenNetworkConnectionEstablished() {
NearbyController.latestSearchLocation = latestLocation
whenever(nearbyParentFragmentView.isNetworkConnectionEstablished()).thenReturn(false)
nearbyPresenter.onCameraMove(Mockito.mock(com.mapbox.mapboxsdk.geometry.LatLng::class.java))
verify(nearbyParentFragmentView).setProjectorLatLngBounds()
verify(nearbyParentFragmentView).isNetworkConnectionEstablished()
verifyZeroInteractions(nearbyParentFragmentView)
}
}