Skip to content
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

Managers 테스트 구현 #102

Merged
merged 4 commits into from
Dec 7, 2023
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
@@ -0,0 +1,70 @@
package com.example.speechbuddy

import android.content.Context
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.example.speechbuddy.data.local.AuthTokenPrefsManager
import com.example.speechbuddy.domain.models.AuthToken
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.runBlocking
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class AuthTokenPrefsManagerTest {

private lateinit var context: Context
private lateinit var authTokenPrefsManager: AuthTokenPrefsManager

private val testAuthToken = AuthToken("testAccessToken", "testRefreshToken")

@Before
fun setup() {
context = ApplicationProvider.getApplicationContext<Context>()
authTokenPrefsManager = AuthTokenPrefsManager(context)
}

@After
fun teardown() {
runBlocking {
authTokenPrefsManager.clearAuthToken()
}
}

@Test
fun should_save_and_retrieve_auth_token() {
runBlocking {
authTokenPrefsManager.saveAuthToken(testAuthToken)
val observedAuthToken = authTokenPrefsManager.preferencesFlow.first()
assertEquals(testAuthToken, observedAuthToken)
}
}

@Test
fun should_clear_auth_token() {
runBlocking {
authTokenPrefsManager.clearAuthToken()

val observedAuthToken = authTokenPrefsManager.preferencesFlow.first()
val expectedAuthToken = AuthToken("", "")
assertEquals(expectedAuthToken, observedAuthToken)
}
}

@Test
fun should_reset_auth_token_on_clear() {
runBlocking {
val newAuthToken = AuthToken("newAccessToken", "newRefreshToken")
authTokenPrefsManager.saveAuthToken(newAuthToken)

authTokenPrefsManager.clearAuthToken()

val observedAuthToken = authTokenPrefsManager.preferencesFlow.first()
val expectedAuthToken = AuthToken("", "")
assertEquals(expectedAuthToken, observedAuthToken)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package com.example.speechbuddy

import android.content.Context
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.example.speechbuddy.data.local.SettingsPrefsManager
import com.example.speechbuddy.domain.models.SettingsPreferences
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.runBlocking
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class SettingsPrefsManagerTest {

private lateinit var context: Context
private lateinit var settingsPrefsManager: SettingsPrefsManager

private val testSettings = SettingsPreferences(
autoBackup = false,
darkMode = true,
initialPage = false,
lastBackupDate = "2023-01-01"
)

@Before
fun setup() {
context = ApplicationProvider.getApplicationContext<Context>()
settingsPrefsManager = SettingsPrefsManager(context)

runBlocking {
settingsPrefsManager.saveAutoBackup(testSettings.autoBackup)
settingsPrefsManager.saveDarkMode(testSettings.darkMode)
settingsPrefsManager.saveInitialPage(testSettings.initialPage)
settingsPrefsManager.saveLastBackupDate(testSettings.lastBackupDate)
}
}

@After
fun teardown() {
runBlocking {
settingsPrefsManager.resetSettings()
}
}

@Test
fun should_save_and_retrieve_settings() {
runBlocking {
val observedSettings = settingsPrefsManager.settingsPreferencesFlow.first()
assertEquals(testSettings, observedSettings)
}
}

@Test
fun should_save_auto_backup_setting() {
runBlocking {
val testAutoBackupValue = true
settingsPrefsManager.saveAutoBackup(testAutoBackupValue)

val observedSettings = settingsPrefsManager.settingsPreferencesFlow.first()

val expectedSettings = SettingsPreferences(
autoBackup = testAutoBackupValue,
darkMode = true,
initialPage = false,
lastBackupDate = "2023-01-01"
)
assertEquals(expectedSettings, observedSettings)
}
}

@Test
fun should_save_dark_mode_setting() {
runBlocking {
val testDarkModeValue = false
settingsPrefsManager.saveDarkMode(testDarkModeValue)

val observedSettings = settingsPrefsManager.settingsPreferencesFlow.first()

val expectedSettings = SettingsPreferences(
autoBackup = false,
darkMode = testDarkModeValue,
initialPage = false,
lastBackupDate = "2023-01-01"
)
assertEquals(expectedSettings, observedSettings)
}
}

@Test
fun should_save_initial_page_setting() {
runBlocking {
val testInitialPageValue = true
settingsPrefsManager.saveInitialPage(testInitialPageValue)

val observedSettings = settingsPrefsManager.settingsPreferencesFlow.first()

val expectedSettings = SettingsPreferences(
autoBackup = false,
darkMode = true,
initialPage = testInitialPageValue,
lastBackupDate = "2023-01-01"
)
assertEquals(expectedSettings, observedSettings)
}
}

@Test
fun should_save_last_backup_date_setting() {
runBlocking {
val testLastBackupDateValue = "2023-02-15"
settingsPrefsManager.saveLastBackupDate(testLastBackupDateValue)

val observedSettings = settingsPrefsManager.settingsPreferencesFlow.first()

val expectedSettings = SettingsPreferences(
autoBackup = false,
darkMode = true,
initialPage = false,
lastBackupDate = testLastBackupDateValue
)
assertEquals(expectedSettings, observedSettings)
}
}

@Test
fun should_reset_settings() {
runBlocking {
// Set some non-default values first
settingsPrefsManager.saveAutoBackup(false)
settingsPrefsManager.saveDarkMode(true)
settingsPrefsManager.saveInitialPage(false)
settingsPrefsManager.saveLastBackupDate("2023-01-01")

settingsPrefsManager.resetSettings()

val observedSettings = settingsPrefsManager.settingsPreferencesFlow.first()
val defaultSettings = SettingsPreferences(
autoBackup = true,
darkMode = false,
initialPage = true,
lastBackupDate = ""
)
assertEquals(defaultSettings, observedSettings)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.example.speechbuddy

import android.content.Context
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.example.speechbuddy.data.local.UserIdPrefsManager
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.runBlocking
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class UserIdPrefsManagerTest {

private lateinit var context: Context
private lateinit var userIdPrefsManager: UserIdPrefsManager

@Before
fun setup() {
context = ApplicationProvider.getApplicationContext<Context>()
userIdPrefsManager = UserIdPrefsManager(context)
}

@After
fun teardown() {
runBlocking {
userIdPrefsManager.clearUserId()
}
}

@Test
fun should_save_user_id() {
runBlocking {
userIdPrefsManager.saveUserId(USER_ID_VALUE)

val observedUserId = userIdPrefsManager.preferencesFlow.first()
assertEquals(USER_ID_VALUE, observedUserId)
}
}

@Test
fun should_clear_user_id() {
runBlocking {
userIdPrefsManager.saveUserId(USER_ID_VALUE)

userIdPrefsManager.clearUserId()
val observedUserId = userIdPrefsManager.preferencesFlow.first()
assertEquals(null, observedUserId)
}
}

companion object {
const val USER_ID_VALUE = 123
}
}
Loading