Skip to content

Commit

Permalink
Include open tabs in the suggestions (#5019)
Browse files Browse the repository at this point in the history
Task/Issue URL:

Added all open tabs to suggestions.

All opened tabs (no checks on the query) are placed above history, for
simplicity.
Matching and ranking to be done separately.

- [x] Install from this branch.
- [x] Open a few websites in different tabs.
- [x] Search for something and notice the "Tab" suggestions. There is no
matching done, only check the UI (the switch to tab text is not yet
present).

| Example 1 (with history)  | Example 2 (without history) |
| ------ | ----- |

|![Screenshot_20240916_215653](https://github.com/user-attachments/assets/af875653-1dd1-4376-bda7-9f753c4a354c)|![Screenshot_20240916_220717](https://github.com/user-attachments/assets/27a7a05e-c0d1-43d2-ae68-37db43b71f20)|

---
- To see the specific tasks where the Asana app for GitHub is being
used, see below:
  - https://app.asana.com/0/0/1208264037618740

---------

Co-authored-by: Dax The Translator <[email protected]>
  • Loading branch information
anikiki and daxmobile committed Oct 23, 2024
1 parent e5313ba commit 4549790
Show file tree
Hide file tree
Showing 62 changed files with 1,532 additions and 699 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,15 @@ import com.duckduckgo.adclick.api.AdClickManager
import com.duckduckgo.app.ValueCaptorObserver
import com.duckduckgo.app.accessibility.data.AccessibilitySettingsDataStore
import com.duckduckgo.app.accessibility.data.AccessibilitySettingsSharedPreferences
import com.duckduckgo.app.autocomplete.AutocompleteTabsFeature
import com.duckduckgo.app.autocomplete.api.AutoComplete
import com.duckduckgo.app.autocomplete.api.AutoComplete.AutoCompleteResult
import com.duckduckgo.app.autocomplete.api.AutoComplete.AutoCompleteSuggestion.AutoCompleteBookmarkSuggestion
import com.duckduckgo.app.autocomplete.api.AutoComplete.AutoCompleteSuggestion.AutoCompleteDefaultSuggestion
import com.duckduckgo.app.autocomplete.api.AutoComplete.AutoCompleteSuggestion.AutoCompleteHistoryRelatedSuggestion.AutoCompleteHistorySearchSuggestion
import com.duckduckgo.app.autocomplete.api.AutoComplete.AutoCompleteSuggestion.AutoCompleteHistoryRelatedSuggestion.AutoCompleteHistorySuggestion
import com.duckduckgo.app.autocomplete.api.AutoComplete.AutoCompleteSuggestion.AutoCompleteSearchSuggestion
import com.duckduckgo.app.autocomplete.api.AutoComplete.AutoCompleteSuggestion.AutoCompleteUrlSuggestion.AutoCompleteBookmarkSuggestion
import com.duckduckgo.app.autocomplete.api.AutoComplete.AutoCompleteSuggestion.AutoCompleteUrlSuggestion.AutoCompleteSwitchToTabSuggestion
import com.duckduckgo.app.autocomplete.api.AutoCompleteApi
import com.duckduckgo.app.autocomplete.api.AutoCompleteScorer
import com.duckduckgo.app.autocomplete.api.AutoCompleteService
Expand Down Expand Up @@ -238,9 +240,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 @@ -251,6 +250,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 @@ -490,6 +490,7 @@ class BrowserTabViewModelTest {
private val mockAutoCompleteRepository: AutoCompleteRepository = mock()
private val changeOmnibarPositionFeature: ChangeOmnibarPositionFeature = mock()
private val mockHighlightsOnboardingExperimentManager: HighlightsOnboardingExperimentManager = mock()
private val mockAutocompleteTabsFeature: AutocompleteTabsFeature = mock()

@Before
fun before() = runTest {
Expand All @@ -506,8 +507,10 @@ class BrowserTabViewModelTest {
mockNavigationHistory,
mockAutoCompleteScorer,
mockAutoCompleteRepository,
mockTabRepository,
mockUserStageStore,
coroutineRule.testDispatcherProvider,
mockAutocompleteTabsFeature,
)
val fireproofWebsiteRepositoryImpl = FireproofWebsiteRepositoryImpl(
fireproofWebsiteDao,
Expand Down Expand Up @@ -535,6 +538,8 @@ class BrowserTabViewModelTest {
whenever(mockDuckPlayer.isDuckPlayerUri(anyString())).thenReturn(false)
whenever(mockDuckPlayer.getDuckPlayerState()).thenReturn(ENABLED)
whenever(changeOmnibarPositionFeature.refactor()).thenReturn(mockEnabledToggle)
whenever(mockAutocompleteTabsFeature.self()).thenReturn(mockEnabledToggle)
whenever(mockAutocompleteTabsFeature.self().isEnabled()).thenReturn(true)

remoteMessagingModel = givenRemoteMessagingModel(mockRemoteMessagingRepository, mockPixel, coroutineRule.testDispatcherProvider)

Expand Down Expand Up @@ -1393,8 +1398,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 @@ -1453,15 +1458,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.flowTabs).thenReturn(
flowOf(listOf(TabEntity(tabId = "1", position = 1, url = "https://example.com", title = "title"))),
)
doReturn(true).whenever(mockSettingsStore).autoCompleteSuggestionsEnabled

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

testee.autoCompletePublishSubject.accept("title")
testee.onAutoCompleteSuggestionsChanged()
testee.autoCompleteStateFlow.value = "title"
delay(500)
testee.autoCompleteSuggestionsGone()
verify(mockAutoCompleteRepository).submitUserSeenHistoryIAM()
verify(mockPixel).fire(AUTOCOMPLETE_BANNER_SHOWN)
Expand All @@ -1480,16 +1490,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 @@ -2346,6 +2356,37 @@ class BrowserTabViewModelTest {
assertEquals("false", argumentCaptor.firstValue[PixelParameter.BOOKMARK_CAPABLE])
}

@Test
fun whenSearchSuggestionSubmittedWithTabsThenAutoCompleteSearchSelectionPixelSent() = runTest {
whenever(mockSavedSitesRepository.hasBookmarks()).thenReturn(false)
whenever(mockNavigationHistory.hasHistory()).thenReturn(false)
tabsLiveData.value = listOf(TabEntity("1", "https://example.com", position = 0), TabEntity("2", "https://example.com", position = 1))
val suggestions = listOf(AutoCompleteSwitchToTabSuggestion("example", "", "", ""))
testee.autoCompleteViewState.value = autoCompleteViewState().copy(searchResults = AutoCompleteResult("", suggestions))
testee.fireAutocompletePixel(AutoCompleteSwitchToTabSuggestion("example", "", "", ""))

val argumentCaptor = argumentCaptor<Map<String, String>>()
verify(mockPixel).fire(eq(AppPixelName.AUTOCOMPLETE_SWITCH_TO_TAB_SELECTION), argumentCaptor.capture(), any(), any())

assertEquals("true", argumentCaptor.firstValue[PixelParameter.SHOWED_SWITCH_TO_TAB])
assertEquals("true", argumentCaptor.firstValue[PixelParameter.SWITCH_TO_TAB_CAPABLE])
}

@Test
fun whenSearchSuggestionSubmittedWithoutTabsThenAutoCompleteSearchSelectionPixelSent() = runTest {
whenever(mockSavedSitesRepository.hasBookmarks()).thenReturn(false)
whenever(mockNavigationHistory.hasHistory()).thenReturn(false)
tabsLiveData.value = listOf(TabEntity("1", "https://example.com", position = 0))
testee.autoCompleteViewState.value = autoCompleteViewState().copy(searchResults = AutoCompleteResult("", emptyList()))
testee.fireAutocompletePixel(AutoCompleteSwitchToTabSuggestion("example", "", "", ""))

val argumentCaptor = argumentCaptor<Map<String, String>>()
verify(mockPixel).fire(eq(AppPixelName.AUTOCOMPLETE_SWITCH_TO_TAB_SELECTION), argumentCaptor.capture(), any(), any())

assertEquals("false", argumentCaptor.firstValue[PixelParameter.SHOWED_SWITCH_TO_TAB])
assertEquals("false", argumentCaptor.firstValue[PixelParameter.SWITCH_TO_TAB_CAPABLE])
}

@Test
fun whenUserSelectToEditQueryThenMoveCaretToTheEnd() = runTest {
testee.onUserSelectedToEditQuery("foo")
Expand Down Expand Up @@ -5837,15 +5878,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 @@ -5854,15 +5891,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 Expand Up @@ -6000,6 +6033,20 @@ class BrowserTabViewModelTest {
verify(mockPixel).fire(ONBOARDING_DAX_CTA_CANCEL_BUTTON, mapOf(PixelParameter.CTA_SHOWN to DAX_FIRE_DIALOG_CTA))
}

@Test
fun whenUserSelectedAutocompleteWithAutoCompleteSwitchToTabSuggestionThenSwitchToTabCommandSentWithTabId() = runTest {
val tabId = "tabId"
val suggestion = AutoCompleteSwitchToTabSuggestion(phrase = "phrase", title = "title", url = "https://www.example.com", tabId = tabId)
whenever(mockSavedSitesRepository.hasBookmarks()).thenReturn(false)
whenever(mockNavigationHistory.hasHistory()).thenReturn(false)

testee.userSelectedAutocomplete(suggestion)

assertCommandIssued<Command.SwitchToTab> {
assertEquals(tabId, this.tabId)
}
}

private fun aCredential(): LoginCredentials {
return LoginCredentials(domain = null, username = null, password = null)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (c) 2024 DuckDuckGo
*
* 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
*
* http://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.duckduckgo.app.autocomplete

import com.duckduckgo.anvil.annotations.ContributesRemoteFeature
import com.duckduckgo.di.scopes.AppScope
import com.duckduckgo.feature.toggles.api.Toggle

@ContributesRemoteFeature(
scope = AppScope::class,
featureName = "autocompleteTabs",
)
interface AutocompleteTabsFeature {
@Toggle.DefaultValue(false)
fun self(): Toggle
}
Loading

0 comments on commit 4549790

Please sign in to comment.