Unable to use performclick() on customview in Roboelectric testcase?

I have a customviews like below in my xml

                   <com.examle.RowPhoto
                    android:id="@+id/agent_floating_row"
                    android:layout_marginStart="@dimen/size_16dp"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:visibility="gone"
                    app:cardElevation="@dimen/size_0dp" />

Class for the same is

class RowPhoto(context: Context, attributeSet: AttributeSet?) : CardView(context, attributeSet){
private var rowClick: RowEventCallback? = null
private var mRowConfig: FloatingRowConfig? = null
private lateinit var mRowEnum: FloatingRowEnum


init {

    LayoutInflater.from(context).inflate(R.layout.row_photo, this, true)


   RowPhotoVariationId.setOnClickListener {
        rowClick?.onRowclick()
    }
}

fun setOnRowClickListener(rowClick: RowEventCallback) {
    this.rowClick = rowClick
}

cant change anything in the above class because it is re-usable component used across many activities/fragments.

and in my Testclass, i want to use performclick() on that entireview. my TestClass is something like below.

@Config(sdk = [ Build.VERSION_CODES.P])
@RunWith(RobolectricTestRunner::class)
class ActvityTest{
    private lateinit var activity: Activity
    private lateinit var myCustomView: RowPhoto
    @Before
    fun setUp(){
        activity = Robolectric.buildActivity(Activity::class.java).create().resume().get()
        val attributeSet = with(Robolectric.buildAttributeSet()) {
            build()
        }

        myCustomView = RowPhoto(activity, attributeSet)
    }

    @Test
    fun test_activity_not_null(){
        assertNotNull(activity)
    }




    @Test
    fun checkNavigation(){
        // myCustomView.performClick()
        // activity.findViewById<View>(R.id.agent_floating_row).performClick()
        myCustomView.performClick()
    }
}

Click listener and navigation Methods in the activities are something like these

private fun setOnCLickListener() {
    agent_floating_row.setOnRowClickListener(object : RowEventCallback {
        override fun onRowclick() {
            navigationdActivity()
        }
    })
}
fun navigationdActivity() {
    val intent = Intent(this@Activity,MyHero::class.java)
    startActivity(intent)
}

But Performclick() is not triggering, control is not getting inside setOnCLickListener() function. any Help ?


You can use reflections to access the private field private var rowClick: RowEventCallback? = null of the custom view and then you can call the click interface in the test.

So the test should look something like this

@Test
fun checkNavigation(){
    val myCustomView = activity.agent_floating_row
    val eventListener = RowPhoto::class.java.getDeclaredField("rowClick")
    eventListener.isAccessible = true

    val clickInterface = eventListener.get(myCustomView) as RowEventCallback
    clickInterface.onRowclick()
}