2023-10-03 22:34:51 +03:00
|
|
|
package com.x8bit.bitwarden
|
|
|
|
|
|
|
|
import android.content.Intent
|
|
|
|
import com.x8bit.bitwarden.data.auth.repository.AuthRepository
|
2024-01-17 02:41:34 +03:00
|
|
|
import com.x8bit.bitwarden.data.auth.repository.model.UserState
|
2024-01-20 00:01:43 +03:00
|
|
|
import com.x8bit.bitwarden.data.auth.repository.util.getCaptchaCallbackTokenResult
|
2024-01-17 02:41:34 +03:00
|
|
|
import com.x8bit.bitwarden.data.platform.repository.SettingsRepository
|
|
|
|
import com.x8bit.bitwarden.data.platform.repository.model.Environment
|
|
|
|
import com.x8bit.bitwarden.ui.platform.base.BaseViewModelTest
|
|
|
|
import com.x8bit.bitwarden.ui.platform.feature.settings.appearance.model.AppTheme
|
2024-01-20 00:01:43 +03:00
|
|
|
import com.x8bit.bitwarden.ui.platform.manager.intent.IntentManager
|
2023-10-03 22:34:51 +03:00
|
|
|
import io.mockk.every
|
2024-01-17 02:41:34 +03:00
|
|
|
import io.mockk.just
|
2023-10-03 22:34:51 +03:00
|
|
|
import io.mockk.mockk
|
2024-01-17 02:41:34 +03:00
|
|
|
import io.mockk.runs
|
2023-10-03 22:34:51 +03:00
|
|
|
import io.mockk.verify
|
2024-01-17 02:41:34 +03:00
|
|
|
import kotlinx.coroutines.flow.MutableStateFlow
|
|
|
|
import org.junit.jupiter.api.Assertions.assertEquals
|
|
|
|
import org.junit.jupiter.api.Test
|
2023-10-03 22:34:51 +03:00
|
|
|
|
2024-01-17 02:41:34 +03:00
|
|
|
class MainViewModelTest : BaseViewModelTest() {
|
2023-10-03 22:34:51 +03:00
|
|
|
|
2024-01-17 02:41:34 +03:00
|
|
|
private val mutableAppThemeFlow = MutableStateFlow(AppTheme.DEFAULT)
|
|
|
|
private val mutableUserStateFlow = MutableStateFlow<UserState?>(DEFAULT_USER_STATE)
|
|
|
|
val authRepository = mockk<AuthRepository> {
|
|
|
|
every { userStateFlow } returns mutableUserStateFlow
|
|
|
|
every { activeUserId } returns USER_ID
|
2024-01-20 00:01:43 +03:00
|
|
|
every { specialCircumstance } returns null
|
|
|
|
every { specialCircumstance = any() } just runs
|
2023-10-03 22:34:51 +03:00
|
|
|
}
|
2024-01-17 02:41:34 +03:00
|
|
|
private val settingsRepository = mockk<SettingsRepository> {
|
|
|
|
every { appTheme } returns AppTheme.DEFAULT
|
|
|
|
every { appThemeStateFlow } returns mutableAppThemeFlow
|
2023-10-03 22:34:51 +03:00
|
|
|
}
|
2024-01-20 00:01:43 +03:00
|
|
|
private val intentManager: IntentManager = mockk {
|
|
|
|
every { getShareDataFromIntent(any()) } returns null
|
|
|
|
}
|
|
|
|
|
2023-10-03 22:34:51 +03:00
|
|
|
@Test
|
2024-01-17 02:41:34 +03:00
|
|
|
fun `on AppThemeChanged should update state`() {
|
|
|
|
val viewModel = createViewModel()
|
|
|
|
|
|
|
|
assertEquals(
|
|
|
|
MainState(
|
|
|
|
theme = AppTheme.DEFAULT,
|
|
|
|
),
|
|
|
|
viewModel.stateFlow.value,
|
|
|
|
)
|
|
|
|
viewModel.trySendAction(
|
|
|
|
MainAction.Internal.ThemeUpdate(
|
|
|
|
theme = AppTheme.DARK,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
assertEquals(
|
|
|
|
MainState(
|
|
|
|
theme = AppTheme.DARK,
|
|
|
|
),
|
|
|
|
viewModel.stateFlow.value,
|
|
|
|
)
|
|
|
|
|
|
|
|
verify {
|
|
|
|
settingsRepository.appTheme
|
|
|
|
settingsRepository.appThemeStateFlow
|
2023-10-03 22:34:51 +03:00
|
|
|
}
|
2024-01-17 02:41:34 +03:00
|
|
|
}
|
|
|
|
|
2024-01-20 00:01:43 +03:00
|
|
|
@Suppress("MaxLineLength")
|
2024-01-17 02:41:34 +03:00
|
|
|
@Test
|
2024-01-20 00:01:43 +03:00
|
|
|
fun `on ReceiveFirstIntent with share data should set the special circumstance to ShareNewSend`() {
|
2024-01-17 02:41:34 +03:00
|
|
|
val viewModel = createViewModel()
|
2024-01-20 00:01:43 +03:00
|
|
|
val mockIntent = mockk<Intent>()
|
|
|
|
val shareData = mockk<IntentManager.ShareData>()
|
|
|
|
every { mockIntent.getCaptchaCallbackTokenResult() } returns null
|
|
|
|
every { intentManager.getShareDataFromIntent(mockIntent) } returns shareData
|
|
|
|
|
|
|
|
viewModel.trySendAction(
|
|
|
|
MainAction.ReceiveFirstIntent(
|
|
|
|
intent = mockIntent,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
verify {
|
|
|
|
authRepository.specialCircumstance = UserState.SpecialCircumstance.ShareNewSend(
|
|
|
|
data = shareData,
|
|
|
|
shouldFinishWhenComplete = true,
|
|
|
|
)
|
2023-10-03 22:34:51 +03:00
|
|
|
}
|
2024-01-20 00:01:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@Suppress("MaxLineLength")
|
|
|
|
@Test
|
|
|
|
fun `on ReceiveNewIntent with share data should set the special circumstance to ShareNewSend`() {
|
|
|
|
val viewModel = createViewModel()
|
|
|
|
val mockIntent = mockk<Intent>()
|
|
|
|
val shareData = mockk<IntentManager.ShareData>()
|
|
|
|
every { mockIntent.getCaptchaCallbackTokenResult() } returns null
|
|
|
|
every { intentManager.getShareDataFromIntent(mockIntent) } returns shareData
|
|
|
|
|
|
|
|
viewModel.trySendAction(
|
|
|
|
MainAction.ReceiveNewIntent(
|
|
|
|
intent = mockIntent,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
verify {
|
|
|
|
authRepository.specialCircumstance = UserState.SpecialCircumstance.ShareNewSend(
|
|
|
|
data = shareData,
|
|
|
|
shouldFinishWhenComplete = false,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-17 02:41:34 +03:00
|
|
|
private fun createViewModel() = MainViewModel(
|
|
|
|
authRepository = authRepository,
|
|
|
|
settingsRepository = settingsRepository,
|
2024-01-20 00:01:43 +03:00
|
|
|
intentManager = intentManager,
|
2024-01-17 02:41:34 +03:00
|
|
|
)
|
|
|
|
|
2023-10-03 22:34:51 +03:00
|
|
|
companion object {
|
2024-01-17 02:41:34 +03:00
|
|
|
private const val USER_ID = "userID"
|
|
|
|
private val DEFAULT_USER_STATE = UserState(
|
|
|
|
activeUserId = USER_ID,
|
|
|
|
accounts = listOf(
|
|
|
|
UserState.Account(
|
|
|
|
userId = USER_ID,
|
|
|
|
name = "Active User",
|
|
|
|
email = "active@bitwarden.com",
|
|
|
|
environment = Environment.Us,
|
|
|
|
avatarColorHex = "#aa00aa",
|
|
|
|
isPremium = true,
|
|
|
|
isLoggedIn = true,
|
|
|
|
isVaultUnlocked = true,
|
|
|
|
organizations = emptyList(),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
2023-10-03 22:34:51 +03:00
|
|
|
}
|
|
|
|
}
|