Skip to content

Commit

Permalink
fix: Crash on inserting mention #WPB-15717 (#3856)
Browse files Browse the repository at this point in the history
  • Loading branch information
borichellow authored Feb 6, 2025
1 parent 5084c57 commit 24cdf16
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,7 @@ fun EnabledMessageComposer(
)

val mentionSearchResult = messageComposerViewState.value.mentionSearchResult
if (mentionSearchResult.isNotEmpty() && inputStateHolder.isTextExpanded
) {
if (mentionSearchResult.isNotEmpty() && inputStateHolder.isTextExpanded) {
DropDownMentionsSuggestions(
currentSelectedLineIndex = currentSelectedLineIndex,
cursorCoordinateY = cursorCoordinateY,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import androidx.compose.runtime.MutableState
import androidx.compose.runtime.snapshotFlow
import androidx.compose.ui.text.TextRange
import androidx.compose.ui.text.substring
import com.wire.android.appLogger
import com.wire.android.ui.home.conversations.model.UIMention
import com.wire.android.ui.home.conversations.model.UIMessage
import com.wire.android.ui.home.conversations.model.UIQuotedMessage
Expand Down Expand Up @@ -200,6 +201,10 @@ class MessageCompositionHolder(
userId = UserId(contact.id, contact.domain),
handler = String.MENTION_SYMBOL + contact.name
)
if (mention.start < 0 || mention.length < 2) {
appLogger.e("MessageCompositionHolder: Failure to add mention")
return
}
insertMentionIntoText(mention)
messageComposition.update {
it.copy(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.text.TextRange
import com.wire.android.config.SnapshotExtension
import com.wire.android.framework.TestConversation
import com.wire.android.ui.home.conversationslist.model.Membership
import com.wire.android.ui.home.messagecomposer.model.MessageComposition
import com.wire.android.ui.home.newconversation.model.Contact
import com.wire.kalium.logic.data.user.ConnectionState
import io.mockk.MockKAnnotations
import io.mockk.impl.annotations.MockK
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -174,4 +177,56 @@ class MessageCompositionHolderTest {
state.messageTextState.text.toString()
)
}

@Test
fun `given non empty text, when adding mention, mention is added`() = runTest {
// given
val text = "@men"
state.messageTextState.edit {
replace(0, length, text)
selection = TextRange(start = text.length, end = text.length)
}

// when
state.addMention(
Contact(
id = "id",
domain = "domain",
name = "name",
handle = "men handle",
connectionState = ConnectionState.ACCEPTED,
membership = Membership.Guest
)
)

// then
assertEquals("@name ", state.messageTextState.text.toString())
assertEquals(1, state.messageComposition.value.selectedMentions.size)
}

@Test
fun `given non empty text without @ symbol, when adding mention, nothing happen`() = runTest {
// given
val text = "mann"
state.messageTextState.edit {
replace(0, length, text)
selection = TextRange(start = 4, end = 4)
}

// when
state.addMention(
Contact(
id = "id",
domain = "domain",
name = "name",
handle = "men handle",
connectionState = ConnectionState.ACCEPTED,
membership = Membership.Guest
)
)

// then
assertEquals(text, state.messageTextState.text.toString())
assertEquals(0, state.messageComposition.value.selectedMentions.size)
}
}

0 comments on commit 24cdf16

Please sign in to comment.