Add non admin group test

This commit is contained in:
andrew 2023-09-20 23:29:35 +09:30
parent cd0022e0ee
commit 7738b19760
2 changed files with 90 additions and 18 deletions

View File

@ -242,7 +242,9 @@ val defaultTimes = listOf(12.hours, 1.days, 7.days, 14.days)
val afterSendTimes = defaultTimes val afterSendTimes = defaultTimes
val afterSendModes = afterSendTimes.map { it.inWholeSeconds }.map(ExpiryMode::AfterSend) val afterSendModes = afterSendTimes.map { it.inWholeSeconds }.map(ExpiryMode::AfterSend)
val legacyModes = afterSendTimes.map { it.inWholeSeconds }.map(ExpiryMode::Legacy)
fun afterSendOptions(state: State) = afterSendModes.map { timeOption(it, state) } fun afterSendOptions(state: State) = afterSendModes.map { timeOption(it, state) }
fun legacyOptions(state: State) = legacyModes.map { timeOption(it, state) }
val afterReadTimes = buildList { val afterReadTimes = buildList {
add(5.minutes) add(5.minutes)
@ -252,16 +254,26 @@ val afterReadTimes = buildList {
val afterReadModes = afterReadTimes.map { it.inWholeSeconds }.map(ExpiryMode::AfterRead) val afterReadModes = afterReadTimes.map { it.inWholeSeconds }.map(ExpiryMode::AfterRead)
fun afterReadOptions(state: State) = afterReadModes.map { timeOption(it, state) } fun afterReadOptions(state: State) = afterReadModes.map { timeOption(it, state) }
private fun timeOptions(state: State): List<OptionModel>? = private fun timeOptions(
if (state.typeOptionsHidden) timeOptionsAfterSend(state) state: State
else when (state.expiryMode) { ): List<OptionModel>? {
is ExpiryMode.Legacy, is ExpiryMode.AfterRead -> debugOptions(state) + afterReadOptions(state) val type = state.takeUnless {
is ExpiryMode.AfterSend -> debugOptions(state) + afterSendOptions(state) it.typeOptionsHidden
else -> null }?.expiryType ?: if (state.isNewConfigEnabled) ExpiryType.AFTER_SEND else ExpiryType.LEGACY
}
private fun timeOptionsAfterSend(state: State): List<OptionModel> = return when (type) {
listOf(offTypeOption(state)) + debugOptions(state) + afterSendOptions(state) ExpiryType.AFTER_READ -> afterReadOptions(state)
ExpiryType.AFTER_SEND -> afterSendOptions(state)
ExpiryType.LEGACY -> legacyOptions(state)
else -> null
}?.let {
buildList {
if (state.typeOptionsHidden) add(offTypeOption(state))
addAll(debugOptions(state))
addAll(it)
}
}
}
fun timeOption( fun timeOption(
mode: ExpiryMode, mode: ExpiryMode,

View File

@ -13,12 +13,16 @@ import org.junit.runner.RunWith
import org.mockito.Mock import org.mockito.Mock
import org.mockito.Mockito import org.mockito.Mockito
import org.mockito.junit.MockitoJUnitRunner import org.mockito.junit.MockitoJUnitRunner
import org.mockito.kotlin.any
import org.mockito.kotlin.whenever import org.mockito.kotlin.whenever
import org.session.libsession.messaging.messages.ExpirationConfiguration import org.session.libsession.messaging.messages.ExpirationConfiguration
import org.session.libsession.utilities.Address import org.session.libsession.utilities.Address
import org.session.libsession.utilities.GroupRecord
import org.session.libsession.utilities.GroupUtil
import org.session.libsession.utilities.SSKEnvironment import org.session.libsession.utilities.SSKEnvironment
import org.session.libsession.utilities.TextSecurePreferences import org.session.libsession.utilities.TextSecurePreferences
import org.session.libsession.utilities.recipients.Recipient import org.session.libsession.utilities.recipients.Recipient
import org.session.libsignal.utilities.guava.Optional
import org.thoughtcrime.securesms.MainCoroutineRule import org.thoughtcrime.securesms.MainCoroutineRule
import org.thoughtcrime.securesms.database.GroupDatabase import org.thoughtcrime.securesms.database.GroupDatabase
import org.thoughtcrime.securesms.database.Storage import org.thoughtcrime.securesms.database.Storage
@ -32,6 +36,8 @@ import kotlin.time.Duration.Companion.minutes
private const val THREAD_ID = 1L private const val THREAD_ID = 1L
private const val LOCAL_NUMBER = "05---local---address" private const val LOCAL_NUMBER = "05---local---address"
private val LOCAL_ADDRESS = Address.fromSerialized(LOCAL_NUMBER) private val LOCAL_ADDRESS = Address.fromSerialized(LOCAL_NUMBER)
private const val GROUP_NUMBER = "${GroupUtil.OPEN_GROUP_PREFIX}4133"
private val GROUP_ADDRESS = Address.fromSerialized(GROUP_NUMBER)
@OptIn(ExperimentalCoroutinesApi::class) @OptIn(ExperimentalCoroutinesApi::class)
@RunWith(MockitoJUnitRunner::class) @RunWith(MockitoJUnitRunner::class)
@ -48,6 +54,7 @@ class ExpirationSettingsViewModelTest {
@Mock lateinit var groupDb: GroupDatabase @Mock lateinit var groupDb: GroupDatabase
@Mock lateinit var storage: Storage @Mock lateinit var storage: Storage
@Mock lateinit var recipient: Recipient @Mock lateinit var recipient: Recipient
@Mock lateinit var groupRecord: GroupRecord
@Test @Test
fun `note to self, off, new config`() = runTest { fun `note to self, off, new config`() = runTest {
@ -120,10 +127,50 @@ class ExpirationSettingsViewModelTest {
CardModel( CardModel(
R.string.activity_expiration_settings_timer, R.string.activity_expiration_settings_timer,
typeOption(ExpiryMode.NONE, selected = true), typeOption(ExpiryMode.NONE, selected = true),
timeOption(ExpiryType.AFTER_SEND, 12.hours), timeOption(ExpiryType.LEGACY, 12.hours),
timeOption(ExpiryType.AFTER_SEND, 1.days), timeOption(ExpiryType.LEGACY, 1.days),
timeOption(ExpiryType.AFTER_SEND, 7.days), timeOption(ExpiryType.LEGACY, 7.days),
timeOption(ExpiryType.AFTER_SEND, 14.days) timeOption(ExpiryType.LEGACY, 14.days)
)
)
)
}
@Test
fun `group, off, not admin, new config`() = runTest {
mockGroup(ExpiryMode.NONE)
val viewModel = createViewModel()
advanceUntilIdle()
assertThat(
viewModel.state.value
).isEqualTo(
State(
isGroup = true,
isSelfAdmin = false,
address = GROUP_ADDRESS,
isNoteToSelf = false,
expiryMode = ExpiryMode.NONE,
isNewConfigEnabled = true,
persistedMode = ExpiryMode.NONE,
showDebugOptions = false
)
)
assertThat(
viewModel.uiState.value
).isEqualTo(
UiState(
showGroupFooter = true,
CardModel(
R.string.activity_expiration_settings_timer,
typeOption(ExpiryMode.NONE, enabled = false, selected = true),
timeOption(ExpiryType.AFTER_SEND, 12.hours, enabled = false),
timeOption(ExpiryType.AFTER_SEND, 1.days, enabled = false),
timeOption(ExpiryType.AFTER_SEND, 7.days, enabled = false),
timeOption(ExpiryType.AFTER_SEND, 14.days, enabled = false)
) )
) )
) )
@ -254,10 +301,10 @@ class ExpirationSettingsViewModelTest {
), ),
CardModel( CardModel(
GetString(R.string.activity_expiration_settings_timer), GetString(R.string.activity_expiration_settings_timer),
timeOption(ExpiryType.AFTER_SEND, 12.hours, selected = true), timeOption(ExpiryType.LEGACY, 12.hours, selected = true),
timeOption(ExpiryType.AFTER_SEND, 1.days), timeOption(ExpiryType.LEGACY, 1.days),
timeOption(ExpiryType.AFTER_SEND, 7.days), timeOption(ExpiryType.LEGACY, 7.days),
timeOption(ExpiryType.AFTER_SEND, 14.days) timeOption(ExpiryType.LEGACY, 14.days)
) )
) )
) )
@ -435,7 +482,7 @@ class ExpirationSettingsViewModelTest {
mock1on1(ExpiryType.AFTER_SEND.mode(time), someAddress) mock1on1(ExpiryType.AFTER_SEND.mode(time), someAddress)
} }
private fun mock1on1(mode: ExpiryMode, someAddress: Address, new: Boolean = true) { private fun mock1on1(mode: ExpiryMode, someAddress: Address) {
val config = config(mode) val config = config(mode)
whenever(threadDb.getRecipientForThreadId(Mockito.anyLong())).thenReturn(recipient) whenever(threadDb.getRecipientForThreadId(Mockito.anyLong())).thenReturn(recipient)
@ -445,6 +492,19 @@ class ExpirationSettingsViewModelTest {
whenever(recipient.address).thenReturn(someAddress) whenever(recipient.address).thenReturn(someAddress)
} }
private fun mockGroup(mode: ExpiryMode) {
val config = config(mode)
whenever(threadDb.getRecipientForThreadId(Mockito.anyLong())).thenReturn(recipient)
whenever(storage.getExpirationConfiguration(Mockito.anyLong())).thenReturn(config)
whenever(textSecurePreferences.getLocalNumber()).thenReturn(LOCAL_NUMBER)
whenever(recipient.isClosedGroupRecipient).thenReturn(false)
whenever(recipient.address).thenReturn(GROUP_ADDRESS)
whenever(recipient.isClosedGroupRecipient).thenReturn(true)
whenever(groupDb.getGroup(any<String>())).thenReturn(Optional.of(groupRecord))
whenever(groupRecord.admins).thenReturn(listOf())
}
private fun config(mode: ExpiryMode) = ExpirationConfiguration( private fun config(mode: ExpiryMode) = ExpirationConfiguration(
threadId = THREAD_ID, threadId = THREAD_ID,
expiryMode = mode, expiryMode = mode,