package com.gensee.multi_instance

import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import com.gensee.media.RtmpPlayer
import org.junit.Assert.*
import org.junit.Test
import org.junit.runner.RunWith

/**
 * 最简单的测试：验证反射逻辑是否正常工作
 */
@RunWith(AndroidJUnit4::class)
class SimpleReflectionTest {

    @Test
    fun testReflectiveMethodAccess() {
        val player = RtmpPlayer()
        android.util.Log.i("SimpleReflectionTest", "Player created: ${player.javaClass.name}")

        // 测试反射获取 initAudioPlayer 方法
        try {
            val initMethod = player.javaClass.superclass.getDeclaredMethod("initAudioPlayer")
            android.util.Log.i("SimpleReflectionTest", "Found initAudioPlayer method: ${initMethod.name}")

            initMethod.isAccessible = true
            android.util.Log.i("SimpleReflectionTest", "Set method accessible")

            // 尝试调用
            initMethod.invoke(player)
            android.util.Log.i("SimpleReflectionTest", "Successfully called initAudioPlayer")

            Thread.sleep(500)

            // 测试反射获取 audioRelease 方法
            val releaseMethod = player.javaClass.superclass.getDeclaredMethod("audioRelease")
            android.util.Log.i("SimpleReflectionTest", "Found audioRelease method: ${releaseMethod.name}")

            releaseMethod.isAccessible = true
            releaseMethod.invoke(player)
            android.util.Log.i("SimpleReflectionTest", "Successfully called audioRelease")

            Thread.sleep(200)

            android.util.Log.i("SimpleReflectionTest", "Test PASSED")
            assertTrue("Reflection test should succeed", true)

        } catch (e: Exception) {
            android.util.Log.e("SimpleReflectionTest", "Test FAILED", e)
            fail("Reflection test failed: ${e.message}")
        }
    }
}
