package fr.free.nrw.commons.nearby;

import android.app.Activity;
import android.location.LocationManager;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.Shadows;
import org.robolectric.android.controller.ActivityController;
import org.robolectric.annotation.Config;
import org.robolectric.shadow.api.Shadow;
import org.robolectric.shadows.ShadowLocationManager;

import fr.free.nrw.commons.BuildConfig;
import fr.free.nrw.commons.TestCommonsApplication;
import fr.free.nrw.commons.location.LatLng;

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

@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);

    private ActivityController<NearbyActivity> activityController;
    private NearbyActivity nearbyActivity;

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);

        TestCommonsApplication application = (TestCommonsApplication) RuntimeEnvironment.application;
        when(application.getLocationServiceManager().getLastLocation()).thenReturn(ST_LOUIS_MO_LAT_LNG);
        when(application.getLocationServiceManager().isProviderEnabled()).thenReturn(true);

        activityController = Robolectric.buildActivity(NearbyActivity.class);
        nearbyActivity = activityController.get();
    }

    @Test
    public void activityLaunchesAndShowsList() {
        activityController.create().resume().visible();
        assertNotNull(nearbyActivity.getSupportFragmentManager().findFragmentByTag("NearbyListFragment"));
    }

}
@@ -49,61 +49,21 @@
 oid.
-app.Activity;%0Aimport android.location.LocationManager
+view.MenuItem
 ;%0A%0Ai
@@ -142,16 +142,73 @@
 unWith;%0A
+import org.mockito.InjectMocks;%0Aimport org.mockito.Mock;%0A
 import o
@@ -347,258 +347,25 @@
 ric.
-RuntimeEnvironment;%0Aimport org.robolectric.Shadows;%0Aimport org.robolectric.android.controller.ActivityController;%0Aimport org.robolectric.annotation.Config;%0Aimport org.robolectric.shadow.api.Shadow;%0Aimport org.robolectric.shadows.ShadowLocationManager
+annotation.Config
 ;%0A%0Ai
@@ -403,16 +403,46 @@
 Config;%0A
+import fr.free.nrw.commons.R;%0A
 import f
@@ -527,16 +527,216 @@
 .LatLng;
+%0Aimport fr.free.nrw.commons.location.LocationServiceManager;%0Aimport io.reactivex.android.plugins.RxAndroidPlugins;%0Aimport io.reactivex.plugins.RxJavaPlugins;%0Aimport io.reactivex.schedulers.Schedulers;
 %0A%0Aimport
@@ -820,16 +820,64 @@
 to.when;
+%0Aimport static org.robolectric.Shadows.shadowOf;
 %0A%0A@RunWi
@@ -1087,16 +1087,28 @@
 _LAT_LNG
+%0A           
  = new L
@@ -1149,70 +1149,83 @@
     
-private ActivityController%3CNearbyActivity%3E activityController;
+@Mock%0A    private LocationServiceManager locationManager;%0A%0A    @InjectMocks
 %0A   
@@ -1332,53 +1332,158 @@
     
-MockitoAnnotations.initMocks(this
+// ensure waiting all threads to complete%0A        RxJavaPlugins.setIoSchedulerHandler(%0A                scheduler -%3E Schedulers.trampoline()
 );%0A
-%0A
         
 Test
@@ -1482,154 +1482,528 @@
     
-TestCommonsApplication application = (TestCommonsApplication) RuntimeEnvironment.application;%0A        when(application.getLocationService
+RxJavaPlugins.setComputationSchedulerHandler(%0A                scheduler -%3E Schedulers.trampoline());%0A        RxJavaPlugins.setNewThreadSchedulerHandler(%0A                scheduler -%3E Schedulers.trampoline());%0A        RxAndroidPlugins.setInitMainThreadSchedulerHandler(%0A                scheduler -%3E Schedulers.trampoline());%0A%0A        nearbyActivity = Robolectric.setupActivity(NearbyActivity.class);%0A%0A        // replace methods and fields with mocks%0A        MockitoAnnotations.initMocks(this);%0A        when(location
 Manager
-()
 .get
@@ -2068,47 +2068,23 @@
 hen(
-application.getLocationService
+location
 Manager
-()
 .isP
@@ -2123,203 +2123,168 @@
 e);%0A
-%0A
     
+%7D%0A%0A
     
-activityController = Robolectric.buildActivity(NearbyActivity.class);%0A        nearbyActivity = activityController.get();%0A    %7D%0A%0A    @Test%0A    public void 
+@Test%0A    public void pressRefreshAndShowList() %7B%0A        MenuItem refresh = shadowOf(nearbyActivity).getOptionsMenu().findItem(R.id.
 acti
-vityLaunchesAndShowsList() %7B
+on_refresh);
 %0A   
@@ -2292,53 +2292,52 @@
     
-a
+nearbyA
 ctivity
-Controller.create().resume().visible(
+.onOptionsItemSelected(refresh
 );%0A 
@@ -2418,17 +2418,33 @@
 ntByTag(
-%22
+%0A                
 NearbyLi
@@ -2457,9 +2457,30 @@
 ment
-%22
+.class.getSimpleName()
 ));%0A

Fix NearbyActivityTest on timing issues