1
0
Fork 0

Add tests for OnSharedPreferenceChangeListener (#12019)

master
Tiger Oakes 2020-06-26 11:04:46 -07:00 committed by GitHub
parent 64947a8686
commit 80de3851f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 156 additions and 2 deletions

View File

@ -10,9 +10,9 @@ import androidx.appcompat.content.res.AppCompatResources
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import mozilla.components.feature.sitepermissions.SitePermissions
import mozilla.components.support.ktx.android.content.res.resolveAttribute
import mozilla.components.support.ktx.android.view.putCompoundDrawablesRelative
import org.mozilla.fenix.ext.getPreferenceKey
import org.mozilla.fenix.theme.ThemeManager
fun SitePermissions.toggle(featurePhone: PhoneFeature): SitePermissions {
return update(featurePhone, get(featurePhone).toggle())
@ -41,7 +41,7 @@ fun SitePermissions.update(field: PhoneFeature, value: SitePermissions.Status) =
* as a result we have to apply it programmatically. More info about this issue https://github.com/mozilla-mobile/fenix/issues/1414
*/
fun RadioButton.setStartCheckedIndicator() {
val attr = ThemeManager.resolveAttribute(android.R.attr.listChoiceIndicatorSingle, context)
val attr = context.theme.resolveAttribute(android.R.attr.listChoiceIndicatorSingle)
val buttonDrawable = AppCompatResources.getDrawable(context, attr)
buttonDrawable?.apply {
setBounds(0, 0, intrinsicWidth, intrinsicHeight)

View File

@ -0,0 +1,103 @@
/* 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
import android.graphics.Rect
import android.widget.RadioButton
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.SwitchPreference
import io.mockk.Called
import io.mockk.MockKAnnotations
import io.mockk.every
import io.mockk.impl.annotations.MockK
import io.mockk.mockk
import io.mockk.verify
import mozilla.components.support.ktx.android.view.putCompoundDrawablesRelative
import mozilla.components.support.test.robolectric.testContext
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotNull
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
@RunWith(FenixRobolectricTestRunner::class)
class ExtensionsTest {
@MockK(relaxUnitFun = true) private lateinit var radioButton: RadioButton
@MockK private lateinit var fragment: PreferenceFragmentCompat
private lateinit var preference: Preference
@Before
fun setup() {
MockKAnnotations.init(this)
preference = Preference(testContext)
every { radioButton.context } returns testContext
every {
fragment.getString(R.string.pref_key_accessibility_force_enable_zoom)
} returns "pref_key_accessibility_force_enable_zoom"
every {
fragment.getString(R.string.pref_key_accessibility_auto_size)
} returns "pref_key_accessibility_auto_size"
}
@Test
fun `test radiobutton setStartCheckedIndicator`() {
radioButton.setStartCheckedIndicator()
verify { radioButton.putCompoundDrawablesRelative(start = withArg {
assertEquals(Rect(0, 0, it.intrinsicWidth, it.intrinsicHeight), it.bounds)
}) }
}
@Test
fun `set change listener with typed argument`() {
val callback = mockk<(Preference, String) -> Unit>(relaxed = true)
preference.setOnPreferenceChangeListener<String> { pref, value ->
callback(pref, value)
true
}
assertFalse(preference.callChangeListener(10))
verify { callback wasNot Called }
assertTrue(preference.callChangeListener("Hello"))
verify { callback(preference, "Hello") }
}
@Test
fun `requirePreference returns corresponding preference`() {
val switchPreference = mockk<SwitchPreference>()
every {
fragment.findPreference<SwitchPreference>("pref_key_accessibility_auto_size")
} returns switchPreference
assertEquals(
switchPreference,
fragment.requirePreference<SwitchPreference>(R.string.pref_key_accessibility_auto_size)
)
}
@Test
fun `requirePreference throws if null preference is returned`() {
every {
fragment.findPreference<SwitchPreference>("pref_key_accessibility_force_enable_zoom")
} returns null
var exception: IllegalArgumentException? = null
try {
fragment.requirePreference<SwitchPreference>(R.string.pref_key_accessibility_force_enable_zoom)
} catch (e: IllegalArgumentException) {
exception = e
}
assertNotNull(exception)
}
}

View File

@ -0,0 +1,51 @@
/* 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
import android.content.SharedPreferences
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LifecycleRegistry
import io.mockk.Called
import io.mockk.mockk
import io.mockk.verify
import org.junit.Before
import org.junit.Test
class OnSharedPreferenceChangeListenerTest {
private lateinit var sharedPrefs: SharedPreferences
private lateinit var listener: (SharedPreferences, String) -> Unit
private lateinit var owner: LifecycleOwner
private lateinit var lifecycleRegistry: LifecycleRegistry
@Before
fun setup() {
sharedPrefs = mockk(relaxUnitFun = true)
listener = mockk(relaxed = true)
owner = LifecycleOwner { lifecycleRegistry }
lifecycleRegistry = LifecycleRegistry(owner)
}
@Test
fun `test listener is registered based on lifecycle`() {
sharedPrefs.registerOnSharedPreferenceChangeListener(owner, listener)
verify { sharedPrefs wasNot Called }
lifecycleRegistry.currentState = Lifecycle.State.CREATED
verify { sharedPrefs.registerOnSharedPreferenceChangeListener(any()) }
lifecycleRegistry.currentState = Lifecycle.State.DESTROYED
verify { sharedPrefs.unregisterOnSharedPreferenceChangeListener(any()) }
}
@Test
fun `listener should call lambda`() {
val wrapper = OnSharedPreferenceChangeListener(mockk(), listener)
wrapper.onSharedPreferenceChanged(sharedPrefs, "key")
verify { listener(sharedPrefs, "key") }
}
}