SearchFragmentStore: Remove Session reference from state and read values from BrowserStore.
parent
942b901b81
commit
388c144a62
|
@ -69,7 +69,7 @@ class DefaultSearchController(
|
||||||
private fun openSearchOrUrl(url: String) {
|
private fun openSearchOrUrl(url: String) {
|
||||||
activity.openToBrowserAndLoad(
|
activity.openToBrowserAndLoad(
|
||||||
searchTermOrURL = url,
|
searchTermOrURL = url,
|
||||||
newTab = store.state.session == null,
|
newTab = store.state.tabId == null,
|
||||||
from = BrowserDirection.FromSearch,
|
from = BrowserDirection.FromSearch,
|
||||||
engine = store.state.searchEngineSource.searchEngine
|
engine = store.state.searchEngineSource.searchEngine
|
||||||
)
|
)
|
||||||
|
@ -103,8 +103,8 @@ class DefaultSearchController(
|
||||||
override fun handleTextChanged(text: String) {
|
override fun handleTextChanged(text: String) {
|
||||||
val settings = activity.settings()
|
val settings = activity.settings()
|
||||||
// Display the search shortcuts on each entry of the search fragment (see #5308)
|
// Display the search shortcuts on each entry of the search fragment (see #5308)
|
||||||
val textMatchesCurrentUrl = store.state.session?.url ?: "" == text
|
val textMatchesCurrentUrl = store.state.url == text
|
||||||
val textMatchesCurrentSearch = store.state.session?.searchTerms ?: "" == text
|
val textMatchesCurrentSearch = store.state.searchTerms == text
|
||||||
|
|
||||||
store.dispatch(SearchFragmentAction.UpdateQuery(text))
|
store.dispatch(SearchFragmentAction.UpdateQuery(text))
|
||||||
store.dispatch(
|
store.dispatch(
|
||||||
|
@ -126,7 +126,7 @@ class DefaultSearchController(
|
||||||
override fun handleUrlTapped(url: String) {
|
override fun handleUrlTapped(url: String) {
|
||||||
activity.openToBrowserAndLoad(
|
activity.openToBrowserAndLoad(
|
||||||
searchTermOrURL = url,
|
searchTermOrURL = url,
|
||||||
newTab = store.state.session == null,
|
newTab = store.state.tabId == null,
|
||||||
from = BrowserDirection.FromSearch
|
from = BrowserDirection.FromSearch
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -138,7 +138,7 @@ class DefaultSearchController(
|
||||||
|
|
||||||
activity.openToBrowserAndLoad(
|
activity.openToBrowserAndLoad(
|
||||||
searchTermOrURL = searchTerms,
|
searchTermOrURL = searchTerms,
|
||||||
newTab = store.state.session == null,
|
newTab = store.state.tabId == null,
|
||||||
from = BrowserDirection.FromSearch,
|
from = BrowserDirection.FromSearch,
|
||||||
engine = store.state.searchEngineSource.searchEngine,
|
engine = store.state.searchEngineSource.searchEngine,
|
||||||
forceSearch = true
|
forceSearch = true
|
||||||
|
|
|
@ -30,6 +30,7 @@ import kotlinx.android.synthetic.main.fragment_search.*
|
||||||
import kotlinx.android.synthetic.main.fragment_search.view.*
|
import kotlinx.android.synthetic.main.fragment_search.view.*
|
||||||
import kotlinx.android.synthetic.main.search_suggestions_onboarding.view.*
|
import kotlinx.android.synthetic.main.search_suggestions_onboarding.view.*
|
||||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||||
|
import mozilla.components.browser.state.selector.findTab
|
||||||
import mozilla.components.browser.toolbar.BrowserToolbar
|
import mozilla.components.browser.toolbar.BrowserToolbar
|
||||||
import mozilla.components.concept.storage.HistoryStorage
|
import mozilla.components.concept.storage.HistoryStorage
|
||||||
import mozilla.components.feature.qr.QrFeature
|
import mozilla.components.feature.qr.QrFeature
|
||||||
|
@ -86,11 +87,12 @@ class SearchFragment : Fragment(), UserInteractionHandler {
|
||||||
): View? {
|
): View? {
|
||||||
val activity = activity as HomeActivity
|
val activity = activity as HomeActivity
|
||||||
val args by navArgs<SearchFragmentArgs>()
|
val args by navArgs<SearchFragmentArgs>()
|
||||||
val session = args.sessionId
|
|
||||||
?.let(requireComponents.core.sessionManager::findSessionById)
|
val tabId = args.sessionId
|
||||||
|
val tab = tabId?.let { requireComponents.core.store.state.findTab(it) }
|
||||||
|
|
||||||
val view = inflater.inflate(R.layout.fragment_search, container, false)
|
val view = inflater.inflate(R.layout.fragment_search, container, false)
|
||||||
val url = session?.url.orEmpty()
|
val url = tab?.content?.url.orEmpty()
|
||||||
val currentSearchEngine = SearchEngineSource.Default(
|
val currentSearchEngine = SearchEngineSource.Default(
|
||||||
requireComponents.search.provider.getDefaultEngine(requireContext())
|
requireComponents.search.provider.getDefaultEngine(requireContext())
|
||||||
)
|
)
|
||||||
|
@ -103,6 +105,8 @@ class SearchFragment : Fragment(), UserInteractionHandler {
|
||||||
SearchFragmentStore(
|
SearchFragmentStore(
|
||||||
SearchFragmentState(
|
SearchFragmentState(
|
||||||
query = url,
|
query = url,
|
||||||
|
url = url,
|
||||||
|
searchTerms = tab?.content?.searchTerms.orEmpty(),
|
||||||
searchEngineSource = currentSearchEngine,
|
searchEngineSource = currentSearchEngine,
|
||||||
defaultEngineSource = currentSearchEngine,
|
defaultEngineSource = currentSearchEngine,
|
||||||
showSearchSuggestions = shouldShowSearchSuggestions(isPrivate),
|
showSearchSuggestions = shouldShowSearchSuggestions(isPrivate),
|
||||||
|
@ -111,7 +115,7 @@ class SearchFragment : Fragment(), UserInteractionHandler {
|
||||||
showClipboardSuggestions = requireContext().settings().shouldShowClipboardSuggestions,
|
showClipboardSuggestions = requireContext().settings().shouldShowClipboardSuggestions,
|
||||||
showHistorySuggestions = requireContext().settings().shouldShowHistorySuggestions,
|
showHistorySuggestions = requireContext().settings().shouldShowHistorySuggestions,
|
||||||
showBookmarkSuggestions = requireContext().settings().shouldShowBookmarkSuggestions,
|
showBookmarkSuggestions = requireContext().settings().shouldShowBookmarkSuggestions,
|
||||||
session = session,
|
tabId = tabId,
|
||||||
pastedText = args.pastedText,
|
pastedText = args.pastedText,
|
||||||
searchAccessPoint = args.searchAccessPoint
|
searchAccessPoint = args.searchAccessPoint
|
||||||
)
|
)
|
||||||
|
@ -234,7 +238,7 @@ class SearchFragment : Fragment(), UserInteractionHandler {
|
||||||
(activity as HomeActivity)
|
(activity as HomeActivity)
|
||||||
.openToBrowserAndLoad(
|
.openToBrowserAndLoad(
|
||||||
searchTermOrURL = result,
|
searchTermOrURL = result,
|
||||||
newTab = searchStore.state.session == null,
|
newTab = searchStore.state.tabId == null,
|
||||||
from = BrowserDirection.FromSearch
|
from = BrowserDirection.FromSearch
|
||||||
)
|
)
|
||||||
dialog.dismiss()
|
dialog.dismiss()
|
||||||
|
@ -265,7 +269,7 @@ class SearchFragment : Fragment(), UserInteractionHandler {
|
||||||
searchTermOrURL = SupportUtils.getGenericSumoURLForTopic(
|
searchTermOrURL = SupportUtils.getGenericSumoURLForTopic(
|
||||||
SupportUtils.SumoTopic.SEARCH_SUGGESTION
|
SupportUtils.SumoTopic.SEARCH_SUGGESTION
|
||||||
),
|
),
|
||||||
newTab = searchStore.state.session == null,
|
newTab = searchStore.state.tabId == null,
|
||||||
from = BrowserDirection.FromSearch
|
from = BrowserDirection.FromSearch
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -298,7 +302,7 @@ class SearchFragment : Fragment(), UserInteractionHandler {
|
||||||
(activity as HomeActivity)
|
(activity as HomeActivity)
|
||||||
.openToBrowserAndLoad(
|
.openToBrowserAndLoad(
|
||||||
searchTermOrURL = requireContext().components.clipboardHandler.url ?: "",
|
searchTermOrURL = requireContext().components.clipboardHandler.url ?: "",
|
||||||
newTab = searchStore.state.session == null,
|
newTab = searchStore.state.tabId == null,
|
||||||
from = BrowserDirection.FromSearch
|
from = BrowserDirection.FromSearch
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,6 @@
|
||||||
package org.mozilla.fenix.search
|
package org.mozilla.fenix.search
|
||||||
|
|
||||||
import mozilla.components.browser.search.SearchEngine
|
import mozilla.components.browser.search.SearchEngine
|
||||||
import mozilla.components.browser.session.Session
|
|
||||||
import mozilla.components.lib.state.Action
|
import mozilla.components.lib.state.Action
|
||||||
import mozilla.components.lib.state.State
|
import mozilla.components.lib.state.State
|
||||||
import mozilla.components.lib.state.Store
|
import mozilla.components.lib.state.Store
|
||||||
|
@ -34,6 +33,9 @@ sealed class SearchEngineSource {
|
||||||
/**
|
/**
|
||||||
* The state for the Search Screen
|
* The state for the Search Screen
|
||||||
* @property query The current search query string
|
* @property query The current search query string
|
||||||
|
* @property url The current URL of the tab (if this fragment is shown for an already existing tab)
|
||||||
|
* @property searchTerms The search terms used to search previously in this tab (if this fragment is shown
|
||||||
|
* for an already existing tab)
|
||||||
* @property searchEngineSource The current selected search engine with the context of how it was selected
|
* @property searchEngineSource The current selected search engine with the context of how it was selected
|
||||||
* @property defaultEngineSource The current default search engine source
|
* @property defaultEngineSource The current default search engine source
|
||||||
* @property showSearchSuggestions Whether or not to show search suggestions from the search engine in the AwesomeBar
|
* @property showSearchSuggestions Whether or not to show search suggestions from the search engine in the AwesomeBar
|
||||||
|
@ -42,11 +44,12 @@ sealed class SearchEngineSource {
|
||||||
* @property showClipboardSuggestions Whether or not to show clipboard suggestion in the AwesomeBar
|
* @property showClipboardSuggestions Whether or not to show clipboard suggestion in the AwesomeBar
|
||||||
* @property showHistorySuggestions Whether or not to show history suggestions in the AwesomeBar
|
* @property showHistorySuggestions Whether or not to show history suggestions in the AwesomeBar
|
||||||
* @property showBookmarkSuggestions Whether or not to show the bookmark suggestion in the AwesomeBar
|
* @property showBookmarkSuggestions Whether or not to show the bookmark suggestion in the AwesomeBar
|
||||||
* @property session The current session if available
|
|
||||||
* @property pastedText The text pasted from the long press toolbar menu
|
* @property pastedText The text pasted from the long press toolbar menu
|
||||||
*/
|
*/
|
||||||
data class SearchFragmentState(
|
data class SearchFragmentState(
|
||||||
val query: String,
|
val query: String,
|
||||||
|
val url: String,
|
||||||
|
val searchTerms: String,
|
||||||
val searchEngineSource: SearchEngineSource,
|
val searchEngineSource: SearchEngineSource,
|
||||||
val defaultEngineSource: SearchEngineSource.Default,
|
val defaultEngineSource: SearchEngineSource.Default,
|
||||||
val showSearchSuggestions: Boolean,
|
val showSearchSuggestions: Boolean,
|
||||||
|
@ -55,7 +58,7 @@ data class SearchFragmentState(
|
||||||
val showClipboardSuggestions: Boolean,
|
val showClipboardSuggestions: Boolean,
|
||||||
val showHistorySuggestions: Boolean,
|
val showHistorySuggestions: Boolean,
|
||||||
val showBookmarkSuggestions: Boolean,
|
val showBookmarkSuggestions: Boolean,
|
||||||
val session: Session?,
|
val tabId: String?,
|
||||||
val pastedText: String? = null,
|
val pastedText: String? = null,
|
||||||
val searchAccessPoint: Event.PerformedSearch.SearchAccessPoint?
|
val searchAccessPoint: Event.PerformedSearch.SearchAccessPoint?
|
||||||
) : State
|
) : State
|
||||||
|
|
|
@ -198,7 +198,7 @@ class AwesomeBarView(
|
||||||
updateSuggestionProvidersVisibility(state)
|
updateSuggestionProvidersVisibility(state)
|
||||||
|
|
||||||
// Do not make suggestions based on user's current URL unless it's a search shortcut
|
// Do not make suggestions based on user's current URL unless it's a search shortcut
|
||||||
if (state.query == state.session?.url && !state.showSearchShortcuts) {
|
if (state.query == state.url && !state.showSearchShortcuts) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -125,7 +125,7 @@ class ToolbarView(
|
||||||
/* Only set the search terms if pasted text is null so that the search term doesn't
|
/* Only set the search terms if pasted text is null so that the search term doesn't
|
||||||
overwrite pastedText when view enters `editMode` */
|
overwrite pastedText when view enters `editMode` */
|
||||||
if (searchState.pastedText.isNullOrEmpty()) {
|
if (searchState.pastedText.isNullOrEmpty()) {
|
||||||
view.setSearchTerms(searchState.session?.searchTerms.orEmpty())
|
view.setSearchTerms(searchState.searchTerms)
|
||||||
}
|
}
|
||||||
|
|
||||||
// We must trigger an onTextChanged so when search terms are set when transitioning to `editMode`
|
// We must trigger an onTextChanged so when search terms are set when transitioning to `editMode`
|
||||||
|
|
|
@ -15,7 +15,6 @@ import mozilla.components.browser.search.SearchEngine
|
||||||
import mozilla.components.browser.session.Session
|
import mozilla.components.browser.session.Session
|
||||||
import mozilla.components.browser.session.SessionManager
|
import mozilla.components.browser.session.SessionManager
|
||||||
import mozilla.components.support.test.robolectric.testContext
|
import mozilla.components.support.test.robolectric.testContext
|
||||||
import org.junit.Assert.assertEquals
|
|
||||||
import org.junit.Assert.assertFalse
|
import org.junit.Assert.assertFalse
|
||||||
import org.junit.Before
|
import org.junit.Before
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
|
@ -43,7 +42,6 @@ class DefaultSearchControllerTest {
|
||||||
private val store: SearchFragmentStore = mockk(relaxed = true)
|
private val store: SearchFragmentStore = mockk(relaxed = true)
|
||||||
private val navController: NavController = mockk(relaxed = true)
|
private val navController: NavController = mockk(relaxed = true)
|
||||||
private val defaultSearchEngine: SearchEngine? = mockk(relaxed = true)
|
private val defaultSearchEngine: SearchEngine? = mockk(relaxed = true)
|
||||||
private val session: Session? = mockk(relaxed = true)
|
|
||||||
private val searchEngine: SearchEngine = mockk(relaxed = true)
|
private val searchEngine: SearchEngine = mockk(relaxed = true)
|
||||||
private val metrics: MetricController = mockk(relaxed = true)
|
private val metrics: MetricController = mockk(relaxed = true)
|
||||||
private val sessionManager: SessionManager = mockk(relaxed = true)
|
private val sessionManager: SessionManager = mockk(relaxed = true)
|
||||||
|
@ -55,7 +53,7 @@ class DefaultSearchControllerTest {
|
||||||
@Before
|
@Before
|
||||||
fun setUp() {
|
fun setUp() {
|
||||||
every { activity.searchEngineManager.defaultSearchEngine } returns defaultSearchEngine
|
every { activity.searchEngineManager.defaultSearchEngine } returns defaultSearchEngine
|
||||||
every { store.state.session } returns session
|
every { store.state.tabId } returns "test-tab-id"
|
||||||
every { store.state.searchEngineSource.searchEngine } returns searchEngine
|
every { store.state.searchEngineSource.searchEngine } returns searchEngine
|
||||||
every { activity.metrics } returns metrics
|
every { activity.metrics } returns metrics
|
||||||
every { activity.components.core.sessionManager } returns sessionManager
|
every { activity.components.core.sessionManager } returns sessionManager
|
||||||
|
@ -79,7 +77,7 @@ class DefaultSearchControllerTest {
|
||||||
verify {
|
verify {
|
||||||
activity.openToBrowserAndLoad(
|
activity.openToBrowserAndLoad(
|
||||||
searchTermOrURL = url,
|
searchTermOrURL = url,
|
||||||
newTab = session == null,
|
newTab = false,
|
||||||
from = BrowserDirection.FromSearch,
|
from = BrowserDirection.FromSearch,
|
||||||
engine = searchEngine
|
engine = searchEngine
|
||||||
)
|
)
|
||||||
|
@ -105,7 +103,7 @@ class DefaultSearchControllerTest {
|
||||||
verify {
|
verify {
|
||||||
activity.openToBrowserAndLoad(
|
activity.openToBrowserAndLoad(
|
||||||
searchTermOrURL = SupportUtils.getMozillaPageUrl(SupportUtils.MozillaPage.MANIFESTO),
|
searchTermOrURL = SupportUtils.getMozillaPageUrl(SupportUtils.MozillaPage.MANIFESTO),
|
||||||
newTab = session == null,
|
newTab = false,
|
||||||
from = BrowserDirection.FromSearch,
|
from = BrowserDirection.FromSearch,
|
||||||
engine = searchEngine
|
engine = searchEngine
|
||||||
)
|
)
|
||||||
|
@ -163,14 +161,12 @@ class DefaultSearchControllerTest {
|
||||||
@Test
|
@Test
|
||||||
fun `show search shortcuts when setting enabled AND query equals url`() {
|
fun `show search shortcuts when setting enabled AND query equals url`() {
|
||||||
val text = "mozilla.org"
|
val text = "mozilla.org"
|
||||||
every { session?.url } returns "mozilla.org"
|
every { store.state.url } returns "mozilla.org"
|
||||||
testContext.settings().preferences
|
testContext.settings().preferences
|
||||||
.edit()
|
.edit()
|
||||||
.putBoolean(testContext.getString(R.string.pref_key_show_search_shortcuts), true)
|
.putBoolean(testContext.getString(R.string.pref_key_show_search_shortcuts), true)
|
||||||
.apply()
|
.apply()
|
||||||
|
|
||||||
assertEquals(text, session?.url)
|
|
||||||
|
|
||||||
controller.handleTextChanged(text)
|
controller.handleTextChanged(text)
|
||||||
|
|
||||||
verify { store.dispatch(SearchFragmentAction.ShowSearchShortcutEnginePicker(true)) }
|
verify { store.dispatch(SearchFragmentAction.ShowSearchShortcutEnginePicker(true)) }
|
||||||
|
@ -226,7 +222,7 @@ class DefaultSearchControllerTest {
|
||||||
verify {
|
verify {
|
||||||
activity.openToBrowserAndLoad(
|
activity.openToBrowserAndLoad(
|
||||||
searchTermOrURL = url,
|
searchTermOrURL = url,
|
||||||
newTab = session == null,
|
newTab = false,
|
||||||
from = BrowserDirection.FromSearch
|
from = BrowserDirection.FromSearch
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -242,7 +238,7 @@ class DefaultSearchControllerTest {
|
||||||
verify {
|
verify {
|
||||||
activity.openToBrowserAndLoad(
|
activity.openToBrowserAndLoad(
|
||||||
searchTermOrURL = searchTerms,
|
searchTermOrURL = searchTerms,
|
||||||
newTab = session == null,
|
newTab = false,
|
||||||
from = BrowserDirection.FromSearch,
|
from = BrowserDirection.FromSearch,
|
||||||
engine = searchEngine,
|
engine = searchEngine,
|
||||||
forceSearch = true
|
forceSearch = true
|
||||||
|
|
|
@ -48,6 +48,9 @@ class SearchFragmentStoreTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun emptyDefaultState(): SearchFragmentState = SearchFragmentState(
|
private fun emptyDefaultState(): SearchFragmentState = SearchFragmentState(
|
||||||
|
tabId = null,
|
||||||
|
url = "",
|
||||||
|
searchTerms = "",
|
||||||
query = "",
|
query = "",
|
||||||
searchEngineSource = mockk(),
|
searchEngineSource = mockk(),
|
||||||
defaultEngineSource = mockk(),
|
defaultEngineSource = mockk(),
|
||||||
|
@ -57,7 +60,6 @@ class SearchFragmentStoreTest {
|
||||||
showClipboardSuggestions = false,
|
showClipboardSuggestions = false,
|
||||||
showHistorySuggestions = false,
|
showHistorySuggestions = false,
|
||||||
showBookmarkSuggestions = false,
|
showBookmarkSuggestions = false,
|
||||||
session = null,
|
|
||||||
searchAccessPoint = Event.PerformedSearch.SearchAccessPoint.NONE
|
searchAccessPoint = Event.PerformedSearch.SearchAccessPoint.NONE
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue