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"));
    }

}
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()));
    }

}
Fix NearbyActivityTest on timing issues