From dfc3355054526e59a3e51d80fa5426c6319a45dd Mon Sep 17 00:00:00 2001 From: Jaehwa Noh Date: Mon, 9 Dec 2024 21:12:46 +0900 Subject: [PATCH] Empty search text is added in recent searches problem fix. (#1223) * Add if state to prevent blank query. * Create test emptySearchText_NotAddToRecentSearches - For empty query not add in recent query list. * Apply early return. * assertEquals to assertNull. Co-authored-by: Simon Marquis * Import org.junit.Assert.assertNull * Change org.junit.Assert.assertNull to kotlin.test.assertNull. Change-Id: I60083b0b9a1dcaed84d957b49c91e5163d5659d6 * Add 'or' to prevent search when query is blank. Change-Id: If498de05889d6d5946b1bbb1d1c10cb311bb9b52 * Create stateIsEmptyQuery_withThreeWhiteSpacesSearchQuery test. Change-Id: I6d6e8f9fe988ca83fae484e4f2338ad6fe58b676 * Add UI logic to prevent blank text search in SearchScreen. - Early return to do not call onSearchExplicitlyTriggered() Change-Id: I28db284eac9059fb6f48dd718721a1fc5fdb8f7a * Apply trim at query to remove whitespace. Change-Id: Id349af4bb1af3d0f1c3973e6fb9e6f3e7b924d9b * Create test stateIsEmptyQuery_withThreeWhiteSpacesAndOneLetterSearchQuery Change-Id: Iee59f6085e4045b2e3c532f78804926290b6c6c5 * Update feature/search/src/test/kotlin/com/google/samples/apps/nowinandroid/feature/search/SearchViewModelTest.kt Change name to emptySearchText_isNotAddedTorecentSearches() Co-authored-by: Don Turner * Change name to searchTextWithThreeSpaces_isEmptyQuery() Co-authored-by: Don Turner * Change name to searchTextWithThreeWhiteSpacesAndOneCharacter_isEmptyQuery() Change-Id: I4c82b12e2c9bd4ff480e94061caf257a603e38da * Revert "Change name to searchTextWithThreeWhiteSpacesAndOneCharacter_isEmptyQuery()" This reverts commit e4c176598ab11d96802963a0c814c446fc32e16b. * Change name to searchTextWithThreeSpacesAndOneLetter_isEmptyQuery Co-authored-by: Don Turner * Add end curly bracket. Change-Id: I7bd113e7b65d646f5321e34a7df2868f6e239cad --------- Co-authored-by: Simon Marquis Co-authored-by: Don Turner --- .../feature/search/SearchScreen.kt | 2 + .../feature/search/SearchViewModel.kt | 3 +- .../feature/search/SearchViewModelTest.kt | 38 +++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/feature/search/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/search/SearchScreen.kt b/feature/search/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/search/SearchScreen.kt index ff91941a8..b617f98a9 100644 --- a/feature/search/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/search/SearchScreen.kt +++ b/feature/search/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/search/SearchScreen.kt @@ -522,6 +522,7 @@ private fun SearchTextField( .focusRequester(focusRequester) .onKeyEvent { if (it.key == Key.Enter) { + if (searchQuery.isBlank()) return@onKeyEvent false onSearchExplicitlyTriggered() true } else { @@ -536,6 +537,7 @@ private fun SearchTextField( ), keyboardActions = KeyboardActions( onSearch = { + if (searchQuery.isBlank()) return@KeyboardActions onSearchExplicitlyTriggered() }, ), diff --git a/feature/search/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/search/SearchViewModel.kt b/feature/search/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/search/SearchViewModel.kt index 6c2af240c..36947880e 100644 --- a/feature/search/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/search/SearchViewModel.kt +++ b/feature/search/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/search/SearchViewModel.kt @@ -59,7 +59,7 @@ class SearchViewModel @Inject constructor( flowOf(SearchResultUiState.SearchNotReady) } else { searchQuery.flatMapLatest { query -> - if (query.length < SEARCH_QUERY_MIN_LENGTH) { + if (query.trim().length < SEARCH_QUERY_MIN_LENGTH) { flowOf(SearchResultUiState.EmptyQuery) } else { getSearchContentsUseCase(query) @@ -102,6 +102,7 @@ class SearchViewModel @Inject constructor( * search query in the search text field, defining this method. */ fun onSearchTriggered(query: String) { + if (query.isBlank()) return viewModelScope.launch { recentSearchRepository.insertOrReplaceRecentSearch(searchQuery = query) } diff --git a/feature/search/src/test/kotlin/com/google/samples/apps/nowinandroid/feature/search/SearchViewModelTest.kt b/feature/search/src/test/kotlin/com/google/samples/apps/nowinandroid/feature/search/SearchViewModelTest.kt index a62965b52..1b866cec2 100644 --- a/feature/search/src/test/kotlin/com/google/samples/apps/nowinandroid/feature/search/SearchViewModelTest.kt +++ b/feature/search/src/test/kotlin/com/google/samples/apps/nowinandroid/feature/search/SearchViewModelTest.kt @@ -41,6 +41,7 @@ import org.junit.Rule import org.junit.Test import kotlin.test.assertEquals import kotlin.test.assertIs +import kotlin.test.assertNull /** * To learn more about how this test handles Flows created with stateIn, see @@ -122,6 +123,43 @@ class SearchViewModelTest { assertEquals(SearchNotReady, viewModel.searchResultUiState.value) } + @Test + fun emptySearchText_isNotAddedToRecentSearches() = runTest { + viewModel.onSearchTriggered("") + + val recentSearchQueriesStream = getRecentQueryUseCase() + val recentSearchQueries = recentSearchQueriesStream.first() + val recentSearchQuery = recentSearchQueries.firstOrNull() + + assertNull(recentSearchQuery) + } + + @Test + fun searchTextWithThreeSpaces_isEmptyQuery() = runTest { + searchContentsRepository.addNewsResources(newsResourcesTestData) + searchContentsRepository.addTopics(topicsTestData) + val collectJob = launch(UnconfinedTestDispatcher()) { viewModel.searchResultUiState.collect() } + + viewModel.onSearchQueryChanged(" ") + + assertIs(viewModel.searchResultUiState.value) + + collectJob.cancel() + } + + @Test + fun searchTextWithThreeSpacesAndOneLetter_isEmptyQuery() = runTest { + searchContentsRepository.addNewsResources(newsResourcesTestData) + searchContentsRepository.addTopics(topicsTestData) + val collectJob = launch(UnconfinedTestDispatcher()) { viewModel.searchResultUiState.collect() } + + viewModel.onSearchQueryChanged(" a") + + assertIs(viewModel.searchResultUiState.value) + + collectJob.cancel() + } + @Test fun whenToggleNewsResourceSavedIsCalled_bookmarkStateIsUpdated() = runTest { val newsResourceId = "123"