Added advanced query support in Nearby (#4714)

* Added feature for advanced query options in Nearby

* Fix unit-tests coverage

* wip-tests

* Added log to identify if the nearby request is via an advanced query

* Rolledback test-dependency updates

* Fix tests

* build fix

* Added basic tests for relevant method in OkHttpJsonApiClient & NearbyController

* Added NearbyParentFragmentPresenter Tests

* Added AdvancedQueryFragment Unit Tests

* Added more tests for NearbyParentFragmentPresenter

* Reset ContributionsFragment with Upstream

* Overload method loadNearbyPlaces for CustomQuery

* Added more tests

* Added more tests

* Fixed tests for NearbyParentFragmentPresenter
This commit is contained in:
Ashish 2021-12-13 21:11:33 +05:30 committed by GitHub
parent bd00ce2071
commit a0ff15cdc2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 823 additions and 47 deletions

View file

@ -0,0 +1,89 @@
package fr.free.nrw.commons
import com.google.gson.Gson
import com.nhaarman.mockitokotlin2.any
import com.nhaarman.mockitokotlin2.verify
import fr.free.nrw.commons.explore.depictions.DepictsClient
import fr.free.nrw.commons.location.LatLng
import fr.free.nrw.commons.mwapi.OkHttpJsonApiClient
import okhttp3.Call
import okhttp3.HttpUrl
import okhttp3.OkHttpClient
import okhttp3.Response
import org.junit.Before
import org.junit.Test
import org.mockito.Mock
import org.mockito.Mockito
import org.mockito.MockitoAnnotations
import java.lang.Exception
class OkHttpJsonApiClientTests {
@Mock
lateinit var okhttpClient: OkHttpClient
@Mock
lateinit var depictsClient: DepictsClient
@Mock
lateinit var wikiMediaToolforgeUrl: HttpUrl
@Mock
lateinit var wikiMediaTestToolforgeUrl: HttpUrl
var sparqlQueryUrl: String = "https://www.testqparql.com"
var campaignsUrl: String = "https://www.testcampaignsurl.com"
@Mock
lateinit var gson: Gson
@Mock
lateinit var latLng: LatLng
private lateinit var okHttpJsonApiClient: OkHttpJsonApiClient
@Mock
lateinit var call: Call
@Mock
lateinit var response: Response
@Before
fun setUp() {
MockitoAnnotations.initMocks(this)
okHttpJsonApiClient = OkHttpJsonApiClient(
okhttpClient,
depictsClient,
wikiMediaToolforgeUrl,
wikiMediaTestToolforgeUrl,
sparqlQueryUrl,
campaignsUrl,
gson
)
Mockito.`when`(okhttpClient.newCall(any())).thenReturn(call)
Mockito.`when`(call.execute()).thenReturn(response)
}
@Test
fun testGetNearbyPlacesCustomQuery() {
Mockito.`when`(response.message()).thenReturn("test")
try {
okHttpJsonApiClient.getNearbyPlaces(latLng, "test", 10.0, true, "test")
} catch (e: Exception) {
assert(e.message.equals("test"))
}
verify(okhttpClient).newCall(any())
verify(call).execute()
}
@Test
fun testGetNearbyPlaces() {
Mockito.`when`(response.message()).thenReturn("test")
try {
okHttpJsonApiClient.getNearbyPlaces(latLng, "test", 10.0, true)
} catch (e: Exception) {
assert(e.message.equals("test"))
}
verify(okhttpClient).newCall(any())
verify(call).execute()
}
}

View file

@ -0,0 +1,129 @@
package fr.free.nrw.commons.nearby
import android.content.Context
import android.os.Bundle
import android.os.Looper
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.appcompat.widget.AppCompatButton
import androidx.appcompat.widget.AppCompatEditText
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentTransaction
import com.nhaarman.mockitokotlin2.any
import com.nhaarman.mockitokotlin2.verify
import fr.free.nrw.commons.R
import fr.free.nrw.commons.TestAppAdapter
import fr.free.nrw.commons.TestCommonsApplication
import fr.free.nrw.commons.contributions.MainActivity
import fr.free.nrw.commons.nearby.fragments.AdvanceQueryFragment
import org.junit.Assert
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.Mockito
import org.mockito.MockitoAnnotations
import org.robolectric.Robolectric
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.Shadows
import org.robolectric.annotation.Config
import org.robolectric.annotation.LooperMode
import org.wikipedia.AppAdapter
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [21], application = TestCommonsApplication::class)
@LooperMode(LooperMode.Mode.PAUSED)
class AdvanceQueryFragmentUnitTests {
private lateinit var context: Context
private lateinit var activity: MainActivity
private lateinit var fragment: AdvanceQueryFragment
private lateinit var viewGroup: ViewGroup
@Mock
private lateinit var layoutInflater: LayoutInflater
@Mock
private lateinit var callback: AdvanceQueryFragment.Callback
@Mock
private lateinit var view: View
@Mock
private lateinit var etQuery: AppCompatEditText
@Mock
private lateinit var btnReset: AppCompatButton
@Mock
private lateinit var btnApply: AppCompatButton
@Mock
private lateinit var bundle: Bundle
@Before
fun setUp() {
MockitoAnnotations.initMocks(this)
AppAdapter.set(TestAppAdapter())
context = RuntimeEnvironment.application.applicationContext
activity = Robolectric.buildActivity(MainActivity::class.java).create().get()
viewGroup = FrameLayout(context)
Mockito.`when`(bundle.getString("query")).thenReturn("test")
fragment = AdvanceQueryFragment()
fragment.callback = callback
fragment.arguments = bundle
val fragmentManager: FragmentManager = activity.supportFragmentManager
val fragmentTransaction: FragmentTransaction = fragmentManager.beginTransaction()
fragmentTransaction.add(fragment, null)
fragmentTransaction.commit()
Mockito.`when`(layoutInflater.inflate(R.layout.fragment_advance_query, viewGroup, false))
.thenReturn(view)
Mockito.`when`(view.findViewById<AppCompatEditText>(R.id.et_query)).thenReturn(etQuery)
Mockito.`when`(view.findViewById<AppCompatButton>(R.id.btn_apply)).thenReturn(btnApply)
Mockito.`when`(view.findViewById<AppCompatButton>(R.id.btn_reset)).thenReturn(btnReset)
}
@Test
@Throws(Exception::class)
fun checkFragmentNotNull() {
Shadows.shadowOf(Looper.getMainLooper()).idle()
Assert.assertNotNull(fragment)
}
@Test
fun testOnCreateView() {
Shadows.shadowOf(Looper.getMainLooper()).idle()
fragment.onCreateView(layoutInflater, viewGroup, bundle)
verify(layoutInflater).inflate(R.layout.fragment_advance_query, viewGroup, false)
}
@Test
fun testOnViewCreated() {
fragment.onCreateView(layoutInflater, viewGroup, bundle)
fragment.onViewCreated(view, bundle)
verify(etQuery).setText("test")
Mockito.`when`(btnReset.post(any())).thenAnswer {
it.getArgument(0, Runnable::class.java).run()
}
Mockito.`when`(btnApply.post(any())).thenAnswer {
it.getArgument(0, Runnable::class.java).run()
}
btnReset.performClick()
btnApply.performClick()
}
@Test
fun testHideKeyboard() {
fragment.hideKeyBoard()
}
}

View file

@ -0,0 +1,74 @@
package fr.free.nrw.commons.nearby
import com.nhaarman.mockitokotlin2.any
import com.nhaarman.mockitokotlin2.anyOrNull
import com.nhaarman.mockitokotlin2.eq
import fr.free.nrw.commons.location.LatLng
import org.junit.Before
import org.junit.Test
import org.mockito.Mock
import org.mockito.Mockito
import org.mockito.MockitoAnnotations
import java.util.*
class NearbyControllerTest {
@Mock
private lateinit var nearbyPlaces: NearbyPlaces
@Mock
lateinit var searchLatLong: LatLng
@Mock
lateinit var currentLatLng: LatLng
var customQuery: String = "test"
private lateinit var nearbyController: NearbyController
@Before
fun setUp() {
MockitoAnnotations.initMocks(this)
nearbyController = NearbyController(nearbyPlaces)
}
@Test
fun testLoadAttractionsForLocationTest() {
Mockito.`when`(nearbyPlaces.radiusExpander(any(), any(), any(), any(), any()))
.thenReturn(Collections.emptyList())
nearbyController.loadAttractionsFromLocation(
searchLatLong,
currentLatLng,
false,
false,
true,
customQuery
)
Mockito.verify(nearbyPlaces).radiusExpander(
eq(currentLatLng),
any(String::class.java),
eq(false),
eq(true),
eq(customQuery)
)
}
@Test
fun testLoadAttractionsForLocationTestNoQuery() {
Mockito.`when`(nearbyPlaces.radiusExpander(any(), any(), any(), any(), anyOrNull()))
.thenReturn(Collections.emptyList())
nearbyController.loadAttractionsFromLocation(
searchLatLong,
currentLatLng,
false,
false,
true
)
Mockito.verify(nearbyPlaces).radiusExpander(
eq(currentLatLng),
any(String::class.java),
eq(false),
eq(true),
eq(null)
)
}
fun <T> any(type: Class<T>): T = Mockito.any<T>(type)
}

View file

@ -15,6 +15,7 @@ import org.mockito.ArgumentMatchers
import org.mockito.Mock
import org.mockito.Mockito
import org.mockito.MockitoAnnotations
import java.util.*
/**
* The unit test class for NearbyParentFragmentPresenter
@ -32,6 +33,8 @@ class NearbyParentFragmentPresenterTest {
internal lateinit var selectedLabels: List<Label>
@Mock
internal lateinit var marker: Marker
@Mock
internal lateinit var nearbyPlaces: NearbyPlaces
private lateinit var nearbyPresenter: NearbyParentFragmentPresenter
private lateinit var mapboxCameraTarget: com.mapbox.mapboxsdk.geometry.LatLng
@ -392,6 +395,14 @@ class NearbyParentFragmentPresenterTest {
assertFalse(hasNearbyHandledBackPress)
}
@Test
fun testBackButtonClickedWhenAdvancedFragmentIsVisible() {
whenever(nearbyParentFragmentView.isAdvancedQueryFragmentVisible()).thenReturn(true)
val hasNearbyHandledBackPress = nearbyPresenter.backButtonClicked()
verify(nearbyParentFragmentView).showHideAdvancedQueryFragment(false)
assertTrue(hasNearbyHandledBackPress)
}
@Test
fun testMarkerUnselected() {
nearbyPresenter.markerUnselected()
@ -429,6 +440,50 @@ class NearbyParentFragmentPresenterTest {
verify(nearbyParentFragmentView).recenterMap(latestLocation)
}
@Test
fun testOnLocationChangeTypeCustomQuery() {
nearbyPresenter.setAdvancedQuery("Point(17.865 82.343)\"")
whenever(nearbyParentFragmentView.isNetworkConnectionEstablished()).thenReturn(true)
whenever(nearbyParentFragmentView.getLastLocation()).thenReturn(latestLocation)
nearbyPresenter.updateMapAndList(LocationChangeType.CUSTOM_QUERY)
expectMapAndListUpdate()
verify(nearbyParentFragmentView).setProgressBarVisibility(true)
verify(nearbyParentFragmentView).populatePlaces(any(), any())
}
@Test
fun testOnLocationChangeTypeCustomQueryInvalidQuery() {
whenever(nearbyParentFragmentView.isNetworkConnectionEstablished).thenReturn(true)
whenever(nearbyParentFragmentView.lastLocation).thenReturn(latestLocation)
nearbyPresenter.setAdvancedQuery("")
nearbyPresenter.updateMapAndList(LocationChangeType.CUSTOM_QUERY)
expectMapAndListUpdate()
nearbyPresenter.setAdvancedQuery("Point(")
nearbyPresenter.updateMapAndList(LocationChangeType.CUSTOM_QUERY)
expectMapAndListUpdate()
}
@Test
fun testOnLocationChangeTypeCustomQueryUnParsableQuery() {
whenever(nearbyParentFragmentView.isNetworkConnectionEstablished).thenReturn(true)
whenever(nearbyParentFragmentView.lastLocation).thenReturn(latestLocation)
nearbyPresenter.setAdvancedQuery("Point()\"")
nearbyPresenter.updateMapAndList(LocationChangeType.CUSTOM_QUERY)
expectMapAndListUpdate()
whenever(nearbyParentFragmentView.isNetworkConnectionEstablished).thenReturn(true)
whenever(nearbyParentFragmentView.lastLocation).thenReturn(latestLocation)
nearbyPresenter.setAdvancedQuery("Point(ab)\"")
nearbyPresenter.updateMapAndList(LocationChangeType.CUSTOM_QUERY)
expectMapAndListUpdate()
whenever(nearbyParentFragmentView.isNetworkConnectionEstablished).thenReturn(true)
whenever(nearbyParentFragmentView.lastLocation).thenReturn(latestLocation)
nearbyPresenter.setAdvancedQuery("Point(ab ab)\"")
nearbyPresenter.updateMapAndList(LocationChangeType.CUSTOM_QUERY)
expectMapAndListUpdate()
}
@Test
fun testOnCameraMoveWhenSearchLocationNull() {
NearbyController.latestSearchLocation = null
@ -456,4 +511,27 @@ class NearbyParentFragmentPresenterTest {
verify(nearbyParentFragmentView).isNetworkConnectionEstablished()
verifyZeroInteractions(nearbyParentFragmentView)
}
@Test
fun testSetAdvancedQuery(){
nearbyPresenter.setAdvancedQuery("test")
}
@Test
fun testUpdateMapMarkers(){
var nearbyPlacesInfo = NearbyController(nearbyPlaces).NearbyPlacesInfo()
nearbyPlacesInfo.boundaryCoordinates= arrayOf()
nearbyPlacesInfo.curLatLng=latestLocation
nearbyPlacesInfo.searchLatLng=latestLocation
nearbyPlacesInfo.placeList = null
whenever(bookmarkLocationsDao.allBookmarksLocations).thenReturn(Collections.emptyList())
nearbyPresenter.updateMapMarkers(nearbyPlacesInfo, marker, true)
Mockito.verify(nearbyParentFragmentView).updateMapMarkers(any(), eq(marker))
Mockito.verify(nearbyParentFragmentView).addCurrentLocationMarker(latestLocation)
Mockito.verify(nearbyParentFragmentView).updateMapToTrackPosition(latestLocation)
Mockito.verify(nearbyParentFragmentView).setProgressBarVisibility(false)
Mockito.verify(nearbyParentFragmentView).centerMapToPosition(latestLocation)
}
}

View file

@ -0,0 +1,40 @@
package fr.free.nrw.commons.nearby
import com.nhaarman.mockitokotlin2.any
import com.nhaarman.mockitokotlin2.eq
import com.nhaarman.mockitokotlin2.times
import com.nhaarman.mockitokotlin2.verify
import fr.free.nrw.commons.location.LatLng
import fr.free.nrw.commons.mwapi.OkHttpJsonApiClient
import org.junit.Before
import org.junit.Test
import org.mockito.Mock
import org.mockito.MockitoAnnotations
class NearbyPlacesTest {
@Mock
private lateinit var okHttpJsonApiClient: OkHttpJsonApiClient
@Mock
private lateinit var currentLatLong: LatLng
private lateinit var nearbyPlaces: NearbyPlaces
@Before
fun setUp() {
MockitoAnnotations.initMocks(this)
nearbyPlaces = NearbyPlaces(okHttpJsonApiClient)
}
@Test
fun testRadiusExpander() {
nearbyPlaces.radiusExpander(currentLatLong, "test", true, true, "test")
verify(okHttpJsonApiClient, times(5)).getNearbyPlaces(
eq(currentLatLong),
eq("test"),
any(),
eq(true),
eq("test")
)
}
}