mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 12:23:58 +01:00
commit
b21d1e3fbb
6 changed files with 65 additions and 44 deletions
|
|
@ -94,7 +94,12 @@ android {
|
|||
}
|
||||
|
||||
sourceSets {
|
||||
// use kotlin only in tests (for now)
|
||||
test.java.srcDirs += 'src/test/kotlin'
|
||||
|
||||
// use main assets and resources in test
|
||||
test.assets.srcDirs += 'src/main/assets'
|
||||
test.resources.srcDirs += 'src/main/resoures'
|
||||
}
|
||||
|
||||
buildTypes {
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ public class NearbyPlaces {
|
|||
|
||||
public NearbyPlaces() {
|
||||
try {
|
||||
wikidataQuery = FileUtils.readFromResource("/assets/queries/nearby_query.rq");
|
||||
wikidataQuery = FileUtils.readFromResource("/queries/nearby_query.rq");
|
||||
Timber.v(wikidataQuery);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
|
|
|
|||
|
|
@ -4,8 +4,10 @@ import android.os.Environment;
|
|||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
|
||||
|
|
@ -16,16 +18,18 @@ public class FileUtils {
|
|||
/**
|
||||
* Read and return the content of a resource file as string.
|
||||
*
|
||||
* @param fileName asset file's path (e.g. "/assets/queries/nearby_query.rq")
|
||||
* @param fileName asset file's path (e.g. "/queries/nearby_query.rq")
|
||||
* @return the content of the file
|
||||
*/
|
||||
public static String readFromResource(String fileName) throws IOException {
|
||||
StringBuilder buffer = new StringBuilder();
|
||||
BufferedReader reader = null;
|
||||
try {
|
||||
reader = new BufferedReader(
|
||||
new InputStreamReader(
|
||||
CommonsApplication.class.getResourceAsStream(fileName), "UTF-8"));
|
||||
InputStream inputStream = FileUtils.class.getResourceAsStream(fileName);
|
||||
if (inputStream == null) {
|
||||
throw new FileNotFoundException(fileName);
|
||||
}
|
||||
reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
buffer.append(line + "\n");
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
package fr.free.nrw.commons;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.support.v4.util.LruCache;
|
||||
|
||||
|
|
@ -54,47 +55,36 @@ public class TestCommonsApplication extends CommonsApplication {
|
|||
@Override
|
||||
public void onCreate() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
super.onCreate();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected RefWatcher setupLeakCanary() {
|
||||
// No leakcanary in unit tests.
|
||||
return RefWatcher.DISABLED;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonsApplicationComponent injector() {
|
||||
if (mockApplicationComponent == null) {
|
||||
mockApplicationComponent = DaggerCommonsApplicationComponent.builder()
|
||||
.appModule(new CommonsApplicationModule(this) {
|
||||
@Override
|
||||
public AccountUtil providesAccountUtil() {
|
||||
public AccountUtil providesAccountUtil(Context context) {
|
||||
return accountUtil;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SharedPreferences providesApplicationSharedPreferences() {
|
||||
public SharedPreferences providesApplicationSharedPreferences(Context context) {
|
||||
return appSharedPreferences;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SharedPreferences providesDefaultSharedPreferences() {
|
||||
public SharedPreferences providesDefaultSharedPreferences(Context context) {
|
||||
return defaultSharedPreferences;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SharedPreferences providesOtherSharedPreferences() {
|
||||
public SharedPreferences providesOtherSharedPreferences(Context context) {
|
||||
return otherSharedPreferences;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UploadController providesUploadController(SessionManager sessionManager, SharedPreferences sharedPreferences) {
|
||||
public UploadController providesUploadController(Context context, SessionManager sessionManager, SharedPreferences sharedPreferences) {
|
||||
return uploadController;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SessionManager providesSessionManager(MediaWikiApi mediaWikiApi) {
|
||||
public SessionManager providesSessionManager(Context context, MediaWikiApi mediaWikiApi) {
|
||||
return sessionManager;
|
||||
}
|
||||
|
||||
|
|
@ -104,7 +94,7 @@ public class TestCommonsApplication extends CommonsApplication {
|
|||
}
|
||||
|
||||
@Override
|
||||
public LocationServiceManager provideLocationServiceManager() {
|
||||
public LocationServiceManager provideLocationServiceManager(Context context) {
|
||||
return locationServiceManager;
|
||||
}
|
||||
|
||||
|
|
@ -114,7 +104,7 @@ public class TestCommonsApplication extends CommonsApplication {
|
|||
}
|
||||
|
||||
@Override
|
||||
public DBOpenHelper provideDBOpenHelper() {
|
||||
public DBOpenHelper provideDBOpenHelper(Context context) {
|
||||
return dbOpenHelper;
|
||||
}
|
||||
|
||||
|
|
@ -129,7 +119,13 @@ public class TestCommonsApplication extends CommonsApplication {
|
|||
}
|
||||
}).build();
|
||||
}
|
||||
return mockApplicationComponent;
|
||||
super.onCreate();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected RefWatcher setupLeakCanary() {
|
||||
// No leakcanary in unit tests.
|
||||
return RefWatcher.DISABLED;
|
||||
}
|
||||
|
||||
public AccountUtil getAccountUtil() {
|
||||
|
|
|
|||
|
|
@ -1,53 +1,69 @@
|
|||
package fr.free.nrw.commons.nearby;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.location.LocationManager;
|
||||
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.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.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);
|
||||
private static final LatLng ST_LOUIS_MO_LAT_LNG
|
||||
= new LatLng(38.627003, -90.199402, 0);
|
||||
|
||||
private ActivityController<NearbyActivity> activityController;
|
||||
@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);
|
||||
|
||||
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();
|
||||
when(locationManager.getLastLocation()).thenReturn(ST_LOUIS_MO_LAT_LNG);
|
||||
when(locationManager.isProviderEnabled()).thenReturn(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void activityLaunchesAndShowsList() {
|
||||
activityController.create().resume().visible();
|
||||
assertNotNull(nearbyActivity.getSupportFragmentManager().findFragmentByTag("NearbyListFragment"));
|
||||
public void pressRefreshAndShowList() {
|
||||
MenuItem refresh = shadowOf(nearbyActivity).getOptionsMenu().findItem(R.id.action_refresh);
|
||||
nearbyActivity.onOptionsItemSelected(refresh);
|
||||
assertNotNull(nearbyActivity.getSupportFragmentManager().findFragmentByTag(
|
||||
NearbyListFragment.class.getSimpleName()));
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue