package com.cottondb.cinemas import android.app.Application import androidx.compose.foundation.background import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.runtime.CompositionLocalProvider import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.test.junit4.createComposeRule import androidx.compose.ui.test.onNodeWithTag import androidx.compose.ui.test.assertIsDisplayed import androidx.compose.ui.test.assertTextEquals import androidx.compose.ui.test.performScrollTo import androidx.compose.ui.unit.Density import com.cottondb.cinemas.stream.Ink import com.cottondb.cinemas.stream.RemoteFace import com.cottondb.cinemas.stream.RemoteGrow import androidx.compose.foundation.layout.size import androidx.compose.ui.unit.dp import androidx.compose.ui.test.onRoot import com.github.takahirom.roborazzi.RoborazziOptions import com.github.takahirom.roborazzi.captureRoboImage import org.junit.Assert.assertEquals import org.junit.Assert.assertTrue import androidx.compose.runtime.getValue import androidx.compose.runtime.setValue import com.cottondb.cinemas.stream.Say import com.cottondb.cinemas.stream.TvBarFace import com.cottondb.specimen.specimen import kotlinx.serialization.json.Json import kotlinx.serialization.json.JsonObject import kotlinx.serialization.json.jsonObject import kotlinx.serialization.json.jsonPrimitive import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner import org.robolectric.annotation.Config import org.robolectric.annotation.GraphicsMode import java.io.File /** * THE TV, DRAWN WITHOUT ONE (the emulator finds no Cast receiver): the bar over the foot and the remote, from an invented * cinemas.remote answer for an episode with a next one, in Cinemas' own words; and the remote at the largest text. * What only a real phone and TV prove (finding the TV, the session, the receiver's clock) is said in report.md. */ @RunWith(RobolectricTestRunner::class) @GraphicsMode(GraphicsMode.Mode.NATIVE) @Config(sdk = [35], qualifiers = "w400dp-h860dp-mdpi", application = Application::class) class TvShotsTest { @get:Rule val compose = createComposeRule() private val lines: JsonObject = Json.parseToJsonElement(File("../../../crates/cotton-core/kits/shell/lines/cinemas.json").readText()).jsonObject private val say: Say = { key, fills -> var text = lines["stream"]!!.jsonObject[key]!!.jsonPrimitive.content fills.forEach { (k, v) -> text = text.replace("{$k}", v) } text } private fun home(key: String) = lines["home"]!!.jsonObject[key]!!.jsonPrimitive.content /** What cinemas.remote answers 12 min 10 s into Lowmarsh S1 E2 on the living-room TV (invented). */ private val answer = Json.parseToJsonElement(""" {"playing": "${home("remote_on").replace("{device}", "Living room TV")}", "title": "The Tide", "caption": "${home("remote_episode").replace("{series}", "Lowmarsh").replace("{season}", "1").replace("{episode}", "2")}", "at": "12:10", "left": "${home("remote_left").replace("{left}", "35:50")}", "next": {"table": "film_episode", "id": "e3", "label": "${home("remote_next")}"}, "volume": "${home("remote_volume")}"}""").jsonObject private fun remote(scale: Float, name: String) { compose.specimen("cinemas", name, width = 400, height = 860, ground = Ink.ground) { CompositionLocalProvider(LocalDensity provides Density(LocalDensity.current.density, scale)) { RemoteFace( say, home("act_subtitles"), "Living room TV", answer, "Lowmarsh", null, null, position = 730_000, length = 2_880_000, isPlaying = true, skip = 15_000, hasTitle = true, onClose = {}, onSeek = {}, onBack = {}, onPlay = {}, onForward = {}, onSubtitles = {}, onNext = { _, _ -> }, onStop = {}, onPhone = {}, ) } } } @Test fun the_remote_says_where_it_plays_the_episode_the_time_left_and_the_next_one() { remote(1f, "tv-remote") compose.onNodeWithTag("remote-device").assertTextEquals("Playing on Living room TV") compose.onNodeWithTag("remote-left").assertTextEquals("−35:50") compose.onNodeWithTag("remote-next").assertIsDisplayed() compose.onNodeWithTag("remote-subtitles").assertIsDisplayed() } @Test fun the_remote_at_the_largest_text_scrolls_to_stop_on_tv() { remote(2f, "tv-remote-largest") compose.onNodeWithTag("remote-stop").performScrollTo().assertIsDisplayed() } @Test fun the_remote_grows_out_of_the_tv_bar_and_fills_the_screen() { // The bar stands at the foot (8 in, 64 high, above where the tabs would be); the remote grows from that box. val bar = androidx.compose.ui.geometry.Rect(8f, 700f, 392f, 764f) var open by androidx.compose.runtime.mutableStateOf(false) compose.mainClock.autoAdvance = false compose.setContent { Box(Modifier.size(400.dp, 860.dp).background(Ink.ground)) { RemoteGrow(visible = open, bar = bar) { RemoteFace( say, home("act_subtitles"), "Living room TV", answer, "Lowmarsh", null, null, position = 730_000, length = 2_880_000, isPlaying = true, skip = 15_000, hasTitle = true, onClose = {}, onSeek = {}, onBack = {}, onPlay = {}, onForward = {}, onSubtitles = {}, onNext = { _, _ -> }, onStop = {}, onPhone = {}, ) } } } compose.mainClock.advanceTimeByFrame() compose.runOnUiThread { open = true; androidx.compose.runtime.snapshots.Snapshot.sendApplyNotifications() } repeat(3) { compose.mainClock.advanceTimeByFrame() } val early = compose.onNodeWithTag("remote-grow", useUnmergedTree = true).fetchSemanticsNode().boundsInRoot assertTrue("it starts near the bar's box, not the screen: $early", early.top > 400f && early.height < 400f) compose.mainClock.advanceTimeBy(140) compose.onRoot().captureRoboImage("src/testCinemas/specimens/tv-remote-growing.png", roborazziOptions = RoborazziOptions(compareOptions = RoborazziOptions.CompareOptions(changeThreshold = 0.005f))) compose.mainClock.advanceTimeBy(1_500) val grown = compose.onNodeWithTag("remote-grow", useUnmergedTree = true).fetchSemanticsNode().boundsInRoot assertEquals("it ends the whole screen: $grown", 0f, grown.top, 1f) assertEquals(860f, grown.height, 1f) } @Test fun the_bar_over_the_foot_says_the_title_and_the_tv() { compose.specimen("cinemas", "tv-bar", width = 400, height = 120, ground = Ink.ground) { Box(Modifier.fillMaxSize(), contentAlignment = Alignment.Center) { TvBarFace(say, "Living room TV", "Lowmarsh · S1 E2", null, isPlaying = true, onOpen = {}, onPlay = {}) } } compose.onNodeWithTag("tv-bar").assertIsDisplayed() } }