test(US-11): add failing tests for settings persistence
This commit is contained in:
parent
628129977f
commit
c646709438
3 changed files with 82 additions and 0 deletions
|
|
@ -55,6 +55,7 @@ dependencies {
|
||||||
implementation(libs.lifecycle.viewmodel.compose)
|
implementation(libs.lifecycle.viewmodel.compose)
|
||||||
|
|
||||||
testImplementation(libs.junit)
|
testImplementation(libs.junit)
|
||||||
|
testImplementation(libs.mockito.kotlin)
|
||||||
androidTestImplementation(libs.androidx.junit)
|
androidTestImplementation(libs.androidx.junit)
|
||||||
androidTestImplementation(libs.androidx.espresso.core)
|
androidTestImplementation(libs.androidx.espresso.core)
|
||||||
androidTestImplementation(platform(libs.androidx.compose.bom))
|
androidTestImplementation(platform(libs.androidx.compose.bom))
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,79 @@
|
||||||
|
package net.jeena.pacer
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.content.SharedPreferences
|
||||||
|
import org.junit.Assert.assertEquals
|
||||||
|
import org.junit.Before
|
||||||
|
import org.junit.Test
|
||||||
|
import org.mockito.kotlin.any
|
||||||
|
import org.mockito.kotlin.eq
|
||||||
|
import org.mockito.kotlin.mock
|
||||||
|
import org.mockito.kotlin.verify
|
||||||
|
import org.mockito.kotlin.whenever
|
||||||
|
|
||||||
|
class SettingsRepositoryTest {
|
||||||
|
|
||||||
|
private lateinit var prefs: SharedPreferences
|
||||||
|
private lateinit var editor: SharedPreferences.Editor
|
||||||
|
private lateinit var context: Context
|
||||||
|
private lateinit var repo: SettingsRepository
|
||||||
|
|
||||||
|
@Before
|
||||||
|
fun setUp() {
|
||||||
|
editor = mock()
|
||||||
|
whenever(editor.putInt(any(), any())).thenReturn(editor)
|
||||||
|
whenever(editor.putFloat(any(), any())).thenReturn(editor)
|
||||||
|
|
||||||
|
prefs = mock()
|
||||||
|
whenever(prefs.edit()).thenReturn(editor)
|
||||||
|
whenever(prefs.getInt(eq(SettingsRepository.KEY_BPM), any())).thenReturn(120)
|
||||||
|
whenever(prefs.getFloat(eq(SettingsRepository.KEY_VOLUME), any())).thenReturn(1f)
|
||||||
|
|
||||||
|
context = mock()
|
||||||
|
whenever(context.getSharedPreferences(any(), any())).thenReturn(prefs)
|
||||||
|
|
||||||
|
repo = SettingsRepository(context)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `saveBpm writes to shared preferences`() {
|
||||||
|
repo.saveBpm(130)
|
||||||
|
verify(editor).putInt(SettingsRepository.KEY_BPM, 130)
|
||||||
|
verify(editor).apply()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `saveVolume writes to shared preferences`() {
|
||||||
|
repo.saveVolume(0.7f)
|
||||||
|
verify(editor).putFloat(SettingsRepository.KEY_VOLUME, 0.7f)
|
||||||
|
verify(editor).apply()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `loadBpm returns stored value`() {
|
||||||
|
whenever(prefs.getInt(eq(SettingsRepository.KEY_BPM), any())).thenReturn(130)
|
||||||
|
assertEquals(130, repo.loadBpm())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `loadVolume returns stored value`() {
|
||||||
|
whenever(prefs.getFloat(eq(SettingsRepository.KEY_VOLUME), any())).thenReturn(0.5f)
|
||||||
|
assertEquals(0.5f, repo.loadVolume())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `loadBpm returns default 120 when nothing saved`() {
|
||||||
|
whenever(prefs.getInt(eq(SettingsRepository.KEY_BPM), any())).thenAnswer {
|
||||||
|
it.getArgument(1) as Int
|
||||||
|
}
|
||||||
|
assertEquals(120, repo.loadBpm())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `loadVolume returns default 1f when nothing saved`() {
|
||||||
|
whenever(prefs.getFloat(eq(SettingsRepository.KEY_VOLUME), any())).thenAnswer {
|
||||||
|
it.getArgument(1) as Float
|
||||||
|
}
|
||||||
|
assertEquals(1f, repo.loadVolume())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -6,6 +6,7 @@ lifecycleRuntimeKtx = "2.8.3"
|
||||||
activityCompose = "1.9.0"
|
activityCompose = "1.9.0"
|
||||||
composeBom = "2024.06.00"
|
composeBom = "2024.06.00"
|
||||||
junit = "4.13.2"
|
junit = "4.13.2"
|
||||||
|
mockitoKotlin = "5.4.0"
|
||||||
androidxJunit = "1.2.1"
|
androidxJunit = "1.2.1"
|
||||||
espressoCore = "3.6.1"
|
espressoCore = "3.6.1"
|
||||||
|
|
||||||
|
|
@ -23,6 +24,7 @@ androidx-ui-test-junit4 = { group = "androidx.compose.ui", name = "ui-test-junit
|
||||||
androidx-material3 = { group = "androidx.compose.material3", name = "material3" }
|
androidx-material3 = { group = "androidx.compose.material3", name = "material3" }
|
||||||
lifecycle-viewmodel-compose = { group = "androidx.lifecycle", name = "lifecycle-viewmodel-compose", version.ref = "lifecycleRuntimeKtx" }
|
lifecycle-viewmodel-compose = { group = "androidx.lifecycle", name = "lifecycle-viewmodel-compose", version.ref = "lifecycleRuntimeKtx" }
|
||||||
junit = { group = "junit", name = "junit", version.ref = "junit" }
|
junit = { group = "junit", name = "junit", version.ref = "junit" }
|
||||||
|
mockito-kotlin = { group = "org.mockito.kotlin", name = "mockito-kotlin", version.ref = "mockitoKotlin" }
|
||||||
androidx-junit = { group = "androidx.test.ext", name = "junit", version.ref = "androidxJunit" }
|
androidx-junit = { group = "androidx.test.ext", name = "junit", version.ref = "androidxJunit" }
|
||||||
androidx-espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espressoCore" }
|
androidx-espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espressoCore" }
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue