跳转至

apps\player\src\test\java\cn\nfarms\player\LrcTest.kt

源码路径:apps\player\src\test\java\cn\nfarms\player\LrcTest.kt · 66 行

package cn.nfarms.player

import cn.nfarms.player.data.Lrc
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Test

/**
 * LRC 歌词解析。
 *
 * 「解析出来是空的」这类 bug 在界面上表现为"歌词功能坏了",
 * 但根因往往只是没认 `[mm:ss]`(不带毫秒)这种常见写法。
 */
class LrcTest {

    @Test
    fun parses_both_millisecond_and_plain_timestamps() {
        val text = """
            [ti:测试]
            [ar:NFarms]
            [00:01.50]第一句
            [00:04]第二句
            [01:02.123]第三句
        """.trimIndent()

        val lines = Lrc.parse(text)
        assertEquals(3, lines.size)
        assertEquals(1_500, lines[0].atMs)
        assertEquals("第一句", lines[0].text)
        assertEquals(4_000, lines[1].atMs)
        assertEquals("第二句", lines[1].text)
        assertEquals(62_123, lines[2].atMs)
        assertEquals("第三句", lines[2].text)
    }

    @Test
    fun ignores_tags_and_blank_lines() {
        val lines = Lrc.parse("[ti:x]\n\n[al:y]\n[00:02.00]只有这句\n")
        assertEquals(1, lines.size)
    }

    @Test
    fun sorts_by_time() {
        val lines = Lrc.parse("[00:05.00]后\n[00:01.00]前")
        assertEquals("前", lines[0].text)
        assertEquals("后", lines[1].text)
    }

    @Test
    fun garbage_yields_empty_not_crash() {
        assertTrue(Lrc.parse("这不是歌词\n随便什么").isEmpty())
        assertTrue(Lrc.parse("").isEmpty())
    }

    /** 选中行的判定:最后一条 <= 当前进度(不是"第一条 >= ")。 */
    @Test
    fun current_line_is_last_line_at_or_before_position() {
        val lines = Lrc.parse("[00:00.00]a\n[00:05.00]b\n[00:10.00]c")
        fun idxAt(ms: Long) = lines.indexOfLast { it.atMs <= ms }
        assertEquals(0, idxAt(0))
        assertEquals(0, idxAt(4_999))
        assertEquals(1, idxAt(5_000))
        assertEquals(2, idxAt(60_000))
    }
}