Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
3.8 kB · 108 lines
Kotlin
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109package social.grain
import androidx.compose.foundation.layout.fillMaxSizeimport androidx.compose.foundation.lazy.LazyColumnimport androidx.compose.foundation.lazy.itemsimport androidx.compose.ui.Modifierimport androidx.compose.ui.test.junit4.v2.createComposeRuleimport androidx.compose.ui.test.onAllNodesWithContentDescriptionimport androidx.compose.ui.test.onNodeWithTextimport androidx.test.ext.junit.runners.AndroidJUnit4import kotlinx.coroutines.runBlockingimport kotlinx.serialization.json.Jsonimport okhttp3.OkHttpClientimport org.junit.Assert.assertTrueimport org.junit.Ruleimport org.junit.Testimport org.junit.runner.RunWithimport social.grain.data.api.XrpcClientimport social.grain.data.api.getFeedimport social.grain.data.auth.AuthManagerimport social.grain.data.model.GrainGalleryimport social.grain.ui.components.GalleryCardimport social.grain.ui.theme.GrainThemeimport java.util.concurrent.TimeUnit
/** * Renders real galleries through the real card. * * Feed data is messier than any fixture: portrait and landscape mixed in one * gallery, missing titles, null fav counts, single-photo posts. Composing live * responses catches the layout assumptions a hand-written sample would satisfy * by accident — an `aspectRatio` of zero crashes the whole feed, not one card. */@RunWith(AndroidJUnit4::class)class GalleryCardRenderTest {
@get:Rule val composeRule = createComposeRule()
private val client = XrpcClient( baseUrl = AuthManager.SERVER_URL, http = OkHttpClient.Builder() .connectTimeout(20, TimeUnit.SECONDS) .readTimeout(20, TimeUnit.SECONDS) .build(), json = Json { ignoreUnknownKeys = true explicitNulls = false coerceInputValues = true }, )
private fun liveGalleries(limit: Int): List<GrainGallery> = runBlocking { client.getFeed(feed = "recent", limit = limit).items.orEmpty() }
@Test fun rendersAFeedOfLiveGalleriesWithoutCrashing() { val galleries = liveGalleries(20) assertTrue("No galleries to render", galleries.isNotEmpty())
composeRule.setContent { GrainTheme { LazyColumn(Modifier.fillMaxSize()) { items(galleries, key = { it.uri }) { GalleryCard(gallery = it) } } } } composeRule.waitForIdle()
// Every card draws a favorite control, so finding one proves the first // card composed all the way through rather than bailing early. val favoriteNodes = composeRule.onAllNodesWithContentDescription("Favorite") .fetchSemanticsNodes() val unfavoriteNodes = composeRule.onAllNodesWithContentDescription("Unfavorite") .fetchSemanticsNodes() assertTrue( "No engagement row rendered — the card didn't compose", favoriteNodes.isNotEmpty() || unfavoriteNodes.isNotEmpty(), ) }
@Test fun rendersTheAuthorHandleOnTheFirstCard() { val gallery = liveGalleries(1).first()
composeRule.setContent { GrainTheme { GalleryCard(gallery = gallery) } } composeRule.waitForIdle()
val name = gallery.creator.displayName?.takeIf { it.isNotBlank() } ?: gallery.creator.handle composeRule.onNodeWithText(name).assertExists() }
@Test fun aGalleryWithNoPhotosStillRenders() { // The appview can return a gallery whose photos are all deleted; the // card must degrade to header-plus-caption rather than throw. val stripped = liveGalleries(1).first().copy(items = emptyList())
composeRule.setContent { GrainTheme { GalleryCard(gallery = stripped) } } composeRule.waitForIdle() }}