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 9dcc1da
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 50 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,42 @@
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,19 @@
* limitations under the License.
*/

package com.google.android.gnd.inject;
package com.google.android.gnd.rx;

import com.google.android.gnd.persistence.local.LocalDataStoreModule;
import com.google.android.gnd.persistence.local.LocalDataStoreTest;
import dagger.Component;
import dagger.Binds;
import dagger.Module;
import dagger.hilt.InstallIn;
import dagger.hilt.android.components.ApplicationComponent;
import javax.inject.Singleton;

@Singleton
@Component(modules = {TestApplicationModule.class, LocalDataStoreModule.class})
public interface TestComponent {
@InstallIn(ApplicationComponent.class)
@Module
public abstract class SchedulersModule {

void inject(LocalDataStoreTest localDataStoreTest);
@Binds
@Singleton
abstract Schedulers schedulers(RxSchedulers rxSchedulers);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,18 @@

package com.google.android.gnd;

import android.app.Application;
import com.google.android.gnd.TestScheduler;
import com.google.android.gnd.rx.Schedulers;
import dagger.Binds;
import dagger.Module;
import dagger.hilt.InstallIn;
import dagger.hilt.android.components.ApplicationComponent;
import javax.inject.Singleton;

/**
* We don't want to initialize the GndApplicationComponent for Robolectric tests. Instead, tests
* might want to use their own modules with testing-purpose or mocked dependencies.
*/
public class TestApplication extends Application {}
@InstallIn(ApplicationComponent.class)
@Module
abstract class TestSchedulersModule {
@Binds
@Singleton
abstract Schedulers schedulers(TestScheduler testScheduler);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
import androidx.arch.core.executor.testing.InstantTaskExecutorRule;
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.model.Mutation;
import com.google.android.gnd.model.Project;
import com.google.android.gnd.model.User;
Expand All @@ -47,8 +45,13 @@
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.rx.SchedulersModule;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
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;
Expand All @@ -62,10 +65,15 @@
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;


@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 +209,7 @@ private static void assertEquivalent(ObservationMutation mutation, Observation o

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

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,21 @@
* limitations under the License.
*/

package com.google.android.gnd.inject;
package com.google.android.gnd.persistence.local;

import android.content.Context;
import androidx.room.Room;
import androidx.test.core.app.ApplicationProvider;
import com.google.android.gnd.TestScheduler;
import com.google.android.gnd.persistence.local.room.LocalDatabase;
import com.google.android.gnd.rx.Schedulers;
import dagger.Binds;
import dagger.Module;
import dagger.Provides;
import dagger.hilt.InstallIn;
import dagger.hilt.android.components.ApplicationComponent;
import javax.inject.Singleton;

@InstallIn(ApplicationComponent.class)
@Module
abstract class TestApplicationModule {

abstract class TestLocalDatabaseModule {
@Provides
static Context contextProvider() {
return ApplicationProvider.getApplicationContext();
Expand All @@ -39,11 +38,7 @@ static Context contextProvider() {
@Singleton
static LocalDatabase localDatabaseProvider(Context context) {
return Room.inMemoryDatabaseBuilder(context, LocalDatabase.class)
.allowMainThreadQueries()
.build();
.allowMainThreadQueries()
.build();
}

@Binds
@Singleton
abstract Schedulers schedulers(TestScheduler testScheduler);
}
}

0 comments on commit 9dcc1da

Please sign in to comment.