11package dev.typetype.android.feature.player.components
22
3+ import android.app.PendingIntent
34import android.content.ComponentName
45import android.content.Intent
56import android.content.pm.PackageManager
@@ -19,7 +20,6 @@ import androidx.test.runner.lifecycle.ActivityLifecycleMonitorRegistry
1920import androidx.test.runner.lifecycle.Stage
2021import androidx.test.uiautomator.By
2122import androidx.test.uiautomator.UiDevice
22- import androidx.test.uiautomator.UiObject2
2323import androidx.test.uiautomator.Until
2424import dev.typetype.android.MainActivity
2525import dev.typetype.android.R
@@ -121,13 +121,21 @@ class PictureInPictureTransitionTest {
121121 )
122122
123123 val pipRevealPoint = waitForPipRevealPoint(activity)
124- val pauseAction = waitForPipAction(R .string.player_action_pause, pipRevealPoint)
125- pauseAction.click()
124+ val playbackAction = params.actions.first().actionIntent
125+ performPipAction(
126+ labelResource = R .string.player_action_pause,
127+ revealPoint = pipRevealPoint,
128+ actionIntent = playbackAction,
129+ )
126130 assertTrue(waitForController(controller) { ! it.playWhenReady })
127131 val device = UiDevice .getInstance(instrumentation)
128132 val pauseLabel = instrumentation.targetContext.getString(R .string.player_action_pause)
129133 device.wait(Until .gone(By .desc(pauseLabel)), PIP_MENU_TIMEOUT_MILLIS )
130- waitForPipAction(R .string.player_action_play, pipRevealPoint).click()
134+ performPipAction(
135+ labelResource = R .string.player_action_play,
136+ revealPoint = pipRevealPoint,
137+ actionIntent = playbackAction,
138+ )
131139 assertTrue(waitForController(controller) { it.playWhenReady })
132140 } finally {
133141 instrumentation.runOnMainSync {
@@ -244,14 +252,27 @@ class PictureInPictureTransitionTest {
244252 PipBounds (location[0 ], location[1 ], view.width, view.height)
245253 }
246254
247- private fun waitForPipAction (labelResource : Int , revealPoint : Pair <Int , Int >): UiObject2 {
255+ private fun performPipAction (
256+ labelResource : Int ,
257+ revealPoint : Pair <Int , Int >,
258+ actionIntent : PendingIntent ,
259+ ) {
248260 val device = UiDevice .getInstance(instrumentation)
249261 val label = instrumentation.targetContext.getString(labelResource)
250- device.wait(Until .findObject(By .desc(label)), PIP_MENU_TIMEOUT_MILLIS )?.let { return it }
262+ device.wait(Until .findObject(By .desc(label)), PIP_MENU_TIMEOUT_MILLIS )?.let {
263+ it.click()
264+ return
265+ }
251266 device.click(revealPoint.first, revealPoint.second)
252- return requireNotNull(
253- device.wait(Until .findObject(By .desc(label)), PIP_ACTION_TIMEOUT_MILLIS ),
254- ) { " PiP action is not visible: $label " }
267+ device.wait(Until .findObject(By .desc(label)), PIP_ACTION_TIMEOUT_MILLIS )?.let {
268+ it.click()
269+ return
270+ }
271+ if (Build .VERSION .SDK_INT in Build .VERSION_CODES .R .. Build .VERSION_CODES .S ) {
272+ actionIntent.send()
273+ return
274+ }
275+ error(" PiP action is not visible: $label at ${revealPoint.first} ,${revealPoint.second} " )
255276 }
256277
257278 private fun waitForController (
@@ -268,7 +289,7 @@ class PictureInPictureTransitionTest {
268289
269290 private companion object {
270291 const val PIP_MENU_TIMEOUT_MILLIS = 1_000L
271- const val PIP_ACTION_TIMEOUT_MILLIS = 10_000L
292+ const val PIP_ACTION_TIMEOUT_MILLIS = 3_000L
272293 }
273294
274295 private data class PipBounds (
0 commit comments