1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
package social.grain
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.ui.Modifier
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.junit4.v2.createComposeRule
import androidx.compose.ui.test.onNodeWithContentDescription
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.test.ext.junit.runners.AndroidJUnit4
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import social.grain.data.model.AtLabel
import social.grain.data.model.GrainGallery
import social.grain.data.model.GrainPhoto
import social.grain.data.model.H3Location
import social.grain.data.model.LabelDefinition
import social.grain.data.model.LabelLocale
import social.grain.support.Fixtures
import social.grain.support.awaitContentDescription
import social.grain.support.awaitText
import social.grain.ui.components.GalleryCard
import social.grain.ui.theme.GrainTheme
/**
* The feed card — the one component every screen in the app puts on screen.
*
* Most of what it does is invisible to a view model test: which photo the
* height comes from, whether a labelled gallery is covered before it is drawn,
* whether the alt badge appears at all. All of those fail silently, as a card
* that simply looks wrong.
*/
@RunWith(AndroidJUnit4::class)
class GalleryCardTest {
@get:Rule
val composeRule = createComposeRule()
private fun show(
gallery: GrainGallery = Fixtures.gallery(),
labelDefinitions: List<LabelDefinition> = emptyList(),
onOpen: () -> Unit = {},
onProfileTap: (String) -> Unit = {},
onFavoriteToggle: () -> Unit = {},
onCommentTap: () -> Unit = {},
onShare: () -> Unit = {},
onFavoritesTap: (() -> Unit)? = null,
onLocationTap: ((String, String) -> Unit)? = null,
onReport: (() -> Unit)? = null,
onDelete: (() -> Unit)? = null,
) {
composeRule.setContent {
GrainTheme {
GalleryCard(
gallery = gallery,
labelDefinitions = labelDefinitions,
onOpen = onOpen,
onProfileTap = onProfileTap,
onFavoriteToggle = onFavoriteToggle,
onCommentTap = onCommentTap,
onShare = onShare,
onFavoritesTap = onFavoritesTap,
onLocationTap = onLocationTap,
onReport = onReport,
onDelete = onDelete,
modifier = Modifier.fillMaxSize(),
)
}
}
}
@Test
fun aCardShowsTheAuthorTheCaptionAndTheCounts() {
show()
composeRule.onNodeWithText("Alice").assertIsDisplayed()
composeRule.onNodeWithText("@alice.grain.social").assertIsDisplayed()
composeRule.onNodeWithText("A walk in the fog").assertIsDisplayed()
composeRule.onNodeWithText("Six frames from a grey morning").assertIsDisplayed()
composeRule.onNodeWithText("12").assertIsDisplayed()
composeRule.onNodeWithText("3").assertIsDisplayed()
}
@Test
fun anAuthorWithNoDisplayNameFallsBackToTheirHandle() {
show(gallery = Fixtures.gallery(creator = Fixtures.profile(displayName = null)))
composeRule.awaitText("@alice.grain.social")
}
@Test
fun aCountOfZeroIsLeftOffRatherThanDrawnAsAZero() {
show(gallery = Fixtures.gallery(favCount = 0, commentCount = 0))
composeRule.onNodeWithText("0").assertDoesNotExist()
}
@Test
fun anUnfavoritedGalleryOffersToFavoriteIt() {
show()
composeRule.awaitContentDescription("Favorite")
}
@Test
fun aFavoritedGalleryOffersToUndoIt() {
show(gallery = Fixtures.gallery(favUri = "at://fav/1"))
composeRule.awaitContentDescription("Unfavorite")
}
@Test
fun theEngagementRowReportsEachTapToItsOwnCallback() {
var favorited = 0
var comments = 0
var shared = 0
show(
onFavoriteToggle = { favorited += 1 },
onCommentTap = { comments += 1 },
onShare = { shared += 1 },
)
composeRule.onNodeWithContentDescription("Favorite").performClick()
composeRule.onNodeWithContentDescription("Comments").performClick()
composeRule.onNodeWithContentDescription("Share").performClick()
assertEquals(1, favorited)
assertEquals(1, comments)
assertEquals(1, shared)
}
@Test
fun tappingTheTitleOpensTheGallery() {
var opened = 0
show(onOpen = { opened += 1 })
composeRule.onNodeWithText("A walk in the fog").performClick()
assertEquals(1, opened)
}
@Test
fun aPlaceNameWithACellBehindItOpensThatLocation() {
var tapped: Pair<String, String>? = null
show(
gallery = Fixtures.gallery(
location = H3Location(value = "8928308280fffff", name = "Mission District"),
),
onLocationTap = { cell, name -> tapped = cell to name },
)
composeRule.onNodeWithText("Mission District").performClick()
assertEquals("8928308280fffff" to "Mission District", tapped)
}
@Test
fun aPlaceWithNoCellIsPlainTextWithNothingToOpen() {
var tapped = 0
show(
gallery = Fixtures.gallery(location = H3Location(value = "", name = "Somewhere")),
onLocationTap = { _, _ -> tapped += 1 },
)
composeRule.onNodeWithText("Somewhere").performClick()
assertEquals("There is no feed to open without a cell", 0, tapped)
}
@Test
fun theOverflowIsAbsentWhenThereIsNothingToPutInIt() {
show()
composeRule.onNodeWithContentDescription("More options").assertDoesNotExist()
}
@Test
fun theOverflowOffersReportingAndDeletingWhenBothArePossible() {
var reported = 0
show(onReport = { reported += 1 }, onDelete = {})
composeRule.onNodeWithContentDescription("More options").performClick()
composeRule.awaitText("Delete gallery")
composeRule.onNodeWithText("Report").performClick()
assertEquals(1, reported)
}
@Test
fun theOverflowDeleteEntryReportsItsOwnTap() {
var deleted = 0
show(onDelete = { deleted += 1 })
composeRule.onNodeWithContentDescription("More options").performClick()
composeRule.onNodeWithText("Delete gallery").performClick()
assertEquals(1, deleted)
}
@Test
fun aPhotoWithADescriptionAdvertisesItAndOpensItOnTap() {
show(
gallery = Fixtures.gallery(photos = 0).copy(
items = listOf(Fixtures.photo(alt = "A tree in fog")),
),
)
composeRule.onNodeWithContentDescription("Show alt text").performClick()
composeRule.awaitText("A tree in fog")
composeRule.awaitContentDescription("Hide alt text")
}
@Test
fun aPhotoWithNoDescriptionAdvertisesNothing() {
show()
composeRule.onNodeWithContentDescription("Show alt text").assertDoesNotExist()
}
@Test
fun aGalleryLabelledForContentIsCoveredUntilTheReaderAsks() {
show(gallery = Fixtures.gallery(labels = listOf(AtLabel(`val` = "!warn"))))
composeRule.onNodeWithText("A walk in the fog").assertDoesNotExist()
composeRule.onNodeWithText("Show anyway").performClick()
composeRule.onNodeWithText("A walk in the fog").assertIsDisplayed()
}
@Test
fun aGalleryLabelledForMediaKeepsItsCaptionAndCoversOnlyThePhotos() {
show(gallery = Fixtures.gallery(labels = listOf(AtLabel(`val` = "porn"))))
composeRule.onNodeWithText("A walk in the fog").assertIsDisplayed()
composeRule.awaitText("Adult Content")
}
@Test
fun aLabelThatOnlyBadgesLeavesTheCardAloneAndNamesItself() {
show(
gallery = Fixtures.gallery(labels = listOf(AtLabel(`val` = "spoiler"))),
labelDefinitions = listOf(
LabelDefinition(
identifier = "spoiler",
locales = listOf(LabelLocale("Spoiler")),
blurs = "none",
defaultSetting = "warn",
),
),
)
composeRule.onNodeWithText("A walk in the fog").assertIsDisplayed()
composeRule.awaitText("Spoiler")
}
@Test
fun aBadgeKeepsTheCaptionBlockAliveOnAGalleryWithNoWords() {
show(
gallery = Fixtures.gallery(
title = null,
description = null,
labels = listOf(AtLabel(`val` = "some-new-label")),
),
)
composeRule.awaitText("some-new-label")
}
@Test
fun aGalleryWithNoWordsAtAllDrawsNoCaptionBlock() {
show(gallery = Fixtures.gallery(title = null, description = null))
composeRule.onNodeWithText("Six frames from a grey morning").assertDoesNotExist()
}
@Test
fun theSocialProofLineNamesUpToTwoPeopleAndCountsTheRest() {
val followers = listOf(
Fixtures.profile(did = "did:plc:bob", handle = "bob.test", displayName = "Bob"),
Fixtures.profile(did = "did:plc:carol", handle = "carol.test", displayName = "Carol"),
Fixtures.profile(did = "did:plc:dave", handle = "dave.test", displayName = "Dave"),
)
var opened = 0
show(
gallery = Fixtures.gallery().copy(favedByFollowing = followers),
onFavoritesTap = { opened += 1 },
)
composeRule.onNodeWithText("Favorited by Bob, Carol and others you follow").performClick()
assertEquals(1, opened)
}
@Test
fun twoPeopleAreNamedWithAnAnd() {
show(
gallery = Fixtures.gallery().copy(
favedByFollowing = listOf(
Fixtures.profile(did = "did:plc:bob", displayName = "Bob"),
Fixtures.profile(did = "did:plc:carol", displayName = "Carol"),
),
),
)
composeRule.awaitText("Favorited by Bob and Carol")
}
@Test
fun onePersonIsNamedOnTheirOwn() {
show(
gallery = Fixtures.gallery().copy(
favedByFollowing = listOf(Fixtures.profile(displayName = "Bob")),
),
)
composeRule.awaitText("Favorited by Bob")
}
@Test
fun aGalleryNobodyYouFollowFavoritedHasNoProofLine() {
show()
composeRule.onNodeWithText("Favorited by Bob", substring = true).assertDoesNotExist()
}
@Test
fun aGalleryWithNoPhotosStillDrawsItsHeaderAndCaption() {
show(gallery = Fixtures.gallery(photos = 0))
composeRule.onNodeWithText("A walk in the fog").assertIsDisplayed()
}
@Test
fun cameraDataIsShownWhenAPhotoCarriesIt() {
val photo = Fixtures.photo().copy(
exif = social.grain.data.model.GrainExif(
make = "Fujifilm",
model = "X100V",
fNumber = "2.0",
exposureTime = "1/500",
focalLengthIn35mmFormat = "35",
iSO = 400,
),
)
show(gallery = Fixtures.gallery(photos = 0).copy(items = listOf(photo)))
composeRule.awaitText("Fujifilm X100V")
composeRule.awaitText("35mm · f/2 · 1/500s · ISO 400")
}
@Test
fun aMixedGalleryStillRendersEveryFrameItWasGiven() {
val photos = listOf(
GrainPhoto(uri = "at://p/1", aspectRatio = social.grain.data.model.AspectRatio(3, 2)),
GrainPhoto(uri = "at://p/2", aspectRatio = social.grain.data.model.AspectRatio(9, 16)),
GrainPhoto(uri = "at://p/3", aspectRatio = social.grain.data.model.AspectRatio(1, 1)),
)
show(gallery = Fixtures.gallery(photos = 0).copy(items = photos))
// Exists rather than displayed: a portrait gallery is taller than the
// window, so the caption is below the fold on a card this size.
composeRule.awaitText("A walk in the fog")
}
@Test
fun aPhotoWithADegenerateRatioDoesNotTakeTheCardDown() {
show(
gallery = Fixtures.gallery(photos = 0).copy(
items = listOf(
GrainPhoto(uri = "at://p/1", aspectRatio = social.grain.data.model.AspectRatio(0, 0)),
),
),
)
composeRule.onNodeWithText("A walk in the fog").assertIsDisplayed()
}
@Test
fun tappingTheAuthorOpensTheirProfile() {
var opened: String? = null
show(onProfileTap = { opened = it })
composeRule.onNodeWithText("Alice").performClick()
assertTrue(opened == "did:plc:alice")
}
}