1
0
Fork 0

For #6981 - Fixes tests to support locale aware changes

master
Jeff Boek 2020-02-18 17:28:35 -08:00
parent 1ba64f9559
commit 020608275d
2 changed files with 18 additions and 8 deletions

View File

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

View File

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