Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
4.8 kB · 113 lines
Kotlin
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114package social.grain
import androidx.compose.foundation.layout.Columnimport androidx.compose.foundation.layout.fillMaxWidthimport androidx.compose.foundation.layout.heightimport androidx.compose.foundation.lazy.grid.GridCellsimport androidx.compose.foundation.lazy.grid.LazyVerticalGridimport androidx.compose.foundation.lazy.grid.itemsimport androidx.compose.foundation.layout.Boximport androidx.compose.foundation.layout.fillMaxSizeimport androidx.compose.foundation.pager.rememberPagerStateimport androidx.compose.material3.Surfaceimport androidx.compose.material3.Textimport androidx.compose.ui.Modifierimport androidx.compose.ui.platform.testTagimport androidx.compose.ui.test.assertIsDisplayedimport androidx.compose.ui.test.onNodeWithTagimport androidx.compose.ui.test.performTouchInputimport androidx.compose.ui.test.swipeUpimport androidx.compose.ui.test.junit4.v2.createComposeRuleimport androidx.compose.ui.test.onNodeWithTextimport androidx.test.ext.junit.runners.AndroidJUnit4import org.junit.Ruleimport org.junit.Testimport org.junit.runner.RunWithimport social.grain.feature.profile.CollapsingHeaderTabsimport androidx.compose.ui.unit.dpimport social.grain.ui.theme.GrainTheme
/** * The profile header is measured outside the pager and reported at whatever * height the collapse has left it, which is easy to get wrong in a way that * silently drops everything below the first row — the display name, the handle * and the bio all vanished that way once. */@RunWith(AndroidJUnit4::class)class ProfileHeaderCollapseTest {
@get:Rule val composeRule = createComposeRule()
private val lines = (1..6).map { "header line $it" }
private fun setContent(scrollableContent: Boolean = false) { composeRule.setContent { GrainTheme { Surface(Modifier.fillMaxSize()) { CollapsingHeaderTabs( pagerState = rememberPagerState { 2 }, header = { Column { lines.forEach { Text(it) } } }, tabRow = { Text("Galleries") }, content = { if (scrollableContent) { LazyVerticalGrid( columns = GridCells.Fixed(3), modifier = Modifier.testTag("grid"), ) { items((1..60).toList()) { Box(Modifier.fillMaxWidth().height(120.dp)) { Text("$it") } } } } else { Text("photos") } }, ) } } } }
@Test fun everyLineOfTheHeaderIsShown() { setContent() lines.forEach { composeRule.onNodeWithText(it).assertIsDisplayed() } }
@Test fun theTabBarAndTheGridSitBelowTheWholeHeader() { setContent() val lastLine = composeRule.onNodeWithText(lines.last()).fetchSemanticsNode() val tabs = composeRule.onNodeWithText("Galleries").fetchSemanticsNode() val grid = composeRule.onNodeWithText("photos").fetchSemanticsNode() val headerBottom = lastLine.positionInRoot.y + lastLine.size.height
assert(tabs.positionInRoot.y >= headerBottom) { "tab bar overlapped the header: ${tabs.positionInRoot.y} < $headerBottom" } assert(grid.positionInRoot.y >= tabs.positionInRoot.y + tabs.size.height) { "grid overlapped the tab bar: ${grid.positionInRoot.y} < ${tabs.positionInRoot.y}" } }
@Test fun scrollingTheGridTakesTheHeaderWithItAndBringsItBack() { setContent(scrollableContent = true)
val restingTop = composeRule.onNodeWithText(lines.first()).fetchSemanticsNode().positionInRoot.y composeRule.onNodeWithTag("grid").performTouchInput { swipeUp() } composeRule.waitForIdle() val collapsedTop = composeRule.onNodeWithText(lines.first()).fetchSemanticsNode().positionInRoot.y
assert(collapsedTop < restingTop) { "header did not collapse: $collapsedTop is not above $restingTop" } // The tab bar rides with the header rather than crossing it, whatever // the collapse has done. val lastLine = composeRule.onNodeWithText(lines.last()).fetchSemanticsNode() val tabs = composeRule.onNodeWithText("Galleries").fetchSemanticsNode() assert(tabs.positionInRoot.y >= lastLine.positionInRoot.y + lastLine.size.height) { "tab bar overlapped the header once collapsed" } }}