Skip to content

Commit

Permalink
Workaround for KSP support on testFixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonMarquis committed Jun 23, 2024
1 parent 7d85256 commit 8f377a3
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import dagger.hilt.testing.TestInstallIn
components = [SingletonComponent::class],
replaces = [DataModule::class],
)
internal interface TestDataModule {
interface TestDataModule {
@Binds
fun bindsTopicRepository(it: FakeTopicsRepository): TopicsRepository

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import javax.inject.Inject
/**
* Fake implementation of the [RecentSearchRepository]
*/
internal class FakeRecentSearchRepository @Inject constructor() : RecentSearchRepository {
class FakeRecentSearchRepository @Inject constructor() : RecentSearchRepository {
override suspend fun insertOrReplaceRecentSearch(searchQuery: String) = Unit

override fun getRecentSearchQueries(limit: Int): Flow<List<RecentSearchQuery>> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import javax.inject.Inject
/**
* Fake implementation of the [SearchContentsRepository]
*/
internal class FakeSearchContentsRepository @Inject constructor() : SearchContentsRepository {
class FakeSearchContentsRepository @Inject constructor() : SearchContentsRepository {

override suspend fun populateFtsData() = Unit
override fun searchContents(searchQuery: String): Flow<SearchResult> = flowOf()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import javax.inject.Inject
* This allows us to run the app with fake data, without needing an internet connection or working
* backend.
*/
internal class FakeTopicsRepository @Inject constructor(
class FakeTopicsRepository @Inject constructor(
@Dispatcher(IO) private val ioDispatcher: CoroutineDispatcher,
private val datasource: DemoNiaNetworkDataSource,
) : TopicsRepository {
Expand Down
3 changes: 3 additions & 0 deletions core/testing/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,7 @@ dependencies {
implementation(libs.kotlinx.datetime)
implementation(projects.core.common)
implementation(projects.core.designsystem)
implementation(testFixtures(projects.core.data))
implementation(testFixtures(projects.core.datastore))
implementation(testFixtures(projects.sync))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright 2024 The Android Open Source Project
*
* 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.samples.apps.nowinandroid.core.testing.di

import androidx.datastore.core.DataStore
import com.google.samples.apps.nowinandroid.core.data.di.DataModule
import com.google.samples.apps.nowinandroid.core.data.di.TestDataModule
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.datastore.di.TestDataStoreModule
import com.google.samples.apps.nowinandroid.core.network.di.ApplicationScope
import com.google.samples.apps.nowinandroid.core.sync.di.TestSyncModule
import com.google.samples.apps.nowinandroid.sync.di.SyncModule
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

/**
* KSP is currently not supported on Android testFixtures ([issuetracker](https://issuetracker.google.com/issues/259523353#comment32)).
*
* Including [TestDataStoreModule] in [Module.includes] leads to an unexpected compilation error (maybe due to datastore-proto and KSP ordering, certainly related to the initial issue).
*
* ```
* > Task :app:hiltJavaCompileDemoDebugAndroidTest FAILED
* nowinandroid\app\build\generated\hilt\component_sources\demoDebugAndroidTest\dagger\hilt\android\internal\testing\root\DaggerNavigationTest_HiltComponents_SingletonC.java:38: error: cannot find symbol
* import com.google.samples.apps.nowinandroid.core.datastore.di.TestDataStoreModule_ProvidesUserPreferencesDataStoreFactory;
* ^
* symbol: class TestDataStoreModule_ProvidesUserPreferencesDataStoreFactory
* location: package com.google.samples.apps.nowinandroid.core.datastore.di
* ```
*
* Therefore, a [providesUserPreferencesDataStore] delegate is added in this module.
*/
@Module(includes = [TestDataModule::class, TestSyncModule::class])
@TestInstallIn(
components = [SingletonComponent::class],
replaces = [DataModule::class, SyncModule::class, DataStoreModule::class],
)
internal object TestingModule {
@Provides
@Singleton
fun providesUserPreferencesDataStore(
@ApplicationScope scope: CoroutineScope,
userPreferencesSerializer: UserPreferencesSerializer,
tmpFolder: TemporaryFolder,
): DataStore<UserPreferences> = TestDataStoreModule.providesUserPreferencesDataStore(
scope,
userPreferencesSerializer,
tmpFolder,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import dagger.hilt.testing.TestInstallIn
components = [SingletonComponent::class],
replaces = [SyncModule::class],
)
internal interface TestSyncModule {
interface TestSyncModule {
@Binds
fun bindsSyncManager(it: NeverSyncingSyncManager): SyncManager

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOf
import javax.inject.Inject

internal class NeverSyncingSyncManager @Inject constructor() : SyncManager {
class NeverSyncingSyncManager @Inject constructor() : SyncManager {
override val isSyncing: Flow<Boolean> = flowOf(false)
override fun requestSync() = Unit
}

0 comments on commit 8f377a3

Please sign in to comment.