diff --git a/frontend/app/src/androidTest/java/com/example/speechbuddy/AuthTokenPrefsManagerTest.kt b/frontend/app/src/androidTest/java/com/example/speechbuddy/AuthTokenPrefsManagerTest.kt new file mode 100644 index 00000000..15532061 --- /dev/null +++ b/frontend/app/src/androidTest/java/com/example/speechbuddy/AuthTokenPrefsManagerTest.kt @@ -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() + 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) + } + } +} diff --git a/frontend/app/src/androidTest/java/com/example/speechbuddy/SettingsPrefsManagerTest.kt b/frontend/app/src/androidTest/java/com/example/speechbuddy/SettingsPrefsManagerTest.kt new file mode 100644 index 00000000..b0cf15f3 --- /dev/null +++ b/frontend/app/src/androidTest/java/com/example/speechbuddy/SettingsPrefsManagerTest.kt @@ -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() + 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) + } + } +} diff --git a/frontend/app/src/androidTest/java/com/example/speechbuddy/UserIdPrefsManagerTest.kt b/frontend/app/src/androidTest/java/com/example/speechbuddy/UserIdPrefsManagerTest.kt new file mode 100644 index 00000000..f91824f9 --- /dev/null +++ b/frontend/app/src/androidTest/java/com/example/speechbuddy/UserIdPrefsManagerTest.kt @@ -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() + 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 + } +} diff --git a/frontend/app/src/test/java/com/example/speechbuddy/domain/SessionManagerTest.kt b/frontend/app/src/test/java/com/example/speechbuddy/domain/SessionManagerTest.kt new file mode 100644 index 00000000..6b406fd0 --- /dev/null +++ b/frontend/app/src/test/java/com/example/speechbuddy/domain/SessionManagerTest.kt @@ -0,0 +1,120 @@ +package com.example.speechbuddy.domain + +import androidx.arch.core.executor.testing.InstantTaskExecutorRule +import androidx.lifecycle.Observer +import com.example.speechbuddy.domain.models.AuthToken +import com.example.speechbuddy.utils.Constants.Companion.GUEST_ID +import io.mockk.* +import junit.framework.TestCase.assertEquals +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.test.TestCoroutineDispatcher +import kotlinx.coroutines.test.resetMain +import kotlinx.coroutines.test.runBlockingTest +import kotlinx.coroutines.test.setMain +import org.junit.After +import org.junit.Before +import org.junit.Rule +import org.junit.Test + +@ExperimentalCoroutinesApi +class SessionManagerTest { + + @get:Rule + val rule = InstantTaskExecutorRule() + + private val testCoroutineDispatcher = TestCoroutineDispatcher() + + private lateinit var authTokenObserver: Observer + private lateinit var userIdObserver: Observer + private lateinit var isLoginObserver: Observer + + private lateinit var sessionManager: SessionManager + + @Before + fun setup() { + Dispatchers.setMain(testCoroutineDispatcher) + + authTokenObserver = mockk(relaxed = true) + userIdObserver = mockk(relaxed = true) + isLoginObserver = mockk(relaxed = true) + + sessionManager = SessionManager() + sessionManager.cachedToken.observeForever(authTokenObserver) + sessionManager.userId.observeForever(userIdObserver) + sessionManager.isLogin.observeForever(isLoginObserver) + } + + @After + fun tearDown() { + Dispatchers.resetMain() + testCoroutineDispatcher.cleanupTestCoroutines() + clearAllMocks() + } + + @Test + fun `setAuthToken should update cachedToken LiveData`() = testCoroutineDispatcher.runBlockingTest { + val testAuthToken = AuthToken("testAccessToken", "testRefreshToken") + + sessionManager.setAuthToken(testAuthToken) + + verify { authTokenObserver.onChanged(testAuthToken) } + } + + @Test + fun `setUserId should update userId LiveData`() = testCoroutineDispatcher.runBlockingTest { + val testUserId = 123 + + sessionManager.setUserId(testUserId) + + verify { userIdObserver.onChanged(testUserId) } + } + + @Test + fun `deleteToken should set cachedToken and userId to null`() = testCoroutineDispatcher.runBlockingTest { + sessionManager.deleteToken() + + verify { authTokenObserver.onChanged(null) } + verify { userIdObserver.onChanged(null) } + } + + @Test + fun `enterGuestMode should set userId to GUEST_ID`() = testCoroutineDispatcher.runBlockingTest { + sessionManager.enterGuestMode() + + verify { userIdObserver.onChanged(GUEST_ID) } + } + + @Test + fun `exitGuestMode should set userId to null`() = testCoroutineDispatcher.runBlockingTest { + sessionManager.exitGuestMode() + + verify { userIdObserver.onChanged(null) } + } + + @Test + fun `setIsLogin should update isLogin LiveData`() = testCoroutineDispatcher.runBlockingTest { + sessionManager.setIsLogin(true) + + verify { isLoginObserver.onChanged(true) } + } + + @Test + fun `isAuthorized should reflect correct authorization status`() = + testCoroutineDispatcher.runBlockingTest { + val testAuthToken = AuthToken("testAccessToken", "testRefreshToken") + + sessionManager.setUserId(456) + sessionManager.setAuthToken(testAuthToken) + + verify { userIdObserver.onChanged(any()) } + verify { authTokenObserver.onChanged(any()) } + + val isAuthorizedObserver = slot() + every { isLoginObserver.onChanged(capture(isAuthorizedObserver)) } just Runs + + sessionManager.isAuthorized.observeForever(isLoginObserver) + + assertEquals(true, isAuthorizedObserver.captured) + } +}