mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-27 04:43:54 +01:00
Merge pull request #990 from psh/nearby-unit-test
Created a Robolectric unit test replacement for the (failing) NearbyActivity integration test
This commit is contained in:
commit
078feb9531
11 changed files with 100 additions and 39 deletions
|
|
@ -58,6 +58,7 @@ dependencies {
|
||||||
|
|
||||||
testImplementation 'junit:junit:4.12'
|
testImplementation 'junit:junit:4.12'
|
||||||
testImplementation 'org.robolectric:robolectric:3.4'
|
testImplementation 'org.robolectric:robolectric:3.4'
|
||||||
|
testImplementation 'org.mockito:mockito-all:1.10.19'
|
||||||
|
|
||||||
testImplementation 'com.squareup.okhttp3:mockwebserver:3.8.1'
|
testImplementation 'com.squareup.okhttp3:mockwebserver:3.8.1'
|
||||||
androidTestImplementation 'com.squareup.okhttp3:mockwebserver:3.8.1'
|
androidTestImplementation 'com.squareup.okhttp3:mockwebserver:3.8.1'
|
||||||
|
|
|
||||||
|
|
@ -1,30 +0,0 @@
|
||||||
package fr.free.nrw.commons;
|
|
||||||
|
|
||||||
import android.support.test.espresso.assertion.ViewAssertions;
|
|
||||||
import android.support.test.filters.LargeTest;
|
|
||||||
import android.support.test.rule.ActivityTestRule;
|
|
||||||
import android.support.test.runner.AndroidJUnit4;
|
|
||||||
|
|
||||||
import org.junit.Rule;
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
|
|
||||||
import fr.free.nrw.commons.nearby.NearbyActivity;
|
|
||||||
|
|
||||||
import static android.support.test.espresso.Espresso.onView;
|
|
||||||
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
|
|
||||||
import static android.support.test.espresso.matcher.ViewMatchers.withText;
|
|
||||||
|
|
||||||
@LargeTest
|
|
||||||
@RunWith(AndroidJUnit4.class)
|
|
||||||
public class NearbyActivityTest {
|
|
||||||
@Rule
|
|
||||||
public final ActivityTestRule<NearbyActivity> nearby =
|
|
||||||
new ActivityTestRule<>(NearbyActivity.class);
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testActivityLaunch() {
|
|
||||||
onView(withText(R.string.title_activity_nearby))
|
|
||||||
.check(ViewAssertions.matches(isDisplayed()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -29,6 +29,7 @@ import java.io.IOException;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
|
import dagger.android.AndroidInjector;
|
||||||
import dagger.android.DispatchingAndroidInjector;
|
import dagger.android.DispatchingAndroidInjector;
|
||||||
import dagger.android.HasActivityInjector;
|
import dagger.android.HasActivityInjector;
|
||||||
import fr.free.nrw.commons.auth.AccountUtil;
|
import fr.free.nrw.commons.auth.AccountUtil;
|
||||||
|
|
@ -272,7 +273,7 @@ public class CommonsApplication extends Application implements HasActivityInject
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public DispatchingAndroidInjector<Activity> activityInjector() {
|
public AndroidInjector<Activity> activityInjector() {
|
||||||
return dispatchingActivityInjector;
|
return dispatchingActivityInjector;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -338,7 +338,7 @@ public class NearbyActivity extends NavigationBaseActivity implements LocationUp
|
||||||
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
|
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
|
||||||
Fragment fragment = new NearbyMapFragment();
|
Fragment fragment = new NearbyMapFragment();
|
||||||
fragment.setArguments(bundle);
|
fragment.setArguments(bundle);
|
||||||
fragmentTransaction.replace(R.id.container, fragment);
|
fragmentTransaction.replace(R.id.container, fragment, fragment.getClass().getSimpleName());
|
||||||
fragmentTransaction.commitAllowingStateLoss();
|
fragmentTransaction.commitAllowingStateLoss();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -349,7 +349,7 @@ public class NearbyActivity extends NavigationBaseActivity implements LocationUp
|
||||||
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
|
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
|
||||||
Fragment fragment = new NearbyListFragment();
|
Fragment fragment = new NearbyListFragment();
|
||||||
fragment.setArguments(bundle);
|
fragment.setArguments(bundle);
|
||||||
fragmentTransaction.replace(R.id.container, fragment);
|
fragmentTransaction.replace(R.id.container, fragment, fragment.getClass().getSimpleName());
|
||||||
fragmentTransaction.commitAllowingStateLoss();
|
fragmentTransaction.commitAllowingStateLoss();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ import static org.hamcrest.CoreMatchers.is;
|
||||||
import static org.junit.Assert.assertThat;
|
import static org.junit.Assert.assertThat;
|
||||||
|
|
||||||
@RunWith(RobolectricTestRunner.class)
|
@RunWith(RobolectricTestRunner.class)
|
||||||
@Config(constants = BuildConfig.class, sdk = 21)
|
@Config(constants = BuildConfig.class, sdk = 21, application = TestCommonsApplication.class)
|
||||||
public class MediaTest {
|
public class MediaTest {
|
||||||
@Test
|
@Test
|
||||||
public void displayTitleShouldStripExtension() {
|
public void displayTitleShouldStripExtension() {
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ import static org.hamcrest.CoreMatchers.is;
|
||||||
import static org.junit.Assert.assertThat;
|
import static org.junit.Assert.assertThat;
|
||||||
|
|
||||||
@RunWith(RobolectricTestRunner.class)
|
@RunWith(RobolectricTestRunner.class)
|
||||||
@Config(constants = BuildConfig.class, sdk = 21)
|
@Config(constants = BuildConfig.class, sdk = 21, application = TestCommonsApplication.class)
|
||||||
public class NearbyControllerTest {
|
public class NearbyControllerTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ import static org.hamcrest.CoreMatchers.is;
|
||||||
import static org.junit.Assert.assertThat;
|
import static org.junit.Assert.assertThat;
|
||||||
|
|
||||||
@RunWith(RobolectricTestRunner.class)
|
@RunWith(RobolectricTestRunner.class)
|
||||||
@Config(constants = BuildConfig.class, sdk = 21)
|
@Config(constants = BuildConfig.class, sdk = 21, application = TestCommonsApplication.class)
|
||||||
public class PageTitleTest {
|
public class PageTitleTest {
|
||||||
@Test
|
@Test
|
||||||
public void displayTextShouldNotBeUnderscored() {
|
public void displayTextShouldNotBeUnderscored() {
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,40 @@
|
||||||
package fr.free.nrw.commons;
|
package fr.free.nrw.commons;
|
||||||
|
|
||||||
|
import android.app.Activity;
|
||||||
|
|
||||||
import com.squareup.leakcanary.RefWatcher;
|
import com.squareup.leakcanary.RefWatcher;
|
||||||
|
|
||||||
// This class is automatically discovered by Robolectric
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.MockitoAnnotations;
|
||||||
|
|
||||||
|
import dagger.android.AndroidInjector;
|
||||||
|
import fr.free.nrw.commons.nearby.NearbyPlaces;
|
||||||
|
|
||||||
public class TestCommonsApplication extends CommonsApplication {
|
public class TestCommonsApplication extends CommonsApplication {
|
||||||
|
@Mock
|
||||||
|
private AndroidInjector<Activity> mockInjector;
|
||||||
|
@Mock
|
||||||
|
private NearbyPlaces mockNearbyPlaces;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onCreate() {
|
||||||
|
super.onCreate();
|
||||||
|
MockitoAnnotations.initMocks(this);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected RefWatcher setupLeakCanary() {
|
protected RefWatcher setupLeakCanary() {
|
||||||
// No leakcanary in unit tests.
|
// No leakcanary in unit tests.
|
||||||
return RefWatcher.DISABLED;
|
return RefWatcher.DISABLED;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public AndroidInjector<Activity> activityInjector() {
|
||||||
|
return mockInjector;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public synchronized NearbyPlaces getNearbyPlaces() {
|
||||||
|
return mockNearbyPlaces;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,7 @@ import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import fr.free.nrw.commons.BuildConfig;
|
import fr.free.nrw.commons.BuildConfig;
|
||||||
|
import fr.free.nrw.commons.TestCommonsApplication;
|
||||||
import io.reactivex.observers.TestObserver;
|
import io.reactivex.observers.TestObserver;
|
||||||
import okhttp3.HttpUrl;
|
import okhttp3.HttpUrl;
|
||||||
import okhttp3.mockwebserver.MockResponse;
|
import okhttp3.mockwebserver.MockResponse;
|
||||||
|
|
@ -28,7 +29,7 @@ import static org.junit.Assert.assertFalse;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
@RunWith(RobolectricTestRunner.class)
|
@RunWith(RobolectricTestRunner.class)
|
||||||
@Config(constants = BuildConfig.class, sdk = 21)
|
@Config(constants = BuildConfig.class, sdk = 21, application = TestCommonsApplication.class)
|
||||||
public class ApacheHttpClientMediaWikiApiTest {
|
public class ApacheHttpClientMediaWikiApiTest {
|
||||||
|
|
||||||
private ApacheHttpClientMediaWikiApi testObject;
|
private ApacheHttpClientMediaWikiApi testObject;
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,59 @@
|
||||||
|
package fr.free.nrw.commons.nearby;
|
||||||
|
|
||||||
|
import android.app.Activity;
|
||||||
|
import android.support.v4.app.Fragment;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.Mockito;
|
||||||
|
import org.mockito.MockitoAnnotations;
|
||||||
|
import org.robolectric.Robolectric;
|
||||||
|
import org.robolectric.RobolectricTestRunner;
|
||||||
|
import org.robolectric.RuntimeEnvironment;
|
||||||
|
import org.robolectric.android.controller.ActivityController;
|
||||||
|
import org.robolectric.annotation.Config;
|
||||||
|
|
||||||
|
import dagger.android.AndroidInjector;
|
||||||
|
import fr.free.nrw.commons.BuildConfig;
|
||||||
|
import fr.free.nrw.commons.TestCommonsApplication;
|
||||||
|
import fr.free.nrw.commons.location.LatLng;
|
||||||
|
import fr.free.nrw.commons.location.LocationServiceManager;
|
||||||
|
|
||||||
|
import static junit.framework.Assert.assertNotNull;
|
||||||
|
import static org.mockito.Matchers.isA;
|
||||||
|
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);
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
LocationServiceManager mockLocationManager;
|
||||||
|
|
||||||
|
private ActivityController<NearbyActivity> activityController;
|
||||||
|
private NearbyActivity nearbyActivity;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() throws Exception {
|
||||||
|
MockitoAnnotations.initMocks(this);
|
||||||
|
|
||||||
|
AndroidInjector<Activity> injector = ((TestCommonsApplication) RuntimeEnvironment.application).activityInjector();
|
||||||
|
Mockito.doNothing().when(injector).inject(isA(NearbyActivity.class));
|
||||||
|
when(mockLocationManager.getLastLocation()).thenReturn(ST_LOUIS_MO_LAT_LNG);
|
||||||
|
|
||||||
|
activityController = Robolectric.buildActivity(NearbyActivity.class);
|
||||||
|
nearbyActivity = activityController.get();
|
||||||
|
nearbyActivity.locationManager = mockLocationManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void activityLaunchesAndShowsList() {
|
||||||
|
activityController.create().resume().visible();
|
||||||
|
assertNotNull(nearbyActivity.getSupportFragmentManager().findFragmentByTag("NearbyListFragment"));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -20,13 +20,14 @@ import 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.location.LatLng;
|
import fr.free.nrw.commons.location.LatLng;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
|
||||||
@RunWith(RobolectricTestRunner.class)
|
@RunWith(RobolectricTestRunner.class)
|
||||||
@Config(constants = BuildConfig.class, sdk = 21)
|
@Config(constants = BuildConfig.class, sdk = 21, application = TestCommonsApplication.class)
|
||||||
public class NearbyAdapterFactoryTest {
|
public class NearbyAdapterFactoryTest {
|
||||||
|
|
||||||
private static final Place PLACE = new Place("name", Place.Description.AIRPORT,
|
private static final Place PLACE = new Place("name", Place.Description.AIRPORT,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue