|
|
|
@ -17,17 +17,13 @@
|
|
|
|
|
package com.google.samples.apps.nowinandroid.core.datastore.test
|
|
|
|
|
|
|
|
|
|
import androidx.datastore.core.DataStore
|
|
|
|
|
import androidx.datastore.core.DataStoreFactory
|
|
|
|
|
import com.google.samples.apps.nowinandroid.core.datastore.UserPreferences
|
|
|
|
|
import com.google.samples.apps.nowinandroid.core.datastore.UserPreferencesSerializer
|
|
|
|
|
import com.google.samples.apps.nowinandroid.core.datastore.di.DataStoreModule
|
|
|
|
|
import com.google.samples.apps.nowinandroid.core.network.di.ApplicationScope
|
|
|
|
|
import dagger.Module
|
|
|
|
|
import dagger.Provides
|
|
|
|
|
import dagger.hilt.components.SingletonComponent
|
|
|
|
|
import dagger.hilt.testing.TestInstallIn
|
|
|
|
|
import kotlinx.coroutines.CoroutineScope
|
|
|
|
|
import org.junit.rules.TemporaryFolder
|
|
|
|
|
import javax.inject.Singleton
|
|
|
|
|
|
|
|
|
|
@Module
|
|
|
|
@ -36,26 +32,9 @@ import javax.inject.Singleton
|
|
|
|
|
replaces = [DataStoreModule::class],
|
|
|
|
|
)
|
|
|
|
|
internal object TestDataStoreModule {
|
|
|
|
|
|
|
|
|
|
@Provides
|
|
|
|
|
@Singleton
|
|
|
|
|
fun providesUserPreferencesDataStore(
|
|
|
|
|
@ApplicationScope scope: CoroutineScope,
|
|
|
|
|
userPreferencesSerializer: UserPreferencesSerializer,
|
|
|
|
|
tmpFolder: TemporaryFolder,
|
|
|
|
|
): DataStore<UserPreferences> =
|
|
|
|
|
tmpFolder.testUserPreferencesDataStore(
|
|
|
|
|
coroutineScope = scope,
|
|
|
|
|
userPreferencesSerializer = userPreferencesSerializer,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun TemporaryFolder.testUserPreferencesDataStore(
|
|
|
|
|
coroutineScope: CoroutineScope,
|
|
|
|
|
userPreferencesSerializer: UserPreferencesSerializer = UserPreferencesSerializer(),
|
|
|
|
|
) = DataStoreFactory.create(
|
|
|
|
|
serializer = userPreferencesSerializer,
|
|
|
|
|
scope = coroutineScope,
|
|
|
|
|
) {
|
|
|
|
|
newFile("user_preferences_test.pb")
|
|
|
|
|
serializer: UserPreferencesSerializer,
|
|
|
|
|
): DataStore<UserPreferences> = InMemoryDataStore(serializer.defaultValue)
|
|
|
|
|
}
|
|
|
|
|