Browse Source

For #6981 - Fixes tests to support locale aware changes

master
Jeff Boek 2 years ago
parent
commit
020608275d
  1. 11
      app/src/main/java/org/mozilla/fenix/components/searchengine/FenixSearchEngineProvider.kt
  2. 15
      app/src/test/java/org/mozilla/fenix/components/searchengine/FenixSearchEngineProviderTest.kt

11
app/src/main/java/org/mozilla/fenix/components/searchengine/FenixSearchEngineProvider.kt

@ -17,6 +17,7 @@ import mozilla.components.browser.search.provider.AssetsSearchEngineProvider
import mozilla.components.browser.search.provider.SearchEngineList
import mozilla.components.browser.search.provider.SearchEngineProvider
import mozilla.components.browser.search.provider.filter.SearchEngineFilter
import mozilla.components.browser.search.provider.localization.SearchLocalizationProvider
import mozilla.components.service.location.MozillaLocationService
import mozilla.components.service.location.search.RegionSearchLocalizationProvider
import org.mozilla.fenix.BuildConfig
@ -28,9 +29,9 @@ import java.util.Locale
open class FenixSearchEngineProvider(
private val context: Context
) : SearchEngineProvider, CoroutineScope by CoroutineScope(Job() + Dispatchers.IO) {
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
private val localizationProvider =
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
open val localizationProvider: SearchLocalizationProvider =
RegionSearchLocalizationProvider(
MozillaLocationService(
context,
@ -137,7 +138,8 @@ open class FenixSearchEngineProvider(
}
// When we change the locale we need to update the baseSearchEngines list
private fun updateBaseSearchEngines() {
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
open fun updateBaseSearchEngines() {
baseSearchEngines = async {
AssetsSearchEngineProvider(localizationProvider).loadSearchEngines(context)
}
@ -182,7 +184,8 @@ open class FenixSearchEngineProvider(
return installedIdentifiers + customEngineIdentifiers
}
private suspend fun localeAwareInstalledEnginesKey(): String {
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
suspend fun localeAwareInstalledEnginesKey(): String {
val tag = localizationProvider.determineRegion().let {
val region = it.region?.let { region ->
if (region.isEmpty()) "" else "-$region"

15
app/src/test/java/org/mozilla/fenix/components/searchengine/FenixSearchEngineProviderTest.kt

@ -8,6 +8,8 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runBlockingTest
import mozilla.components.browser.search.SearchEngine
import mozilla.components.browser.search.provider.SearchEngineList
import mozilla.components.browser.search.provider.localization.LocaleSearchLocalizationProvider
import mozilla.components.browser.search.provider.localization.SearchLocalizationProvider
import mozilla.components.support.test.robolectric.testContext
import org.junit.Assert.assertEquals
import org.junit.Before
@ -16,7 +18,6 @@ import org.junit.runner.RunWith
import org.mockito.Mockito.`when`
import org.mockito.Mockito.mock
import org.mozilla.fenix.TestApplication
import org.mozilla.fenix.components.searchengine.FenixSearchEngineProvider.Companion.INSTALLED_ENGINES_KEY
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
@ -41,7 +42,7 @@ class FenixSearchEngineProviderTest {
*/
@Test
fun `GIVEN sharedprefs does not ontain installed engines WHEN installedSearchEngineIdentifiers THEN defaultEngines + customEngines ids are returned`() = runBlockingTest {
fun `GIVEN sharedprefs does not contain installed engines WHEN installedSearchEngineIdentifiers THEN defaultEngines + customEngines ids are returned`() = runBlockingTest {
val expectedDefaults = fenixSearchEngineProvider.baseSearchEngines.toIdSet()
val expectedCustom = fenixSearchEngineProvider.customSearchEngines.toIdSet()
val expected = expectedDefaults + expectedCustom
@ -53,7 +54,7 @@ class FenixSearchEngineProviderTest {
@Test
fun `GIVEN sharedprefs contains installed engines WHEN installedSearchEngineIdentifiers THEN defaultEngines + customEngines ids are returned`() = runBlockingTest {
val sp = testContext.getSharedPreferences(FenixSearchEngineProvider.PREF_FILE, Context.MODE_PRIVATE)
sp.edit().putStringSet(INSTALLED_ENGINES_KEY, persistedInstalledEngines).apply()
sp.edit().putStringSet(fenixSearchEngineProvider.localeAwareInstalledEnginesKey(), persistedInstalledEngines).apply()
val expectedStored = persistedInstalledEngines
val expectedCustom = fenixSearchEngineProvider.customSearchEngines.toIdSet()
@ -70,7 +71,11 @@ private suspend fun Deferred<SearchEngineList>.toIdSet() =
private val persistedInstalledEngines = setOf("bing", "ecosia")
class FakeFenixSearchEngineProvider(context: Context) : FenixSearchEngineProvider(context) {
override val baseSearchEngines: Deferred<SearchEngineList>
override val localizationProvider: SearchLocalizationProvider
get() = LocaleSearchLocalizationProvider()
override var baseSearchEngines: Deferred<SearchEngineList>
set(_) { throw NotImplementedError("Setting not currently supported on this fake") }
get() {
val google = mockSearchEngine(id = "google-b-1-m", n = "Google")
@ -107,6 +112,8 @@ class FakeFenixSearchEngineProvider(context: Context) : FenixSearchEngineProvide
}
set(_) = throw NotImplementedError("Setting not currently supported on this fake")
override fun updateBaseSearchEngines() { }
private fun mockSearchEngine(
id: String,
n: String = id

Loading…
Cancel
Save