Commit 8fa28efe authored by Emily Kager's avatar Emily Kager Committed by Sebastian Kaspari
Browse files

Closes #2277 - Add ability to set default search engine

parent b9ef4ec1
Loading
Loading
Loading
Loading
+6 −5
Original line number Diff line number Diff line
@@ -8,10 +8,10 @@ import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import kotlinx.coroutines.async
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
@@ -28,6 +28,7 @@ class SearchEngineManager(
) {
    private var deferredSearchEngines: Deferred<List<SearchEngine>>? = null
    private val scope = CoroutineScope(Dispatchers.Default)
    var defaultSearchEngine: SearchEngine? = null

    /**
     * Asynchronously load search engines from providers. Inherits caller's [CoroutineScope.coroutineContext].
@@ -55,8 +56,8 @@ class SearchEngineManager(
    /**
     * Returns the default search engine.
     *
     * The default engine is the first engine loaded by the first provider passed to the
     * constructor of SearchEngineManager.
     * If defaultSearchEngine has not been set, the default engine is the first engine loaded by the
     * first provider passed to the constructor of SearchEngineManager.
     *
     * Optionally a name can be passed to this method (e.g. from the user's preferences). If
     * a matching search engine was loaded then this search engine will be returned instead.
@@ -66,7 +67,7 @@ class SearchEngineManager(
        val searchEngines = getSearchEngines(context)

        return when (name) {
            EMPTY -> searchEngines[0]
            EMPTY -> defaultSearchEngine ?: searchEngines[0]
            else -> searchEngines.find { it.name == name } ?: searchEngines[0]
        }
    }
+39 −0
Original line number Diff line number Diff line
@@ -114,6 +114,45 @@ class SearchEngineManagerTest {
        }
    }

    @Test
    fun `manager returns set default engine as default when no identifier is specified`() {
        runBlocking {
            val provider = mockProvider(
                listOf(
                    mockSearchEngine("mozsearch"),
                    mockSearchEngine("google"),
                    mockSearchEngine("bing")
                )
            )

            val manager = SearchEngineManager(listOf(provider))
            manager.defaultSearchEngine = mockSearchEngine("bing")

            val default = manager.getDefaultSearchEngine(RuntimeEnvironment.application)
            assertEquals("bing", default.identifier)
        }
    }

    @Test
    fun `manager returns engine from identifier as default when identifier is specified`() {
        runBlocking {
            val provider = mockProvider(
                listOf(
                    mockSearchEngine("mozsearch", "Mozilla Search"),
                    mockSearchEngine("google", "Google Search"),
                    mockSearchEngine("bing", "Bing Search")
                )
            )

            val manager = SearchEngineManager(listOf(provider))
            manager.defaultSearchEngine = mockSearchEngine("bing")

            val default =
                manager.getDefaultSearchEngine(RuntimeEnvironment.application, "Google Search")
            assertEquals("google", default.identifier)
        }
    }

    @Ignore
    @Test
    fun `locale update broadcast will trigger reload`() {
+3 −0
Original line number Diff line number Diff line
@@ -24,6 +24,9 @@ permalink: /changelog/
* **feature-downloads**
  * Fixing bug #2265. In some occasions, when trying to download a file, the download failed and the download notification shows "Unsuccessful download".
  
* **feature-search**
  * Adds default search engine var to `SearchEngineManager`

* **service-experiments**
  * A new client-side experiments SDK for running segmenting user populations to run multi-branch experiments on them. This component is going to replace `service-fretboard`. The SDK is currently in development and the component is not ready to be used yet.