Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package io.github.dewfallapp

import android.graphics.Bitmap
import android.graphics.Color
import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.graphics.asAndroidBitmap
import androidx.compose.ui.test.SemanticsMatcher
import androidx.compose.ui.test.captureToImage
import androidx.compose.ui.test.hasContentDescription
import androidx.compose.ui.test.hasSetTextAction
import androidx.compose.ui.test.hasText
import androidx.compose.ui.test.junit4.v2.createAndroidComposeRule
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.onRoot
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.performImeAction
import androidx.compose.ui.test.performTextInput
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import io.github.dewfallapp.core.playback.PlaybackConnection
import io.github.dewfallapp.fake.TestVideo
import io.github.dewfallapp.feature.player.ui.R as PlayerR
import javax.inject.Inject
import kotlin.test.AfterTest
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import org.junit.Rule
import org.junit.runner.RunWith

/** How long the test waits for a screen to show what it expects. */
private const val SCREEN_TIMEOUT_MS = 10_000L

/** How long the test waits for the first frames, as in SearchThenPlayTest. */
private const val PICTURE_TIMEOUT_MS = 60_000L

/**
* Checks that the video screen draws its background, its minimize button and its details while the
* SurfaceView shows a picture. On Android 10 the SurfaceView once cleared every pixel that the
* screen had drawn before it, so the window showed black there, with no minimize button (#211). The
* test reads the window's own pixels, where those cleared pixels are transparent. Screenshot tests
* can't catch it, because they draw no SurfaceView hole. See docs/testing.md#emulator-tests.
*/
@HiltAndroidTest
@RunWith(AndroidJUnit4::class)
class VideoScreenBackgroundTest {
@get:Rule(order = 0) val hilt = HiltAndroidRule(this)

/** An UnconfinedTestDispatcher, for the reason that SearchThenPlayTest gives. */
// UnconfinedTestDispatcher is still marked experimental.
@OptIn(ExperimentalCoroutinesApi::class)
@get:Rule(order = 1)
val compose = createAndroidComposeRule<MainActivity>(UnconfinedTestDispatcher())

@Inject lateinit var playback: PlaybackConnection

@BeforeTest
fun inject() {
hilt.inject()
}

/** So the next test starts the service with its own objects. */
@AfterTest
fun stop() {
compose.stopPlayback(playback)
}

@Test
fun backgroundAndMinimizeButtonShowOverThePicture() {
compose.onNode(hasSetTextAction()).performTextInput(TestVideo.QUERY)
compose.onNode(hasSetTextAction()).performImeAction()
waitForNode(hasText(TestVideo.TITLE))
compose.onNodeWithText(TestVideo.TITLE).performClick()
waitForNode(hasText(TestVideo.DESCRIPTION))

// The SurfaceView clears its hole on each draw after its first frame, and the seek bar and
// the times redraw the screen while the video plays. So by the time the video plays its
// picture, the screen has drawn with the hole. A paused player keeps the last frame, and
// the screen then stays still for the capture.
compose.waitUntil(PICTURE_TIMEOUT_MS) { showsPicture() }
compose.runOnUiThread { playback.player.value?.pause() }
compose.waitUntil(SCREEN_TIMEOUT_MS) {
compose.runOnUiThread { playback.player.value?.isPlaying == false }
}

val minimize =
InstrumentationRegistry.getInstrumentation()
.targetContext
.getString(PlayerR.string.player_minimize)
val button = boundsOf(hasContentDescription(minimize))
val description = boundsOf(hasText(TestVideo.DESCRIPTION))
val window = compose.onRoot().captureToImage().asAndroidBitmap()

assertTrue(pixelsIn(window, button).isNotEmpty(), "The button is outside the window")
assertTrue(pixelsIn(window, description).isNotEmpty(), "The details are outside the window")
assertEquals(0, seeThroughPixels(window, button), "See-through pixels in the button")
assertEquals(0, seeThroughPixels(window, description), "See-through pixels in the details")
assertTrue(colorsIn(window, button) > 1, "The minimize button shows no icon")
}

/**
* Waits until a node matches [matcher], for up to [SCREEN_TIMEOUT_MS]. Compose's own
* waitUntilAtLeastOneExists does the same, but is still an experimental API.
*/
private fun waitForNode(matcher: SemanticsMatcher) {
compose.waitUntil(SCREEN_TIMEOUT_MS) {
compose.onAllNodes(matcher).fetchSemanticsNodes().isNotEmpty()
}
}

/**
* Whether the player plays the test video's picture. Media3's Player runs on the main thread.
*/
private fun showsPicture(): Boolean = compose.runOnUiThread {
val player = playback.player.value
player != null && player.isPlaying && player.videoSize.width == TestVideo.WIDTH
}

private fun boundsOf(matcher: SemanticsMatcher): Rect =
compose.onNode(matcher).fetchSemanticsNode().boundsInRoot

/** How many pixels of [bitmap] inside [bounds] aren't fully opaque. */
private fun seeThroughPixels(bitmap: Bitmap, bounds: Rect): Int =
pixelsIn(bitmap, bounds).count { Color.alpha(it) != OPAQUE }

/** How many different colors [bitmap] has inside [bounds]. */
private fun colorsIn(bitmap: Bitmap, bounds: Rect): Int = pixelsIn(bitmap, bounds).toSet().size

private fun pixelsIn(bitmap: Bitmap, bounds: Rect): List<Int> {
val left = bounds.left.toInt().coerceIn(0, bitmap.width)
val top = bounds.top.toInt().coerceIn(0, bitmap.height)
val right = bounds.right.toInt().coerceIn(left, bitmap.width)
val bottom = bounds.bottom.toInt().coerceIn(top, bitmap.height)
val pixels = IntArray((right - left) * (bottom - top))
if (pixels.isNotEmpty()) {
bitmap.getPixels(pixels, 0, right - left, left, top, right - left, bottom - top)
}
return pixels.toList()
}

private companion object {
/** The alpha of a fully opaque pixel. */
const val OPAQUE = 255
}
}
4 changes: 2 additions & 2 deletions docs/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -920,7 +920,7 @@ PlaybackSources is the ExoPlayer's MediaSource.Factory. It reads the item's tag:

The video screen's VideoArea, in `:feature:player-ui`, sits above the details, as the "Player" frames show:

- The picture is media3-ui-compose's ContentFrame on a SurfaceView, in a 16:9 box that takes at most 45% of the window's height. Compose's clip doesn't round a SurfaceView, so the box draws its rounded corners over it in the screen's background role, `surface`. The shutter before the first frame is `surfaceContainerHighest`, not ContentFrame's black.
- The picture is ClippedContentFrame, in the same module, on a SurfaceView, in a 16:9 box that takes at most 45% of the window's height. ClippedContentFrame does what media3-ui-compose 1.11.1's ContentFrame does with ContentScale.Fit, through the same `rememberPresentationState`, `resizeWithContentScale` and `PlayerSurface`, and adds `clipToBounds()` after the resize, at the SurfaceView's own bounds. Android 10 and 11 make a SurfaceView's hole with `Canvas.drawColor(0, PorterDuff.Mode.CLEAR)` ([SurfaceView at android-10.0.0_r47](https://android.googlesource.com/platform/frameworks/base/+/refs/tags/android-10.0.0_r47/core/java/android/view/SurfaceView.java)), which clears everything inside the canvas's clip, and Compose draws an AndroidView into its enclosing layer's canvas with no clip of its own. Without the clip, that cleared the screen's background and minimize button, which the window then showed as black (#211). Android 12 and later clear only the view's rectangle, with `Canvas.punchHole` ([SurfaceView at android-12.0.0_r34](https://android.googlesource.com/platform/frameworks/base/+/refs/tags/android-12.0.0_r34/core/java/android/view/SurfaceView.java)), which is what the clip keeps on Android 10 and 11. That clip is a rectangle: a rounded Compose clip doesn't round a SurfaceView's picture, so the box draws its rounded corners over it in the screen's background role, `surface`. The shutter before the first frame is `surfaceContainerHighest`, not ContentFrame's black. When Media3 updates, compare ClippedContentFrame with ContentFrame.
- `Modifier.keepScreenOn()` applies while the player plays, so the screen doesn't time out while the user watches, which would stop the activity and turn the picture off.
- The seek bar ticks as many times as it has pixels, with `rememberProgressStateWithTickCount`, and the times tick once a second, with `rememberProgressStateWithTickInterval`. The play and pause button uses `rememberPlayPauseButtonState`. At a video's end it offers to play it again from the start. A live stream says "Live" in place of the seek bar and the times.
- `rememberPlaybackStatus`, in the same module, listens with media3-common-ktx's `Player.listen` for loading, live, the end and playing. The mini player and fullscreen live in the same module, so it stays there, and `:core:playback` has no Compose.
Expand Down Expand Up @@ -952,7 +952,7 @@ The fullscreen button on the video screen sends EnterFullscreen, and PlayerState

- **The window.** FullscreenWindow, while it is composed, asks for `SCREEN_ORIENTATION_USER_LANDSCAPE`, which is landscape either way round as the rotation lock allows ([activity element](https://developer.android.com/guide/topics/manifest/activity-element#screen)), sets `BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE`, and shows the system bars only while the controls show ([Hide system bars for immersive mode](https://developer.android.com/develop/ui/views/layout/immersive)). It gives the bars light icons over the black window, again after every configuration change, because Android 17 no longer restarts the activity for some changes ([Handle configuration changes](https://developer.android.com/guide/topics/resources/runtime-changes)) and activity 1.13.0 calls edge to edge again then ([activity release notes](https://developer.android.com/jetpack/androidx/releases/activity)). Leaving sets portrait, the manifest's orientation for MainActivity, restores the icons and shows the bars, unless the activity is only changing configurations. Android 17 ignores orientation requests, and `getRequestedOrientation()`, on displays whose smallest width is greater than 600dp ([Android 17's change](https://developer.android.com/about/versions/17/changes/ff-restrictions-ignored)), so fullscreen keeps the window's shape there, and leaving sets portrait instead of restoring a saved value. Lint's SourceLockedOrientationActivity check is suppressed for that call, as the manifest's LockedOrientationActivity is.
- **The back gesture.** PlayerContent calls `BackHandler(enabled = state.fullscreen)` every time, as its reference asks, because conditional calls can change which handler runs ([BackHandler](https://developer.android.com/reference/kotlin/androidx/activity/compose/BackHandler.composable)). The entry composes after NavDisplay's own handler, and the last one composed runs, so the gesture leaves fullscreen and not the screen.
- **The layout.** Inside `DewfallTheme(darkTheme = true)`, the window is `scrim`. The picture is ContentFrame with its default ContentScale.Fit, padded with the display cutout only, so showing the bars never moves it. Before the first frame a 16:9 box in `surfaceContainerHighest` stands in for it. The title in `titleMedium` and the channel name in `bodySmall` and `onSurfaceVariant` sit at the top in a box at most 480dp wide, and the play and pause button, the times, the seek bar and the exit button at the bottom, both on `surface` at `DewfallOpacity.DURATION_SCRIM`. They sit 16dp inside `WindowInsets.systemBarsIgnoringVisibility` and the cutout, which stay the same while the bars hide ([About window insets](https://developer.android.com/develop/ui/compose/system/insets)), so no bar ever covers them. The frames put the overlays inside a 16:9 picture, but a video of another shape would leave little room, so they follow the window. Fullscreen shows the player's own failure in a Banner in the picture's place, with "Try again" where it helps.
- **The layout.** Inside `DewfallTheme(darkTheme = true)`, the window is `scrim`. The picture is ClippedContentFrame, as on the video screen, with ContentScale.Fit, so on Android 10 and 11 the pillars beside it keep their `scrim`, padded with the display cutout only, so showing the bars never moves it. Before the first frame a 16:9 box in `surfaceContainerHighest` stands in for it. The title in `titleMedium` and the channel name in `bodySmall` and `onSurfaceVariant` sit at the top in a box at most 480dp wide, and the play and pause button, the times, the seek bar and the exit button at the bottom, both on `surface` at `DewfallOpacity.DURATION_SCRIM`. They sit 16dp inside `WindowInsets.systemBarsIgnoringVisibility` and the cutout, which stay the same while the bars hide ([About window insets](https://developer.android.com/develop/ui/compose/system/insets)), so no bar ever covers them. The frames put the overlays inside a 16:9 picture, but a video of another shape would leave little room, so they follow the window. Fullscreen shows the player's own failure in a Banner in the picture's place, with "Try again" where it helps.
- **The controls.** FullscreenControls, in `:feature:player-logic`, decides with pure functions. They show while the video isn't playing, `Player.isPlaying` being false while it pauses, waits for data, has ended or failed, and while touch exploration is on, which fullscreen follows with `addTouchExplorationStateChangeListener`, because a screen reader's swipes send the app no pointer events. Otherwise a tap toggles them, and they hide after `calculateRecommendedTimeoutMillis(3000, ...)` milliseconds, which the accessibility settings may lengthen ([Android 10 features](https://developer.android.com/about/versions/10/features)). Every touch restarts the wait, and a key shows them and restarts it. Pausing, the end, a failure and playing again show them, as Media3's PlayerView does ([PlayerView.java at 1.11.1](https://github.com/androidx/media/blob/1.11.1/libraries/ui/src/main/java/androidx/media3/ui/PlayerView.java), `auto_show`). The picture's tap is named "Show controls" or "Hide controls". They appear without animation, because the design system's motion only answers a tap.

### How the playback tests run
Expand Down
10 changes: 8 additions & 2 deletions docs/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ Record the sets again:

## Emulator tests

Emulator tests are the [slow tests](#slow-tests). They run the app on an emulator or a phone, and live in app/src/androidTest/. Today there are two: SearchThenPlayTest (#46) and TryAgainOfflineTest (#212).
Emulator tests are the [slow tests](#slow-tests). They run the app on an emulator or a phone, and live in app/src/androidTest/. Today there are three: SearchThenPlayTest (#46), TryAgainOfflineTest (#212) and VideoScreenBackgroundTest (#211).

### What search then play checks

Expand Down Expand Up @@ -571,10 +571,16 @@ TryAgainOfflineTest checks #212's bug: a video opened with the network off staye
3. With FakeNetwork all off, it opens the second test video. Its details and its links fail. The test waits until the player is ended with no item, which it is only once the session's prepare has run on the empty player that the failed links left, and then checks that the failure is still NETWORK. Before the fix, that prepare cleared the earlier video's error, and PlaybackService then cleared the second video's failure too.
4. With FakeNetwork back on, it taps the one "Try again", and waits until the second video plays past 3 seconds.

Both tests end with `stopPlayback`, which stops the player and PlaybackService and waits until the connection has lost its player. The tests share one process, and Hilt makes new objects for each test, but a service that still runs keeps the objects of the test that started it, so the next test's connection would wait for a session that never comes. On November 22, 2026, TryAgainOfflineTest timed out that way whenever it ran after SearchThenPlayTest.
Every test ends with `stopPlayback`, which stops the player and PlaybackService and waits until the connection has lost its player. The tests share one process, and Hilt makes new objects for each test, but a service that still runs keeps the objects of the test that started it, so the next test's connection would wait for a session that never comes. On November 22, 2026, TryAgainOfflineTest timed out that way whenever it ran after SearchThenPlayTest.

It finds "Try again" and the network message by their text from `:feature:player-ui`'s resources. The details and the playback both failed with NETWORK, so the screen shows one "Try again", and the test's tap would fail if there were two. The waits for the player take up to 60 seconds, because ExoPlayer retries a failed load a few times, with growing delays, before it reports the error.

### What the video screen background test checks

VideoScreenBackgroundTest opens the test video the same way as SearchThenPlayTest, waits until it plays its 320x180 picture, and pauses it, so the screen stays still with the last frame. It then captures the window with Compose's `captureToImage`, which copies the window's own pixels, and checks two regions: the minimize button and the description. Neither may have a pixel that isn't fully opaque, and the button must have more than one color, so its icon shows.

It guards #211. Android 10 and 11 make a SurfaceView's hole by clearing everything inside the canvas's clip, and before ClippedContentFrame clipped it, that cleared the video screen's background and minimize button. Those pixels were transparent in the window, and black on the screen. Android 12 and later clear only the SurfaceView's rectangle, so the test matters on the workflow's Android 10 emulator. Screenshot tests can't catch it, because they draw no SurfaceView hole.

### The fakes

- **FakeYouTubeModule** replaces YouTubeModule in every emulator test, through Hilt's `@TestInstallIn` ([Hilt testing guide](https://developer.android.com/training/dependency-injection/hilt-testing)). It binds the YouTube layer's five interfaces to fakes that know TestVideo's two made-up videos, and the player's two media clients to AssetMedia. Nothing builds NewPipeYouTube, so the tests send nothing to YouTube or googlevideo.com, locally or in CI. The repositories in `:core:data` are the real ones, as in [Fast tests](#fast-tests)' "repositories with fake data sources".
Expand Down
Loading