Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Crash on inserting mention #WPB-15717 #3856

Merged
merged 5 commits into from
Feb 6, 2025
Merged
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 @@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Perhaps we can add tests for this case, I see we have a few on MessageCompositionHolderTest :D

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)
}
}
Loading