Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android: Migrate auto-complete to Flow and Coroutines #5123

Open
wants to merge 7 commits into
base: feature/ana/address_the_tasks_in_the_meta_section
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,6 @@ import com.duckduckgo.sync.api.favicons.FaviconsFetchingPrompt
import com.duckduckgo.voice.api.VoiceSearchAvailability
import com.duckduckgo.voice.api.VoiceSearchAvailabilityPixelLogger
import dagger.Lazy
import io.reactivex.Observable
import io.reactivex.Single
import io.reactivex.observers.TestObserver
import java.io.File
import java.math.BigInteger
import java.security.cert.X509Certificate
Expand All @@ -246,6 +243,7 @@ import java.util.concurrent.CopyOnWriteArrayList
import java.util.concurrent.TimeUnit
import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asSharedFlow
Expand Down Expand Up @@ -1385,8 +1383,8 @@ class BrowserTabViewModelTest {
}

@Test
fun whenTriggeringAutocompleteThenAutoCompleteSuggestionsShown() {
whenever(mockAutoCompleteService.autoComplete("foo")).thenReturn(Observable.just(emptyList()))
fun whenTriggeringAutocompleteThenAutoCompleteSuggestionsShown() = runTest {
whenever(mockAutoCompleteService.autoComplete("foo")).thenReturn(emptyList())
doReturn(true).whenever(mockSettingsStore).autoCompleteSuggestionsEnabled
testee.triggerAutocomplete("foo", true, hasQueryChanged = true)
assertTrue(autoCompleteViewState().showSuggestions)
Expand Down Expand Up @@ -1445,18 +1443,18 @@ class BrowserTabViewModelTest {
@Test
fun wheneverAutoCompleteIsGoneAndHistoryIAMHasBeenShownThenNotifyUserSeenIAM() {
runTest {
whenever(mockAutoCompleteService.autoComplete("title")).thenReturn(Observable.just(emptyList()))
whenever(mockSavedSitesRepository.getBookmarksObservable()).thenReturn(
Single.just(listOf(Bookmark("abc", "title", "https://example.com", lastModified = null))),
whenever(mockAutoCompleteService.autoComplete("title")).thenReturn(emptyList())
whenever(mockSavedSitesRepository.getBookmarks()).thenReturn(
flowOf(listOf(Bookmark("abc", "title", "https://example.com", lastModified = null))),
)
whenever(mockSavedSitesRepository.getFavoritesObservable()).thenReturn(
Single.just(listOf(Favorite("abc", "title", "https://example.com", position = 1, lastModified = null))),
whenever(mockSavedSitesRepository.getFavorites()).thenReturn(
flowOf(listOf(Favorite("abc", "title", "https://example.com", position = 1, lastModified = null))),
)
whenever(mockNavigationHistory.getHistorySingle()).thenReturn(
Single.just(listOf(VisitedPage("https://foo.com".toUri(), "title", listOf(LocalDateTime.now())))),
whenever(mockNavigationHistory.getHistory()).thenReturn(
flowOf(listOf(VisitedPage("https://foo.com".toUri(), "title", listOf(LocalDateTime.now())))),
)
whenever(mockTabRepository.getTabsObservable()).thenReturn(
Single.just(listOf(TabEntity(tabId = "1", position = 1, url = "https://example.com", title = "title"))),
whenever(mockTabRepository.flowTabs).thenReturn(
flowOf(listOf(TabEntity(tabId = "1", position = 1, url = "https://example.com", title = "title"))),
)
doReturn(true).whenever(mockSettingsStore).autoCompleteSuggestionsEnabled

Expand All @@ -1465,7 +1463,8 @@ class BrowserTabViewModelTest {
whenever(mockAutoCompleteScorer.score("title", "https://foo.com".toUri(), 1, "title")).thenReturn(1)
whenever(mockUserStageStore.getUserAppStage()).thenReturn(ESTABLISHED)

testee.autoCompletePublishSubject.accept("title")
testee.autoCompleteStateFlow.value = "title"
delay(500)
testee.autoCompleteSuggestionsGone()
verify(mockAutoCompleteRepository).submitUserSeenHistoryIAM()
verify(mockPixel).fire(AUTOCOMPLETE_BANNER_SHOWN)
Expand All @@ -1475,16 +1474,16 @@ class BrowserTabViewModelTest {
@Test
fun wheneverAutoCompleteIsGoneAndHistoryIAMHasNotBeenShownThenDoNotNotifyUserSeenIAM() {
runTest {
whenever(mockAutoCompleteService.autoComplete("query")).thenReturn(Observable.just(emptyList()))
whenever(mockSavedSitesRepository.getBookmarksObservable()).thenReturn(
Single.just(listOf(Bookmark("abc", "title", "https://example.com", lastModified = null))),
whenever(mockAutoCompleteService.autoComplete("query")).thenReturn(emptyList())
whenever(mockSavedSitesRepository.getBookmarks()).thenReturn(
flowOf(listOf(Bookmark("abc", "title", "https://example.com", lastModified = null))),
)
whenever(mockSavedSitesRepository.getFavoritesObservable()).thenReturn(
Single.just(listOf(Favorite("abc", "title", "https://example.com", position = 1, lastModified = null))),
whenever(mockSavedSitesRepository.getFavorites()).thenReturn(
flowOf(listOf(Favorite("abc", "title", "https://example.com", position = 1, lastModified = null))),
)
whenever(mockNavigationHistory.getHistorySingle()).thenReturn(Single.just(listOf()))
whenever(mockNavigationHistory.getHistory()).thenReturn(flowOf(emptyList()))
doReturn(true).whenever(mockSettingsStore).autoCompleteSuggestionsEnabled
testee.autoCompletePublishSubject.accept("query")
testee.autoCompleteStateFlow.value = "query"
testee.autoCompleteSuggestionsGone()
verify(mockAutoCompleteRepository, never()).submitUserSeenHistoryIAM()
verify(mockPixel, never()).fire(AUTOCOMPLETE_BANNER_SHOWN)
Expand Down Expand Up @@ -5831,15 +5830,11 @@ class BrowserTabViewModelTest {
val suggestion = AutoCompleteHistorySuggestion(phrase = "phrase", title = "title", url = "url", isAllowedInTopHits = false)
val omnibarText = "foo"

val testObserver = TestObserver.create<String>()
testee.autoCompletePublishSubject.subscribe(testObserver)

testee.onRemoveSearchSuggestionConfirmed(suggestion, omnibarText)

verify(mockPixel).fire(AppPixelName.AUTOCOMPLETE_RESULT_DELETED)
verify(mockPixel).fire(AppPixelName.AUTOCOMPLETE_RESULT_DELETED_DAILY, type = Daily())
verify(mockNavigationHistory).removeHistoryEntryByUrl(suggestion.url)
testObserver.assertValue(omnibarText)
assertCommandIssued<Command.AutocompleteItemRemoved>()
}

Expand All @@ -5848,15 +5843,11 @@ class BrowserTabViewModelTest {
val suggestion = AutoCompleteHistorySearchSuggestion(phrase = "phrase", isAllowedInTopHits = false)
val omnibarText = "foo"

val testObserver = TestObserver.create<String>()
testee.autoCompletePublishSubject.subscribe(testObserver)

testee.onRemoveSearchSuggestionConfirmed(suggestion, omnibarText)

verify(mockPixel).fire(AppPixelName.AUTOCOMPLETE_RESULT_DELETED)
verify(mockPixel).fire(AppPixelName.AUTOCOMPLETE_RESULT_DELETED_DAILY, type = Daily())
verify(mockNavigationHistory).removeHistoryEntryByQuery(suggestion.phrase)
testObserver.assertValue(omnibarText)
assertCommandIssued<Command.AutocompleteItemRemoved>()
}

Expand Down
Loading
Loading