package com.cottondb.messages.warm import android.app.Application import androidx.activity.ComponentActivity import androidx.activity.compose.BackHandler import androidx.compose.foundation.layout.Box import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height import androidx.compose.ui.platform.testTag import androidx.compose.runtime.mutableStateOf import androidx.compose.ui.Modifier import androidx.compose.ui.test.assertIsDisplayed import androidx.compose.ui.test.junit4.createAndroidComposeRule import androidx.compose.ui.test.onNodeWithTag import androidx.compose.ui.test.onNodeWithText import androidx.compose.ui.test.performClick import androidx.compose.ui.test.performScrollTo import androidx.compose.ui.test.performTouchInput import androidx.compose.ui.test.swipeDown import androidx.compose.ui.test.swipeUp import androidx.compose.ui.test.click import androidx.compose.ui.unit.dp import org.junit.Assert.assertEquals import org.junit.Assert.assertTrue import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner import 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() 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) } }