2019-09-24 20:33:29 +02:00
|
|
|
package com.keylesspalace.tusky
|
|
|
|
|
|
|
|
import androidx.test.ext.junit.runners.AndroidJUnit4
|
2022-01-04 19:28:49 +01:00
|
|
|
import com.keylesspalace.tusky.entity.Attachment
|
2019-09-24 20:33:29 +02:00
|
|
|
import com.keylesspalace.tusky.entity.Filter
|
|
|
|
import com.keylesspalace.tusky.entity.Poll
|
|
|
|
import com.keylesspalace.tusky.entity.PollOption
|
|
|
|
import com.keylesspalace.tusky.entity.Status
|
2021-06-11 20:15:40 +02:00
|
|
|
import com.keylesspalace.tusky.network.FilterModel
|
2019-09-24 20:33:29 +02:00
|
|
|
import org.junit.Assert.assertFalse
|
|
|
|
import org.junit.Assert.assertTrue
|
|
|
|
import org.junit.Before
|
|
|
|
import org.junit.Test
|
|
|
|
import org.junit.runner.RunWith
|
2022-04-14 19:49:49 +02:00
|
|
|
import org.mockito.kotlin.mock
|
2019-09-24 20:33:29 +02:00
|
|
|
import org.robolectric.annotation.Config
|
2022-01-04 19:28:49 +01:00
|
|
|
import java.util.ArrayList
|
2021-06-28 21:13:24 +02:00
|
|
|
import java.util.Date
|
2019-09-24 20:33:29 +02:00
|
|
|
|
2020-02-25 19:49:15 +01:00
|
|
|
@Config(sdk = [28])
|
2019-09-24 20:33:29 +02:00
|
|
|
@RunWith(AndroidJUnit4::class)
|
|
|
|
class FilterTest {
|
|
|
|
|
2022-04-14 19:49:49 +02:00
|
|
|
private lateinit var filterModel: FilterModel
|
2019-09-24 20:33:29 +02:00
|
|
|
|
|
|
|
@Before
|
|
|
|
fun setup() {
|
2021-06-11 20:15:40 +02:00
|
|
|
filterModel = FilterModel()
|
|
|
|
val filters = listOf(
|
|
|
|
Filter(
|
|
|
|
id = "123",
|
|
|
|
phrase = "badWord",
|
|
|
|
context = listOf(Filter.HOME),
|
|
|
|
expiresAt = null,
|
|
|
|
irreversible = false,
|
|
|
|
wholeWord = false
|
|
|
|
),
|
|
|
|
Filter(
|
|
|
|
id = "123",
|
|
|
|
phrase = "badWholeWord",
|
|
|
|
context = listOf(Filter.HOME, Filter.PUBLIC),
|
|
|
|
expiresAt = null,
|
|
|
|
irreversible = false,
|
|
|
|
wholeWord = true
|
|
|
|
),
|
|
|
|
Filter(
|
|
|
|
id = "123",
|
|
|
|
phrase = "@twitter.com",
|
|
|
|
context = listOf(Filter.HOME),
|
|
|
|
expiresAt = null,
|
|
|
|
irreversible = false,
|
|
|
|
wholeWord = true
|
|
|
|
)
|
|
|
|
)
|
2019-09-24 20:33:29 +02:00
|
|
|
|
2021-06-11 20:15:40 +02:00
|
|
|
filterModel.initWithFilters(filters)
|
2019-09-24 20:33:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun shouldNotFilter() {
|
2021-06-11 20:15:40 +02:00
|
|
|
assertFalse(
|
|
|
|
filterModel.shouldFilterStatus(
|
2019-09-24 20:33:29 +02:00
|
|
|
mockStatus(content = "should not be filtered")
|
2021-06-11 20:15:40 +02:00
|
|
|
)
|
|
|
|
)
|
2019-09-24 20:33:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun shouldFilter_whenContentMatchesBadWord() {
|
2021-06-11 20:15:40 +02:00
|
|
|
assertTrue(
|
|
|
|
filterModel.shouldFilterStatus(
|
2019-09-24 20:33:29 +02:00
|
|
|
mockStatus(content = "one two badWord three")
|
2021-06-11 20:15:40 +02:00
|
|
|
)
|
|
|
|
)
|
2019-09-24 20:33:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun shouldFilter_whenContentMatchesBadWordPart() {
|
2021-06-11 20:15:40 +02:00
|
|
|
assertTrue(
|
|
|
|
filterModel.shouldFilterStatus(
|
2019-09-24 20:33:29 +02:00
|
|
|
mockStatus(content = "one two badWordPart three")
|
2021-06-11 20:15:40 +02:00
|
|
|
)
|
|
|
|
)
|
2019-09-24 20:33:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun shouldFilter_whenContentMatchesBadWholeWord() {
|
2021-06-11 20:15:40 +02:00
|
|
|
assertTrue(
|
|
|
|
filterModel.shouldFilterStatus(
|
2019-09-24 20:33:29 +02:00
|
|
|
mockStatus(content = "one two badWholeWord three")
|
2021-06-11 20:15:40 +02:00
|
|
|
)
|
|
|
|
)
|
2019-09-24 20:33:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2020-01-06 16:16:54 +01:00
|
|
|
fun shouldNotFilter_whenContentDoesNotMatchWholeWord() {
|
2021-06-11 20:15:40 +02:00
|
|
|
assertFalse(
|
|
|
|
filterModel.shouldFilterStatus(
|
2019-09-24 20:33:29 +02:00
|
|
|
mockStatus(content = "one two badWholeWordTest three")
|
2021-06-11 20:15:40 +02:00
|
|
|
)
|
|
|
|
)
|
2019-09-24 20:33:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun shouldFilter_whenSpoilerTextDoesMatch() {
|
2021-06-11 20:15:40 +02:00
|
|
|
assertTrue(
|
|
|
|
filterModel.shouldFilterStatus(
|
2019-09-24 20:33:29 +02:00
|
|
|
mockStatus(
|
2021-06-11 20:15:40 +02:00
|
|
|
content = "should not be filtered",
|
|
|
|
spoilerText = "badWord should be filtered"
|
2019-09-24 20:33:29 +02:00
|
|
|
)
|
2021-06-11 20:15:40 +02:00
|
|
|
)
|
|
|
|
)
|
2019-09-24 20:33:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun shouldFilter_whenPollTextDoesMatch() {
|
2021-06-11 20:15:40 +02:00
|
|
|
assertTrue(
|
|
|
|
filterModel.shouldFilterStatus(
|
2019-09-24 20:33:29 +02:00
|
|
|
mockStatus(
|
2021-06-11 20:15:40 +02:00
|
|
|
content = "should not be filtered",
|
|
|
|
spoilerText = "should not be filtered",
|
|
|
|
pollOptions = listOf("should not be filtered", "badWord")
|
2019-09-24 20:33:29 +02:00
|
|
|
)
|
2021-06-11 20:15:40 +02:00
|
|
|
)
|
|
|
|
)
|
2019-09-24 20:33:29 +02:00
|
|
|
}
|
|
|
|
|
2022-01-04 19:28:49 +01:00
|
|
|
@Test
|
|
|
|
fun shouldFilter_whenMediaDescriptionDoesMatch() {
|
|
|
|
assertTrue(
|
|
|
|
filterModel.shouldFilterStatus(
|
|
|
|
mockStatus(
|
|
|
|
content = "should not be filtered",
|
|
|
|
spoilerText = "should not be filtered",
|
|
|
|
attachmentsDescriptions = listOf("should not be filtered", "badWord"),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-01-06 16:16:54 +01:00
|
|
|
@Test
|
|
|
|
fun shouldFilterPartialWord_whenWholeWordFilterContainsNonAlphanumericCharacters() {
|
2021-06-11 20:15:40 +02:00
|
|
|
assertTrue(
|
|
|
|
filterModel.shouldFilterStatus(
|
2020-01-06 16:16:54 +01:00
|
|
|
mockStatus(content = "one two someone@twitter.com three")
|
2021-06-11 20:15:40 +02:00
|
|
|
)
|
|
|
|
)
|
2020-01-06 16:16:54 +01:00
|
|
|
}
|
|
|
|
|
2019-09-24 20:33:29 +02:00
|
|
|
private fun mockStatus(
|
2021-06-11 20:15:40 +02:00
|
|
|
content: String = "",
|
|
|
|
spoilerText: String = "",
|
2022-01-04 19:28:49 +01:00
|
|
|
pollOptions: List<String>? = null,
|
|
|
|
attachmentsDescriptions: List<String>? = null
|
2019-09-24 20:33:29 +02:00
|
|
|
): Status {
|
|
|
|
return Status(
|
2021-06-11 20:15:40 +02:00
|
|
|
id = "123",
|
|
|
|
url = "https://mastodon.social/@Tusky/100571663297225812",
|
|
|
|
account = mock(),
|
|
|
|
inReplyToId = null,
|
|
|
|
inReplyToAccountId = null,
|
|
|
|
reblog = null,
|
2022-04-15 13:20:27 +02:00
|
|
|
content = content,
|
2021-06-11 20:15:40 +02:00
|
|
|
createdAt = Date(),
|
|
|
|
emojis = emptyList(),
|
|
|
|
reblogsCount = 0,
|
|
|
|
favouritesCount = 0,
|
|
|
|
reblogged = false,
|
|
|
|
favourited = false,
|
|
|
|
bookmarked = false,
|
|
|
|
sensitive = false,
|
|
|
|
spoilerText = spoilerText,
|
|
|
|
visibility = Status.Visibility.PUBLIC,
|
2022-01-04 19:28:49 +01:00
|
|
|
attachments = if (attachmentsDescriptions != null) {
|
|
|
|
ArrayList(
|
|
|
|
attachmentsDescriptions.map {
|
|
|
|
Attachment(
|
|
|
|
id = "1234",
|
|
|
|
url = "",
|
|
|
|
previewUrl = null,
|
|
|
|
meta = null,
|
|
|
|
type = Attachment.Type.IMAGE,
|
|
|
|
description = it,
|
|
|
|
blurhash = null
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
} else arrayListOf(),
|
2021-06-11 20:15:40 +02:00
|
|
|
mentions = listOf(),
|
2022-02-25 18:56:21 +01:00
|
|
|
tags = listOf(),
|
2021-06-11 20:15:40 +02:00
|
|
|
application = null,
|
|
|
|
pinned = false,
|
|
|
|
muted = false,
|
|
|
|
poll = if (pollOptions != null) {
|
|
|
|
Poll(
|
|
|
|
id = "1234",
|
|
|
|
expiresAt = null,
|
|
|
|
expired = false,
|
|
|
|
multiple = false,
|
|
|
|
votesCount = 0,
|
|
|
|
votersCount = 0,
|
|
|
|
options = pollOptions.map {
|
|
|
|
PollOption(it, 0)
|
|
|
|
},
|
2021-09-17 22:12:17 +02:00
|
|
|
voted = false,
|
|
|
|
ownVotes = null
|
2021-06-11 20:15:40 +02:00
|
|
|
)
|
|
|
|
} else null,
|
|
|
|
card = null
|
2019-09-24 20:33:29 +02:00
|
|
|
)
|
|
|
|
}
|
2021-06-28 21:13:24 +02:00
|
|
|
}
|