Change-Id: Id17d0316aa5db431448401a382e2a7629909b637refactor/remove_data_test_module
parent
bdcd41e14e
commit
3ad70e39e0
@ -1,26 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2022 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.data.test
|
|
||||||
|
|
||||||
import com.google.samples.apps.nowinandroid.core.data.util.NetworkMonitor
|
|
||||||
import kotlinx.coroutines.flow.Flow
|
|
||||||
import kotlinx.coroutines.flow.flowOf
|
|
||||||
import javax.inject.Inject
|
|
||||||
|
|
||||||
class AlwaysOnlineNetworkMonitor @Inject constructor() : NetworkMonitor {
|
|
||||||
override val isOnline: Flow<Boolean> = flowOf(true)
|
|
||||||
}
|
|
@ -1,71 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2022 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.data.test
|
|
||||||
|
|
||||||
import com.google.samples.apps.nowinandroid.core.data.di.DataModule
|
|
||||||
import com.google.samples.apps.nowinandroid.core.data.repository.NewsRepository
|
|
||||||
import com.google.samples.apps.nowinandroid.core.data.repository.RecentSearchRepository
|
|
||||||
import com.google.samples.apps.nowinandroid.core.data.repository.SearchContentsRepository
|
|
||||||
import com.google.samples.apps.nowinandroid.core.data.repository.TopicsRepository
|
|
||||||
import com.google.samples.apps.nowinandroid.core.data.repository.UserDataRepository
|
|
||||||
import com.google.samples.apps.nowinandroid.core.data.repository.fake.FakeNewsRepository
|
|
||||||
import com.google.samples.apps.nowinandroid.core.data.repository.fake.FakeRecentSearchRepository
|
|
||||||
import com.google.samples.apps.nowinandroid.core.data.repository.fake.FakeSearchContentsRepository
|
|
||||||
import com.google.samples.apps.nowinandroid.core.data.repository.fake.FakeTopicsRepository
|
|
||||||
import com.google.samples.apps.nowinandroid.core.data.repository.fake.FakeUserDataRepository
|
|
||||||
import com.google.samples.apps.nowinandroid.core.data.util.NetworkMonitor
|
|
||||||
import dagger.Binds
|
|
||||||
import dagger.Module
|
|
||||||
import dagger.hilt.components.SingletonComponent
|
|
||||||
import dagger.hilt.testing.TestInstallIn
|
|
||||||
|
|
||||||
@Module
|
|
||||||
@TestInstallIn(
|
|
||||||
components = [SingletonComponent::class],
|
|
||||||
replaces = [DataModule::class],
|
|
||||||
)
|
|
||||||
interface TestDataModule {
|
|
||||||
@Binds
|
|
||||||
fun bindsTopicRepository(
|
|
||||||
fakeTopicsRepository: FakeTopicsRepository,
|
|
||||||
): TopicsRepository
|
|
||||||
|
|
||||||
@Binds
|
|
||||||
fun bindsNewsResourceRepository(
|
|
||||||
fakeNewsRepository: FakeNewsRepository,
|
|
||||||
): NewsRepository
|
|
||||||
|
|
||||||
@Binds
|
|
||||||
fun bindsUserDataRepository(
|
|
||||||
userDataRepository: FakeUserDataRepository,
|
|
||||||
): UserDataRepository
|
|
||||||
|
|
||||||
@Binds
|
|
||||||
fun bindsRecentSearchRepository(
|
|
||||||
recentSearchRepository: FakeRecentSearchRepository,
|
|
||||||
): RecentSearchRepository
|
|
||||||
|
|
||||||
@Binds
|
|
||||||
fun bindsSearchContentsRepository(
|
|
||||||
searchContentsRepository: FakeSearchContentsRepository,
|
|
||||||
): SearchContentsRepository
|
|
||||||
|
|
||||||
@Binds
|
|
||||||
fun bindsNetworkMonitor(
|
|
||||||
networkMonitor: AlwaysOnlineNetworkMonitor,
|
|
||||||
): NetworkMonitor
|
|
||||||
}
|
|
@ -1,73 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2022 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.data.repository.fake
|
|
||||||
|
|
||||||
import com.google.samples.apps.nowinandroid.core.data.Synchronizer
|
|
||||||
import com.google.samples.apps.nowinandroid.core.data.model.asEntity
|
|
||||||
import com.google.samples.apps.nowinandroid.core.data.repository.NewsRepository
|
|
||||||
import com.google.samples.apps.nowinandroid.core.data.repository.NewsResourceQuery
|
|
||||||
import com.google.samples.apps.nowinandroid.core.database.model.NewsResourceEntity
|
|
||||||
import com.google.samples.apps.nowinandroid.core.database.model.asExternalModel
|
|
||||||
import com.google.samples.apps.nowinandroid.core.model.data.NewsResource
|
|
||||||
import com.google.samples.apps.nowinandroid.core.network.Dispatcher
|
|
||||||
import com.google.samples.apps.nowinandroid.core.network.NiaDispatchers.IO
|
|
||||||
import com.google.samples.apps.nowinandroid.core.network.fake.FakeNiaNetworkDataSource
|
|
||||||
import com.google.samples.apps.nowinandroid.core.network.model.NetworkNewsResource
|
|
||||||
import kotlinx.coroutines.CoroutineDispatcher
|
|
||||||
import kotlinx.coroutines.flow.Flow
|
|
||||||
import kotlinx.coroutines.flow.flow
|
|
||||||
import kotlinx.coroutines.flow.flowOn
|
|
||||||
import javax.inject.Inject
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Fake implementation of the [NewsRepository] that retrieves the news resources from a JSON String.
|
|
||||||
*
|
|
||||||
* This allows us to run the app with fake data, without needing an internet connection or working
|
|
||||||
* backend.
|
|
||||||
*/
|
|
||||||
class FakeNewsRepository @Inject constructor(
|
|
||||||
@Dispatcher(IO) private val ioDispatcher: CoroutineDispatcher,
|
|
||||||
private val datasource: FakeNiaNetworkDataSource,
|
|
||||||
) : NewsRepository {
|
|
||||||
|
|
||||||
override fun getNewsResources(
|
|
||||||
query: NewsResourceQuery,
|
|
||||||
): Flow<List<NewsResource>> =
|
|
||||||
flow {
|
|
||||||
emit(
|
|
||||||
datasource
|
|
||||||
.getNewsResources()
|
|
||||||
.filter { networkNewsResource ->
|
|
||||||
// Filter out any news resources which don't match the current query.
|
|
||||||
// If no query parameters (filterTopicIds or filterNewsIds) are specified
|
|
||||||
// then the news resource is returned.
|
|
||||||
listOfNotNull(
|
|
||||||
true,
|
|
||||||
query.filterNewsIds?.contains(networkNewsResource.id),
|
|
||||||
query.filterTopicIds?.let { filterTopicIds ->
|
|
||||||
networkNewsResource.topics.intersect(filterTopicIds).isNotEmpty()
|
|
||||||
},
|
|
||||||
)
|
|
||||||
.all(true::equals)
|
|
||||||
}
|
|
||||||
.map(NetworkNewsResource::asEntity)
|
|
||||||
.map(NewsResourceEntity::asExternalModel),
|
|
||||||
)
|
|
||||||
}.flowOn(ioDispatcher)
|
|
||||||
|
|
||||||
override suspend fun syncWith(synchronizer: Synchronizer) = true
|
|
||||||
}
|
|
@ -1,35 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2023 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.data.repository.fake
|
|
||||||
|
|
||||||
import com.google.samples.apps.nowinandroid.core.data.model.RecentSearchQuery
|
|
||||||
import com.google.samples.apps.nowinandroid.core.data.repository.RecentSearchRepository
|
|
||||||
import kotlinx.coroutines.flow.Flow
|
|
||||||
import kotlinx.coroutines.flow.flowOf
|
|
||||||
import javax.inject.Inject
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Fake implementation of the [RecentSearchRepository]
|
|
||||||
*/
|
|
||||||
class FakeRecentSearchRepository @Inject constructor() : RecentSearchRepository {
|
|
||||||
override suspend fun insertOrReplaceRecentSearch(searchQuery: String) { /* no-op */ }
|
|
||||||
|
|
||||||
override fun getRecentSearchQueries(limit: Int): Flow<List<RecentSearchQuery>> =
|
|
||||||
flowOf(emptyList())
|
|
||||||
|
|
||||||
override suspend fun clearRecentSearches() { /* no-op */ }
|
|
||||||
}
|
|
@ -1,33 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2023 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.data.repository.fake
|
|
||||||
|
|
||||||
import com.google.samples.apps.nowinandroid.core.data.repository.SearchContentsRepository
|
|
||||||
import com.google.samples.apps.nowinandroid.core.model.data.SearchResult
|
|
||||||
import kotlinx.coroutines.flow.Flow
|
|
||||||
import kotlinx.coroutines.flow.flowOf
|
|
||||||
import javax.inject.Inject
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Fake implementation of the [SearchContentsRepository]
|
|
||||||
*/
|
|
||||||
class FakeSearchContentsRepository @Inject constructor() : SearchContentsRepository {
|
|
||||||
|
|
||||||
override suspend fun populateFtsData() { /* no-op */ }
|
|
||||||
override fun searchContents(searchQuery: String): Flow<SearchResult> = flowOf()
|
|
||||||
override fun getSearchContentsCount(): Flow<Int> = flowOf(1)
|
|
||||||
}
|
|
@ -1,63 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2022 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.data.repository.fake
|
|
||||||
|
|
||||||
import com.google.samples.apps.nowinandroid.core.data.Synchronizer
|
|
||||||
import com.google.samples.apps.nowinandroid.core.data.repository.TopicsRepository
|
|
||||||
import com.google.samples.apps.nowinandroid.core.model.data.Topic
|
|
||||||
import com.google.samples.apps.nowinandroid.core.network.Dispatcher
|
|
||||||
import com.google.samples.apps.nowinandroid.core.network.NiaDispatchers.IO
|
|
||||||
import com.google.samples.apps.nowinandroid.core.network.fake.FakeNiaNetworkDataSource
|
|
||||||
import kotlinx.coroutines.CoroutineDispatcher
|
|
||||||
import kotlinx.coroutines.flow.Flow
|
|
||||||
import kotlinx.coroutines.flow.flow
|
|
||||||
import kotlinx.coroutines.flow.flowOn
|
|
||||||
import kotlinx.coroutines.flow.map
|
|
||||||
import javax.inject.Inject
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Fake implementation of the [TopicsRepository] that retrieves the topics from a JSON String, and
|
|
||||||
* uses a local DataStore instance to save and retrieve followed topic ids.
|
|
||||||
*
|
|
||||||
* This allows us to run the app with fake data, without needing an internet connection or working
|
|
||||||
* backend.
|
|
||||||
*/
|
|
||||||
class FakeTopicsRepository @Inject constructor(
|
|
||||||
@Dispatcher(IO) private val ioDispatcher: CoroutineDispatcher,
|
|
||||||
private val datasource: FakeNiaNetworkDataSource,
|
|
||||||
) : TopicsRepository {
|
|
||||||
override fun getTopics(): Flow<List<Topic>> = flow {
|
|
||||||
emit(
|
|
||||||
datasource.getTopics().map {
|
|
||||||
Topic(
|
|
||||||
id = it.id,
|
|
||||||
name = it.name,
|
|
||||||
shortDescription = it.shortDescription,
|
|
||||||
longDescription = it.longDescription,
|
|
||||||
url = it.url,
|
|
||||||
imageUrl = it.imageUrl,
|
|
||||||
)
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}.flowOn(ioDispatcher)
|
|
||||||
|
|
||||||
override fun getTopic(id: String): Flow<Topic> {
|
|
||||||
return getTopics().map { it.first { topic -> topic.id == id } }
|
|
||||||
}
|
|
||||||
|
|
||||||
override suspend fun syncWith(synchronizer: Synchronizer) = true
|
|
||||||
}
|
|
@ -1,68 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2022 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.data.repository.fake
|
|
||||||
|
|
||||||
import com.google.samples.apps.nowinandroid.core.data.repository.UserDataRepository
|
|
||||||
import com.google.samples.apps.nowinandroid.core.datastore.NiaPreferencesDataSource
|
|
||||||
import com.google.samples.apps.nowinandroid.core.model.data.DarkThemeConfig
|
|
||||||
import com.google.samples.apps.nowinandroid.core.model.data.ThemeBrand
|
|
||||||
import com.google.samples.apps.nowinandroid.core.model.data.UserData
|
|
||||||
import kotlinx.coroutines.flow.Flow
|
|
||||||
import javax.inject.Inject
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Fake implementation of the [UserDataRepository] that returns hardcoded user data.
|
|
||||||
*
|
|
||||||
* This allows us to run the app with fake data, without needing an internet connection or working
|
|
||||||
* backend.
|
|
||||||
*/
|
|
||||||
class FakeUserDataRepository @Inject constructor(
|
|
||||||
private val niaPreferencesDataSource: NiaPreferencesDataSource,
|
|
||||||
) : UserDataRepository {
|
|
||||||
|
|
||||||
override val userData: Flow<UserData> =
|
|
||||||
niaPreferencesDataSource.userData
|
|
||||||
|
|
||||||
override suspend fun setFollowedTopicIds(followedTopicIds: Set<String>) =
|
|
||||||
niaPreferencesDataSource.setFollowedTopicIds(followedTopicIds)
|
|
||||||
|
|
||||||
override suspend fun toggleFollowedTopicId(followedTopicId: String, followed: Boolean) =
|
|
||||||
niaPreferencesDataSource.toggleFollowedTopicId(followedTopicId, followed)
|
|
||||||
|
|
||||||
override suspend fun updateNewsResourceBookmark(newsResourceId: String, bookmarked: Boolean) {
|
|
||||||
niaPreferencesDataSource.toggleNewsResourceBookmark(newsResourceId, bookmarked)
|
|
||||||
}
|
|
||||||
|
|
||||||
override suspend fun setNewsResourceViewed(newsResourceId: String, viewed: Boolean) =
|
|
||||||
niaPreferencesDataSource.setNewsResourceViewed(newsResourceId, viewed)
|
|
||||||
|
|
||||||
override suspend fun setThemeBrand(themeBrand: ThemeBrand) {
|
|
||||||
niaPreferencesDataSource.setThemeBrand(themeBrand)
|
|
||||||
}
|
|
||||||
|
|
||||||
override suspend fun setDarkThemeConfig(darkThemeConfig: DarkThemeConfig) {
|
|
||||||
niaPreferencesDataSource.setDarkThemeConfig(darkThemeConfig)
|
|
||||||
}
|
|
||||||
|
|
||||||
override suspend fun setDynamicColorPreference(useDynamicColor: Boolean) {
|
|
||||||
niaPreferencesDataSource.setDynamicColorPreference(useDynamicColor)
|
|
||||||
}
|
|
||||||
|
|
||||||
override suspend fun setShouldHideOnboarding(shouldHideOnboarding: Boolean) {
|
|
||||||
niaPreferencesDataSource.setShouldHideOnboarding(shouldHideOnboarding)
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in new issue