diff --git a/app/src/test/kotlin/com/wire/android/ui/home/messagecomposer/MessageComposerStateHolderTest.kt b/app/src/test/kotlin/com/wire/android/ui/home/messagecomposer/MessageComposerStateHolderTest.kt index 8a55dfb5bd5..7d435d6b520 100644 --- a/app/src/test/kotlin/com/wire/android/ui/home/messagecomposer/MessageComposerStateHolderTest.kt +++ b/app/src/test/kotlin/com/wire/android/ui/home/messagecomposer/MessageComposerStateHolderTest.kt @@ -19,12 +19,12 @@ package com.wire.android.ui.home.messagecomposer import android.content.Context +import androidx.compose.foundation.text.input.TextFieldState +import androidx.compose.foundation.text.input.setTextAndPlaceCursorAtEnd import androidx.compose.runtime.MutableState import androidx.compose.runtime.State import androidx.compose.runtime.mutableStateOf import androidx.compose.ui.focus.FocusRequester -import androidx.compose.ui.text.TextRange -import androidx.compose.ui.text.input.TextFieldValue import com.wire.android.config.CoroutineTestExtension import com.wire.android.config.SnapshotExtension import com.wire.android.framework.TestConversation @@ -65,7 +65,7 @@ class MessageComposerStateHolderTest { private lateinit var messageCompositionHolder: State private lateinit var additionalOptionStateHolder: AdditionalOptionStateHolder private lateinit var state: MessageComposerStateHolder - private lateinit var messageTextFieldValue: MutableState + private lateinit var messageTextState: TextFieldState @BeforeEach fun before() { @@ -74,16 +74,16 @@ class MessageComposerStateHolderTest { every { focusRequester.captureFocus() } returns true messageComposerViewState = mutableStateOf(MessageComposerViewState()) messageComposition = mutableStateOf(MessageComposition(TestConversation.ID)) - messageTextFieldValue = mutableStateOf(TextFieldValue()) + messageTextState = TextFieldState() messageCompositionInputStateHolder = MessageCompositionInputStateHolder( - messageTextFieldValue = messageTextFieldValue, + messageTextState = messageTextState, keyboardController = null, focusRequester = focusRequester ) messageCompositionHolder = mutableStateOf( MessageCompositionHolder( messageComposition = messageComposition, - messageTextFieldValue = messageTextFieldValue, + messageTextState = messageTextState, onClearDraft = {}, onSaveDraft = {}, onSearchMentionQueryChanged = {}, @@ -134,13 +134,9 @@ class MessageComposerStateHolderTest { editMessageText = "edit_message_text", mentions = listOf() ) - - state.messageCompositionHolder.value.messageTextFieldValue.value = - messageTextFieldValue.value.copy( - text = messageTextFieldValue.value.text + "some text", - selection = TextRange(messageTextFieldValue.value.text.length + "some text".length) - ) - + state.messageCompositionHolder.value.messageTextState.edit { + append("some text") + } assertInstanceOf(InputType.Editing::class.java, messageCompositionInputStateHolder.inputType).also { assertEquals(true, it.isEditButtonEnabled) } @@ -154,7 +150,7 @@ class MessageComposerStateHolderTest { state.toReply(mockMessageWithText) // then - assertEquals(String.EMPTY, messageCompositionHolder.value.messageTextFieldValue.value.text) + assertEquals(String.EMPTY, messageCompositionHolder.value.messageTextState.text.toString()) assertInstanceOf(InputType.Composing::class.java, messageCompositionInputStateHolder.inputType) } @@ -162,15 +158,13 @@ class MessageComposerStateHolderTest { fun `given some message was being composed, when setting toReply, then input continues with the current text`() = runTest { // given val currentText = "Potato" - messageTextFieldValue.value = messageTextFieldValue.value.copy( - text = currentText, - selection = TextRange(currentText.length) - ) + messageCompositionHolder.value.messageTextState.setTextAndPlaceCursorAtEnd(currentText) + // when state.toReply(mockMessageWithText) // then - assertEquals(currentText, messageCompositionHolder.value.messageTextFieldValue.value.text) + assertEquals(currentText, messageCompositionHolder.value.messageTextState.text.toString()) } @Test @@ -197,7 +191,7 @@ class MessageComposerStateHolderTest { // then assertEquals( String.EMPTY, - messageCompositionHolder.value.messageTextFieldValue.value.text + messageCompositionHolder.value.messageTextState.text.toString() ) assertEquals( null, diff --git a/app/src/test/kotlin/com/wire/android/ui/home/messagecomposer/state/MessageCompositionHolderTest.kt b/app/src/test/kotlin/com/wire/android/ui/home/messagecomposer/state/MessageCompositionHolderTest.kt index e0cab8fb694..91fee977b50 100644 --- a/app/src/test/kotlin/com/wire/android/ui/home/messagecomposer/state/MessageCompositionHolderTest.kt +++ b/app/src/test/kotlin/com/wire/android/ui/home/messagecomposer/state/MessageCompositionHolderTest.kt @@ -18,10 +18,10 @@ package com.wire.android.ui.home.messagecomposer.state import android.content.Context +import androidx.compose.foundation.text.input.TextFieldState import androidx.compose.runtime.MutableState import androidx.compose.runtime.mutableStateOf import androidx.compose.ui.text.TextRange -import androidx.compose.ui.text.input.TextFieldValue import com.wire.android.config.SnapshotExtension import com.wire.android.framework.TestConversation import com.wire.android.ui.home.messagecomposer.model.MessageComposition @@ -47,7 +47,7 @@ class MessageCompositionHolderTest { private lateinit var state: MessageCompositionHolder private lateinit var messageComposition: MutableState - private lateinit var messageTextFieldValue: MutableState + private lateinit var messageTextState: TextFieldState private val dispatcher = StandardTestDispatcher() @BeforeEach @@ -56,10 +56,10 @@ class MessageCompositionHolderTest { Dispatchers.setMain(dispatcher) messageComposition = mutableStateOf(MessageComposition(TestConversation.ID)) - messageTextFieldValue = mutableStateOf(TextFieldValue()) + messageTextState = TextFieldState() state = MessageCompositionHolder( messageComposition = messageComposition, - messageTextFieldValue = messageTextFieldValue, + messageTextState = messageTextState, onClearDraft = {}, onSaveDraft = {}, onSearchMentionQueryChanged = {}, @@ -82,7 +82,7 @@ class MessageCompositionHolderTest { // then assertEquals( "# ", - state.messageTextFieldValue.value.text + state.messageTextState.text.toString() ) } @@ -95,7 +95,7 @@ class MessageCompositionHolderTest { // then assertEquals( "****", - state.messageTextFieldValue.value.text + state.messageTextState.text.toString() ) } @@ -108,52 +108,62 @@ class MessageCompositionHolderTest { // then assertEquals( "__", - state.messageTextFieldValue.value.text + state.messageTextState.text.toString() ) } @Test fun `given non empty text, when adding header markdown on selection, then # is added to the text`() = runTest { // given - val newText = "header" - state.messageTextFieldValue.value = messageTextFieldValue.value.copy( - text = newText, - selection = TextRange(0, 6) - ) + state.messageTextState.edit { + replace(0, length, "header") + selection = TextRange( + start = 0, + end = 6 + ) + } + // when state.addOrRemoveMessageMarkdown(markdown = RichTextMarkdown.Header) // then assertEquals( "# header", - state.messageTextFieldValue.value.text + state.messageTextState.text.toString() ) } @Test fun `given non empty text, when adding bold markdown on selection, then 2x star char is added to the text`() = runTest { // given - state.messageTextFieldValue.value = messageTextFieldValue.value.copy( - text = "bold", // Replace the entire text with "bold" - selection = TextRange(0, 4) - ) + state.messageTextState.edit { + replace(0, length, "bold") + selection = TextRange( + start = 0, + end = 4 + ) + } + // when state.addOrRemoveMessageMarkdown(markdown = RichTextMarkdown.Bold) // then assertEquals( "**bold**", - state.messageTextFieldValue.value.text + state.messageTextState.text.toString() ) } @Test fun `given non empty text, when adding italic markdown on selection, then 2x _ is added to the text`() = runTest { // given - state.messageTextFieldValue.value = messageTextFieldValue.value.copy( - text = "italic", // Replace the entire text with "bold" - selection = TextRange(0, 6) - ) + state.messageTextState.edit { + replace(0, length, "italic") + selection = TextRange( + start = 0, + end = 6 + ) + } // when state.addOrRemoveMessageMarkdown(markdown = RichTextMarkdown.Italic) @@ -161,7 +171,7 @@ class MessageCompositionHolderTest { // then assertEquals( "_italic_", - state.messageTextFieldValue.value.text + state.messageTextState.text.toString() ) } } diff --git a/app/src/test/kotlin/com/wire/android/ui/home/messagecomposer/state/MessageCompositionInputStateHolderTest.kt b/app/src/test/kotlin/com/wire/android/ui/home/messagecomposer/state/MessageCompositionInputStateHolderTest.kt index 81f3a08a537..9364fc0fd32 100644 --- a/app/src/test/kotlin/com/wire/android/ui/home/messagecomposer/state/MessageCompositionInputStateHolderTest.kt +++ b/app/src/test/kotlin/com/wire/android/ui/home/messagecomposer/state/MessageCompositionInputStateHolderTest.kt @@ -20,11 +20,10 @@ package com.wire.android.ui.home.messagecomposer.state -import androidx.compose.runtime.mutableStateOf +import androidx.compose.foundation.text.input.TextFieldState +import androidx.compose.foundation.text.input.setTextAndPlaceCursorAtEnd import androidx.compose.ui.focus.FocusRequester import androidx.compose.ui.platform.SoftwareKeyboardController -import androidx.compose.ui.text.TextRange -import androidx.compose.ui.text.input.TextFieldValue import androidx.compose.ui.unit.dp import com.wire.android.config.CoroutineTestExtension import com.wire.android.config.SnapshotExtension @@ -234,14 +233,14 @@ class MessageCompositionInputStateHolderTest { class Arrangement { - private val textFieldValue = mutableStateOf(TextFieldValue()) + private val textFieldState = TextFieldState() val softwareKeyboardController = mockk() private val focusRequester = mockk() private val state by lazy { - MessageCompositionInputStateHolder(textFieldValue, softwareKeyboardController, focusRequester) + MessageCompositionInputStateHolder(textFieldState, softwareKeyboardController, focusRequester) } init { @@ -252,10 +251,7 @@ class MessageCompositionInputStateHolderTest { } fun withText(text: String) = apply { - textFieldValue.value = textFieldValue.value.copy( - text = text, - selection = TextRange(text.length) - ) + textFieldState.setTextAndPlaceCursorAtEnd(text) } fun arrange() = state to this