Cotton code
130 lines · 5.2 KB · KotlinRaw
package com.cottondb.messages.warmimport android.app.Applicationimport androidx.activity.ComponentActivityimport androidx.activity.compose.BackHandlerimport androidx.compose.foundation.layout.Boximport androidx.compose.foundation.clickableimport androidx.compose.foundation.layout.fillMaxSizeimport androidx.compose.foundation.layout.fillMaxWidthimport androidx.compose.foundation.layout.heightimport androidx.compose.ui.platform.testTagimport androidx.compose.runtime.mutableStateOfimport androidx.compose.ui.Modifierimport androidx.compose.ui.test.assertIsDisplayedimport androidx.compose.ui.test.junit4.createAndroidComposeRuleimport androidx.compose.ui.test.onNodeWithTagimport androidx.compose.ui.test.onNodeWithTextimport androidx.compose.ui.test.performClickimport androidx.compose.ui.test.performScrollToimport androidx.compose.ui.test.performTouchInputimport androidx.compose.ui.test.swipeDownimport androidx.compose.ui.test.swipeUpimport androidx.compose.ui.test.clickimport androidx.compose.ui.unit.dpimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertTrueimport org.junit.Ruleimport org.junit.Testimport org.junit.runner.RunWithimport org.robolectric.RobolectricTestRunnerimport org.robolectric.annotation.Config/** * Every sheet (Pieces.kt `Sheet`, behind the row menu, Ask, Message info, Blocked, the profile, a group's member…): * a pull down, a tap outside and Back close it, Back closes only the sheet, a tap on it stays on it, and a sheet longer * than the screen (64 members' Message info, the largest text, sideways) scrolls to its last row. Invented rows. */@RunWith(RobolectricTestRunner::class)@Config(sdk = [35], qualifiers = "w400dp-h860dp-xhdpi", application = Application::class)class SheetTest {    @get:Rule val compose = createAndroidComposeRule<ComponentActivity>()    private var closed = 0    private var pageBack = 0    private val open = mutableStateOf(true)    private fun sheet(rows: Int) {        compose.setContent {            WarmTheme(night = false) {                Box(Modifier.fillMaxSize()) {                    // The page under the sheet has its own Back (a place, a person's page).                    BackHandler { pageBack++ }                    if (open.value) Sheet("Close", "test-sheet", { closed++; open.value = false }, title = "Message info") {                        (1..rows).forEach { i -> SheetRow("Member $i", "row-$i") {} }                    }                }            }        }        compose.waitForIdle()    }    @Test    fun a_long_sheet_scrolls_to_its_last_row() {        sheet(64)        compose.onNodeWithTag("row-64").performScrollTo().assertIsDisplayed()        compose.onNodeWithText("Member 64").assertIsDisplayed()        assertEquals(0, closed)    }    @Test    fun a_pull_down_closes_the_sheet() {        sheet(3)        compose.onNodeWithTag("test-sheet").performTouchInput { swipeDown(startY = top + 10f, endY = top + 600f, durationMillis = 300) }        compose.waitForIdle()        assertEquals(1, closed)    }    @Test    fun a_long_sheet_scrolled_down_scrolls_back_before_it_closes() {        sheet(64)        compose.onNodeWithTag("test-sheet").performTouchInput { swipeUp(durationMillis = 400) }        compose.waitForIdle()        // Scrolled into its rows, a short pull scrolls them back rather than closing the sheet.        compose.onNodeWithTag("test-sheet").performTouchInput { swipeDown(startY = centerY - 100f, endY = centerY + 100f, durationMillis = 400) }        compose.waitForIdle()        assertEquals(0, closed)    }    @Test    fun a_tap_on_the_sheet_stays_and_a_tap_outside_closes_it() {        sheet(3)        compose.onNodeWithText("Message info").performClick()        compose.waitForIdle()        assertEquals(0, closed)        compose.onNodeWithTag("test-sheet-outside").performTouchInput { click(androidx.compose.ui.geometry.Offset(centerX, 40.dp.toPx())) }        compose.waitForIdle()        assertEquals(1, closed)    }    @Test    fun back_closes_only_the_sheet() {        sheet(3)        compose.runOnUiThread { compose.activity.onBackPressedDispatcher.onBackPressed() }        compose.waitForIdle()        assertEquals(1, closed)        assertEquals(0, pageBack)        compose.runOnUiThread { compose.activity.onBackPressedDispatcher.onBackPressed() }        compose.waitForIdle()        assertTrue(pageBack == 1 && closed == 1)    }    /** Under You, the place switch is drawn after the Blocked sheet: it stood over the sheet's foot and took its taps. */    @Test    fun a_sheet_stands_over_what_is_drawn_after_it() {        var switched = 0        compose.setContent {            WarmTheme(night = false) {                Box(Modifier.fillMaxSize()) {                    Sheet("Close", "test-sheet", { closed++ }, title = "Blocked") { SheetRow("Nobody", "row-nobody") {} }                    Box(Modifier.align(androidx.compose.ui.Alignment.BottomCenter).fillMaxWidth().height(160.dp).testTag("a-switch").clickable { switched++ })                }            }        }        compose.waitForIdle()        compose.onNodeWithTag("a-switch").performTouchInput { click(center) }        compose.waitForIdle()        assertEquals(0, switched)        assertEquals(0, closed)    }}