Skip to content
This repository has been archived by the owner on Feb 20, 2023. It is now read-only.

Add tests for OnSharedPreferenceChangeListener #12019

Merged
merged 1 commit into from
Jun 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/src/main/java/org/mozilla/fenix/settings/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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)
Expand Down
103 changes: 103 additions & 0 deletions app/src/test/java/org/mozilla/fenix/settings/ExtensionsTest.kt
Original file line number Diff line number Diff line change
@@ -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)
}
}
Original file line number Diff line number Diff line change
@@ -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") }
}
}