Skip to content

Commit a85897c

Browse files
committed
merged two method and tests updates
1 parent 0dc9a85 commit a85897c

File tree

2 files changed

+70
-36
lines changed

2 files changed

+70
-36
lines changed

app/src/main/java/fr/free/nrw/commons/nearby/fragments/NearbyParentFragment.java

+13-32
Original file line numberDiff line numberDiff line change
@@ -486,22 +486,30 @@ public void onResume() {
486486
if (isResumed() && ((MainActivity)getActivity()).activeFragment == ActiveFragment.NEARBY) {
487487
if(!isPermissionDenied && !applicationKvStore.getBoolean("doNotAskForLocationPermission", false)){
488488
if (!locationManager.isGPSProviderEnabled()) {
489-
startMapWithoutGPS();
489+
startMapWithCondition("Without GPS");
490490
} else {
491491
startTheMap();
492492
}
493493
}else{
494-
startMapWithoutPermission();
494+
startMapWithCondition("Without Permission");
495495
}
496496
}
497497
}
498498

499499
/**
500-
* Starts the map without GPS
501-
* By default it points to 51.50550,-0.07520 this coordinates
500+
* Starts the map without GPS and without permission
501+
* By default it points to 51.50550,-0.07520 coordinates, other than that it points to the
502+
* last known location which can be get by the key "LastLocation" from applicationKvStore
503+
*
504+
* @param condition : for which condition the map should start
502505
*/
503-
private void startMapWithoutGPS() {
506+
private void startMapWithCondition(final String condition) {
504507
mapView.onStart();
508+
509+
if (condition.equals("Without Permission")) {
510+
applicationKvStore.putBoolean("doNotAskForLocationPermission", true);
511+
}
512+
505513
final CameraPosition position;
506514
if (applicationKvStore.getString("LastLocation")!=null) {
507515
final String[] locationLatLng
@@ -531,33 +539,6 @@ private void startMapWithoutGPS() {
531539
}
532540
}
533541

534-
private void startMapWithoutPermission() {
535-
mapView.onStart();
536-
537-
applicationKvStore.putBoolean("doNotAskForLocationPermission", true);
538-
final CameraPosition position;
539-
if(applicationKvStore.getString("LastLocation")!=null) { // Checking for last searched location
540-
String[] locationLatLng = applicationKvStore.getString("LastLocation").split(",");
541-
lastKnownLocation = new fr.free.nrw.commons.location.LatLng(Double.valueOf(locationLatLng[0]), Double.valueOf(locationLatLng[1]), 1f);
542-
position = new CameraPosition.Builder()
543-
.target(LocationUtils.commonsLatLngToMapBoxLatLng(lastKnownLocation))
544-
.zoom(ZOOM_LEVEL)
545-
.build();
546-
}else {
547-
lastKnownLocation = new fr.free.nrw.commons.location.LatLng(51.50550,-0.07520,1f);
548-
position = new CameraPosition.Builder()
549-
.target(LocationUtils.commonsLatLngToMapBoxLatLng(lastKnownLocation))
550-
.zoom(ZOOM_OUT)
551-
.build();
552-
}
553-
if(mapBox != null){
554-
mapBox.moveCamera(CameraUpdateFactory.newCameraPosition(position));
555-
addOnCameraMoveListener();
556-
presenter.onMapReady();
557-
removeCurrentLocationMarker();
558-
}
559-
}
560-
561542
private void registerNetworkReceiver() {
562543
if (getActivity() != null) {
563544
getActivity().registerReceiver(broadcastReceiver, intentFilter);

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

+57-4
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,11 @@ class NearbyParentFragmentUnitTest {
5454
@Throws(Exception::class)
5555
fun `Start map without gps test when last location known`() {
5656
val method: Method = NearbyParentFragment::class.java.getDeclaredMethod(
57-
"startMapWithoutGPS"
57+
"startMapWithCondition",
58+
String::class.java
5859
)
5960
method.isAccessible = true
60-
method.invoke(fragment)
61+
method.invoke(fragment, "Without GPS")
6162
verify(mapView, times(1)).onStart()
6263
verify(applicationKvStore, times(1)).getString("LastLocation")
6364
verify(presenter, times(1)).onMapReady()
@@ -77,10 +78,11 @@ class NearbyParentFragmentUnitTest {
7778
fun `Start map without gps test when last location unknown`() {
7879
`when`(applicationKvStore.getString("LastLocation")).thenReturn("23.76,56.876")
7980
val method: Method = NearbyParentFragment::class.java.getDeclaredMethod(
80-
"startMapWithoutGPS"
81+
"startMapWithCondition",
82+
String::class.java
8183
)
8284
method.isAccessible = true
83-
method.invoke(fragment)
85+
method.invoke(fragment, "Without GPS")
8486
verify(mapView, times(1)).onStart()
8587
verify(applicationKvStore, times(2)).getString("LastLocation")
8688
verify(presenter, times(1)).onMapReady()
@@ -94,4 +96,55 @@ class NearbyParentFragmentUnitTest {
9496
verify(mapBox, times(1))
9597
.moveCamera(CameraUpdateFactory.newCameraPosition(position))
9698
}
99+
100+
@Test
101+
@Throws(Exception::class)
102+
fun `Start map without location permission test when last location known`() {
103+
val method: Method = NearbyParentFragment::class.java.getDeclaredMethod(
104+
"startMapWithCondition",
105+
String::class.java
106+
)
107+
method.isAccessible = true
108+
method.invoke(fragment, "Without Permission")
109+
verify(mapView, times(1)).onStart()
110+
verify(applicationKvStore, times(1)).getString("LastLocation")
111+
verify(applicationKvStore, times(1))
112+
.putBoolean("doNotAskForLocationPermission", true)
113+
verify(presenter, times(1)).onMapReady()
114+
val position = CameraPosition.Builder()
115+
.target(LatLng(
116+
51.50550,
117+
-0.07520, 0.0
118+
))
119+
.zoom(0.0)
120+
.build()
121+
verify(mapBox, times(1))
122+
.moveCamera(CameraUpdateFactory.newCameraPosition(position))
123+
}
124+
125+
@Test
126+
@Throws(Exception::class)
127+
fun `Start map without location permission test when last location unknown`() {
128+
`when`(applicationKvStore.getString("LastLocation")).thenReturn("23.76,56.876")
129+
val method: Method = NearbyParentFragment::class.java.getDeclaredMethod(
130+
"startMapWithCondition",
131+
String::class.java
132+
)
133+
method.isAccessible = true
134+
method.invoke(fragment, "Without Permission")
135+
verify(mapView, times(1)).onStart()
136+
verify(applicationKvStore, times(2)).getString("LastLocation")
137+
verify(applicationKvStore, times(1))
138+
.putBoolean("doNotAskForLocationPermission", true)
139+
verify(presenter, times(1)).onMapReady()
140+
val position = CameraPosition.Builder()
141+
.target(LatLng(
142+
23.76,
143+
56.876, 0.0
144+
))
145+
.zoom(14.0)
146+
.build()
147+
verify(mapBox, times(1))
148+
.moveCamera(CameraUpdateFactory.newCameraPosition(position))
149+
}
97150
}

0 commit comments

Comments
 (0)