Skip to content
Open
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
Expand Up @@ -62,6 +62,7 @@ open class MessageListPage {
companion object {
val inputField get() = By.res("Stream_ComposerInputField")
val sendButton get() = By.res("Stream_ComposerSendButton")
val cooldownIndicator get() = By.res("Stream_ComposerCooldownIndicator")
val saveButton get() = By.res("Stream_ComposerSaveButton")
val recordAudioButton get() = By.res("Stream_ComposerAudioRecordingButton")
val commandsButton get() = By.res("Stream_ComposerCommandsButton")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import io.getstream.chat.android.compose.uiautomator.findObject
import io.getstream.chat.android.compose.uiautomator.findObjects
import io.getstream.chat.android.compose.uiautomator.height
import io.getstream.chat.android.compose.uiautomator.isDisplayed
import io.getstream.chat.android.compose.uiautomator.isEnabled
import io.getstream.chat.android.compose.uiautomator.retryOnStaleObjectException
import io.getstream.chat.android.compose.uiautomator.seconds
import io.getstream.chat.android.compose.uiautomator.wait
Expand Down Expand Up @@ -239,6 +240,23 @@ fun UserRobot.assertComposerText(expectedText: String): UserRobot {
return this
}

fun UserRobot.assertCooldownIsShown(): UserRobot {
assertTrue(Composer.cooldownIndicator.waitToAppear().isDisplayed())
assertFalse(Composer.sendButton.isDisplayed())
return this
}

fun UserRobot.assertCooldownIsNotShown(): UserRobot {
assertFalse(Composer.cooldownIndicator.waitToDisappear().isDisplayed())
return this
}

fun UserRobot.assertComposerIsDisabledInSlowMode(): UserRobot {
assertFalse(Composer.inputField.isEnabled())
assertFalse(Composer.attachmentsButton.isEnabled())
return this
}

fun UserRobot.assertScrollToBottomButton(isDisplayed: Boolean): UserRobot {
if (isDisplayed) {
assertTrue(MessageListPage.MessageList.scrollToBottomButton.waitToAppear().isDisplayed())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (c) 2014-2026 Stream.io Inc. All rights reserved.
*
* Licensed under the Stream License;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://github.com/GetStream/stream-chat-android/blob/main/LICENSE
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.getstream.chat.android.compose.tests

import io.getstream.chat.android.compose.robots.assertComposerIsDisabledInSlowMode
import io.getstream.chat.android.compose.robots.assertCooldownIsShown
import io.getstream.chat.android.compose.sample.ui.InitTestActivity
import io.qameta.allure.kotlin.Allure.step
import org.junit.Test

class SlowModeTests : StreamTestCase() {

override fun initTestActivity() = InitTestActivity.UserLogin

private val cooldownDuration = 15
private val message = "message"

@Test
fun test_cooldownIsShownWhenNewMessageIsSent() {
step("GIVEN slow mode is enabled on the channel") {
backendRobot.setCooldown(enabled = true, duration = cooldownDuration)
}
step("AND user opens the channel") {
userRobot.login().openChannel()
}
step("WHEN user sends a new message") {
userRobot.sendMessage(message)
}
step("THEN slow mode is active and the cooldown is shown") {
userRobot.assertCooldownIsShown()
}
}

@Test
fun test_composerIsDisabledWhenSlowModeIsActive() {
step("GIVEN slow mode is enabled on the channel") {
backendRobot.setCooldown(enabled = true, duration = cooldownDuration)
}
step("AND user opens the channel") {
userRobot.login().openChannel()
}
step("WHEN user sends a new message") {
userRobot.sendMessage(message)
}
step("THEN the cooldown is shown and the composer is locked") {
userRobot
.assertCooldownIsShown()
.assertComposerIsDisabledInSlowMode()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2010,6 +2010,7 @@ public final class io/getstream/chat/android/compose/ui/messages/composer/Compos
public final fun getLambda$-1206314464$stream_chat_android_compose_release ()Lkotlin/jvm/functions/Function2;
public final fun getLambda$-1267828661$stream_chat_android_compose_release ()Lkotlin/jvm/functions/Function2;
public final fun getLambda$-1344661276$stream_chat_android_compose_release ()Lkotlin/jvm/functions/Function2;
public final fun getLambda$-1483541199$stream_chat_android_compose_release ()Lkotlin/jvm/functions/Function2;
public final fun getLambda$-173232017$stream_chat_android_compose_release ()Lkotlin/jvm/functions/Function2;
public final fun getLambda$1180759242$stream_chat_android_compose_release ()Lkotlin/jvm/functions/Function2;
public final fun getLambda$1309976052$stream_chat_android_compose_release ()Lkotlin/jvm/functions/Function2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import io.getstream.chat.android.compose.ui.theme.ChatTheme
Expand All @@ -43,6 +44,7 @@ public fun CoolDownIndicator(
) {
Box(
modifier = modifier
.testTag("Stream_ComposerCooldownIndicator")
.size(48.dp)
.padding(8.dp)
.background(shape = RoundedCornerShape(24.dp), color = ChatTheme.colors.textDisabled),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,18 @@ public fun MessageInput(
recordingActions: AudioRecordingActions = AudioRecordingActions.None,
onActiveCommandDismiss: () -> Unit = {},
) {
// While slow mode is active the whole composer is locked, so the border uses the disabled colour.
val isInCoolDown = messageComposerState.coolDownTime > 0
val borderColor = if (isInCoolDown) {
ChatTheme.colors.borderUtilityDisabled
} else {
ChatTheme.colors.borderCoreDefault
}
Column(
modifier = modifier
.border(
width = 1.dp,
color = ChatTheme.colors.borderCoreDefault,
color = borderColor,
shape = MessageInputShape,
)
.then(
Expand Down Expand Up @@ -315,7 +322,6 @@ private fun MessageComposerInputSlowModePreview() {
internal fun MessageComposerInputSlowMode() {
MessageInput(
messageComposerState = PreviewMessageComposerState.copy(
inputValue = "Slow mode, wait 9s",
coolDownTime = 9,
),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,24 @@ internal fun MessageComposerFixedStyleWithCommandSuggestions() {
)
}

@Preview
@Composable
private fun MessageComposerSlowModePreview() {
ChatTheme {
MessageComposerSlowMode()
}
}

@Composable
internal fun MessageComposerSlowMode() {
MessageComposer(
messageComposerState = PreviewMessageComposerState.copy(
coolDownTime = 9,
),
onSendMessage = { _, _ -> },
)
}

@Preview(showBackground = true)
@Composable
private fun MessageComposerFloatingStylePreview() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,27 +113,36 @@ internal fun MessageComposerInputCenterContent(
if (value.isEmpty()) {
TextFieldPlaceholder(
canSendMessage = canSendMessage,
coolDownTime = state.coolDownTime,
activeCommandDescriptionRes = state.activeCommand?.placeholderRes,
)
}
}
},
maxLines = TextFieldMaxLines,
singleLine = false,
enabled = canSendMessage,
enabled = canSendMessage && state.coolDownTime <= 0,
keyboardOptions = KeyboardOptions(capitalization = KeyboardCapitalization.Sentences),
)
}

@Composable
private fun TextFieldPlaceholder(
canSendMessage: Boolean,
coolDownTime: Int,
@StringRes activeCommandDescriptionRes: Int?,
) {
val text = if (canSendMessage) {
stringResource(activeCommandDescriptionRes ?: UiCommonR.string.stream_ui_message_composer_placeholder_default)
} else {
stringResource(UiCommonR.string.stream_ui_message_composer_placeholder_cannot_send_messages)
val text = when {
coolDownTime > 0 ->
stringResource(UiCommonR.string.stream_ui_message_composer_placeholder_slow_mode, coolDownTime)

canSendMessage ->
stringResource(
activeCommandDescriptionRes ?: UiCommonR.string.stream_ui_message_composer_placeholder_default,
)

else ->
stringResource(UiCommonR.string.stream_ui_message_composer_placeholder_cannot_send_messages)
}
Text(
text = text,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ internal fun MessageComposerLeadingContent(

val isCommandActive = messageInputState.activeCommand != null

// During slow mode the button stays visible but becomes non-interactive.
val isInCoolDown = messageInputState.coolDownTime > 0

val showAttachmentButton = canSendMessage && !isRecording && canUploadFile && !isCommandActive

AnimatedContent(
Expand All @@ -73,6 +76,7 @@ internal fun MessageComposerLeadingContent(
AttachmentPickerButton(
isPickerVisible = isAttachmentPickerVisible,
iconRotation = iconRotation,
enabled = !isInCoolDown,
onClick = onAttachmentsClick,
onClickLabel = onAttachmentsClickLabel,
)
Expand All @@ -84,6 +88,7 @@ internal fun MessageComposerLeadingContent(
private fun AttachmentPickerButton(
isPickerVisible: Boolean,
iconRotation: Float,
enabled: Boolean = true,
onClick: () -> Unit,
onClickLabel: String? = null,
) {
Expand All @@ -106,7 +111,7 @@ private fun AttachmentPickerButton(
.padding(end = 8.dp)
.border(
width = 1.dp,
color = ChatTheme.colors.borderCoreDefault,
color = if (enabled) ChatTheme.colors.borderCoreDefault else ChatTheme.colors.borderUtilityDisabled,
shape = CircleShape,
)
.then(
Expand All @@ -120,11 +125,14 @@ private fun AttachmentPickerButton(
.testTag("Stream_ComposerAttachmentsButton")
.semantics {
stateDescription = pickerStateDescription
onClick(label = resolvedActionLabel) {
onClick()
true
if (enabled) {
onClick(label = resolvedActionLabel) {
onClick()
true
}
}
},
enabled = enabled,
colors = IconButtonDefaults.filledIconButtonColors(
containerColor = ChatTheme.colors.backgroundCoreElevation1,
disabledContainerColor = ChatTheme.colors.backgroundCoreElevation1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import io.getstream.chat.android.compose.ui.messages.composer.MessageComposerFlo
import io.getstream.chat.android.compose.ui.messages.composer.MessageComposerFloatingStyleWithCommandSuggestions
import io.getstream.chat.android.compose.ui.messages.composer.MessageComposerFloatingStyleWithUserSuggestions
import io.getstream.chat.android.compose.ui.messages.composer.MessageComposerFloatingStyleWithVisibleAttachmentPicker
import io.getstream.chat.android.compose.ui.messages.composer.MessageComposerSlowMode
import org.junit.Rule
import org.junit.Test

Expand Down Expand Up @@ -64,6 +65,13 @@ internal class MessageComposerTest : PaparazziComposeTest {
}
}

@Test
fun `slow mode`() {
snapshotWithDarkMode(contentAlignment = Alignment.BottomCenter) {
MessageComposerSlowMode()
}
}

@Test
fun `floating style`() {
snapshotWithDarkMode(contentAlignment = Alignment.BottomCenter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@
package io.getstream.chat.android.compose.ui.messages.composer

import androidx.annotation.UiThread
import androidx.compose.ui.test.assertIsEnabled
import androidx.compose.ui.test.assertIsNotEnabled
import androidx.compose.ui.test.hasText
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.onNodeWithText
import androidx.test.ext.junit.runners.AndroidJUnit4
import io.getstream.chat.android.client.test.MockedChatClientTest
import io.getstream.chat.android.compose.ui.theme.ChatTheme
import io.getstream.chat.android.compose.viewmodel.messages.MessageComposerViewModel
import io.getstream.chat.android.models.ChannelCapabilities
import io.getstream.chat.android.previewdata.PreviewUserData
import io.getstream.chat.android.randomCommand
import io.getstream.chat.android.randomFloat
Expand Down Expand Up @@ -102,6 +106,42 @@ internal class MessageComposerScreenTest : MockedChatClientTest {
composeTestRule.onNodeWithText("Also send in Channel").assertExists()
}

@Test
@UiThread
fun `slow mode disables input and attachment button`() {
val capabilities = setOf(ChannelCapabilities.SEND_MESSAGE, ChannelCapabilities.UPLOAD_FILE)
whenever(mockViewModel.messageComposerState) doReturn
MutableStateFlow(MessageComposerState(ownCapabilities = capabilities, coolDownTime = 9))

composeTestRule.setContent {
ChatTheme {
MessageComposer(viewModel = mockViewModel)
}
}

composeTestRule.onNodeWithText("9").assertExists()
composeTestRule.onNodeWithText("Slow mode, wait 9s…").assertExists()
composeTestRule.onNodeWithTag("Stream_ComposerInputField").assertIsNotEnabled()
composeTestRule.onNodeWithTag("Stream_ComposerAttachmentsButton").assertIsNotEnabled()
}

@Test
@UiThread
fun `composer stays interactive without cooldown`() {
val capabilities = setOf(ChannelCapabilities.SEND_MESSAGE, ChannelCapabilities.UPLOAD_FILE)
whenever(mockViewModel.messageComposerState) doReturn
MutableStateFlow(MessageComposerState(ownCapabilities = capabilities, coolDownTime = 0))

composeTestRule.setContent {
ChatTheme {
MessageComposer(viewModel = mockViewModel)
}
}

composeTestRule.onNodeWithTag("Stream_ComposerInputField").assertIsEnabled()
composeTestRule.onNodeWithTag("Stream_ComposerAttachmentsButton").assertIsEnabled()
}

@Test
@UiThread
fun `audio recording`() {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ public class BackendRobot(
return this
}

public fun setCooldown(enabled: Boolean, duration: Int): BackendRobot {
waitForMockServerToStart()
mockServer.postRequest("config/cooldown?enabled=$enabled&duration=$duration")
return this
}

public fun revokeToken(duration: Int = 5) {
waitForMockServerToStart()
mockServer.postRequest("jwt/revoke_token?duration=$duration")
Expand Down
Loading
Loading