parent
3373f8b987
commit
2291fb07aa
|
@ -16,32 +16,44 @@ interface LocaleSettingsController {
|
||||||
}
|
}
|
||||||
|
|
||||||
class DefaultLocaleSettingsController(
|
class DefaultLocaleSettingsController(
|
||||||
private val context: Context,
|
private val activity: Activity,
|
||||||
private val localeSettingsStore: LocaleSettingsStore
|
private val localeSettingsStore: LocaleSettingsStore
|
||||||
) : LocaleSettingsController {
|
) : LocaleSettingsController {
|
||||||
|
|
||||||
override fun handleLocaleSelected(locale: Locale) {
|
override fun handleLocaleSelected(locale: Locale) {
|
||||||
if (localeSettingsStore.state.selectedLocale == locale &&
|
if (localeSettingsStore.state.selectedLocale == locale &&
|
||||||
!LocaleManager.isDefaultLocaleSelected(context)) {
|
!LocaleManager.isDefaultLocaleSelected(activity)) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
localeSettingsStore.dispatch(LocaleSettingsAction.Select(locale))
|
localeSettingsStore.dispatch(LocaleSettingsAction.Select(locale))
|
||||||
LocaleManager.setNewLocale(context, locale.toLanguageTag())
|
LocaleManager.setNewLocale(activity, locale.toLanguageTag())
|
||||||
LocaleManager.updateBaseConfiguration(context, locale)
|
LocaleManager.updateBaseConfiguration(activity, locale)
|
||||||
(context as Activity).recreate()
|
activity.recreate()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun handleDefaultLocaleSelected() {
|
override fun handleDefaultLocaleSelected() {
|
||||||
if (LocaleManager.isDefaultLocaleSelected(context)) {
|
if (LocaleManager.isDefaultLocaleSelected(activity)) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
localeSettingsStore.dispatch(LocaleSettingsAction.Select(localeSettingsStore.state.localeList[0]))
|
localeSettingsStore.dispatch(LocaleSettingsAction.Select(localeSettingsStore.state.localeList[0]))
|
||||||
LocaleManager.resetToSystemDefault(context)
|
LocaleManager.resetToSystemDefault(activity)
|
||||||
LocaleManager.updateBaseConfiguration(context, localeSettingsStore.state.localeList[0])
|
LocaleManager.updateBaseConfiguration(activity, localeSettingsStore.state.localeList[0])
|
||||||
(context as Activity).recreate()
|
activity.recreate()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun handleSearchQueryTyped(query: String) {
|
override fun handleSearchQueryTyped(query: String) {
|
||||||
localeSettingsStore.dispatch(LocaleSettingsAction.Search(query))
|
localeSettingsStore.dispatch(LocaleSettingsAction.Search(query))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update the locale for the configuration of the app context's resources
|
||||||
|
*/
|
||||||
|
@Suppress("Deprecation")
|
||||||
|
fun LocaleManager.updateBaseConfiguration(context: Context, locale: Locale) {
|
||||||
|
val resources = context.applicationContext.resources
|
||||||
|
val config = resources.configuration
|
||||||
|
config.setLocale(locale)
|
||||||
|
config.setLayoutDirection(locale)
|
||||||
|
resources.updateConfiguration(config, resources.displayMetrics)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,14 +5,9 @@
|
||||||
package org.mozilla.fenix.settings.advanced
|
package org.mozilla.fenix.settings.advanced
|
||||||
|
|
||||||
import android.view.LayoutInflater
|
import android.view.LayoutInflater
|
||||||
import android.view.View
|
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
import androidx.annotation.VisibleForTesting
|
|
||||||
import androidx.core.view.isVisible
|
|
||||||
import androidx.recyclerview.widget.DiffUtil
|
import androidx.recyclerview.widget.DiffUtil
|
||||||
import androidx.recyclerview.widget.RecyclerView
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
import kotlinx.android.synthetic.main.locale_settings_item.view.*
|
|
||||||
import mozilla.components.support.locale.LocaleManager
|
|
||||||
import org.mozilla.fenix.R
|
import org.mozilla.fenix.R
|
||||||
import java.util.Locale
|
import java.util.Locale
|
||||||
|
|
||||||
|
@ -95,67 +90,3 @@ class LocaleAdapter(private val interactor: LocaleSettingsViewInteractor) :
|
||||||
DEFAULT, LOCALE;
|
DEFAULT, LOCALE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class LocaleViewHolder(
|
|
||||||
view: View,
|
|
||||||
selectedLocale: Locale,
|
|
||||||
private val interactor: LocaleSettingsViewInteractor
|
|
||||||
) : BaseLocaleViewHolder(view, selectedLocale) {
|
|
||||||
private val icon = view.locale_selected_icon
|
|
||||||
private val title = view.locale_title_text
|
|
||||||
private val subtitle = view.locale_subtitle_text
|
|
||||||
|
|
||||||
override fun bind(locale: Locale) {
|
|
||||||
// capitalisation is done using the rules of the appropriate locale (endonym and exonym)
|
|
||||||
title.text = locale.getDisplayName(locale).capitalize(locale)
|
|
||||||
subtitle.text = locale.displayName.capitalize(Locale.getDefault())
|
|
||||||
icon.isVisible = isCurrentLocaleSelected(locale, isDefault = false)
|
|
||||||
|
|
||||||
itemView.setOnClickListener {
|
|
||||||
interactor.onLocaleSelected(locale)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class SystemLocaleViewHolder(
|
|
||||||
view: View,
|
|
||||||
selectedLocale: Locale,
|
|
||||||
private val interactor: LocaleSettingsViewInteractor
|
|
||||||
) : BaseLocaleViewHolder(view, selectedLocale) {
|
|
||||||
private val icon = view.locale_selected_icon
|
|
||||||
private val title = view.locale_title_text
|
|
||||||
private val subtitle = view.locale_subtitle_text
|
|
||||||
|
|
||||||
override fun bind(locale: Locale) {
|
|
||||||
title.text = itemView.context.getString(R.string.default_locale_text)
|
|
||||||
subtitle.visibility = View.GONE
|
|
||||||
icon.isVisible = isCurrentLocaleSelected(locale, isDefault = true)
|
|
||||||
itemView.setOnClickListener {
|
|
||||||
interactor.onDefaultLocaleSelected()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
abstract class BaseLocaleViewHolder(
|
|
||||||
view: View,
|
|
||||||
private val selectedLocale: Locale
|
|
||||||
) : RecyclerView.ViewHolder(view) {
|
|
||||||
|
|
||||||
@VisibleForTesting(otherwise = VisibleForTesting.PROTECTED)
|
|
||||||
internal fun isCurrentLocaleSelected(locale: Locale, isDefault: Boolean): Boolean {
|
|
||||||
return if (isDefault) {
|
|
||||||
locale == selectedLocale && LocaleManager.isDefaultLocaleSelected(itemView.context)
|
|
||||||
} else {
|
|
||||||
locale == selectedLocale && !LocaleManager.isDefaultLocaleSelected(itemView.context)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
abstract fun bind(locale: Locale)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Similar to Kotlin's capitalize with locale parameter, but that method is currently experimental
|
|
||||||
*/
|
|
||||||
private fun String.capitalize(locale: Locale): String {
|
|
||||||
return substring(0, 1).toUpperCase(locale) + substring(1)
|
|
||||||
}
|
|
||||||
|
|
|
@ -51,15 +51,3 @@ fun LocaleManager.getSelectedLocale(
|
||||||
fun LocaleManager.isDefaultLocaleSelected(context: Context): Boolean {
|
fun LocaleManager.isDefaultLocaleSelected(context: Context): Boolean {
|
||||||
return getCurrentLocale(context) == null
|
return getCurrentLocale(context) == null
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Update the locale for the configuration of the app context's resources
|
|
||||||
*/
|
|
||||||
@Suppress("DEPRECATION")
|
|
||||||
fun LocaleManager.updateBaseConfiguration(context: Context, locale: Locale) {
|
|
||||||
val resources = context.applicationContext.resources
|
|
||||||
val config = resources.configuration
|
|
||||||
config.setLocale(locale)
|
|
||||||
config.setLayoutDirection(locale)
|
|
||||||
resources.updateConfiguration(config, resources.displayMetrics)
|
|
||||||
}
|
|
||||||
|
|
|
@ -43,7 +43,7 @@ class LocaleSettingsFragment : Fragment() {
|
||||||
store = getStore()
|
store = getStore()
|
||||||
interactor = LocaleSettingsInteractor(
|
interactor = LocaleSettingsInteractor(
|
||||||
controller = DefaultLocaleSettingsController(
|
controller = DefaultLocaleSettingsController(
|
||||||
context = requireContext(),
|
activity = requireActivity(),
|
||||||
localeSettingsStore = store
|
localeSettingsStore = store
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
|
@ -0,0 +1,72 @@
|
||||||
|
/* 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.settings.advanced
|
||||||
|
|
||||||
|
import android.view.View
|
||||||
|
import androidx.annotation.VisibleForTesting
|
||||||
|
import androidx.core.view.isVisible
|
||||||
|
import kotlinx.android.synthetic.main.locale_settings_item.*
|
||||||
|
import mozilla.components.support.locale.LocaleManager
|
||||||
|
import org.mozilla.fenix.R
|
||||||
|
import org.mozilla.fenix.utils.view.ViewHolder
|
||||||
|
import java.util.Locale
|
||||||
|
|
||||||
|
class LocaleViewHolder(
|
||||||
|
view: View,
|
||||||
|
selectedLocale: Locale,
|
||||||
|
private val interactor: LocaleSettingsViewInteractor
|
||||||
|
) : BaseLocaleViewHolder(view, selectedLocale) {
|
||||||
|
|
||||||
|
override fun bind(locale: Locale) {
|
||||||
|
// capitalisation is done using the rules of the appropriate locale (endonym and exonym)
|
||||||
|
locale_title_text.text = locale.getDisplayName(locale).capitalize(locale)
|
||||||
|
locale_subtitle_text.text = locale.displayName.capitalize(Locale.getDefault())
|
||||||
|
locale_selected_icon.isVisible = isCurrentLocaleSelected(locale, isDefault = false)
|
||||||
|
|
||||||
|
itemView.setOnClickListener {
|
||||||
|
interactor.onLocaleSelected(locale)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class SystemLocaleViewHolder(
|
||||||
|
view: View,
|
||||||
|
selectedLocale: Locale,
|
||||||
|
private val interactor: LocaleSettingsViewInteractor
|
||||||
|
) : BaseLocaleViewHolder(view, selectedLocale) {
|
||||||
|
|
||||||
|
override fun bind(locale: Locale) {
|
||||||
|
locale_title_text.text = itemView.context.getString(R.string.default_locale_text)
|
||||||
|
locale_subtitle_text.visibility = View.GONE
|
||||||
|
locale_selected_icon.isVisible = isCurrentLocaleSelected(locale, isDefault = true)
|
||||||
|
itemView.setOnClickListener {
|
||||||
|
interactor.onDefaultLocaleSelected()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract class BaseLocaleViewHolder(
|
||||||
|
view: View,
|
||||||
|
private val selectedLocale: Locale
|
||||||
|
) : ViewHolder(view) {
|
||||||
|
|
||||||
|
@VisibleForTesting(otherwise = VisibleForTesting.PROTECTED)
|
||||||
|
internal fun isCurrentLocaleSelected(locale: Locale, isDefault: Boolean): Boolean {
|
||||||
|
return if (isDefault) {
|
||||||
|
locale == selectedLocale && LocaleManager.isDefaultLocaleSelected(itemView.context)
|
||||||
|
} else {
|
||||||
|
locale == selectedLocale && !LocaleManager.isDefaultLocaleSelected(itemView.context)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract fun bind(locale: Locale)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Similar to Kotlin's capitalize with locale parameter, but that method is currently experimental
|
||||||
|
*/
|
||||||
|
private fun String.capitalize(locale: Locale): String {
|
||||||
|
return substring(0, 1).toUpperCase(locale) + substring(1)
|
||||||
|
}
|
|
@ -0,0 +1,59 @@
|
||||||
|
/* 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.settings.advanced
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.view.View
|
||||||
|
import io.mockk.every
|
||||||
|
import io.mockk.mockk
|
||||||
|
import io.mockk.mockkObject
|
||||||
|
import mozilla.components.support.locale.LocaleManager
|
||||||
|
import org.junit.Assert.assertFalse
|
||||||
|
import org.junit.Assert.assertTrue
|
||||||
|
import org.junit.Before
|
||||||
|
import org.junit.Test
|
||||||
|
import java.util.Locale
|
||||||
|
|
||||||
|
class BaseLocaleViewHolderTest {
|
||||||
|
|
||||||
|
private val selectedLocale = Locale("en", "UK")
|
||||||
|
private val view: View = mockk()
|
||||||
|
private val context: Context = mockk()
|
||||||
|
|
||||||
|
private val localeViewHolder = object : BaseLocaleViewHolder(view, selectedLocale) {
|
||||||
|
override fun bind(locale: Locale) = Unit
|
||||||
|
}
|
||||||
|
|
||||||
|
@Before
|
||||||
|
fun setup() {
|
||||||
|
mockkObject(LocaleManager)
|
||||||
|
every { view.context } returns context
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `verify other locale checker returns false`() {
|
||||||
|
every { LocaleManager.getCurrentLocale(context) } returns mockk()
|
||||||
|
val otherLocale = mockk<Locale>()
|
||||||
|
|
||||||
|
assertFalse(localeViewHolder.isCurrentLocaleSelected(otherLocale, isDefault = true))
|
||||||
|
assertFalse(localeViewHolder.isCurrentLocaleSelected(otherLocale, isDefault = false))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `verify selected locale checker returns true`() {
|
||||||
|
every { LocaleManager.getCurrentLocale(context) } returns mockk()
|
||||||
|
|
||||||
|
assertFalse(localeViewHolder.isCurrentLocaleSelected(selectedLocale, isDefault = true))
|
||||||
|
assertTrue(localeViewHolder.isCurrentLocaleSelected(selectedLocale, isDefault = false))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `verify default locale checker returns true`() {
|
||||||
|
every { LocaleManager.getCurrentLocale(context) } returns null
|
||||||
|
|
||||||
|
assertTrue(localeViewHolder.isCurrentLocaleSelected(selectedLocale, isDefault = true))
|
||||||
|
assertFalse(localeViewHolder.isCurrentLocaleSelected(selectedLocale, isDefault = false))
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,48 +0,0 @@
|
||||||
package org.mozilla.fenix.settings.advanced
|
|
||||||
|
|
||||||
import android.content.Context
|
|
||||||
import android.view.View
|
|
||||||
import io.mockk.every
|
|
||||||
import io.mockk.mockk
|
|
||||||
import io.mockk.mockkStatic
|
|
||||||
import mozilla.components.support.locale.LocaleManager
|
|
||||||
import org.junit.Assert.assertTrue
|
|
||||||
import org.junit.Before
|
|
||||||
import org.junit.Test
|
|
||||||
import java.util.Locale
|
|
||||||
|
|
||||||
class LocaleAdapterTest {
|
|
||||||
|
|
||||||
private val selectedLocale = Locale("en", "UK")
|
|
||||||
private val view: View = mockk(relaxed = true)
|
|
||||||
private val context: Context = mockk(relaxed = true)
|
|
||||||
|
|
||||||
private val localeViewHolder: BaseLocaleViewHolder =
|
|
||||||
object : BaseLocaleViewHolder(view, selectedLocale) {
|
|
||||||
|
|
||||||
override fun bind(locale: Locale) {
|
|
||||||
// not required
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Before
|
|
||||||
fun setup() {
|
|
||||||
every { view.context } returns context
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun `verify selected locale checker returns true`() {
|
|
||||||
mockkStatic("org.mozilla.fenix.settings.advanced.LocaleManagerExtensionKt")
|
|
||||||
every { LocaleManager.isDefaultLocaleSelected(context) } returns false
|
|
||||||
|
|
||||||
assertTrue(localeViewHolder.isCurrentLocaleSelected(selectedLocale, isDefault = false))
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun `verify default locale checker returns true`() {
|
|
||||||
mockkStatic("org.mozilla.fenix.settings.advanced.LocaleManagerExtensionKt")
|
|
||||||
every { LocaleManager.isDefaultLocaleSelected(context) } returns true
|
|
||||||
|
|
||||||
assertTrue(localeViewHolder.isCurrentLocaleSelected(selectedLocale, isDefault = true))
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -8,7 +8,6 @@ import android.content.Context
|
||||||
import io.mockk.every
|
import io.mockk.every
|
||||||
import io.mockk.mockk
|
import io.mockk.mockk
|
||||||
import io.mockk.mockkObject
|
import io.mockk.mockkObject
|
||||||
import io.mockk.mockkStatic
|
|
||||||
import mozilla.components.support.locale.LocaleManager
|
import mozilla.components.support.locale.LocaleManager
|
||||||
import org.junit.Assert.assertEquals
|
import org.junit.Assert.assertEquals
|
||||||
import org.junit.Assert.assertFalse
|
import org.junit.Assert.assertFalse
|
||||||
|
@ -24,9 +23,12 @@ import java.util.Locale
|
||||||
@RunWith(FenixRobolectricTestRunner::class)
|
@RunWith(FenixRobolectricTestRunner::class)
|
||||||
class LocaleManagerExtensionTest {
|
class LocaleManagerExtensionTest {
|
||||||
|
|
||||||
|
private lateinit var context: Context
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
fun setup() {
|
fun setup() {
|
||||||
mockkStatic("org.mozilla.fenix.settings.advanced.LocaleManagerExtensionKt")
|
context = mockk()
|
||||||
|
mockkObject(LocaleManager)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -44,8 +46,6 @@ class LocaleManagerExtensionTest {
|
||||||
@Test
|
@Test
|
||||||
@Config(qualifiers = "en-rUS")
|
@Config(qualifiers = "en-rUS")
|
||||||
fun `default locale selected`() {
|
fun `default locale selected`() {
|
||||||
val context: Context = mockk()
|
|
||||||
mockkObject(LocaleManager)
|
|
||||||
every { LocaleManager.getCurrentLocale(context) } returns null
|
every { LocaleManager.getCurrentLocale(context) } returns null
|
||||||
|
|
||||||
assertTrue(LocaleManager.isDefaultLocaleSelected(context))
|
assertTrue(LocaleManager.isDefaultLocaleSelected(context))
|
||||||
|
@ -54,8 +54,6 @@ class LocaleManagerExtensionTest {
|
||||||
@Test
|
@Test
|
||||||
@Config(qualifiers = "en-rUS")
|
@Config(qualifiers = "en-rUS")
|
||||||
fun `custom locale selected`() {
|
fun `custom locale selected`() {
|
||||||
val context: Context = mockk()
|
|
||||||
mockkObject(LocaleManager)
|
|
||||||
val selectedLocale = Locale("en", "UK")
|
val selectedLocale = Locale("en", "UK")
|
||||||
every { LocaleManager.getCurrentLocale(context) } returns selectedLocale
|
every { LocaleManager.getCurrentLocale(context) } returns selectedLocale
|
||||||
|
|
||||||
|
@ -65,13 +63,9 @@ class LocaleManagerExtensionTest {
|
||||||
@Test
|
@Test
|
||||||
@Config(qualifiers = "en-rUS")
|
@Config(qualifiers = "en-rUS")
|
||||||
fun `match current stored locale string with a Locale from our list`() {
|
fun `match current stored locale string with a Locale from our list`() {
|
||||||
val context: Context = mockk()
|
|
||||||
mockkObject(LocaleManager)
|
|
||||||
val otherLocale = Locale("fr")
|
val otherLocale = Locale("fr")
|
||||||
val selectedLocale = Locale("en", "UK")
|
val selectedLocale = Locale("en", "UK")
|
||||||
val localeList = ArrayList<Locale>()
|
val localeList = listOf(otherLocale, selectedLocale)
|
||||||
localeList.add(otherLocale)
|
|
||||||
localeList.add(selectedLocale)
|
|
||||||
|
|
||||||
every { LocaleManager.getCurrentLocale(context) } returns selectedLocale
|
every { LocaleManager.getCurrentLocale(context) } returns selectedLocale
|
||||||
|
|
||||||
|
@ -81,13 +75,9 @@ class LocaleManagerExtensionTest {
|
||||||
@Test
|
@Test
|
||||||
@Config(qualifiers = "en-rUS")
|
@Config(qualifiers = "en-rUS")
|
||||||
fun `match null stored locale with the default Locale from our list`() {
|
fun `match null stored locale with the default Locale from our list`() {
|
||||||
val context: Context = mockk()
|
|
||||||
mockkObject(LocaleManager)
|
|
||||||
val firstLocale = Locale("fr")
|
val firstLocale = Locale("fr")
|
||||||
val secondLocale = Locale("en", "UK")
|
val secondLocale = Locale("en", "UK")
|
||||||
val localeList = ArrayList<Locale>()
|
val localeList = listOf(firstLocale, secondLocale)
|
||||||
localeList.add(firstLocale)
|
|
||||||
localeList.add(secondLocale)
|
|
||||||
|
|
||||||
every { LocaleManager.getCurrentLocale(context) } returns null
|
every { LocaleManager.getCurrentLocale(context) } returns null
|
||||||
|
|
||||||
|
|
|
@ -5,14 +5,15 @@
|
||||||
package org.mozilla.fenix.settings.advanced
|
package org.mozilla.fenix.settings.advanced
|
||||||
|
|
||||||
import android.app.Activity
|
import android.app.Activity
|
||||||
import android.content.Context
|
|
||||||
import io.mockk.Runs
|
import io.mockk.Runs
|
||||||
import io.mockk.every
|
import io.mockk.every
|
||||||
import io.mockk.just
|
import io.mockk.just
|
||||||
import io.mockk.mockk
|
import io.mockk.mockk
|
||||||
import io.mockk.mockkObject
|
import io.mockk.mockkObject
|
||||||
import io.mockk.mockkStatic
|
import io.mockk.mockkStatic
|
||||||
|
import io.mockk.spyk
|
||||||
import io.mockk.verify
|
import io.mockk.verify
|
||||||
|
import io.mockk.verifyAll
|
||||||
import mozilla.components.support.locale.LocaleManager
|
import mozilla.components.support.locale.LocaleManager
|
||||||
import org.junit.Before
|
import org.junit.Before
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
|
@ -20,118 +21,113 @@ import java.util.Locale
|
||||||
|
|
||||||
class LocaleSettingsControllerTest {
|
class LocaleSettingsControllerTest {
|
||||||
|
|
||||||
private val context: Context = mockk<Activity>(relaxed = true)
|
private val activity = mockk<Activity>(relaxed = true)
|
||||||
private val localeSettingsStore: LocaleSettingsStore = mockk(relaxed = true)
|
private val localeSettingsStore: LocaleSettingsStore = mockk(relaxed = true)
|
||||||
|
private val mockState = LocaleSettingsState(mockk(), mockk(), mockk())
|
||||||
|
|
||||||
private lateinit var controller: LocaleSettingsController
|
private lateinit var controller: DefaultLocaleSettingsController
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
fun setup() {
|
fun setup() {
|
||||||
controller = DefaultLocaleSettingsController(context, localeSettingsStore)
|
controller = spyk(DefaultLocaleSettingsController(activity, localeSettingsStore))
|
||||||
|
|
||||||
|
mockkObject(LocaleManager)
|
||||||
|
mockkStatic("org.mozilla.fenix.settings.advanced.LocaleManagerExtensionKt")
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `don't set locale if same locale is chosen`() {
|
fun `don't set locale if same locale is chosen`() {
|
||||||
val selectedLocale = Locale("en", "UK")
|
val selectedLocale = Locale("en", "UK")
|
||||||
every { localeSettingsStore.state } returns LocaleSettingsState(
|
every { localeSettingsStore.state } returns mockState.copy(selectedLocale = selectedLocale)
|
||||||
mockk(),
|
every { LocaleManager.getCurrentLocale(activity) } returns mockk()
|
||||||
mockk(),
|
|
||||||
selectedLocale
|
|
||||||
)
|
|
||||||
mockkObject(LocaleManager)
|
|
||||||
mockkStatic("org.mozilla.fenix.settings.advanced.LocaleManagerExtensionKt")
|
|
||||||
every { LocaleManager.getCurrentLocale(context) } returns mockk()
|
|
||||||
every { LocaleManager.isDefaultLocaleSelected(context) } returns false
|
|
||||||
|
|
||||||
controller.handleLocaleSelected(selectedLocale)
|
controller.handleLocaleSelected(selectedLocale)
|
||||||
|
|
||||||
verify(
|
verifyAll(inverse = true) {
|
||||||
inverse = true,
|
localeSettingsStore.dispatch(LocaleSettingsAction.Select(selectedLocale))
|
||||||
verifyBlock = { localeSettingsStore.dispatch(LocaleSettingsAction.Select(selectedLocale)) })
|
LocaleManager.setNewLocale(activity, selectedLocale.toLanguageTag())
|
||||||
verify(
|
activity.recreate()
|
||||||
inverse = true,
|
}
|
||||||
verifyBlock = { LocaleManager.setNewLocale(context, selectedLocale.toLanguageTag()) })
|
with(controller) {
|
||||||
verify(
|
verify(inverse = true) { LocaleManager.updateBaseConfiguration(activity, selectedLocale) }
|
||||||
inverse = true,
|
}
|
||||||
verifyBlock = { LocaleManager.updateBaseConfiguration(context, selectedLocale) })
|
|
||||||
verify(inverse = true, verifyBlock = { (context as Activity).recreate() })
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `set a new locale from the list`() {
|
fun `set a new locale from the list if other locale is chosen`() {
|
||||||
val selectedLocale = Locale("en", "UK")
|
val selectedLocale = Locale("en", "UK")
|
||||||
val otherLocale: Locale = mockk()
|
val otherLocale: Locale = mockk()
|
||||||
every { localeSettingsStore.state } returns LocaleSettingsState(
|
every { localeSettingsStore.state } returns mockState.copy(selectedLocale = otherLocale)
|
||||||
mockk(),
|
every { LocaleManager.setNewLocale(activity, selectedLocale.toLanguageTag()) } returns activity
|
||||||
mockk(),
|
with(controller) {
|
||||||
otherLocale
|
every { LocaleManager.updateBaseConfiguration(activity, selectedLocale) } just Runs
|
||||||
)
|
}
|
||||||
mockkObject(LocaleManager)
|
|
||||||
mockkStatic("org.mozilla.fenix.settings.advanced.LocaleManagerExtensionKt")
|
|
||||||
every { LocaleManager.updateBaseConfiguration(context, selectedLocale) } just Runs
|
|
||||||
every {
|
|
||||||
LocaleManager.setNewLocale(
|
|
||||||
context,
|
|
||||||
selectedLocale.toLanguageTag()
|
|
||||||
)
|
|
||||||
} returns context
|
|
||||||
|
|
||||||
controller.handleLocaleSelected(selectedLocale)
|
controller.handleLocaleSelected(selectedLocale)
|
||||||
|
|
||||||
verify { localeSettingsStore.dispatch(LocaleSettingsAction.Select(selectedLocale)) }
|
verify { localeSettingsStore.dispatch(LocaleSettingsAction.Select(selectedLocale)) }
|
||||||
verify { LocaleManager.setNewLocale(context, selectedLocale.toLanguageTag()) }
|
verify { LocaleManager.setNewLocale(activity, selectedLocale.toLanguageTag()) }
|
||||||
verify { LocaleManager.updateBaseConfiguration(context, selectedLocale) }
|
verify { activity.recreate() }
|
||||||
verify { (context as Activity).recreate() }
|
with(controller) {
|
||||||
|
verify { LocaleManager.updateBaseConfiguration(activity, selectedLocale) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `set a new locale from the list if default locale is not selected`() {
|
||||||
|
val selectedLocale = Locale("en", "UK")
|
||||||
|
every { localeSettingsStore.state } returns mockState.copy(selectedLocale = selectedLocale)
|
||||||
|
every { LocaleManager.getCurrentLocale(activity) } returns null
|
||||||
|
every { LocaleManager.setNewLocale(activity, selectedLocale.toLanguageTag()) } returns activity
|
||||||
|
with(controller) {
|
||||||
|
every { LocaleManager.updateBaseConfiguration(activity, selectedLocale) } just Runs
|
||||||
|
}
|
||||||
|
|
||||||
|
controller.handleLocaleSelected(selectedLocale)
|
||||||
|
|
||||||
|
verify { localeSettingsStore.dispatch(LocaleSettingsAction.Select(selectedLocale)) }
|
||||||
|
verify { LocaleManager.setNewLocale(activity, selectedLocale.toLanguageTag()) }
|
||||||
|
verify { activity.recreate() }
|
||||||
|
with(controller) {
|
||||||
|
verify { LocaleManager.updateBaseConfiguration(activity, selectedLocale) }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `don't set default locale if default locale is already chosen`() {
|
fun `don't set default locale if default locale is already chosen`() {
|
||||||
val selectedLocale = Locale("en", "UK")
|
val selectedLocale = Locale("en", "UK")
|
||||||
val localeList = ArrayList<Locale>()
|
every { localeSettingsStore.state } returns mockState.copy(localeList = listOf(selectedLocale))
|
||||||
localeList.add(selectedLocale)
|
every { LocaleManager.getCurrentLocale(activity) } returns null
|
||||||
every { localeSettingsStore.state } returns LocaleSettingsState(
|
|
||||||
localeList,
|
|
||||||
mockk(),
|
|
||||||
mockk()
|
|
||||||
)
|
|
||||||
mockkStatic("org.mozilla.fenix.settings.advanced.LocaleManagerExtensionKt")
|
|
||||||
every { LocaleManager.isDefaultLocaleSelected(context) } returns true
|
|
||||||
|
|
||||||
controller.handleDefaultLocaleSelected()
|
controller.handleDefaultLocaleSelected()
|
||||||
|
|
||||||
verify(
|
verifyAll(inverse = true) {
|
||||||
inverse = true,
|
localeSettingsStore.dispatch(LocaleSettingsAction.Select(selectedLocale))
|
||||||
verifyBlock = { localeSettingsStore.dispatch(LocaleSettingsAction.Select(selectedLocale)) })
|
LocaleManager.resetToSystemDefault(activity)
|
||||||
verify(
|
activity.recreate()
|
||||||
inverse = true,
|
with(controller) {
|
||||||
verifyBlock = { LocaleManager.resetToSystemDefault(context) })
|
LocaleManager.updateBaseConfiguration(activity, selectedLocale)
|
||||||
verify(
|
}
|
||||||
inverse = true,
|
}
|
||||||
verifyBlock = { LocaleManager.updateBaseConfiguration(context, selectedLocale) })
|
|
||||||
verify(inverse = true, verifyBlock = { (context as Activity).recreate() })
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `set the default locale as the new locale`() {
|
fun `set the default locale as the new locale`() {
|
||||||
val selectedLocale = Locale("en", "UK")
|
val selectedLocale = Locale("en", "UK")
|
||||||
val localeList = ArrayList<Locale>()
|
every { localeSettingsStore.state } returns mockState.copy(localeList = listOf(selectedLocale))
|
||||||
localeList.add(selectedLocale)
|
every { LocaleManager.resetToSystemDefault(activity) } just Runs
|
||||||
every { localeSettingsStore.state } returns LocaleSettingsState(
|
with(controller) {
|
||||||
localeList,
|
every { LocaleManager.updateBaseConfiguration(activity, selectedLocale) } just Runs
|
||||||
mockk(),
|
}
|
||||||
mockk()
|
|
||||||
)
|
|
||||||
mockkObject(LocaleManager)
|
|
||||||
mockkStatic("org.mozilla.fenix.settings.advanced.LocaleManagerExtensionKt")
|
|
||||||
every { LocaleManager.resetToSystemDefault(context) } just Runs
|
|
||||||
every { LocaleManager.updateBaseConfiguration(context, selectedLocale) } just Runs
|
|
||||||
|
|
||||||
controller.handleDefaultLocaleSelected()
|
controller.handleDefaultLocaleSelected()
|
||||||
|
|
||||||
verify { localeSettingsStore.dispatch(LocaleSettingsAction.Select(selectedLocale)) }
|
verify { localeSettingsStore.dispatch(LocaleSettingsAction.Select(selectedLocale)) }
|
||||||
verify { LocaleManager.resetToSystemDefault(context) }
|
verify { LocaleManager.resetToSystemDefault(activity) }
|
||||||
verify { LocaleManager.updateBaseConfiguration(context, selectedLocale) }
|
verify { activity.recreate() }
|
||||||
verify { (context as Activity).recreate() }
|
with(controller) {
|
||||||
|
verify { LocaleManager.updateBaseConfiguration(activity, selectedLocale) }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
@ -18,10 +18,11 @@ class LocaleSettingsStoreTest {
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
fun setup() {
|
fun setup() {
|
||||||
val localeList = ArrayList<Locale>()
|
val localeList = listOf(
|
||||||
localeList.add(Locale("fr")) // default
|
Locale("fr"), // default
|
||||||
localeList.add(otherLocale)
|
otherLocale,
|
||||||
localeList.add(selectedLocale)
|
selectedLocale
|
||||||
|
)
|
||||||
|
|
||||||
localeSettingsStore =
|
localeSettingsStore =
|
||||||
LocaleSettingsStore(LocaleSettingsState(localeList, localeList, selectedLocale))
|
LocaleSettingsStore(LocaleSettingsState(localeList, localeList, selectedLocale))
|
||||||
|
|
|
@ -0,0 +1,86 @@
|
||||||
|
/* 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.settings.advanced
|
||||||
|
|
||||||
|
import android.view.LayoutInflater
|
||||||
|
import android.view.View
|
||||||
|
import androidx.core.view.isVisible
|
||||||
|
import io.mockk.Runs
|
||||||
|
import io.mockk.every
|
||||||
|
import io.mockk.just
|
||||||
|
import io.mockk.mockk
|
||||||
|
import io.mockk.mockkObject
|
||||||
|
import io.mockk.verify
|
||||||
|
import kotlinx.android.synthetic.main.locale_settings_item.view.*
|
||||||
|
import mozilla.components.support.locale.LocaleManager
|
||||||
|
import mozilla.components.support.test.robolectric.testContext
|
||||||
|
import org.junit.Assert.assertEquals
|
||||||
|
import org.junit.Assert.assertFalse
|
||||||
|
import org.junit.Assert.assertTrue
|
||||||
|
import org.junit.Before
|
||||||
|
import org.junit.Test
|
||||||
|
import org.junit.runner.RunWith
|
||||||
|
import org.mozilla.fenix.R
|
||||||
|
import org.mozilla.fenix.helpers.FenixRobolectricTestRunner
|
||||||
|
import java.util.Locale
|
||||||
|
|
||||||
|
@RunWith(FenixRobolectricTestRunner::class)
|
||||||
|
class LocaleViewHoldersTest {
|
||||||
|
|
||||||
|
private val selectedLocale = Locale("en", "US")
|
||||||
|
private lateinit var view: View
|
||||||
|
private lateinit var interactor: LocaleSettingsViewInteractor
|
||||||
|
private lateinit var localeViewHolder: LocaleViewHolder
|
||||||
|
private lateinit var systemLocaleViewHolder: SystemLocaleViewHolder
|
||||||
|
|
||||||
|
@Before
|
||||||
|
fun setup() {
|
||||||
|
mockkObject(LocaleManager)
|
||||||
|
every { LocaleManager.getCurrentLocale(any()) } returns null
|
||||||
|
|
||||||
|
view = LayoutInflater.from(testContext)
|
||||||
|
.inflate(R.layout.locale_settings_item, null)
|
||||||
|
interactor = mockk()
|
||||||
|
|
||||||
|
localeViewHolder = LocaleViewHolder(view, selectedLocale, interactor)
|
||||||
|
systemLocaleViewHolder = SystemLocaleViewHolder(view, selectedLocale, interactor)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `bind LocaleViewHolder`() {
|
||||||
|
localeViewHolder.bind(selectedLocale)
|
||||||
|
|
||||||
|
assertEquals("English (United States)", view.locale_title_text.text)
|
||||||
|
assertEquals("English (United States)", view.locale_subtitle_text.text)
|
||||||
|
assertFalse(view.locale_selected_icon.isVisible)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `LocaleViewHolder calls interactor on click`() {
|
||||||
|
localeViewHolder.bind(selectedLocale)
|
||||||
|
|
||||||
|
every { interactor.onLocaleSelected(selectedLocale) } just Runs
|
||||||
|
view.performClick()
|
||||||
|
verify { interactor.onLocaleSelected(selectedLocale) }
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `bind SystemLocaleViewHolder`() {
|
||||||
|
systemLocaleViewHolder.bind(selectedLocale)
|
||||||
|
|
||||||
|
assertEquals("Follow device language", view.locale_title_text.text)
|
||||||
|
assertFalse(view.locale_subtitle_text.isVisible)
|
||||||
|
assertTrue(view.locale_selected_icon.isVisible)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `SystemLocaleViewHolder calls interactor on click`() {
|
||||||
|
systemLocaleViewHolder.bind(selectedLocale)
|
||||||
|
|
||||||
|
every { interactor.onDefaultLocaleSelected() } just Runs
|
||||||
|
view.performClick()
|
||||||
|
verify { interactor.onDefaultLocaleSelected() }
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user