0

I have an Android GlanceAppWidget and would like to test the onAction in its ActionCallback.

What is the best way to trigger the code in onAction for instrumentation testing?

The below code illustrates what I am trying to do.

@Test
fun testMyActionCallback() {
    val myAction = MyAction()
    myAction.onAction()
    /*assertions*/
}
class MyAction: MyActionAbstract(GlanceAppWidget::class.java)

abstract class MyActionAbstract(
    val myClass: Class<out GlanceAppWidget>,
) : ActionCallback {

    override suspend fun onAction(
        context: Context,
        glanceId: GlanceId,
        parameters: ActionParameters,
    ) {
        val glanceWork1 = OneTimeWorkRequest.Builder(WorkerOne::class.java)
                .build()
        
        val glanceWork2 = OneTimeWorkRequest.Builder(WorkerTwo::class.java)
                .build()

        WorkManager.getInstance(context)
            .beginUniqueWork(
                GLANCE_WORK_NAME,
                ExistingWorkPolicy.REPLACE,
                glanceWork1
            ).then(
                glanceWork2
            )
            .enqueue()
    }
}
mars8
  • 770
  • 1
  • 11
  • 25

0 Answers0