fix(gift): 이름 입력 필터를 보정한다
This commit is contained in:
@@ -2,6 +2,7 @@ package kr.co.vividnext.sodalive.v2.gift
|
||||
|
||||
import android.content.Context
|
||||
import android.text.InputFilter
|
||||
import android.text.SpannableStringBuilder
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.ArrayAdapter
|
||||
@@ -28,12 +29,18 @@ class GiftCategoryAdapter(
|
||||
}
|
||||
|
||||
object GiftSendInputFilters {
|
||||
fun isValidName(name: String): Boolean = NAME_REGEX.matches(name.trim())
|
||||
|
||||
fun senderName() = InputFilter { source, start, end, _, _, _ ->
|
||||
filterChars(source, start, end) { it.isLetterOrDigit() && (it.code < 128 || it in '가'..'힣') }
|
||||
filterChars(source, start, end) {
|
||||
it in 'A'..'Z' || it in 'a'..'z' ||
|
||||
it in '\u1100'..'\u11FF' || it in '\u3130'..'\u318F' ||
|
||||
it in '\uA960'..'\uA97F' || it in '\uAC00'..'\uD7A3' || it in '\uD7B0'..'\uD7FF'
|
||||
}
|
||||
}
|
||||
|
||||
fun phoneNumber() = InputFilter { source, start, end, _, _, _ ->
|
||||
filterChars(source, start, end) { it.isDigit() }
|
||||
filterChars(source, start, end) { it in '0'..'9' }
|
||||
}
|
||||
|
||||
private fun filterChars(
|
||||
@@ -42,9 +49,14 @@ object GiftSendInputFilters {
|
||||
end: Int,
|
||||
accepts: (Char) -> Boolean
|
||||
): CharSequence? {
|
||||
val filtered = source.subSequence(start, end).filter(accepts)
|
||||
val filtered = SpannableStringBuilder(source, start, end)
|
||||
for (index in filtered.lastIndex downTo 0) {
|
||||
if (!accepts(filtered[index])) filtered.delete(index, index + 1)
|
||||
}
|
||||
return if (filtered.length == end - start) null else filtered
|
||||
}
|
||||
|
||||
private val NAME_REGEX = Regex("^[가-힣A-Za-z]{1,12}$")
|
||||
}
|
||||
|
||||
object GiftSendInputErrorBinder {
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
package kr.co.vividnext.sodalive.v2.gift
|
||||
|
||||
import android.app.Application
|
||||
import android.text.SpannableString
|
||||
import android.text.Spanned
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [28], application = Application::class)
|
||||
class GiftSendInputFiltersTest {
|
||||
|
||||
@Test
|
||||
fun `이름 필터는 한글 조합 문자와 composing span을 보존한다`() {
|
||||
val marker = Any()
|
||||
val composing = SpannableString("ㄱ").apply {
|
||||
setSpan(marker, 0, length, Spanned.SPAN_COMPOSING)
|
||||
}
|
||||
|
||||
val unchanged = GiftSendInputFilters.senderName().filter(composing, 0, composing.length, EMPTY, 0, 0)
|
||||
|
||||
assertNull(unchanged)
|
||||
|
||||
val mixed = SpannableString("ㄱ1").apply {
|
||||
setSpan(marker, 0, 1, Spanned.SPAN_COMPOSING)
|
||||
}
|
||||
val filtered = GiftSendInputFilters.senderName().filter(mixed, 0, mixed.length, EMPTY, 0, 0)
|
||||
assertEquals("ㄱ", filtered.toString())
|
||||
assertTrue(filtered is Spanned)
|
||||
assertEquals(0, (filtered as Spanned).getSpanStart(marker))
|
||||
assertEquals(1, filtered.getSpanEnd(marker))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `이름 필터는 한글과 영문 외 문자를 제거한다`() {
|
||||
val filtered = GiftSendInputFilters.senderName().filter("홍Gil1 동!", 0, 8, EMPTY, 0, 0)
|
||||
|
||||
assertEquals("홍Gil동", filtered.toString())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `휴대폰 필터는 ASCII 숫자 외 문자를 제거한다`() {
|
||||
val filtered = GiftSendInputFilters.phoneNumber().filter("011-가2", 0, 6, EMPTY, 0, 0)
|
||||
|
||||
assertEquals("012", filtered.toString())
|
||||
}
|
||||
|
||||
private companion object {
|
||||
val EMPTY = SpannableString("")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user