2019-07-15 18:39:56 +02:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
package org.mozilla.fenix.search
|
|
|
|
|
|
|
|
import io.mockk.mockk
|
2020-07-14 19:32:04 +02:00
|
|
|
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
2019-07-15 18:39:56 +02:00
|
|
|
import kotlinx.coroutines.runBlocking
|
|
|
|
import mozilla.components.browser.search.SearchEngine
|
|
|
|
import org.junit.Assert.assertEquals
|
2020-07-14 19:32:04 +02:00
|
|
|
import org.junit.Assert.assertFalse
|
2019-07-15 18:39:56 +02:00
|
|
|
import org.junit.Assert.assertNotSame
|
2020-07-14 19:32:04 +02:00
|
|
|
import org.junit.Assert.assertTrue
|
2019-07-15 18:39:56 +02:00
|
|
|
import org.junit.Test
|
2019-12-18 16:09:12 +01:00
|
|
|
import org.mozilla.fenix.components.metrics.Event
|
2019-07-15 18:39:56 +02:00
|
|
|
|
2020-07-14 19:32:04 +02:00
|
|
|
@ExperimentalCoroutinesApi
|
2019-08-30 15:21:34 +02:00
|
|
|
class SearchFragmentStoreTest {
|
2019-07-15 18:39:56 +02:00
|
|
|
|
|
|
|
@Test
|
|
|
|
fun updateQuery() = runBlocking {
|
|
|
|
val initialState = emptyDefaultState()
|
2019-08-30 15:21:34 +02:00
|
|
|
val store = SearchFragmentStore(initialState)
|
2019-07-15 18:39:56 +02:00
|
|
|
val query = "test query"
|
|
|
|
|
2019-08-30 15:21:34 +02:00
|
|
|
store.dispatch(SearchFragmentAction.UpdateQuery(query)).join()
|
2019-07-15 18:39:56 +02:00
|
|
|
assertNotSame(initialState, store.state)
|
|
|
|
assertEquals(query, store.state.query)
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun selectSearchShortcutEngine() = runBlocking {
|
|
|
|
val initialState = emptyDefaultState()
|
2019-08-30 15:21:34 +02:00
|
|
|
val store = SearchFragmentStore(initialState)
|
2019-07-15 18:39:56 +02:00
|
|
|
val searchEngine: SearchEngine = mockk()
|
|
|
|
|
2019-08-30 15:21:34 +02:00
|
|
|
store.dispatch(SearchFragmentAction.SearchShortcutEngineSelected(searchEngine)).join()
|
2019-07-15 18:39:56 +02:00
|
|
|
assertNotSame(initialState, store.state)
|
|
|
|
assertEquals(SearchEngineSource.Shortcut(searchEngine), store.state.searchEngineSource)
|
2019-10-03 17:53:16 +02:00
|
|
|
assertEquals(false, store.state.showSearchShortcuts)
|
2019-07-15 18:39:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun showSearchShortcutEnginePicker() = runBlocking {
|
|
|
|
val initialState = emptyDefaultState()
|
2019-08-30 15:21:34 +02:00
|
|
|
val store = SearchFragmentStore(initialState)
|
2019-07-15 18:39:56 +02:00
|
|
|
|
2019-08-30 15:21:34 +02:00
|
|
|
store.dispatch(SearchFragmentAction.ShowSearchShortcutEnginePicker(true)).join()
|
2019-07-15 18:39:56 +02:00
|
|
|
assertNotSame(initialState, store.state)
|
2019-10-03 17:53:16 +02:00
|
|
|
assertEquals(true, store.state.showSearchShortcuts)
|
2019-07-15 18:39:56 +02:00
|
|
|
}
|
|
|
|
|
2020-07-02 11:18:37 +02:00
|
|
|
@Test
|
|
|
|
fun hideSearchShortcutEnginePicker() = runBlocking {
|
|
|
|
val initialState = emptyDefaultState()
|
|
|
|
val store = SearchFragmentStore(initialState)
|
|
|
|
|
|
|
|
store.dispatch(SearchFragmentAction.UpdateShortcutsAvailability(false)).join()
|
|
|
|
assertNotSame(initialState, store.state)
|
|
|
|
assertEquals(false, store.state.showSearchShortcuts)
|
|
|
|
}
|
|
|
|
|
2020-07-14 19:32:04 +02:00
|
|
|
@Test
|
|
|
|
fun showSearchSuggestions() = runBlocking {
|
|
|
|
val initialState = emptyDefaultState()
|
|
|
|
val store = SearchFragmentStore(initialState)
|
|
|
|
|
|
|
|
store.dispatch(SearchFragmentAction.SetShowSearchSuggestions(true)).join()
|
|
|
|
assertNotSame(initialState, store.state)
|
|
|
|
assertTrue(store.state.showSearchSuggestions)
|
|
|
|
|
|
|
|
store.dispatch(SearchFragmentAction.SetShowSearchSuggestions(false)).join()
|
|
|
|
assertFalse(store.state.showSearchSuggestions)
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun allowSearchInPrivateMode() = runBlocking {
|
|
|
|
val initialState = emptyDefaultState()
|
|
|
|
val store = SearchFragmentStore(initialState)
|
|
|
|
|
|
|
|
store.dispatch(SearchFragmentAction.AllowSearchSuggestionsInPrivateModePrompt(true)).join()
|
|
|
|
assertNotSame(initialState, store.state)
|
|
|
|
assertTrue(store.state.showSearchSuggestionsHint)
|
|
|
|
|
|
|
|
store.dispatch(SearchFragmentAction.AllowSearchSuggestionsInPrivateModePrompt(false)).join()
|
|
|
|
assertFalse(store.state.showSearchSuggestionsHint)
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun selectNewDefaultEngine() = runBlocking {
|
|
|
|
val initialState = emptyDefaultState()
|
|
|
|
val store = SearchFragmentStore(initialState)
|
|
|
|
val engine = mockk<SearchEngine>()
|
|
|
|
|
|
|
|
store.dispatch(SearchFragmentAction.SelectNewDefaultSearchEngine(engine)).join()
|
|
|
|
assertNotSame(initialState, store.state)
|
|
|
|
assertEquals(SearchEngineSource.Default(engine), store.state.searchEngineSource)
|
|
|
|
}
|
|
|
|
|
2019-08-30 15:21:34 +02:00
|
|
|
private fun emptyDefaultState(): SearchFragmentState = SearchFragmentState(
|
2020-07-02 11:43:40 +02:00
|
|
|
tabId = null,
|
|
|
|
url = "",
|
|
|
|
searchTerms = "",
|
2019-07-15 18:39:56 +02:00
|
|
|
query = "",
|
|
|
|
searchEngineSource = mockk(),
|
2019-08-01 16:48:38 +02:00
|
|
|
defaultEngineSource = mockk(),
|
2019-11-18 13:14:38 +01:00
|
|
|
showSearchSuggestionsHint = false,
|
2019-09-05 23:45:44 +02:00
|
|
|
showSearchSuggestions = false,
|
2019-10-03 17:53:16 +02:00
|
|
|
showSearchShortcuts = false,
|
2020-07-02 11:18:37 +02:00
|
|
|
areShortcutsAvailable = true,
|
2019-09-05 23:45:44 +02:00
|
|
|
showClipboardSuggestions = false,
|
|
|
|
showHistorySuggestions = false,
|
|
|
|
showBookmarkSuggestions = false,
|
2020-02-21 00:15:47 +01:00
|
|
|
searchAccessPoint = Event.PerformedSearch.SearchAccessPoint.NONE
|
2019-07-15 18:39:56 +02:00
|
|
|
)
|
|
|
|
}
|