Skip to content

Rewrite BatteryOptimizationsFragment to Compose #580

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Feb 23, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@

package at.bitfire.davdroid.settings

import at.bitfire.davdroid.TestUtils.getOrAwaitValue
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Rule
import org.junit.Test
Expand All @@ -16,6 +23,12 @@ import javax.inject.Inject
@HiltAndroidTest
class SettingsManagerTest {

companion object {
/** Use this setting to test SettingsManager methods. Will be removed after every test run. */
const val SETTING_TEST = "test"
}


@get:Rule
val hiltRule = HiltAndroidRule(this)

Expand All @@ -26,16 +39,58 @@ class SettingsManagerTest {
hiltRule.inject()
}

@After
fun removeTestSetting() {
settingsManager.remove(SETTING_TEST)
}


@Test
fun testContainsKey_NotExisting() {
fun test_containsKey_NotExisting() {
assertFalse(settingsManager.containsKey("notExisting"))
}

@Test
fun testContainsKey_Existing() {
fun test_containsKey_Existing() {
// provided by DefaultsProvider
assertEquals(Settings.PROXY_TYPE_SYSTEM, settingsManager.getInt(Settings.PROXY_TYPE))
}


@Test
fun test_getBooleanLive_getValue() = runBlocking(Dispatchers.Main) { // observeForever can't be run in background thread
val live = settingsManager.getBooleanLive(SETTING_TEST)
assertNull(live.value)

// set value
settingsManager.putBoolean(SETTING_TEST, true)
assertTrue(live.getOrAwaitValue()!!)

// set another value
live.value = false
assertFalse(live.getOrAwaitValue()!!)
}


@Test
fun test_ObserverCalledWhenValueChanges() {
val value = CompletableDeferred<Int>()
val observer = SettingsManager.OnChangeListener {
value.complete(settingsManager.getInt(SETTING_TEST))
}

try {
settingsManager.addOnChangeListener(observer)
settingsManager.putInt(SETTING_TEST, 123)

runBlocking {
// wait until observer is called
assertEquals(123, value.await())
}

} finally {
settingsManager.removeOnChangeListener(observer)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ package at.bitfire.davdroid.settings
import android.content.Context
import android.util.NoSuchPropertyException
import androidx.annotation.AnyThread
import androidx.lifecycle.MutableLiveData
import at.bitfire.davdroid.log.Logger
import at.bitfire.davdroid.settings.SettingsManager.OnChangeListener
import dagger.Module
import dagger.Provides
import dagger.hilt.EntryPoint
Expand Down Expand Up @@ -170,6 +172,38 @@ class SettingsManager internal constructor(
fun remove(key: String) = putString(key, null)


/*** LIVE DATA ***/

/**
* Returns a [MutableLiveData] which is backed by the settings with the given key.
* An observer must be added to the returned [MutableLiveData] to make it active.
*/
fun getBooleanLive(key: String) = object : MutableLiveData<Boolean?>() {
private val preferenceChangeListener = OnChangeListener { updateValue() }

private fun updateValue() {
value = getBooleanOrNull(key)
}

// setValue is also called from postValue, so no need to override
override fun setValue(value: Boolean?) {
super.setValue(value)
putBoolean(key, value)
}

override fun onActive() {
super.onActive()
updateValue()
addOnChangeListener(preferenceChangeListener)
}

override fun onInactive() {
super.onInactive()
removeOnChangeListener(preferenceChangeListener)
}
}


/*** HELPERS ***/

fun dump(writer: Writer) {
Expand All @@ -180,7 +214,7 @@ class SettingsManager internal constructor(
}


interface OnChangeListener {
fun interface OnChangeListener {
/**
* Will be called when something has changed in a [SettingsProvider].
* May run in worker thread!
Expand Down
Loading