diff --git a/core/domain/src/test/kotlin/com/google/samples/apps/nowinandroid/core/domain/GetFollowableTopicsUseCaseTest.kt b/core/domain/src/test/kotlin/com/google/samples/apps/nowinandroid/core/domain/GetFollowableTopicsUseCaseTest.kt index 42a31f858..68b05cfbd 100644 --- a/core/domain/src/test/kotlin/com/google/samples/apps/nowinandroid/core/domain/GetFollowableTopicsUseCaseTest.kt +++ b/core/domain/src/test/kotlin/com/google/samples/apps/nowinandroid/core/domain/GetFollowableTopicsUseCaseTest.kt @@ -42,7 +42,7 @@ class GetFollowableTopicsUseCaseTest { ) @Test - fun whenNoParams_followableTopicsAreReturnedWithNoSorting() = runTest { + fun whenSortOrderIsByNone_followableTopicsAreReturnedWithNoSorting() = runTest { // Obtain a stream of followable topics. val followableTopics = useCase() diff --git a/core/domain/src/test/kotlin/com/google/samples/apps/nowinandroid/core/domain/GetRecentSearchQueriesUseCaseTest.kt b/core/domain/src/test/kotlin/com/google/samples/apps/nowinandroid/core/domain/GetRecentSearchQueriesUseCaseTest.kt new file mode 100644 index 000000000..83d38eddb --- /dev/null +++ b/core/domain/src/test/kotlin/com/google/samples/apps/nowinandroid/core/domain/GetRecentSearchQueriesUseCaseTest.kt @@ -0,0 +1,81 @@ +/* + * 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.domain + +import com.google.samples.apps.nowinandroid.core.testing.repository.TestRecentSearchRepository +import com.google.samples.apps.nowinandroid.core.testing.util.MainDispatcherRule +import kotlinx.coroutines.flow.first +import kotlinx.coroutines.test.runTest +import org.junit.Rule +import org.junit.Test +import kotlin.test.assertEquals + +class GetRecentSearchQueriesUseCaseTest { + + @get:Rule + val mainDispatcherRule = MainDispatcherRule() + + private val recentSearchRepository = TestRecentSearchRepository() + + private val useCase = GetRecentSearchQueriesUseCase( + recentSearchRepository, + ) + + @Test + fun whenQueryLimitIsBy10_recentSearchQueriesAreReturnedUpTo10() = runTest { + // Obtain a stream of recent search queries with no param. + val recentSearchQueries = useCase() + + // insert search queries over 10. + testRecentSearchQueries.forEach { query -> + recentSearchRepository.insertOrReplaceRecentSearch(query) + } + + // Check that the number of recent search queries are up to 10. + assertEquals( + expected = 10, + actual = recentSearchQueries.first().size, + ) + } + + @Test + fun whenReceivingRecentSearchQueries_recentSearchQueriesAreReturnedInRecentOrder() = runTest { + // Obtain a stream of recent search queries with no param. + val recentSearchQueries = useCase() + + // insert search queries over 10. + testRecentSearchQueries.forEach { query -> + recentSearchRepository.insertOrReplaceRecentSearch(query) + } + + // Check that search queries is ordered in recently up to 10 + assertEquals( + expected = testRecentSearchQueries.reversed().take(10), + actual = recentSearchQueries.first().map { it.query }, + ) + } +} + +private val testRecentSearchQueries = listOf( + "Compose", "Wear OS", + "Jetpack", "Headlines", + "Architecture", "UI", + "Testing", "Android Studio", + "Performance", "New API", + "Games", "Android TV", + "Camera", "Media", +) diff --git a/core/testing/src/main/kotlin/com/google/samples/apps/nowinandroid/core/testing/repository/TestRecentSearchRepository.kt b/core/testing/src/main/kotlin/com/google/samples/apps/nowinandroid/core/testing/repository/TestRecentSearchRepository.kt index f700bdc31..bf8c08a63 100644 --- a/core/testing/src/main/kotlin/com/google/samples/apps/nowinandroid/core/testing/repository/TestRecentSearchRepository.kt +++ b/core/testing/src/main/kotlin/com/google/samples/apps/nowinandroid/core/testing/repository/TestRecentSearchRepository.kt @@ -18,19 +18,26 @@ package com.google.samples.apps.nowinandroid.core.testing.repository import com.google.samples.apps.nowinandroid.core.data.model.RecentSearchQuery import com.google.samples.apps.nowinandroid.core.data.repository.RecentSearchRepository +import kotlinx.coroutines.channels.BufferOverflow import kotlinx.coroutines.flow.Flow -import kotlinx.coroutines.flow.flowOf +import kotlinx.coroutines.flow.MutableSharedFlow +import kotlinx.coroutines.flow.first +import kotlinx.coroutines.flow.map class TestRecentSearchRepository : RecentSearchRepository { - private val cachedRecentSearches: MutableList = mutableListOf() + private val cachedRecentSearches = MutableSharedFlow>( + replay = 1, + onBufferOverflow = BufferOverflow.DROP_OLDEST, + ).apply { tryEmit(mutableListOf()) } override fun getRecentSearchQueries(limit: Int): Flow> = - flowOf(cachedRecentSearches.sortedByDescending { it.queriedDate }.take(limit)) + cachedRecentSearches.map { it.sortedByDescending { it.queriedDate }.take(limit) } override suspend fun insertOrReplaceRecentSearch(searchQuery: String) { - cachedRecentSearches.add(RecentSearchQuery(searchQuery)) + val searchQueries = cachedRecentSearches.map { it.apply { add(RecentSearchQuery(searchQuery)) } }.first() + cachedRecentSearches.emit(searchQueries) } - override suspend fun clearRecentSearches() = cachedRecentSearches.clear() + override suspend fun clearRecentSearches() = cachedRecentSearches.emit(mutableListOf()) }