Yuito-app-android/app/src/test/java/com/keylesspalace/tusky/FilterTest.kt

186 lines
5.2 KiB
Kotlin
Raw Normal View History

2019-09-24 20:33:29 +02:00
package com.keylesspalace.tusky
import android.text.SpannedString
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.keylesspalace.tusky.entity.Filter
import com.keylesspalace.tusky.entity.Poll
import com.keylesspalace.tusky.entity.PollOption
import com.keylesspalace.tusky.entity.Status
import com.keylesspalace.tusky.network.FilterModel
2019-09-24 20:33:29 +02:00
import com.keylesspalace.tusky.network.MastodonApi
import com.nhaarman.mockitokotlin2.doReturn
2019-09-24 20:33:29 +02:00
import com.nhaarman.mockitokotlin2.mock
import io.reactivex.rxjava3.core.Single
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
import org.robolectric.annotation.Config
import java.util.*
@Config(sdk = [28])
2019-09-24 20:33:29 +02:00
@RunWith(AndroidJUnit4::class)
class FilterTest {
lateinit var filterModel: FilterModel
2019-09-24 20:33:29 +02:00
@Before
fun setup() {
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
filterModel.initWithFilters(filters)
2019-09-24 20:33:29 +02:00
}
@Test
fun shouldNotFilter() {
assertFalse(
filterModel.shouldFilterStatus(
2019-09-24 20:33:29 +02:00
mockStatus(content = "should not be filtered")
)
)
2019-09-24 20:33:29 +02:00
}
@Test
fun shouldFilter_whenContentMatchesBadWord() {
assertTrue(
filterModel.shouldFilterStatus(
2019-09-24 20:33:29 +02:00
mockStatus(content = "one two badWord three")
)
)
2019-09-24 20:33:29 +02:00
}
@Test
fun shouldFilter_whenContentMatchesBadWordPart() {
assertTrue(
filterModel.shouldFilterStatus(
2019-09-24 20:33:29 +02:00
mockStatus(content = "one two badWordPart three")
)
)
2019-09-24 20:33:29 +02:00
}
@Test
fun shouldFilter_whenContentMatchesBadWholeWord() {
assertTrue(
filterModel.shouldFilterStatus(
2019-09-24 20:33:29 +02:00
mockStatus(content = "one two badWholeWord three")
)
)
2019-09-24 20:33:29 +02:00
}
@Test
fun shouldNotFilter_whenContentDoesNotMatchWholeWord() {
assertFalse(
filterModel.shouldFilterStatus(
2019-09-24 20:33:29 +02:00
mockStatus(content = "one two badWholeWordTest three")
)
)
2019-09-24 20:33:29 +02:00
}
@Test
fun shouldFilter_whenSpoilerTextDoesMatch() {
assertTrue(
filterModel.shouldFilterStatus(
2019-09-24 20:33:29 +02:00
mockStatus(
content = "should not be filtered",
spoilerText = "badWord should be filtered"
2019-09-24 20:33:29 +02:00
)
)
)
2019-09-24 20:33:29 +02:00
}
@Test
fun shouldFilter_whenPollTextDoesMatch() {
assertTrue(
filterModel.shouldFilterStatus(
2019-09-24 20:33:29 +02:00
mockStatus(
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
)
)
)
2019-09-24 20:33:29 +02:00
}
@Test
fun shouldFilterPartialWord_whenWholeWordFilterContainsNonAlphanumericCharacters() {
assertTrue(
filterModel.shouldFilterStatus(
mockStatus(content = "one two someone@twitter.com three")
)
)
}
2019-09-24 20:33:29 +02:00
private fun mockStatus(
content: String = "",
spoilerText: String = "",
pollOptions: List<String>? = null
2019-09-24 20:33:29 +02:00
): Status {
return Status(
id = "123",
url = "https://mastodon.social/@Tusky/100571663297225812",
account = mock(),
inReplyToId = null,
inReplyToAccountId = null,
reblog = null,
content = SpannedString(content),
createdAt = Date(),
emojis = emptyList(),
reblogsCount = 0,
favouritesCount = 0,
reblogged = false,
favourited = false,
bookmarked = false,
sensitive = false,
spoilerText = spoilerText,
visibility = Status.Visibility.PUBLIC,
attachments = arrayListOf(),
mentions = listOf(),
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)
},
voted = false
)
} else null,
card = null
2019-09-24 20:33:29 +02:00
)
}
}