137 lines · 7.0 KB · KotlinRaw
package com.cottondb.cinemasimport android.app.Applicationimport androidx.compose.foundation.backgroundimport androidx.compose.foundation.layout.Boximport androidx.compose.foundation.layout.fillMaxSizeimport androidx.compose.runtime.CompositionLocalProviderimport androidx.compose.ui.Alignmentimport androidx.compose.ui.Modifierimport androidx.compose.ui.platform.LocalDensityimport androidx.compose.ui.test.junit4.createComposeRuleimport androidx.compose.ui.test.onNodeWithTagimport androidx.compose.ui.test.assertIsDisplayedimport androidx.compose.ui.test.assertTextEqualsimport androidx.compose.ui.test.performScrollToimport androidx.compose.ui.unit.Densityimport com.cottondb.cinemas.stream.Inkimport com.cottondb.cinemas.stream.RemoteFaceimport com.cottondb.cinemas.stream.RemoteGrowimport androidx.compose.foundation.layout.sizeimport androidx.compose.ui.unit.dpimport androidx.compose.ui.test.onRootimport com.github.takahirom.roborazzi.RoborazziOptionsimport com.github.takahirom.roborazzi.captureRoboImageimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertTrueimport androidx.compose.runtime.getValueimport androidx.compose.runtime.setValueimport com.cottondb.cinemas.stream.Sayimport com.cottondb.cinemas.stream.TvBarFaceimport com.cottondb.specimen.specimenimport kotlinx.serialization.json.Jsonimport kotlinx.serialization.json.JsonObjectimport kotlinx.serialization.json.jsonObjectimport kotlinx.serialization.json.jsonPrimitiveimport org.junit.Ruleimport org.junit.Testimport org.junit.runner.RunWithimport org.robolectric.RobolectricTestRunnerimport org.robolectric.annotation.Configimport org.robolectric.annotation.GraphicsModeimport 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() }}