Add assertions to ensure nearby places appear in the list

This commit is contained in:
Yusuke Matsubara 2018-01-25 20:04:48 +09:00
parent b21d1e3fbb
commit 4e1275ffd4

View file

@ -1,5 +1,8 @@
package fr.free.nrw.commons.nearby; package fr.free.nrw.commons.nearby;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.view.MenuItem; import android.view.MenuItem;
import org.junit.Before; import org.junit.Before;
@ -12,6 +15,7 @@ import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner; import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config; import org.robolectric.annotation.Config;
import edu.emory.mathcs.backport.java.util.Collections;
import fr.free.nrw.commons.BuildConfig; import fr.free.nrw.commons.BuildConfig;
import fr.free.nrw.commons.R; import fr.free.nrw.commons.R;
import fr.free.nrw.commons.TestCommonsApplication; import fr.free.nrw.commons.TestCommonsApplication;
@ -21,7 +25,9 @@ import io.reactivex.android.plugins.RxAndroidPlugins;
import io.reactivex.plugins.RxJavaPlugins; import io.reactivex.plugins.RxJavaPlugins;
import io.reactivex.schedulers.Schedulers; import io.reactivex.schedulers.Schedulers;
import static junit.framework.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import static org.robolectric.Shadows.shadowOf; import static org.robolectric.Shadows.shadowOf;
@ -31,10 +37,18 @@ public class NearbyActivityTest {
private static final LatLng ST_LOUIS_MO_LAT_LNG private static final LatLng ST_LOUIS_MO_LAT_LNG
= new LatLng(38.627003, -90.199402, 0); = new LatLng(38.627003, -90.199402, 0);
private static final Place AIRPORT = new Place(
"name", Place.Label.AIRPORT,
"desc", null,
new LatLng(38.6270, -90.1994, 0),
null);
@Mock @Mock
private LocationServiceManager locationManager; private LocationServiceManager locationManager;
@Mock
private NearbyController nearbyController;
@InjectMocks @InjectMocks
private NearbyActivity nearbyActivity; private NearbyActivity nearbyActivity;
@ -56,14 +70,23 @@ public class NearbyActivityTest {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
when(locationManager.getLastLocation()).thenReturn(ST_LOUIS_MO_LAT_LNG); when(locationManager.getLastLocation()).thenReturn(ST_LOUIS_MO_LAT_LNG);
when(locationManager.isProviderEnabled()).thenReturn(true); when(locationManager.isProviderEnabled()).thenReturn(true);
when(nearbyController.loadAttractionsFromLocation(any(LatLng.class), any(Context.class)))
.thenReturn(Collections.singletonList(AIRPORT));
} }
@Test @Test
public void pressRefreshAndShowList() { public void pressRefreshAndShowList() {
MenuItem refresh = shadowOf(nearbyActivity).getOptionsMenu().findItem(R.id.action_refresh); MenuItem refresh = shadowOf(nearbyActivity).getOptionsMenu().findItem(R.id.action_refresh);
nearbyActivity.onOptionsItemSelected(refresh); nearbyActivity.onOptionsItemSelected(refresh);
assertNotNull(nearbyActivity.getSupportFragmentManager().findFragmentByTag(
NearbyListFragment.class.getSimpleName())); Fragment nearbyListFragment = nearbyActivity.getSupportFragmentManager()
.findFragmentByTag(NearbyListFragment.class.getSimpleName());
assertNotNull(nearbyListFragment);
// one element (AIRPORT) exists in the list
RecyclerView view = nearbyListFragment.getView().findViewById(R.id.listView);
assertNotNull(view.findViewHolderForAdapterPosition(0));
assertNull(view.findViewHolderForAdapterPosition(1));
} }
} }