package fr.free.nrw.commons.nearby;

import android.view.MenuItem;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

import fr.free.nrw.commons.BuildConfig;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.TestCommonsApplication;
import fr.free.nrw.commons.location.LatLng;
import fr.free.nrw.commons.location.LocationServiceManager;
import io.reactivex.android.plugins.RxAndroidPlugins;
import io.reactivex.plugins.RxJavaPlugins;
import io.reactivex.schedulers.Schedulers;

import static junit.framework.Assert.assertNotNull;
import static org.mockito.Mockito.when;
import static org.robolectric.Shadows.shadowOf;

@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21, application = TestCommonsApplication.class)
public class NearbyActivityTest {

    private static final LatLng ST_LOUIS_MO_LAT_LNG
            = new LatLng(38.627003, -90.199402, 0);

    @Mock
    private LocationServiceManager locationManager;

    @InjectMocks
    private NearbyActivity nearbyActivity;

    @Before
    public void setUp() throws Exception {
        // ensure waiting all threads to complete
        RxJavaPlugins.setIoSchedulerHandler(
                scheduler -> Schedulers.trampoline());
        RxJavaPlugins.setComputationSchedulerHandler(
                scheduler -> Schedulers.trampoline());
        RxJavaPlugins.setNewThreadSchedulerHandler(
                scheduler -> Schedulers.trampoline());
        RxAndroidPlugins.setInitMainThreadSchedulerHandler(
                scheduler -> Schedulers.trampoline());

        nearbyActivity = Robolectric.setupActivity(NearbyActivity.class);

        // replace methods and fields with mocks
        MockitoAnnotations.initMocks(this);
        when(locationManager.getLastLocation()).thenReturn(ST_LOUIS_MO_LAT_LNG);
        when(locationManager.isProviderEnabled()).thenReturn(true);
    }

    @Test
    public void pressRefreshAndShowList() {
        MenuItem refresh = shadowOf(nearbyActivity).getOptionsMenu().findItem(R.id.action_refresh);
        nearbyActivity.onOptionsItemSelected(refresh);
        assertNotNull(nearbyActivity.getSupportFragmentManager().findFragmentByTag(
                NearbyListFragment.class.getSimpleName()));
    }

}
@@ -30,16 +30,135 @@
 earby;%0A%0A
+import android.content.Context;%0Aimport android.support.v4.app.Fragment;%0Aimport android.support.v7.widget.RecyclerView;%0A
 import a
@@ -179,16 +179,16 @@
 nuItem;%0A
-
 %0Aimport 
@@ -482,16 +482,72 @@
 onfig;%0A%0A
+import edu.emory.mathcs.backport.java.util.Collections;%0A
 import f
@@ -922,23 +922,63 @@
 tic 
+org.
 junit.
-framework
+Assert.assertNotNull;%0Aimport static org.junit
 .Ass
@@ -984,32 +984,69 @@
 sert.assertN
-otN
 ull;%0A
+import static org.mockito.Matchers.any;%0A
 import stati
@@ -1391,16 +1391,200 @@
 402, 0);
+%0A    private static final Place AIRPORT = new Place(%0A            %22name%22, Place.Label.AIRPORT,%0A            %22desc%22, null,%0A            new LatLng(38.6270, -90.1994, 0),%0A            null);
 %0A%0A    @M
@@ -1640,16 +1640,74 @@
 nager;%0A%0A
+    @Mock%0A    private NearbyController nearbyController;%0A%0A
     @Inj
@@ -2613,16 +2613,179 @@
 (true);%0A
+        when(nearbyController.loadAttractionsFromLocation(any(LatLng.class), any(Context.class)))%0A                .thenReturn(Collections.singletonList(AIRPORT));%0A
     %7D%0A%0A 
@@ -2992,24 +2992,25 @@
 fresh);%0A
+%0A
         
 assertNo
@@ -3005,22 +3005,38 @@
     
-assertNotNull(
+Fragment nearbyListFragment = 
 near
@@ -3073,16 +3073,33 @@
 anager()
+%0A                
 .findFra
@@ -3109,33 +3109,16 @@
 ntByTag(
-%0A                
 NearbyLi
@@ -3149,16 +3149,324 @@
 leName()
+);%0A        assertNotNull(nearbyListFragment);%0A%0A        // one element (AIRPORT) exists in the list%0A        RecyclerView view = nearbyListFragment.getView().findViewById(R.id.listView);%0A        assertNotNull(view.findViewHolderForAdapterPosition(0));%0A        assertNull(view.findViewHolderForAdapterPosition(1
 ));%0A    

Add assertions to ensure nearby places appear in the list