kopia lustrzana https://github.com/ryukoposting/Signal-Android
Fix mention crash with overlapping ranges.
rodzic
2dc41f319c
commit
45a1c5c369
|
@ -5,6 +5,7 @@ import android.text.SpannableStringBuilder;
|
|||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
import androidx.annotation.WorkerThread;
|
||||
|
||||
import com.annimon.stream.Stream;
|
||||
|
@ -64,7 +65,8 @@ public final class MentionUtil {
|
|||
return update(body, mentions, m -> MENTION_PLACEHOLDER);
|
||||
}
|
||||
|
||||
private static @NonNull UpdatedBodyAndMentions update(@Nullable CharSequence body, @NonNull List<Mention> mentions, @NonNull Function<Mention, CharSequence> replacementTextGenerator) {
|
||||
@VisibleForTesting
|
||||
static @NonNull UpdatedBodyAndMentions update(@Nullable CharSequence body, @NonNull List<Mention> mentions, @NonNull Function<Mention, CharSequence> replacementTextGenerator) {
|
||||
if (body == null || mentions.isEmpty()) {
|
||||
return new UpdatedBodyAndMentions(body, mentions);
|
||||
}
|
||||
|
@ -76,7 +78,7 @@ public final class MentionUtil {
|
|||
int bodyIndex = 0;
|
||||
|
||||
for (Mention mention : sortedMentions) {
|
||||
if (invalidMention(body, mention)) {
|
||||
if (invalidMention(body, mention) || bodyIndex > mention.getStart()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,49 @@
|
|||
package org.thoughtcrime.securesms.database
|
||||
|
||||
import android.app.Application
|
||||
import org.hamcrest.MatcherAssert.assertThat
|
||||
import org.hamcrest.Matchers
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
import org.thoughtcrime.securesms.database.model.Mention
|
||||
import org.thoughtcrime.securesms.recipients.RecipientId
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(manifest = Config.NONE, application = Application::class)
|
||||
class MentionUtilTest {
|
||||
|
||||
@Test
|
||||
fun vanillaUpdate() {
|
||||
val mentions = listOf(Mention(RecipientId.from(1L), 0, 1))
|
||||
|
||||
val update: MentionUtil.UpdatedBodyAndMentions = MentionUtil.update("T test", mentions) { it.recipientId.toString() }
|
||||
|
||||
assertThat(update.body, Matchers.`is`("RecipientId::1 test"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun nextToEachOtherUpdate() {
|
||||
val mentions = listOf(
|
||||
Mention(RecipientId.from(1L), 0, 3),
|
||||
Mention(RecipientId.from(2L), 3, 3)
|
||||
)
|
||||
|
||||
val update: MentionUtil.UpdatedBodyAndMentions = MentionUtil.update("ONETWO test", mentions) { it.recipientId.toString() }
|
||||
|
||||
assertThat(update.body, Matchers.`is`("RecipientId::1RecipientId::2 test"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun overlapUpdate() {
|
||||
val mentions = listOf(
|
||||
Mention(RecipientId.from(1L), 0, 3),
|
||||
Mention(RecipientId.from(2L), 1, 5)
|
||||
)
|
||||
|
||||
val update: MentionUtil.UpdatedBodyAndMentions = MentionUtil.update("T test", mentions) { it.recipientId.toString() }
|
||||
|
||||
assertThat(update.body, Matchers.`is`("RecipientId::1est"))
|
||||
}
|
||||
}
|
Ładowanie…
Reference in New Issue