Save the clear clipboard frequency in settings. (#886)

This commit is contained in:
Oleg Semenenko
2024-06-20 17:08:07 +01:00
committed by Álison Fernandes
parent 526ab51a90
commit a985cfaccc
13 changed files with 224 additions and 45 deletions
@@ -4,6 +4,7 @@ import androidx.core.content.edit
import app.cash.turbine.test
import com.x8bit.bitwarden.data.platform.base.FakeSharedPreferences
import com.x8bit.bitwarden.data.platform.datasource.network.di.PlatformNetworkModule
import com.x8bit.bitwarden.data.platform.repository.model.ClearClipboardFrequency
import com.x8bit.bitwarden.data.platform.repository.model.UriMatchType
import com.x8bit.bitwarden.data.platform.repository.model.VaultTimeoutAction
import com.x8bit.bitwarden.ui.platform.feature.settings.appearance.model.AppLanguage
@@ -875,4 +876,64 @@ class SettingsDiskSourceTest {
)
assertFalse(fakeSharedPreferences.contains(screenCaptureAllowKey))
}
@Test
fun `storeClearClipboardFrequency should update SharedPreferences`() {
val clearClipboardBaseKey = "bwPreferencesStorage:clearClipboard"
val mockUserId = "mockUserId"
val clearClipboardKey = "${clearClipboardBaseKey}_$mockUserId"
assertNull(settingsDiskSource.getClearClipboardFrequencySeconds(mockUserId))
assertEquals(fakeSharedPreferences.getInt(mockUserId, 0), 0)
// Updating the disk source updates shared preferences
settingsDiskSource.storeClearClipboardFrequencySeconds(
mockUserId,
ClearClipboardFrequency.ONE_MINUTE.frequencySeconds,
)
assertEquals(
ClearClipboardFrequency.ONE_MINUTE.frequencySeconds,
fakeSharedPreferences.getInt(clearClipboardKey, 0),
)
}
@Suppress("MaxLineLength")
@Test
fun `storeClearClipboardFrequency should clear the SharedPreferences value if the value is null`() {
val clearClipboardBaseKey = "bwPreferencesStorage:clearClipboard"
val mockUserId = "mockUserId"
val clearClipboardKey = "${clearClipboardBaseKey}_$mockUserId"
assertNull(settingsDiskSource.getClearClipboardFrequencySeconds(mockUserId))
assertEquals(fakeSharedPreferences.getInt(mockUserId, 0), 0)
// Updating the disk source updates shared preferences
settingsDiskSource.storeClearClipboardFrequencySeconds(
mockUserId,
null,
)
assertFalse(fakeSharedPreferences.contains(clearClipboardKey))
}
@Test
fun `getClearClipboardFrequency should pull from SharedPreferences`() {
val clearClipboardBaseKey = "bwPreferencesStorage:clearClipboard"
val mockUserId = "mockUserId"
val expectedValue = 20
val clearClipboardKey = "${clearClipboardBaseKey}_$mockUserId"
assertNull(settingsDiskSource.getClearClipboardFrequencySeconds(mockUserId))
assertEquals(fakeSharedPreferences.getInt(mockUserId, 0), 0)
// Update SharedPreferences updates the disk source
fakeSharedPreferences.edit {
putInt(clearClipboardKey, expectedValue)
}
assertEquals(
expectedValue,
settingsDiskSource.getClearClipboardFrequencySeconds(mockUserId),
)
}
}
@@ -44,6 +44,7 @@ class FakeSettingsDiskSource : SettingsDiskSource {
private val storedVaultTimeoutActions = mutableMapOf<String, VaultTimeoutAction?>()
private val storedVaultTimeoutInMinutes = mutableMapOf<String, Int?>()
private val storedUriMatchTypes = mutableMapOf<String, UriMatchType?>()
private val storedClearClipboardFrequency = mutableMapOf<String, Int?>()
private val storedDisableAutofillSavePrompt = mutableMapOf<String, Boolean?>()
private val storedPullToRefreshEnabled = mutableMapOf<String, Boolean?>()
private val storedInlineAutofillEnabled = mutableMapOf<String, Boolean?>()
@@ -121,6 +122,7 @@ class FakeSettingsDiskSource : SettingsDiskSource {
storedInlineAutofillEnabled.remove(userId)
storedBlockedAutofillUris.remove(userId)
storedScreenCaptureAllowed.remove(userId)
storedClearClipboardFrequency.remove(userId)
mutableVaultTimeoutActionsFlowMap.remove(userId)
mutableVaultTimeoutInMinutesFlowMap.remove(userId)
@@ -168,6 +170,13 @@ class FakeSettingsDiskSource : SettingsDiskSource {
getMutableVaultTimeoutActionsFlow(userId = userId).tryEmit(vaultTimeoutAction)
}
override fun getClearClipboardFrequencySeconds(userId: String): Int? =
storedClearClipboardFrequency[userId]
override fun storeClearClipboardFrequencySeconds(userId: String, frequency: Int?) {
storedClearClipboardFrequency[userId] = frequency
}
override fun getDefaultUriMatchType(userId: String): UriMatchType? =
storedUriMatchTypes[userId]
@@ -12,6 +12,7 @@ import com.x8bit.bitwarden.data.platform.base.FakeDispatcherManager
import com.x8bit.bitwarden.data.platform.datasource.disk.util.FakeSettingsDiskSource
import com.x8bit.bitwarden.data.platform.manager.BiometricsEncryptionManager
import com.x8bit.bitwarden.data.platform.repository.model.BiometricsKeyResult
import com.x8bit.bitwarden.data.platform.repository.model.ClearClipboardFrequency
import com.x8bit.bitwarden.data.platform.repository.model.UriMatchType
import com.x8bit.bitwarden.data.platform.repository.model.VaultTimeout
import com.x8bit.bitwarden.data.platform.repository.model.VaultTimeoutAction
@@ -872,6 +873,31 @@ class SettingsRepositoryTest {
assertEquals(false, fakeSettingsDiskSource.getScreenCaptureAllowed(userId))
}
}
@Suppress("MaxLineLength")
@Test
fun `clearClipboardFrequency should pull from and update SettingsDiskSource`() =
runTest {
val userId = "userId"
fakeAuthDiskSource.userState = MOCK_USER_STATE
fakeSettingsDiskSource.storeClearClipboardFrequencySeconds(
userId,
ClearClipboardFrequency.ONE_MINUTE.frequencySeconds,
)
assertEquals(
ClearClipboardFrequency.ONE_MINUTE,
settingsRepository.clearClipboardFrequency,
)
settingsRepository.clearClipboardFrequency = ClearClipboardFrequency.TEN_SECONDS
assertEquals(
ClearClipboardFrequency.TEN_SECONDS,
settingsRepository.clearClipboardFrequency,
)
}
}
private val MOCK_USER_STATE =
@@ -9,6 +9,7 @@ import androidx.compose.ui.test.onAllNodesWithText
import androidx.compose.ui.test.onNodeWithContentDescription
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import com.x8bit.bitwarden.data.platform.repository.model.ClearClipboardFrequency
import com.x8bit.bitwarden.data.platform.repository.util.bufferedMutableSharedFlow
import com.x8bit.bitwarden.ui.platform.base.BaseComposeTest
import com.x8bit.bitwarden.ui.platform.base.util.asText
@@ -103,7 +104,7 @@ class OtherScreenTest : BaseComposeTest() {
verify {
viewModel.trySendAction(
OtherAction.ClearClipboardFrequencyChange(
clearClipboardFrequency = OtherState.ClearClipboardFrequency.TEN_SECONDS,
clearClipboardFrequency = ClearClipboardFrequency.TEN_SECONDS,
),
)
}
@@ -148,7 +149,7 @@ class OtherScreenTest : BaseComposeTest() {
private val DEFAULT_STATE = OtherState(
allowScreenCapture = false,
allowSyncOnRefresh = false,
clearClipboardFrequency = OtherState.ClearClipboardFrequency.DEFAULT,
clearClipboardFrequency = ClearClipboardFrequency.NEVER,
lastSyncTime = "5/14/2023 4:52 PM",
dialogState = null,
)
@@ -4,6 +4,7 @@ import androidx.lifecycle.SavedStateHandle
import app.cash.turbine.test
import com.x8bit.bitwarden.R
import com.x8bit.bitwarden.data.platform.repository.SettingsRepository
import com.x8bit.bitwarden.data.platform.repository.model.ClearClipboardFrequency
import com.x8bit.bitwarden.data.vault.repository.VaultRepository
import com.x8bit.bitwarden.ui.platform.base.BaseViewModelTest
import com.x8bit.bitwarden.ui.platform.base.util.asText
@@ -31,6 +32,8 @@ class OtherViewModelTest : BaseViewModelTest() {
private val mutableVaultLastSyncStateFlow = MutableStateFlow(instant)
private val mutableScreenCaptureAllowedStateFlow = MutableStateFlow(isAllowed)
private val settingsRepository = mockk<SettingsRepository> {
every { clearClipboardFrequency } returns ClearClipboardFrequency.NEVER
every { clearClipboardFrequency = any() } just runs
every { getPullToRefreshEnabledFlow() } returns mutablePullToRefreshStateFlow
every { vaultLastSyncStateFlow } returns mutableVaultLastSyncStateFlow
every { vaultLastSync } returns instant
@@ -49,7 +52,7 @@ class OtherViewModelTest : BaseViewModelTest() {
@Test
fun `initial state should be correct when set`() {
val state = DEFAULT_STATE.copy(
clearClipboardFrequency = OtherState.ClearClipboardFrequency.FIVE_MINUTES,
clearClipboardFrequency = ClearClipboardFrequency.FIVE_MINUTES,
)
val viewModel = createViewModel(state = state)
assertEquals(state, viewModel.stateFlow.value)
@@ -57,23 +60,28 @@ class OtherViewModelTest : BaseViewModelTest() {
@Suppress("MaxLineLength")
@Test
fun `on AllowScreenCaptureToggled should update value in state and SettingsRepository`() = runTest {
val viewModel = createViewModel()
val newScreenCaptureAllowedValue = true
fun `on AllowScreenCaptureToggled should update value in state and SettingsRepository`() =
runTest {
val viewModel = createViewModel()
val newScreenCaptureAllowedValue = true
viewModel.trySendAction(OtherAction.AllowScreenCaptureToggle(newScreenCaptureAllowedValue))
verify(exactly = 1) {
settingsRepository.isScreenCaptureAllowed = newScreenCaptureAllowedValue
}
viewModel.stateFlow.test {
assertEquals(
DEFAULT_STATE.copy(allowScreenCapture = true),
awaitItem(),
viewModel.trySendAction(
OtherAction.AllowScreenCaptureToggle(
newScreenCaptureAllowedValue,
),
)
verify(exactly = 1) {
settingsRepository.isScreenCaptureAllowed = newScreenCaptureAllowedValue
}
viewModel.stateFlow.test {
assertEquals(
DEFAULT_STATE.copy(allowScreenCapture = true),
awaitItem(),
)
}
}
}
@Test
fun `on AllowSyncToggled should update value in state`() {
@@ -110,12 +118,12 @@ class OtherViewModelTest : BaseViewModelTest() {
)
viewModel.trySendAction(
OtherAction.ClearClipboardFrequencyChange(
clearClipboardFrequency = OtherState.ClearClipboardFrequency.ONE_MINUTE,
clearClipboardFrequency = ClearClipboardFrequency.ONE_MINUTE,
),
)
assertEquals(
DEFAULT_STATE.copy(
clearClipboardFrequency = OtherState.ClearClipboardFrequency.ONE_MINUTE,
clearClipboardFrequency = ClearClipboardFrequency.ONE_MINUTE,
),
awaitItem(),
)
@@ -173,7 +181,7 @@ class OtherViewModelTest : BaseViewModelTest() {
private val DEFAULT_STATE = OtherState(
allowScreenCapture = false,
allowSyncOnRefresh = false,
clearClipboardFrequency = OtherState.ClearClipboardFrequency.DEFAULT,
clearClipboardFrequency = ClearClipboardFrequency.NEVER,
lastSyncTime = "10/26/2023 12:00 PM",
dialogState = null,
)