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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
package social.grain
import android.app.Application
import android.content.Intent
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.lifecycle.SavedStateHandle
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import okhttp3.OkHttpClient
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.Shadows
import social.grain.data.location.LocationServices
import social.grain.feature.actors.ActorListMode
import social.grain.feature.actors.ActorListScreen
import social.grain.feature.actors.ActorListViewModel
import social.grain.feature.feed.HashtagFeedScreen
import social.grain.feature.feed.HashtagFeedViewModel
import social.grain.feature.feed.LocationFeedScreen
import social.grain.feature.feed.LocationFeedViewModel
import social.grain.feature.gallery.GalleryDetailScreen
import social.grain.feature.gallery.GalleryDetailViewModel
import social.grain.feature.settings.ModerationMode
import social.grain.feature.settings.ModerationScreen
import social.grain.feature.settings.ModerationViewModel
import social.grain.support.Fixtures
import social.grain.support.Harness
import social.grain.support.awaitContentDescription
import social.grain.support.awaitText
import social.grain.support.MainDispatcherRule
import social.grain.support.awaitCall
import social.grain.support.json
import social.grain.support.testJson
import social.grain.ui.theme.GrainTheme
/**
* The screens that are a list, a title bar and an empty state.
*
* Grouped because that is genuinely all they are: each wraps one view model
* already tested on its own, and what is worth pinning here is the shell — the
* right title, the right words when there is nothing to show, and a back button
* that reports its tap.
*/
@RunWith(AndroidJUnit4::class)
class ScreenShellTest {
@get:Rule
val mainDispatcher = MainDispatcherRule()
@get:Rule
val composeRule = createComposeRule()
private lateinit var harness: Harness
@Before
fun setUp() {
harness = Harness()
harness.server
.on("social.grain.feed.getFeed", """{"items":[${json(Fixtures.gallery())}]}""")
}
@After
fun tearDown() = harness.tearDown()
// MARK: - Actor lists
private fun actorList(
mode: ActorListMode,
subject: String = "did:plc:alice",
onBack: () -> Unit = {},
onOpenProfile: (String) -> Unit = {},
) {
val model = ActorListViewModel(
harness.client,
harness.auth,
SavedStateHandle(mapOf("mode" to mode, "subject" to subject)),
)
composeRule.setContent {
GrainTheme {
ActorListScreen(onBack = onBack, onOpenProfile = onOpenProfile, viewModel = model)
}
}
composeRule.waitForIdle()
}
@Test
fun aFollowerListIsTitledAndListsWhoItWasGiven() {
harness.server.on(
"social.grain.graph.getFollowers",
"""{"totalCount":1,"items":[{"did":"did:plc:bob","handle":"bob.test","displayName":"Bob"}]}""",
)
var opened: String? = null
actorList(ActorListMode.FOLLOWERS, onOpenProfile = { opened = it })
composeRule.awaitText("Followers")
composeRule.awaitText("Bob")
composeRule.onNodeWithText("Bob").performClick()
assertEquals("did:plc:bob", opened)
}
@Test
fun anEmptyFollowerListSaysWhoItIsAboutRatherThanNothing() {
harness.server.on("social.grain.graph.getFollowers", """{"items":[]}""")
actorList(ActorListMode.FOLLOWERS)
composeRule.awaitText("No followers")
composeRule.awaitText("No one is following this account yet.")
}
@Test
fun anEmptyFavoritesListHasItsOwnWording() {
harness.server.on("social.grain.gallery.getFavorites", """{"items":[]}""")
actorList(ActorListMode.GALLERY_FAVORITES, subject = "at://x/gallery/1")
composeRule.awaitText("Favorited by")
composeRule.awaitText("No one has favorited this gallery yet.")
}
@Test
fun theActorListBackButtonReportsItsTap() {
harness.server.on("social.grain.graph.getFollowing", """{"items":[]}""")
var back = 0
actorList(ActorListMode.FOLLOWING, onBack = { back += 1 })
composeRule.onNodeWithContentDescription("Back").performClick()
assertEquals(1, back)
}
// MARK: - Moderation lists
private fun moderation(mode: ModerationMode, onBack: () -> Unit = {}) {
val model = ModerationViewModel(
harness.client,
harness.auth,
SavedStateHandle(mapOf("mode" to mode)),
)
composeRule.setContent {
GrainTheme {
ModerationScreen(onBack = onBack, onOpenProfile = {}, viewModel = model)
}
}
composeRule.waitForIdle()
}
@Test
fun theBlockedListNamesWhoIsBlockedAndOffersToUndoIt() {
harness.server.on(
"social.grain.graph.getBlocks",
"""{"items":[{"did":"did:plc:bob","handle":"bob.test","displayName":"Bob",""" +
""""blockUri":"at://did:plc:me/social.grain.graph.block/b1"}]}""",
)
moderation(ModerationMode.BLOCKED)
composeRule.awaitText("Blocked users")
composeRule.awaitText("Bob")
composeRule.onNodeWithText("Unblock").performClick()
composeRule.waitUntil(10_000) { harness.server.countOf("com.atproto.repo.deleteRecord") >= 1 }
}
@Test
fun anEmptyMutedListSaysSo() {
harness.server.on("social.grain.graph.getMutes", """{"items":[]}""")
var back = 0
moderation(ModerationMode.MUTED, onBack = { back += 1 })
composeRule.awaitText("Muted users")
composeRule.awaitText("You haven't muted anyone.")
composeRule.onNodeWithContentDescription("Back").performClick()
assertEquals(1, back)
}
// MARK: - Gallery detail
private fun galleryDetail(onBack: () -> Unit = {}) {
val model = GalleryDetailViewModel(
harness.client,
harness.auth,
harness.labelDefinitions,
SavedStateHandle(mapOf("uri" to "at://did:plc:alice/social.grain.gallery/gallery1")),
)
composeRule.setContent {
GrainTheme {
GalleryDetailScreen(onBack = onBack, onOpenProfile = {}, viewModel = model)
}
}
composeRule.waitForIdle()
}
@Test
fun theGalleryScreenDrawsTheCardAndItsCommentPrompt() {
harness.server
.on(
"social.grain.gallery.getGallery",
"""{"gallery":${json(Fixtures.gallery())}}""",
)
.on(
"social.grain.comment.getThread",
"""{"comments":[${json(Fixtures.comment())}],"totalCount":3}""",
)
galleryDetail()
composeRule.awaitText("Gallery")
composeRule.awaitText("A walk in the fog")
composeRule.awaitText("View all 3 comments")
}
@Test
fun aGalleryWithNoCommentsInvitesTheFirstOne() {
harness.server
.on(
"social.grain.gallery.getGallery",
"""{"gallery":${json(Fixtures.gallery(commentCount = 0))}}""",
)
.on("social.grain.comment.getThread", """{"comments":[],"totalCount":0}""")
galleryDetail()
composeRule.awaitText("Add a comment…")
}
@Test
fun aGalleryThatIsGoneSaysSoRatherThanSpinningForever() {
harness.server
.on("social.grain.gallery.getGallery", """{"error":"gone"}""", code = 404)
.on("social.grain.comment.getThread", """{"comments":[]}""")
var back = 0
galleryDetail(onBack = { back += 1 })
composeRule.awaitText("Gallery unavailable")
composeRule.onNodeWithContentDescription("Back").performClick()
assertEquals(1, back)
}
@Test
fun theViewersOwnGalleryOffersToDeleteIt() {
harness.server
.on(
"social.grain.gallery.getGallery",
"""{"gallery":${json(
Fixtures.gallery(
creator = Fixtures.profile(did = "did:plc:me", handle = "me.test"),
),
)}}""",
)
.on("social.grain.comment.getThread", """{"comments":[]}""")
galleryDetail()
composeRule.onNodeWithContentDescription("Delete gallery").performClick()
composeRule.awaitText("Delete gallery?")
composeRule.onNodeWithText("Cancel").performClick()
composeRule.waitForIdle()
assertEquals(0, harness.server.countOf("com.atproto.repo.applyWrites"))
}
// MARK: - Tapped-through feeds
@Test
fun aHashtagFeedIsTitledAfterItsTagAndListsWhatItFound() {
var back = 0
val model = HashtagFeedViewModel(
harness.client,
harness.auth,
harness.labelDefinitions,
SavedStateHandle(mapOf("tag" to "fog")),
)
composeRule.setContent {
GrainTheme {
HashtagFeedScreen(
onBack = { back += 1 },
onOpenProfile = {},
onOpenHashtag = {},
viewModel = model,
)
}
}
composeRule.waitForIdle()
composeRule.awaitText("#fog")
composeRule.awaitText("A walk in the fog")
composeRule.onNodeWithContentDescription("Back").performClick()
assertEquals(1, back)
}
@Test
fun anEmptyHashtagFeedNamesTheTagThatFoundNothing() {
harness.server.on("social.grain.feed.getFeed", """{"items":[]}""")
val model = HashtagFeedViewModel(
harness.client,
harness.auth,
harness.labelDefinitions,
SavedStateHandle(mapOf("tag" to "quiet")),
)
composeRule.setContent {
GrainTheme {
HashtagFeedScreen(
onBack = {},
onOpenProfile = {},
onOpenHashtag = {},
viewModel = model,
)
}
}
composeRule.waitForIdle()
composeRule.awaitText("Nothing tagged #quiet yet")
}
@Test
fun aLocationFeedIsTitledWithThePlaceNameTheReaderTapped() {
val model = LocationFeedViewModel(
harness.client,
harness.auth,
LocationServices(OkHttpClient(), testJson),
harness.labelDefinitions,
SavedStateHandle(mapOf("cell" to "8928308280fffff", "name" to "Mission District")),
)
composeRule.setContent {
GrainTheme {
LocationFeedScreen(
onBack = {},
onOpenProfile = {},
onOpenHashtag = {},
viewModel = model,
)
}
}
composeRule.waitForIdle()
composeRule.awaitText("Mission District")
composeRule.awaitText("A walk in the fog")
}
@Test
fun anEmptyLocationFeedNamesThePlaceThatFoundNothing() {
harness.server.on("social.grain.feed.getFeed", """{"items":[]}""")
val model = LocationFeedViewModel(
harness.client,
harness.auth,
LocationServices(OkHttpClient(), testJson),
harness.labelDefinitions,
SavedStateHandle(mapOf("cell" to "89283", "name" to "Nowhere")),
)
composeRule.setContent {
GrainTheme {
LocationFeedScreen(
onBack = {},
onOpenProfile = {},
onOpenHashtag = {},
viewModel = model,
)
}
}
composeRule.waitForIdle()
composeRule.awaitText("Nothing from Nowhere yet")
}
@Test
fun theViewersOwnGalleryInAFeedIsDeletedOnlyOnceConfirmed() {
val mine = Fixtures.gallery(creator = Fixtures.profile(did = "did:plc:me", handle = "me.test"))
harness.server
.on("social.grain.feed.getFeed", """{"items":[${json(mine)}]}""")
.on("com.atproto.repo.applyWrites", "{}")
val model = HashtagFeedViewModel(
harness.client,
harness.auth,
harness.labelDefinitions,
SavedStateHandle(mapOf("tag" to "fog")),
)
composeRule.setContent {
GrainTheme {
HashtagFeedScreen(
onBack = {},
onOpenProfile = {},
onOpenHashtag = {},
viewModel = model,
)
}
}
composeRule.awaitText("A walk in the fog")
composeRule.onNodeWithContentDescription("More options").performClick()
composeRule.onNodeWithText("Delete gallery").performClick()
composeRule.awaitText("Delete gallery?")
composeRule.onNodeWithText("Cancel").performClick()
composeRule.waitForIdle()
assertEquals(0, harness.server.countOf("com.atproto.repo.applyWrites"))
composeRule.onNodeWithContentDescription("More options").performClick()
composeRule.onNodeWithText("Delete gallery").performClick()
composeRule.awaitText("Delete gallery?")
composeRule.onNodeWithText("Delete").performClick()
harness.server.awaitCall("com.atproto.repo.applyWrites")
composeRule.awaitText("Nothing tagged #fog yet")
}
@Test
fun sharingAFeedCardHandsItsLinkToTheSystemChooser() {
val model = HashtagFeedViewModel(
harness.client,
harness.auth,
harness.labelDefinitions,
SavedStateHandle(mapOf("tag" to "fog")),
)
composeRule.setContent {
GrainTheme {
HashtagFeedScreen(
onBack = {},
onOpenProfile = {},
onOpenHashtag = {},
viewModel = model,
)
}
}
composeRule.awaitText("A walk in the fog")
composeRule.onNodeWithContentDescription("Share").performClick()
val chooser = Shadows.shadowOf(ApplicationProvider.getApplicationContext<Application>())
.nextStartedActivity
assertEquals(Intent.ACTION_CHOOSER, chooser.action)
@Suppress("DEPRECATION")
val send = chooser.getParcelableExtra<Intent>(Intent.EXTRA_INTENT)!!
assertEquals(Fixtures.gallery().shareUrl, send.getStringExtra(Intent.EXTRA_TEXT))
}
}