Skip to content

Commit

Permalink
Migrate robolectric tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dturner committed Jun 29, 2020
1 parent 544079b commit 9e30688
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 126 deletions.
5 changes: 5 additions & 0 deletions gnd/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ android {
includeAndroidResources = true
}
}
hilt {
enableTransformForLocalTests = true
}
}


Expand Down Expand Up @@ -209,6 +212,8 @@ dependencies {
testImplementation 'androidx.test:core:1.2.0'
testImplementation 'org.robolectric:robolectric:4.3.1'
testImplementation 'android.arch.core:core-testing:1.1.1'
testImplementation 'com.google.dagger:hilt-android-testing:2.28-alpha'
testAnnotationProcessor 'com.google.dagger:hilt-android-compiler:2.28-alpha'

androidTestImplementation('androidx.test.espresso:espresso-core:3.1.0-alpha4', {
exclude group: 'com.android.support', module: 'support-annotations'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,14 @@
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import androidx.room.Room;
import androidx.work.WorkManager;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gnd.persistence.local.room.LocalDatabase;
import com.google.android.gnd.persistence.remote.RemoteDataStore;
import com.google.android.gnd.persistence.remote.RemoteStorageManager;
import com.google.android.gnd.persistence.remote.firestore.FirestoreDataStore;
import com.google.android.gnd.persistence.remote.firestore.FirestoreStorageManager;
import com.google.android.gnd.persistence.remote.firestore.FirestoreUuidGenerator;
import com.google.android.gnd.persistence.uuid.OfflineUuidGenerator;
import com.google.android.gnd.rx.RxSchedulers;
import com.google.android.gnd.rx.Schedulers;
import com.google.android.gnd.ui.common.ViewModelModule;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.FirebaseFirestoreSettings;
Expand All @@ -41,23 +37,18 @@
import dagger.Provides;
import dagger.hilt.InstallIn;
import dagger.hilt.android.components.ApplicationComponent;
import dagger.hilt.android.qualifiers.ApplicationContext;
import javax.inject.Singleton;

@InstallIn(ApplicationComponent.class)
@Module(includes = {ViewModelModule.class})
abstract class GndApplicationModule {
public abstract class GndApplicationModule {
private static final String SHARED_PREFERENCES_NAME = "shared_prefs";

/** Provides the Firestore implementation of remote data store. */
@Binds
@Singleton
abstract RemoteDataStore remoteDataStore(FirestoreDataStore ds);

@Binds
@Singleton
abstract Schedulers schedulers(RxSchedulers rxSchedulers);

/** Provides the Firestore implementation of offline unique id generation. */
@Binds
@Singleton
Expand Down Expand Up @@ -110,13 +101,4 @@ static FirebaseFirestore firebaseFirestore(FirebaseFirestoreSettings settings) {
static StorageReference firebaseStorageReference() {
return FirebaseStorage.getInstance().getReference();
}

@Provides
@Singleton
static LocalDatabase localDatabase(@ApplicationContext Context context) {
return Room.databaseBuilder(context, LocalDatabase.class, Config.DB_NAME)
// TODO(#128): Disable before official release.
.fallbackToDestructiveMigration()
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.google.android.gnd.persistence.local;

import android.content.Context;
import androidx.room.Room;
import com.google.android.gnd.Config;
import com.google.android.gnd.persistence.local.room.LocalDatabase;
import dagger.Module;
import dagger.Provides;
import dagger.hilt.InstallIn;
import dagger.hilt.android.components.ApplicationComponent;
import dagger.hilt.android.qualifiers.ApplicationContext;
import javax.inject.Singleton;

@InstallIn(ApplicationComponent.class)
@Module
public abstract class LocalDatabaseModule {

@Provides
@Singleton
static LocalDatabase localDatabase(@ApplicationContext Context context) {
return Room.databaseBuilder(context, LocalDatabase.class, Config.DB_NAME)
// TODO(#128): Disable before official release.
.fallbackToDestructiveMigration()
.build();
}
}
16 changes: 16 additions & 0 deletions gnd/src/main/java/com/google/android/gnd/rx/SchedulersModule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.google.android.gnd.rx;

import dagger.Binds;
import dagger.Module;
import dagger.hilt.InstallIn;
import dagger.hilt.android.components.ApplicationComponent;
import javax.inject.Singleton;

@InstallIn(ApplicationComponent.class)
@Module
public abstract class SchedulersModule {

@Binds
@Singleton
abstract Schedulers schedulers(RxSchedulers rxSchedulers);
}
25 changes: 0 additions & 25 deletions gnd/src/test/java/com/google/android/gnd/TestApplication.java

This file was deleted.

This file was deleted.

29 changes: 0 additions & 29 deletions gnd/src/test/java/com/google/android/gnd/inject/TestComponent.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@
import static com.google.common.truth.Truth.assertThat;
import static org.hamcrest.Matchers.samePropertyValuesAs;

import android.content.Context;
import androidx.arch.core.executor.testing.InstantTaskExecutorRule;
import androidx.room.Room;
import androidx.test.core.app.ApplicationProvider;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gnd.TestApplication;
import com.google.android.gnd.inject.DaggerTestComponent;
import com.google.android.gnd.GndApplicationModule;
import com.google.android.gnd.TestScheduler;
import com.google.android.gnd.model.Mutation;
import com.google.android.gnd.model.Project;
import com.google.android.gnd.model.User;
Expand All @@ -47,13 +50,28 @@
import com.google.android.gnd.model.observation.ResponseDelta;
import com.google.android.gnd.model.observation.ResponseMap;
import com.google.android.gnd.model.observation.TextResponse;
import com.google.android.gnd.persistence.local.room.LocalDatabase;
import com.google.android.gnd.rx.Schedulers;
import com.google.android.gnd.rx.SchedulersModule;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import dagger.Binds;
import dagger.Module;
import dagger.Provides;
import dagger.hilt.InstallIn;
import dagger.hilt.android.components.ApplicationComponent;
import dagger.hilt.android.qualifiers.ApplicationContext;
import dagger.hilt.android.testing.BindValue;
import dagger.hilt.android.testing.HiltAndroidRule;
import dagger.hilt.android.testing.HiltAndroidTest;
import dagger.hilt.android.testing.HiltTestApplication;
import dagger.hilt.android.testing.UninstallModules;
import io.reactivex.subscribers.TestSubscriber;
import java.util.AbstractCollection;
import java.util.Date;
import java8.util.Optional;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.hamcrest.MatcherAssert;
import org.junit.Before;
import org.junit.Rule;
Expand All @@ -62,10 +80,40 @@
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;


@InstallIn(ApplicationComponent.class)
@Module
abstract class TestLocalDatabaseModule {
@Provides
static Context contextProvider() {
return ApplicationProvider.getApplicationContext();
}

@Provides
@Singleton
static LocalDatabase localDatabaseProvider(Context context) {
return Room.inMemoryDatabaseBuilder(context, LocalDatabase.class)
.allowMainThreadQueries()
.build();
}
}

@InstallIn(ApplicationComponent.class)
@Module
abstract class TestSchedulersModule {
@Binds
@Singleton
abstract Schedulers schedulers(TestScheduler testScheduler);
}

@HiltAndroidTest
@UninstallModules({SchedulersModule.class,LocalDatabaseModule.class})
@Config(application = HiltTestApplication.class)
@RunWith(RobolectricTestRunner.class)
@Config(application = TestApplication.class)
public class LocalDataStoreTest {

@Rule public HiltAndroidRule hiltRule = new HiltAndroidRule(this);

private static final User TEST_USER =
User.builder().setId("user id").setEmail("[email protected]").setDisplayName("user 1").build();

Expand Down Expand Up @@ -201,7 +249,7 @@ private static void assertEquivalent(ObservationMutation mutation, Observation o

@Before
public void setUp() {
DaggerTestComponent.create().inject(this);
hiltRule.inject();
}

@Test
Expand Down

0 comments on commit 9e30688

Please sign in to comment.